def test_number_deserialization():
    number = MuseumNumber.of("Z.1.b")
    fragment = FragmentSchema().load({
        **FragmentSchema().dump(LemmatizedFragmentFactory.build()),
        "museumNumber":
        MuseumNumberSchema().dump(number),
    })
    assert fragment.number == number
 def create(self, fragment):
     return self._fragments.insert_one(
         {
             "_id": str(fragment.number),
             **FragmentSchema(exclude=["joins"]).dump(fragment),
         }
     )
def test_fragment_exists_true(
        database: Database,
        parallel_repository: MongoParallelRepository) -> None:
    fragment = FragmentFactory.build(number=MUSEUM_NUMBER)
    database[FRAGMENTS_COLLECTION].insert_one(FragmentSchema().dump(fragment))

    assert parallel_repository.fragment_exists(MUSEUM_NUMBER) is True
 def _create_changlelog(self, user: User, fragment: Fragment,
                        updated_fragment: Fragment) -> None:
     schema = FragmentSchema()
     fragment_id = str(fragment.number)
     self._changelog.create(
         COLLECTION,
         user.profile,
         {
             "_id": fragment_id,
             **schema.dump(fragment)
         },
         {
             "_id": fragment_id,
             **schema.dump(updated_fragment)
         },
     )
def test_create(database, fragment_repository):
    fragment = LemmatizedFragmentFactory.build()
    fragment_id = fragment_repository.create(fragment)

    assert fragment_id == str(fragment.number)
    assert database[COLLECTION].find_one(
        {"_id": fragment_id}, projection={"_id": False}
    ) == FragmentSchema(exclude=["joins"]).dump(fragment)
def test_query_by_museum_number_references(
    database, fragment_repository, bibliography_repository
):
    reference = ReferenceFactory.build(with_document=True)
    fragment = LemmatizedFragmentFactory.build(references=(reference,))
    database[COLLECTION].insert_one(FragmentSchema(exclude=["joins"]).dump(fragment))
    bibliography_repository.create(reference.document)
    assert fragment_repository.query_by_museum_number(fragment.number) == fragment
 def update_transliteration(self, fragment):
     self._fragments.update_one(
         fragment_is(fragment),
         {
             "$set": FragmentSchema(
                 only=("text", "notes", "signs", "record", "line_to_vec")
             ).dump(fragment)
         },
     )
def test_query_by_parallel_line_exists(database, fragment_repository):
    parallel_number = MuseumNumber.of("K.1")
    fragment = FragmentFactory.build(
        text=Text(
            (
                ParallelFragment(
                    False, parallel_number, True, Labels(), LineNumber(1), True
                ),
            )
        )
    )
    parallel_fragment = FragmentFactory.build(number=parallel_number)
    database[COLLECTION].insert_many(
        [
            FragmentSchema(exclude=["joins"]).dump(fragment),
            FragmentSchema(exclude=["joins"]).dump(parallel_fragment),
        ]
    )

    assert fragment_repository.query_by_museum_number(fragment.number) == fragment
 def query_by_museum_number(self, number: MuseumNumber):
     data = self._fragments.aggregate(
         [
             {"$match": museum_number_is(number)},
             *join_reference_documents(),
             *join_joins(),
         ]
     )
     try:
         fragment_data = next(data)
         return FragmentSchema(unknown=EXCLUDE).load(fragment_data)
     except StopIteration as error:
         raise NotFoundError(f"Fragment {number} not found.") from error
def test_query_by_museum_number_joins(database, fragment_repository):
    museum_number = MuseumNumber("X", "1")
    first_join = Join(museum_number, is_in_fragmentarium=True)
    second_join = Join(MuseumNumber("X", "2"), is_in_fragmentarium=False)
    fragment = LemmatizedFragmentFactory.build(
        number=museum_number, joins=Joins(((first_join,), (second_join,)))
    )
    database[COLLECTION].insert_one(FragmentSchema(exclude=["joins"]).dump(fragment))
    database[JOINS_COLLECTION].insert_one(
        {
            "fragments": [
                {
                    **JoinSchema(exclude=["is_in_fragmentarium"]).dump(first_join),
                    "group": 0,
                },
                {
                    **JoinSchema(exclude=["is_in_fragmentarium"]).dump(second_join),
                    "group": 1,
                },
            ]
        }
    )
    assert fragment_repository.query_by_museum_number(fragment.number) == fragment
 def update_lemmatization(self, fragment):
     self._fragments.update_one(
         fragment_is(fragment),
         {"$set": FragmentSchema(only=("text",)).dump(fragment)},
     )
def test_query_by_museum_number(database, fragment_repository):
    fragment = LemmatizedFragmentFactory.build()
    database[COLLECTION].insert_one(FragmentSchema(exclude=["joins"]).dump(fragment))

    assert fragment_repository.query_by_museum_number(fragment.number) == fragment
                            Reading.of_name("šu"),
                        ],
                        unique_lemma=(WordId("ūsu I"),),
                    ),
                    AkkadianWord.of(
                        [ValueToken.of("ana")], unique_lemma=(WordId("normalized I"),)
                    ),
                ),
            ),
        )
    ),
    signs="MI DIŠ DIŠ UD ŠU",
)


SCHEMA = FragmentSchema()


def test_create(database, fragment_repository):
    fragment = LemmatizedFragmentFactory.build()
    fragment_id = fragment_repository.create(fragment)

    assert fragment_id == str(fragment.number)
    assert database[COLLECTION].find_one(
        {"_id": fragment_id}, projection={"_id": False}
    ) == FragmentSchema(exclude=["joins"]).dump(fragment)


def test_create_join(database, fragment_repository):
    first_join = JoinFactory.build()
    second_join = JoinFactory.build()
 def _map_fragments(self, cursor):
     return FragmentSchema(unknown=EXCLUDE, many=True).load(cursor)
 def update_references(self, fragment):
     self._fragments.update_one(
         fragment_is(fragment),
         {"$set": FragmentSchema(only=("references",)).dump(fragment)},
     )
def test_default_joins():
    fragment = LemmatizedFragmentFactory.build(joins=Joins())
    data = FragmentSchema(exclude=["joins"]).dump(fragment)
    assert FragmentSchema().load(data) == fragment
def test_number_serialization():
    fragment = LemmatizedFragmentFactory.build()
    data = FragmentSchema().dump(fragment)
    assert data["museumNumber"] == MuseumNumberSchema().dump(fragment.number)
def test_serialization_and_deserialization():
    fragment = LemmatizedFragmentFactory.build(
        joins=Joins(((Join(MuseumNumber("X", "1")), ), )))
    schema = FragmentSchema()
    data = schema.dump(fragment)
    assert schema.load(data) == fragment