Skip to content

Commit

Permalink
Fix lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzohrab committed Oct 21, 2023
1 parent b94d275 commit 393ee59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
49 changes: 32 additions & 17 deletions lute/term/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def find(self, langid, text):
Return a Term business object for the DBTerm with the langid and text.
If no match, return None.
"""
dbt = self._find_db_term_by_langid_and_text(langid, text)
spec = self._search_spec_term(langid, text)
dbt = self._find_db_term_by_spec(spec)
if dbt is None:
return None
return self._build_business_term(dbt)
Expand All @@ -65,10 +66,13 @@ def find_or_new(self, langid, text):
Return a Term business object for the DBTerm with the langid and text.
If no match, return a new term with the text and language.
"""
dbt = self._find_db_term_by_langid_and_text(langid, text)
if dbt is None:
return None
return self._build_business_term(dbt)
t = self.find(langid, text)
if t is not None:
return t
t = Term()
t.language_id = langid
t.text = text
return t


def find_matches(self, langid, text, max_results=50):
Expand All @@ -77,9 +81,8 @@ def find_matches(self, langid, text, max_results=50):
with the same langid, matching the text.
If no match, return [].
"""

lang = Language.find(langid)
text_lc = lang.get_lowercase(text)
spec = self._search_spec_term(langid, text)
text_lc = spec.text_lc
search = text_lc.strip() if text_lc else ''
if search == '':
return []
Expand Down Expand Up @@ -133,10 +136,22 @@ def commit(self):
self.db.session.commit()


def _find_db_term_by_langid_and_text(self, langid, text):
"Find by the given language ID and text."
def _search_spec_term(self, langid, text):
"""
Make a term to get the correct text_lc to search for.
Creating a term does parsing and correct downcasing,
so term.language.id and term.text_lc match what the
db would contain.
"""
lang = Language.find(langid)
text_lc = lang.get_lowercase(text)
return DBTerm(lang, text)


def _find_db_term_by_spec(self, spec):
"Find by the given spec term's language ID and text."
langid = spec.language.id
text_lc = spec.text_lc
query = db.session.query(DBTerm).filter(
DBTerm.language_id == langid,
DBTerm.text_lc == text_lc
Expand All @@ -149,17 +164,15 @@ def _find_db_term_by_langid_and_text(self, langid, text):

def _build_db_term(self, term):
"Convert a term business object to a DBTerm."
lang = Language.find(term.language_id)
if lang is None:
raise ValueError(f'Unknown language {term.language_id} for term')
if term.text is None:
raise ValueError('Text not set for term')

t = self._find_db_term_by_langid_and_text(lang.id, term.text)
spec = self._search_spec_term(term.language_id, term.text)
t = self._find_db_term_by_spec(spec)
if t is None:
t = DBTerm()

t.language = lang
t.language = spec.language
t.text = term.text
t.original_text = term.text
t.status = term.status
Expand All @@ -175,6 +188,7 @@ def _build_db_term(self, term):
t.add_term_tag(tt)

termparents = []
lang = spec.language
create_parents = [
p for p in term.parents
if p is not None and p != '' and
Expand All @@ -191,7 +205,8 @@ def _build_db_term(self, term):


def _find_or_create_parent(self, pt, language, term, termtags) -> DBTerm:
p = self._find_db_term_by_langid_and_text(language.id, pt)
spec = self._search_spec_term(language.id, pt)
p = self._find_db_term_by_spec(spec)

if p is not None:
if (p.translation or '') == '':
Expand Down
26 changes: 13 additions & 13 deletions tests/unit/term/test_Repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_save_new_multiword(app_context, hello_term, repo):

repo.commit()
zws = "\u200B"
assert_sql_result(sql, [ f"HELLO{zws} {zws}THERE; hello{zws} {zws}there; 1" ], 'Saved')
assert_sql_result(sql, [ f"HELLO{zws} {zws}THERE; hello{zws} {zws}there; 3" ], 'Saved')


def test_save_updates_existing(english, app_context, hello_term, repo):
Expand Down Expand Up @@ -268,16 +268,16 @@ def test_find_only_looks_in_specified_language(spanish, english, repo):


def test_find_existing_multi_word(spanish, repo):
"Spaces etc handled correctly."
"Domain objects don't have zero-width strings in them."
add_terms(spanish, ['una bebida'])
zws = "\u200B"
t = repo.find(spanish.id, f"una{zws} {zws}bebida")
assert t.id > 0
assert t.text == f"una{zws} {zws}bebida"
assert t.text == "una bebida"

t = repo.find(spanish.id, f"una bebida")
t = repo.find(spanish.id, "una bebida")
assert t.id > 0
assert t.text == f"una{zws} {zws}bebida"
assert t.text == "una bebida"


## Find or new tests.
Expand All @@ -293,7 +293,7 @@ def test_find_or_new_existing_word(spanish, repo):
def test_find_or_new_non_existing(spanish, repo):
"Returns new term."
t = repo.find_or_new(spanish.id, 'TENGO')
assert t.id == 0
assert t.id is None
assert t.text == "TENGO"


Expand All @@ -303,23 +303,23 @@ def test_find_or_new_existing_multi_word(spanish, repo):
zws = "\u200B"
t = repo.find_or_new(spanish.id, f"una{zws} {zws}bebida")
assert t.id > 0
assert t.text == f"una{zws} {zws}bebida"
assert t.text == "una bebida"

t = repo.find_or_new(spanish.id, f"una bebida")
t = repo.find_or_new(spanish.id, "una bebida")
assert t.id > 0
assert t.text == f"una{zws} {zws}bebida"
assert t.text == "una bebida"


def test_find_or_new_new_multi_word(spanish, repo):
"ZWS added correctly."
zws = "\u200B"
t = repo.find_or_new(spanish.id, f"una{zws} {zws}bebida")
assert t.id == 0
assert t.id is None
assert t.text == f"una{zws} {zws}bebida"

t = repo.find_or_new(spanish.id, f"una bebida")
assert t.id == 0
assert t.text == f"una{zws} {zws}bebida"
t = repo.find_or_new(spanish.id, "una bebida")
assert t.id is None
assert t.text == "una bebida"


## Matches tests.
Expand Down

0 comments on commit 393ee59

Please sign in to comment.