Exemplo n.º 1
0
def test_importing(client, bibliography, sign_repository, signs,
                   text_repository):
    allow_signs(signs, sign_repository)
    chapter = ChapterFactory.build()
    allow_references(chapter, bibliography)
    text_repository.create_chapter(chapter)
    next_line_mumber = (cast(
        TextLine,
        chapter.lines[0].variants[0].manuscripts[0].line).line_number.number +
                        1)
    atf = (f"{chapter.lines[0].number.number+1}. bu\n"
           f"{chapter.manuscripts[0].siglum} {next_line_mumber}. ...")

    updated_chapter = attr.evolve(
        chapter,
        lines=(*chapter.lines, *parse_chapter(atf, chapter.manuscripts)),
        signs=("KU ABZ075 ABZ207a\\u002F207b\\u0020X\n\nKU\nABZ075", ),
        parser_version=ATF_PARSER_VERSION,
    )

    body = {"atf": atf}
    post_result = client.simulate_post(create_chapter_url(chapter, "/import"),
                                       body=json.dumps(body))

    assert post_result.status == falcon.HTTP_OK
    assert post_result.json == create_chapter_dto(updated_chapter)

    get_result = client.simulate_get(create_chapter_url(chapter))

    assert get_result.status == falcon.HTTP_OK
    assert get_result.json == create_chapter_dto(updated_chapter)
Exemplo n.º 2
0
def test_updating(client, bibliography, sign_repository, signs,
                  text_repository) -> None:
    allow_signs(signs, sign_repository)
    chapter = ChapterFactory.build()
    allow_references(chapter, bibliography)
    text_repository.create_chapter(chapter)
    updated_chapter = attr.evolve(
        chapter,
        lines=(attr.evolve(chapter.lines[0], number=LineNumber(1, True)), ),
        parser_version=ATF_PARSER_VERSION,
    )

    body = {
        "new": [],
        "deleted": [],
        "edited": [{
            "index": index,
            "line": line
        } for index, line in enumerate(
            create_chapter_dto(updated_chapter)["lines"])],
    }
    post_result = client.simulate_post(create_chapter_url(chapter, "/lines"),
                                       body=json.dumps(body))

    assert post_result.status == falcon.HTTP_OK
    assert post_result.json == create_chapter_dto(updated_chapter)

    get_result = client.simulate_get(create_chapter_url(chapter))

    assert get_result.status == falcon.HTTP_OK
    assert get_result.json == create_chapter_dto(updated_chapter)
def test_find_implicit_chapter(
        database: Database,
        parallel_repository: MongoParallelRepository) -> None:
    chapter = ChapterFactory.build(text_id=TEXT_ID)
    database[CHAPTERS_COLLECTION].insert_one(ChapterSchema().dump(chapter))

    assert parallel_repository.find_implicit_chapter(TEXT_ID) == ChapterName(
        chapter.stage, chapter.version, chapter.name)
Exemplo n.º 4
0
def test_get(client, text_repository):
    chapter = ChapterFactory.build(
        manuscripts=(ManuscriptFactory.build(id=1, references=tuple()), ))
    text_repository.create_chapter(chapter)

    result = client.simulate_get(create_chapter_url(chapter, "/extant_lines"))

    assert result.status == falcon.HTTP_OK
    assert result.json == ExtantLinesSchema().dump(chapter)["extantLines"]
Exemplo n.º 5
0
def test_updating_alignment(client, bibliography, sign_repository, signs,
                            text_repository):
    allow_signs(signs, sign_repository)
    chapter = ChapterFactory.build()
    allow_references(chapter, bibliography)
    text_repository.create_chapter(chapter)
    alignment = 0
    omitted_words = (1, )
    updated_chapter = attr.evolve(
        chapter,
        lines=(attr.evolve(
            chapter.lines[0],
            variants=(attr.evolve(
                chapter.lines[0].variants[0],
                manuscripts=(attr.evolve(
                    chapter.lines[0].variants[0].manuscripts[0],
                    line=TextLine.of_iterable(
                        chapter.lines[0].variants[0].manuscripts[0].line.
                        line_number,
                        (Word.of(
                            [
                                Reading.of_name("ku"),
                                Joiner.hyphen(),
                                BrokenAway.open(),
                                Reading.of_name("nu"),
                                Joiner.hyphen(),
                                Reading.of_name("ši"),
                                BrokenAway.close(),
                            ],
                            alignment=alignment,
                            variant=Word.of(
                                [Logogram.of_name("KU")],
                                language=Language.SUMERIAN,
                            ),
                        ), ),
                    ),
                    omitted_words=omitted_words,
                ), ),
            ), ),
        ), ),
    )

    expected_chapter = ApiChapterSchema().dump(updated_chapter)

    post_result = client.simulate_post(create_chapter_url(
        chapter, "/alignment"),
                                       body=json.dumps(DTO))

    assert post_result.status == falcon.HTTP_OK
    assert post_result.json == expected_chapter

    get_result = client.simulate_get(create_chapter_url(chapter))

    assert get_result.status == falcon.HTTP_OK
    assert get_result.json == expected_chapter
