|
1 | 1 | from typing import List |
2 | 2 |
|
3 | 3 | from sqlalchemy.engine import Engine |
| 4 | +from sqlalchemy.orm import Session |
| 5 | +from sqlalchemy.sql import text |
4 | 6 |
|
5 | 7 | from birdxplorer_common.models import LanguageIdentifier, Note, Post, TopicId |
6 | | -from birdxplorer_common.storage import NoteRecord, PostRecord, Storage, TopicRecord |
| 8 | +from birdxplorer_common.storage import ( |
| 9 | + NoteRecord, |
| 10 | + PostRecord, |
| 11 | + Storage, |
| 12 | + TopicRecord, |
| 13 | + XUserRecord, |
| 14 | +) |
7 | 15 |
|
8 | 16 |
|
9 | 17 | def test_basic_search( |
@@ -178,3 +186,101 @@ def test_count_search_results( |
178 | 186 | # Verify count matches actual results |
179 | 187 | results = list(storage.search_notes_with_posts(note_includes_text="summary", language=LanguageIdentifier("en"))) |
180 | 188 | assert len(results) == filtered_count |
| 189 | + |
| 190 | + |
| 191 | +def test_search_notes_with_non_enum_language( |
| 192 | + engine_for_test: Engine, |
| 193 | + note_samples: List[Note], |
| 194 | + post_samples: List[Post], |
| 195 | + note_records_sample: List[NoteRecord], |
| 196 | + x_user_records_sample: List[XUserRecord], |
| 197 | + post_records_sample: List[PostRecord], |
| 198 | +) -> None: |
| 199 | + """Test that notes with non-enum language codes (e.g. 'ko') are returned with language='other' instead of skipped""" |
| 200 | + with Session(engine_for_test) as sess: |
| 201 | + # Insert a note with Korean language (not in LanguageIdentifier enum) |
| 202 | + sess.execute( |
| 203 | + text( |
| 204 | + "INSERT INTO notes (note_id, post_id, summary, language, created_at) " |
| 205 | + "VALUES (:note_id, :post_id, :summary, :language, :created_at)" |
| 206 | + ), |
| 207 | + { |
| 208 | + "note_id": "9999999999999999901", |
| 209 | + "post_id": "2234567890123456781", |
| 210 | + "summary": "Korean language note summary", |
| 211 | + "language": "ko", |
| 212 | + "created_at": 1152921600000, |
| 213 | + }, |
| 214 | + ) |
| 215 | + sess.commit() |
| 216 | + |
| 217 | + storage = Storage(engine=engine_for_test) |
| 218 | + results = list(storage.search_notes_with_posts(note_includes_text="Korean language note")) |
| 219 | + assert len(results) == 1 |
| 220 | + note, _ = results[0] |
| 221 | + assert note.language == "other" |
| 222 | + |
| 223 | + |
| 224 | +def test_search_notes_with_null_language( |
| 225 | + engine_for_test: Engine, |
| 226 | + note_samples: List[Note], |
| 227 | + post_samples: List[Post], |
| 228 | + note_records_sample: List[NoteRecord], |
| 229 | + x_user_records_sample: List[XUserRecord], |
| 230 | + post_records_sample: List[PostRecord], |
| 231 | +) -> None: |
| 232 | + """Test that notes with NULL language are returned with language='other' instead of skipped""" |
| 233 | + with Session(engine_for_test) as sess: |
| 234 | + # Insert a note with NULL language |
| 235 | + sess.execute( |
| 236 | + text( |
| 237 | + "INSERT INTO notes (note_id, post_id, summary, language, created_at) " |
| 238 | + "VALUES (:note_id, :post_id, :summary, :language, :created_at)" |
| 239 | + ), |
| 240 | + { |
| 241 | + "note_id": "9999999999999999902", |
| 242 | + "post_id": "2234567890123456781", |
| 243 | + "summary": "Null language note summary", |
| 244 | + "language": None, |
| 245 | + "created_at": 1152921600000, |
| 246 | + }, |
| 247 | + ) |
| 248 | + sess.commit() |
| 249 | + |
| 250 | + storage = Storage(engine=engine_for_test) |
| 251 | + results = list(storage.search_notes_with_posts(note_includes_text="Null language note")) |
| 252 | + assert len(results) == 1 |
| 253 | + note, _ = results[0] |
| 254 | + assert note.language == "other" |
| 255 | + |
| 256 | + |
| 257 | +def test_search_notes_with_invalid_post_id( |
| 258 | + engine_for_test: Engine, |
| 259 | + note_samples: List[Note], |
| 260 | + post_samples: List[Post], |
| 261 | + note_records_sample: List[NoteRecord], |
| 262 | + x_user_records_sample: List[XUserRecord], |
| 263 | + post_records_sample: List[PostRecord], |
| 264 | +) -> None: |
| 265 | + """Test that notes with invalid post_id (e.g. '-1') are returned with post_id='' instead of skipped""" |
| 266 | + with Session(engine_for_test) as sess: |
| 267 | + sess.execute( |
| 268 | + text( |
| 269 | + "INSERT INTO notes (note_id, post_id, summary, language, created_at) " |
| 270 | + "VALUES (:note_id, :post_id, :summary, :language, :created_at)" |
| 271 | + ), |
| 272 | + { |
| 273 | + "note_id": "9999999999999999903", |
| 274 | + "post_id": "-1", |
| 275 | + "summary": "Invalid post id note summary", |
| 276 | + "language": "en", |
| 277 | + "created_at": 1152921600000, |
| 278 | + }, |
| 279 | + ) |
| 280 | + sess.commit() |
| 281 | + |
| 282 | + storage = Storage(engine=engine_for_test) |
| 283 | + results = list(storage.search_notes_with_posts(note_includes_text="Invalid post id note")) |
| 284 | + assert len(results) == 1 |
| 285 | + note, _ = results[0] |
| 286 | + assert note.post_id == "" |
0 commit comments