From 5e845a8ca40ab008500f70810a509cdb61b996ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Ba=C3=B1ados=20Schwerter?= Date: Wed, 15 May 2024 23:53:23 +0000 Subject: [PATCH] Updated tests and presentation.py to pass some tests. I took the predeclared types as the expected behaviour. For underspecified portions, I assumed behaviour in the presentation template as the truth (what django is assuming to present results), which means that the tests were wrong (missed a list level, which is iterated through on the presentation layer) --- src/CreeDictionary/API/search/presentation.py | 35 +++++++++---------- .../tests/API_tests/model_test.py | 4 +-- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/CreeDictionary/API/search/presentation.py b/src/CreeDictionary/API/search/presentation.py index aeb988eed..1c3fab6e5 100644 --- a/src/CreeDictionary/API/search/presentation.py +++ b/src/CreeDictionary/API/search/presentation.py @@ -58,7 +58,7 @@ class _LexicalEntry: entry: List[_ReduplicationResult | SerializedWordform | _InitialChangeResult] text: Optional[str] url: str - id: Optional[str] + id: str | int | None type: LexicalEntryType original_tag: FSTTag @@ -173,9 +173,10 @@ def __init__( show_emoji=self._show_emoji, ) - self.preverbs = [ - lexical_entry["entry"] + self.preverbs: List[SerializedWordform]= [ + cast(SerializedWordform, entry) for lexical_entry in self.lexical_info + for entry in lexical_entry["entry"] if lexical_entry["type"] == "Preverb" ] self.reduplication = [ @@ -453,22 +454,20 @@ def get_lexical_info( animate_emoji: str, show_emoji: str, dict_source: list, -) -> List: +) -> List[dict] : if not result_analysis: return [] result_analysis_tags = result_analysis.prefix_tags first_letters = extract_first_letters(result_analysis) - lexical_info: List = [] + lexical_info: List[_LexicalEntry] = [] for i, tag in enumerate(result_analysis_tags): preverb_result: Optional[Preverb] = None reduplication_string: Optional[str] = None _type: Optional[LexicalEntryType] = None - entry: Optional[ - _ReduplicationResult | SerializedWordform | _InitialChangeResult - ] = None + entry = None if tag in ["RdplW+", "RdplS+"]: reduplication_string = generate_reduplication_string( @@ -488,7 +487,7 @@ def get_lexical_info( # get the actual wordform object and # make sure the result we return is an IPV if preverb_results: - entries: List[_ReduplicationResult | SerializedWordform | _InitialChangeResult] = [] + entries = [] for preverb in preverb_results: lexicon_result = Wordform.objects.get(id=preverb.wordform_id) if lexicon_result: @@ -501,16 +500,16 @@ def get_lexical_info( entries.append(entry) url = "search?q=" + preverb_text _type = "Preverb" - id : Optional[str] = str(cast(SerializedWordform, entries[0])["id"]) + id: Optional[int] = entries[0]["id"] result = _LexicalEntry( - entry=entries, + entry=cast(Any, entries), text=preverb_text, url=url, id=id, type=_type, original_tag=tag, ) - lexical_info.append(serialize_lexical_entry(result)) + lexical_info.append(result) else: # Can't find a match for the preverb in the database. # This happens when searching against the test database for @@ -533,23 +532,23 @@ def get_lexical_info( ).serialize() _type = "Reduplication" if entry and _type != "Preverb" and _type is not None: - url = cast(SerializedWordform, entry).get("lemma_url") + url = entry.get("lemma_url") id = None try: - id = str(cast(SerializedWordform, entry)["id"]) + id = entry[0]["id"] except: id = None - entries = [entry] + entry = [entry] result = _LexicalEntry( - entry=entries, + entry=entry, text=reduplication_string, url=url, id=id, type=_type, original_tag=tag, ) - lexical_info.append(serialize_lexical_entry(result)) - return lexical_info + lexical_info.append(result) + return [serialize_lexical_entry(entry) for entry in lexical_info] def extract_first_letters(analysis: RichAnalysis) -> List[str]: diff --git a/src/CreeDictionary/tests/API_tests/model_test.py b/src/CreeDictionary/tests/API_tests/model_test.py index 56ba91f7d..feed3f0c5 100644 --- a/src/CreeDictionary/tests/API_tests/model_test.py +++ b/src/CreeDictionary/tests/API_tests/model_test.py @@ -227,7 +227,7 @@ def test_search_words_with_reduplication(): search_result = results.pop() assert len(search_result.lexical_info) == 1 - assert search_result.lexical_info[0]["entry"]["text"] == "na-" + assert search_result.lexical_info[0]["entry"][0]["text"] == "na-" assert search_result.lexical_info[0]["type"] == "Reduplication" @@ -241,7 +241,7 @@ def test_search_words_with_inital_change(): search_result = results.pop() assert len(search_result.lexical_info) == 1 - assert search_result.lexical_info[0]["entry"]["text"] == " " + assert search_result.lexical_info[0]["entry"][0]["text"] == " " assert search_result.lexical_info[0]["type"] == "Initial Change"