def test_chapter_exists_true(
        database: Database,
        parallel_repository: MongoParallelRepository) -> None:
    chapter = ChapterFactory.build(
        text_id=TEXT_ID,
        stage=CHAPTER_NAME.stage,
        name=CHAPTER_NAME.name,
    )
    database[CHAPTERS_COLLECTION].insert_one(ChapterSchema().dump(chapter))

    assert parallel_repository.chapter_exists(TEXT_ID, CHAPTER_NAME) is True
Exemplo n.º 7
0
def test_import_invalid_entity(client, bibliography, body, expected_status,
                               sign_repository, signs, text_repository):
    allow_signs(signs, sign_repository)
    chapter = ChapterFactory.build()
    allow_references(chapter, bibliography)
    text_repository.create_chapter(chapter)

    post_result = client.simulate_post(create_chapter_url(chapter, "/import"),
                                       body=json.dumps(body))

    assert post_result.status == expected_status
Exemplo n.º 8
0
def test_updating_invalid_lemmatization(dto, expected_status, client,
                                        bibliography, sign_repository, signs,
                                        text_repository):
    allow_signs(signs, sign_repository)
    chapter = ChapterFactory.build()
    allow_references(chapter, bibliography)
    text_repository.create_chapter(chapter)
    post_result = client.simulate_post(create_chapter_url(
        chapter, "/lemmatization"),
                                       body=json.dumps(dto))

    assert post_result.status == expected_status
def test_of() -> None:
    chapter: Chapter = ChapterFactory.build()
    query = TransliterationQuery([["KU"]])
    assert ChapterInfo.of(chapter, query) == ChapterInfo(
        chapter.id_,
        {chapter.manuscripts[0].id: chapter.manuscripts[0].siglum},
        [chapter.lines[0]],
        {
            chapter.manuscripts[0].id: [
                cast(TextLine, chapter.manuscripts[0].colophon.lines[0])
            ]
        },
    )
Exemplo n.º 10
0
def test_searching_texts(client, bibliography, sign_repository, signs,
                         text_repository):
    allow_signs(signs, sign_repository)
    chapter = ChapterFactory.build()
    allow_references(chapter, bibliography)
    text_repository.create_chapter(chapter)

    get_result = client.simulate_get("/textsearch?transliteration=ku")

    assert get_result.status == falcon.HTTP_OK
    assert get_result.json == [
        ChapterInfoSchema().dump(
            ChapterInfo.of(chapter, TransliterationQuery([["KU"]])))
    ]
Exemplo n.º 11
0
def test_chapter_display_of_chapter() -> None:
    text = TextFactory.build()
    chapter = ChapterFactory.build()

    chapter_display = ChapterDisplay.of_chapter(text, chapter)

    assert chapter_display == ChapterDisplay(
        chapter.id_,
        text.name,
        text.has_doi,
        not text.has_multiple_stages,
        tuple(LineDisplay.of_line(line) for line in chapter.lines),
        chapter.record,
    )
    assert chapter_display.title == make_title(chapter.lines[0].translation)
Exemplo n.º 12
0
def test_updating_invalid_stage(client, bibliography, sign_repository, signs,
                                text_repository):
    allow_signs(signs, sign_repository)
    chapter = ChapterFactory.build()
    allow_references(chapter, bibliography)
    text_repository.create_chapter(chapter)

    post_result = client.simulate_post(
        f"/texts/{chapter.text_id.genre.value}"
        f"/{chapter.text_id.category}/{chapter.text_id.index}"
        "/chapters/invalid/any/lemmatization",
        body=json.dumps(DTO),
    )

    assert post_result.status == falcon.HTTP_NOT_FOUND
