Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug Archaeology Updater #512

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions ebl/fragmentarium/application/fragment_repository.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import List, Sequence, Optional
from typing import List, Sequence, Optional, Literal
from ebl.common.domain.scopes import Scope
from ebl.common.query.query_result import QueryResult, AfORegisterToFragmentQueryResult

Expand All @@ -10,6 +10,18 @@
from ebl.transliteration.domain.museum_number import MuseumNumber
from ebl.fragmentarium.domain.date import Date

UpdatableField = Literal[
"introduction",
"text",
"genres",
"references",
"script",
"notes",
"archaeology",
"date",
"dates_in_text",
]


class FragmentRepository(ABC):
@abstractmethod
Expand Down Expand Up @@ -90,7 +102,11 @@
...

@abstractmethod
def update_field(self, field: str, fragment: Fragment) -> None:
def update_transliteration(self, fragment: Fragment) -> None:
...
Dismissed Show dismissed Hide dismissed

@abstractmethod
def update_field(self, field: UpdatableField, fragment: Fragment) -> None:
...

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions ebl/fragmentarium/application/fragment_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def update_transliteration(
else fragment.update_lowest_join_transliteration(transliteration, user)
)
self._create_changelog(user, fragment, updated_fragment)
self._repository.update_field("transliteration", updated_fragment)
self._repository.update_transliteration(updated_fragment)

return self._create_result(updated_fragment)

Expand Down Expand Up @@ -123,7 +123,7 @@ def update_lemmatization(
updated_fragment = fragment.update_lemmatization(lemmatization)

self._create_changelog(user, fragment, updated_fragment)
self._repository.update_field("lemmatization", updated_fragment)
self._repository.update_field("text", updated_fragment)

return self._create_result(updated_fragment)

Expand Down
48 changes: 24 additions & 24 deletions ebl/fragmentarium/infrastructure/mongo_fragment_repository.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional, Sequence, Iterator
from typing import List, Optional, Sequence, Iterator, get_args

import pymongo
from marshmallow import EXCLUDE
Expand All @@ -13,7 +13,10 @@
)
from ebl.errors import NotFoundError
from ebl.fragmentarium.application.fragment_info_schema import FragmentInfoSchema
from ebl.fragmentarium.application.fragment_repository import FragmentRepository
from ebl.fragmentarium.application.fragment_repository import (
FragmentRepository,
UpdatableField,
)
from ebl.fragmentarium.application.fragment_schema import FragmentSchema, ScriptSchema
from ebl.fragmentarium.application.joins_schema import JoinSchema
from ebl.fragmentarium.application.line_to_vec import LineToVecEntry
Expand Down Expand Up @@ -274,33 +277,30 @@ def query_by_transliterated_not_revised_by_other(
)
return FragmentInfoSchema(many=True).load(cursor)

def update_field(self, field, fragment):
fields_to_update = {
"introduction": ("introduction",),
"lemmatization": ("text",),
"genres": ("genres",),
"references": ("references",),
"script": ("script",),
"notes": ("notes",),
"archaeology": ("archaeology",),
"transliteration": (
"text",
"signs",
"record",
"line_to_vec",
),
"date": ("date",),
"dates_in_text": ("dates_in_text",),
}
def update_transliteration(self, fragment: Fragment):
self._fragments.update_one(
fragment_is(fragment),
{
"$set": FragmentSchema(
only=(
"text",
"signs",
"record",
"line_to_vec",
)
).dump(fragment)
},
)

if field not in fields_to_update:
def update_field(self, field: UpdatableField, fragment: Fragment):
if field not in (valid_fields := get_args(UpdatableField)):
raise ValueError(
f"Unexpected update field {field}, must be one of {','.join(fields_to_update)}"
f"Unexpected update field {field}, must be one of {valid_fields}"
)
query = FragmentSchema(only=fields_to_update[field]).dump(fragment)
query = FragmentSchema(only=(field,)).dump(fragment)
self._fragments.update_one(
fragment_is(fragment),
{"$set": query if query else {field: None}},
{"$set": query or {field: None}},
)
fsimonjetz marked this conversation as resolved.
Show resolved Hide resolved

def query_next_and_previous_folio(self, folio_name, folio_number, number):
Expand Down
17 changes: 12 additions & 5 deletions ebl/tests/fragmentarium/test_fragment_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def test_update_transliteration_with_record(fragment_repository, user):
TransliterationUpdate(parse_atf_lark("$ (the transliteration)")), user
)

fragment_repository.update_field("transliteration", updated_fragment)
fragment_repository.update_transliteration(updated_fragment)
result = fragment_repository.query_by_museum_number(fragment.number)

assert result == updated_fragment
Expand All @@ -367,7 +367,7 @@ def test_update_transliteration_with_record(fragment_repository, user):
def test_update_update_transliteration_not_found(fragment_repository):
transliterated_fragment = TransliteratedFragmentFactory.build()
with pytest.raises(NotFoundError):
fragment_repository.update_field("transliteration", transliterated_fragment)
fragment_repository.update_transliteration(transliterated_fragment)


def test_update_genres(fragment_repository):
Expand Down Expand Up @@ -410,7 +410,7 @@ def test_update_lemmatization(fragment_repository):
lemmatization = Lemmatization(tokens)
updated_fragment = transliterated_fragment.update_lemmatization(lemmatization)

fragment_repository.update_field("lemmatization", updated_fragment)
fragment_repository.update_field("text", updated_fragment)
result = fragment_repository.query_by_museum_number(transliterated_fragment.number)

assert result == updated_fragment
Expand Down Expand Up @@ -446,10 +446,17 @@ def test_update_script(fragment_repository: FragmentRepository):
assert result == updated_fragment


def test_update_update_lemmatization_not_found(fragment_repository):
def test_update_lemmatization_not_found(fragment_repository):
transliterated_fragment = TransliteratedFragmentFactory.build()
with pytest.raises(NotFoundError):
fragment_repository.update_field("lemmatization", transliterated_fragment)
fragment_repository.update_field("text", transliterated_fragment)


def test_update_invalid_field_fails(fragment_repository):
with pytest.raises(ValueError, match="Unexpected update field"):
fragment_repository.update_field(
"some invalid field name", FragmentFactory.build()
)


def test_statistics(database, fragment_repository):
Expand Down
6 changes: 2 additions & 4 deletions ebl/tests/fragmentarium/test_fragment_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_update_transliteration(
).thenReturn()
(
when(fragment_repository)
.update_field("transliteration", transliterated_fragment)
.update_transliteration(transliterated_fragment)
.thenReturn()
)

Expand Down Expand Up @@ -208,9 +208,7 @@ def test_update_lemmatization(
{"_id": str(number), **SCHEMA.dump(transliterated_fragment)},
{"_id": str(number), **SCHEMA.dump(lemmatized_fragment)},
).thenReturn()
when(fragment_repository).update_field(
"lemmatization", lemmatized_fragment
).thenReturn()
when(fragment_repository).update_field("text", lemmatized_fragment).thenReturn()

result = fragment_updater.update_lemmatization(number, lemmatization, user)
assert result == (injected_fragment, False)
Expand Down
Loading