def test_serialize():
    chapter = ChapterFactory.build()
    line = chapter.lines[0].variants[0].manuscripts[0]
    manuscript = chapter.get_manuscript(line.manuscript_id)
    schema = ManuscriptLineDisplaySchema(context={"manuscripts": chapter.manuscripts})

    assert schema.dump(line) == {
        "siglumDisambiguator": manuscript.siglum_disambiguator,
        "periodModifier": manuscript.period_modifier.value,
        "period": manuscript.period.long_name,
        "provenance": manuscript.provenance.long_name,
        "type": manuscript.type.long_name,
        "labels": [label.to_value() for label in line.labels],
        "line": OneOfLineSchema().dump(line.line),
        "paratext": OneOfLineSchema().dump(line.paratext, many=True),
    }
Exemplo n.º 14
0
def test_get(client, text_repository):
    chapter = ChapterFactory.build(
        lines=tuple(),
        manuscripts=(
            ManuscriptFactory.build(references=tuple()),
            ManuscriptFactory.build(colophon=Text(), references=tuple()),
            ManuscriptFactory.build(references=tuple()),
        ),
    )
    text_repository.create_chapter(chapter)

    result = client.simulate_get(create_chapter_url(chapter, "/colophons"))

    assert result.status == falcon.HTTP_OK
    assert result.json == [{
        "siglum": str(manuscript.siglum),
        "text": TextSchema().dump(manuscript.colophon),
    } for manuscript in chapter.manuscripts
                           if not manuscript.colophon.is_empty]
Exemplo n.º 15
0
def test_updating_strophic_information(client, bibliography, sign_repository,
                                       signs, text_repository):
    allow_signs(signs, sign_repository)
    chapter = ChapterFactory.build()
    allow_references(chapter, bibliography)
    text_repository.create_chapter(chapter)
    updated_chapter = attr.evolve(
        chapter,
        lines=(attr.evolve(
            chapter.lines[0],
            is_second_line_of_parallelism=not chapter.lines[0].
            is_second_line_of_parallelism,
            is_beginning_of_section=not chapter.lines[0].
            is_beginning_of_section,
        ), ),
        parser_version=ATF_PARSER_VERSION,
    )

    body = {
        "new": [],
        "deleted": [],
        "edited": [{
            "index": index,
            "line": line
        } for index, line in enumerate(
            create_chapter_dto(updated_chapter)["lines"])],
    }
    post_result = client.simulate_post(create_chapter_url(chapter, "/lines"),
                                       body=json.dumps(body))

    assert post_result.status == falcon.HTTP_OK
    assert post_result.json == create_chapter_dto(updated_chapter)

    get_result = client.simulate_get(create_chapter_url(chapter))

    assert get_result.status == falcon.HTTP_OK
    assert get_result.json == create_chapter_dto(updated_chapter)
from ebl.tests.corpus.support import ANY_USER
from ebl.tests.factories.corpus import ChapterFactory, TextFactory
from ebl.transliteration.domain.alignment import AlignmentError, AlignmentToken
from ebl.transliteration.domain.atf import ATF_PARSER_VERSION
from ebl.transliteration.domain.enclosure_tokens import BrokenAway
from ebl.transliteration.domain.line_number import LineNumber
from ebl.transliteration.domain.normalized_akkadian import AkkadianWord
from ebl.transliteration.domain.sign_tokens import Reading
from ebl.transliteration.domain.text import Text as Transliteration
from ebl.transliteration.domain.text_line import TextLine
from ebl.transliteration.domain.tokens import Joiner, LanguageShift, ValueToken
from ebl.transliteration.domain.word_tokens import Word

CHAPTERS_COLLECTION = "chapters"
TEXT = TextFactory.build()
CHAPTER = ChapterFactory.build(text_id=TEXT.id)
CHAPTER_WITHOUT_DOCUMENTS = attr.evolve(
    CHAPTER,
    manuscripts=tuple(
        attr.evolve(
            manuscript,
            references=tuple(
                attr.evolve(reference, document=None)
                for reference in manuscript.references),
        ) for manuscript in CHAPTER.manuscripts),
)


def expect_bibliography(bibliography, when) -> None:
    for manuscript in CHAPTER.manuscripts:
        for reference in manuscript.references:
def create(include_documents: bool) -> Tuple[Chapter, dict]:
    references = (ReferenceFactory.build(with_document=include_documents),)
    manuscript = ManuscriptFactory.build(references=references)

    first_manuscript_line = ManuscriptLineFactory.build(manuscript_id=manuscript.id)
    second_manuscript_line = ManuscriptLineFactory.build(manuscript_id=manuscript.id)
    line = LineFactory.build(
        variants=(
            LineVariantFactory.build(
                manuscripts=(first_manuscript_line, second_manuscript_line),
                parallel_lines=(ParallelComposition(False, "name", LineNumber(1)),),
            ),
        )
    )

    chapter = ChapterFactory.build(
        manuscripts=(manuscript,),
        uncertain_fragments=(MuseumNumber.of("K.1"),),
        lines=(line,),
    )
    dto = {
        "textId": {
            "genre": chapter.text_id.genre.value,
            "category": chapter.text_id.category,
            "index": chapter.text_id.index,
        },
        "classification": chapter.classification.value,
        "stage": chapter.stage.value,
        "version": chapter.version,
        "name": chapter.name,
        "order": chapter.order,
        "signs": list(chapter.signs),
        "record": RecordSchema().dump(chapter.record),
        "parserVersion": chapter.parser_version,
        "manuscripts": ApiManuscriptSchema(
            exclude=[] if include_documents else ["joins"]
        ).dump(chapter.manuscripts, many=True),
        "uncertainFragments": [str(number) for number in chapter.uncertain_fragments],
        "lines": [
            {
                "number": line.number.label,
                "variants": [
                    {
                        "reconstruction": "".join(
                            [
                                convert_to_atf(None, variant.reconstruction),
                                f"\n{variant.note.atf}" if variant.note else "",
                                *[
                                    f"\n{parallel_line.atf}"
                                    for parallel_line in variant.parallel_lines
                                ],
                            ]
                        ),
                        "reconstructionTokens": OneOfTokenSchema().dump(
                            variant.reconstruction, many=True
                        ),
                        "intertext": "".join(part.value for part in variant.intertext),
                        "manuscripts": [
                            {
                                "manuscriptId": manuscript_line.manuscript_id,
                                "labels": [
                                    label.to_value() for label in manuscript_line.labels
                                ],
                                "number": manuscript_line.line.line_number.atf[:-1]
                                if isinstance(manuscript_line.line, TextLine)
                                else "",
                                "atf": "\n".join(
                                    [
                                        manuscript_line.line.atf[
                                            len(manuscript_line.line.line_number.atf)
                                            + 1 :
                                        ]
                                        if isinstance(manuscript_line.line, TextLine)
                                        else "",
                                        *[
                                            line.atf
                                            for line in manuscript_line.paratext
                                        ],
                                    ]
                                ).strip(),
                                "atfTokens": (
                                    OneOfLineSchema().dump(manuscript_line.line)[
                                        "content"
                                    ]
                                ),
                                "omittedWords": list(manuscript_line.omitted_words),
                            }
                            for manuscript_line in variant.manuscripts
                        ],
                    }
                    for variant in line.variants
                ],
                "isSecondLineOfParallelism": line.is_second_line_of_parallelism,
                "isBeginningOfSection": line.is_beginning_of_section,
                "translation": "\n".join(
                    translation.atf for translation in line.translation
                ),
            }
            for line in chapter.lines
        ],
    }

    return chapter, dto
Exemplo n.º 18
0
def test_updating_lemmatization(client, bibliography, sign_repository, signs,
                                text_repository):
    allow_signs(signs, sign_repository)
    chapter: Chapter = ChapterFactory.build()
    allow_references(chapter, bibliography)
    text_repository.create_chapter(chapter)
    updated_chapter = attr.evolve(
        chapter,
        lines=(attr.evolve(
            chapter.lines[0],
            variants=(attr.evolve(
                chapter.lines[0].variants[0],
                reconstruction=(
                    chapter.lines[0].variants[0].reconstruction[0],
                    chapter.lines[0].variants[0].reconstruction[1].
                    set_unique_lemma(
                        LemmatizationToken(
                            chapter.lines[0].variants[0].reconstruction[1].
                            value,
                            (WordId("aklu I"), ),
                        )),
                    *chapter.lines[0].variants[0].reconstruction[2:6],
                    chapter.lines[0].variants[0].reconstruction[6].
                    set_unique_lemma(
                        LemmatizationToken(
                            chapter.lines[0].variants[0].reconstruction[6].
                            value,
                            tuple(),
                        )),
                ),
                manuscripts=(attr.evolve(
                    chapter.lines[0].variants[0].manuscripts[0],
                    line=TextLine.of_iterable(
                        chapter.lines[0].variants[0].manuscripts[0].line.
                        line_number,
                        (Word.of(
                            [
                                Reading.of_name("ku"),
                                Joiner.hyphen(),
                                BrokenAway.open(),
                                Reading.of_name("nu"),
                                Joiner.hyphen(),
                                Reading.of_name("ši"),
                                BrokenAway.close(),
                            ],
                            unique_lemma=[WordId("aklu I")],
                        ), ),
                    ),
                ), ),
            ), ),
        ), ),
    )

    expected = create_chapter_dto(updated_chapter)

    post_result = client.simulate_post(create_chapter_url(
        chapter, "/lemmatization"),
                                       body=json.dumps(DTO))

    assert post_result.status == falcon.HTTP_OK
    assert post_result.json == expected

    get_result = client.simulate_get(create_chapter_url(chapter))

    assert get_result.status == falcon.HTTP_OK
    assert get_result.json == expected
from ebl.transliteration.domain.genre import Genre
from ebl.transliteration.domain.transliteration_query import TransliterationQuery

TEXTS_COLLECTION = "texts"
CHAPTERS_COLLECTION = "chapters"
JOINS_COLLECTION = "joins"
MANUSCRIPT_ID = 1
MUSEUM_NUMBER = MuseumNumber("X", "1")
UNCERTAIN_FRAGMET = MuseumNumber("X", "2")
TEXT = TextFactory.build()
CHAPTER = ChapterFactory.build(
    text_id=TEXT.id,
    stage=TEXT.chapters[0].stage,
    name=TEXT.chapters[0].name,
    manuscripts=(ManuscriptFactory.build(id=1,
                                         museum_number=MUSEUM_NUMBER,
                                         accession="",
                                         references=tuple()), ),
    lines=(LineFactory.build(manuscript_id=1,
                             translation=TEXT.chapters[0].translation), ),
    uncertain_fragments=tuple(),
)


def when_text_in_collection(database, text=TEXT) -> None:
    database[TEXTS_COLLECTION].insert_one(
        TextSchema(exclude=["chapters"]).dump(text))


def when_chapter_in_collection(database, chapter=CHAPTER) -> None:
    database[CHAPTERS_COLLECTION].insert_one(ChapterSchema().dump(chapter))
Exemplo n.º 20
0
        f"/texts/invalid/1/chapters/{Stage.STANDARD_BABYLONIAN.value}/any/lines",
        body=json.dumps(EMPTY_EDIT_DTO),
    )

    assert post_result.status == falcon.HTTP_NOT_FOUND


def test_updating_invalid_id(client):
    post_result = client.simulate_post(
        "/texts/1/invalid/chapters/any/any/lines",
        body=json.dumps(EMPTY_EDIT_DTO))

    assert post_result.status == falcon.HTTP_NOT_FOUND


LINE_DTO = ApiLineSchema().dump(ChapterFactory.build().lines[0])

INVALID_LINE = {**LINE_DTO, "invalid": True}

TOO_MANY_NOTES = {
    **LINE_DTO,
    "variants": [{
        **LINE_DTO["variants"][0],
        "reconstruction":
        "kur\n#note: extra note\n#note: extra note",
    }],
}

INVALID_RECONSTRUCTION = {
    **LINE_DTO,
    "variants": [{
Exemplo n.º 21
0
def chapter() -> Chapter:
    return ChapterFactory.build()
UNCERTAIN_FRAGMENTS = (MuseumNumber.of("K.1"), )
FIRST_MANUSCRIPT_LINE = ManuscriptLineFactory.build(
    manuscript_id=MANUSCRIPT.id)
SECOND_MANUSCRIPT_LINE = ManuscriptLineFactory.build(
    manuscript_id=MANUSCRIPT.id)

LINE_VARIANT = LineVariantFactory.build(
    manuscripts=(FIRST_MANUSCRIPT_LINE, SECOND_MANUSCRIPT_LINE),
    parallel_lines=(ParallelComposition(False, "name", LineNumber(2)), ),
    intertext=(StringPart("bar"), ),
)
TRANSLATION_LINE = TranslationLine((StringPart("foo"), ), "en", None)
LINE = LineFactory.build(variants=(LINE_VARIANT, ),
                         translation=(TRANSLATION_LINE, ))
CHAPTER = ChapterFactory.build(manuscripts=(MANUSCRIPT, ),
                               uncertain_fragments=UNCERTAIN_FRAGMENTS,
                               lines=(LINE, ))


def strip_documents(chapter: Chapter) -> Chapter:
    return attr.evolve(
        chapter,
        manuscripts=tuple(
            attr.evolve(
                manuscript,
                references=tuple(
                    attr.evolve(reference, document=None)
                    for reference in MANUSCRIPT.references),
            ) for manuscript in chapter.manuscripts),
    )