def test_search_references(client, fragmentarium, bibliography, user):
    bib_entry_1 = BibliographyEntryFactory.build(id="RN.0", pages="254")
    bib_entry_2 = BibliographyEntryFactory.build(id="RN.1")
    bibliography.create(bib_entry_1, user)
    bibliography.create(bib_entry_2, user)

    fragment = FragmentFactory.build(references=(
        ReferenceFactory.build(id="RN.0", pages="254"),
        ReferenceFactory.build(id="RN.1"),
    ))
    fragmentarium.create(fragment)
    result = client.simulate_get(
        "/fragments",
        params={
            "id": fragment.references[0].id,
            "pages": fragment.references[0].pages
        },
    )

    assert result.status == falcon.HTTP_OK

    fragment_expected = fragment.set_references([
        fragment.references[0].set_document(bib_entry_1),
        fragment.references[1].set_document(bib_entry_2),
    ])
    assert result.json == ApiFragmentInfoSchema(many=True).dump(
        [FragmentInfo.of(fragment_expected)])
    assert "Cache-Control" not in result.headers
コード例 #2
0
def test_inject_document_in_fragment_infos(fragment_finder, when,
                                           bibliography):
    bibliography_entry = BibliographyEntryFactory.build()
    fragment_1 = FragmentInfo.of(
        FragmentFactory.build(
            number="K.1", references=(ReferenceFactory.build(id="RN.0"), )))
    fragment_2 = FragmentInfo.of(
        FragmentFactory.build(
            number="K.2",
            references=(
                ReferenceFactory.build(id="RN.1"),
                ReferenceFactory.build(id="RN.2"),
            ),
        ))
    fragment_expected_1 = fragment_1.set_references(
        [fragment_1.references[0].set_document(bibliography_entry)])
    fragment_expected_2 = fragment_2.set_references([
        fragment_2.references[0].set_document(bibliography_entry),
        fragment_2.references[1].set_document(bibliography_entry),
    ])
    (when(fragment_finder).search_references("id", "pages").thenReturn(
        [fragment_1, fragment_2]))
    (when(bibliography).find("RN.0").thenReturn(bibliography_entry))
    (when(bibliography).find("RN.1").thenReturn(bibliography_entry))
    (when(bibliography).find("RN.2").thenReturn(bibliography_entry))

    assert fragment_finder.search_references_in_fragment_infos(
        "id", "pages") == [
            fragment_expected_1,
            fragment_expected_2,
        ]
def test_create(database, bibliography_repository, create_mongo_bibliography_entry):
    bibliography_entry = BibliographyEntryFactory.build()
    bibliography_repository.create(bibliography_entry)

    assert (
        database[COLLECTION].find_one({"_id": bibliography_entry["id"]})
        == create_mongo_bibliography_entry()
    )
コード例 #4
0
def test_to_dict() -> None:
    bibliography_entry = BibliographyEntryFactory.build()
    reference_with_document = create_reference_with_document(bibliography_entry)

    assert ReferenceSchema().dump(reference_with_document) == {
        **SERIALIZED_REFERENCE,
        "id": reference_with_document.id,
    }
コード例 #5
0
def test_create_entry_invalid(transform, client):
    bibliography_entry = BibliographyEntryFactory.build()
    invalid_entry = transform(bibliography_entry)
    body = json.dumps(invalid_entry)

    put_result = client.simulate_post("/bibliography", body=body)

    assert put_result.status == falcon.HTTP_BAD_REQUEST
コード例 #6
0
def test_update_entry_not_found(client):
    bibliography_entry = BibliographyEntryFactory.build()
    id_ = bibliography_entry["id"]
    body = json.dumps(bibliography_entry)

    post_result = client.simulate_post(f"/bibliography/{id_}", body=body)

    assert post_result.status == falcon.HTTP_NOT_FOUND
def test_find(database, bibliography_repository, create_mongo_bibliography_entry):
    bibliography_entry = BibliographyEntryFactory.build()
    mongo_entry_ = create_mongo_bibliography_entry(bibliography_entry)
    database[COLLECTION].insert_one(mongo_entry_)

    assert (
        bibliography_repository.query_by_id(bibliography_entry["id"])
        == bibliography_entry
    )
def test_update(bibliography_repository):
    bibliography_entry = BibliographyEntryFactory.build()
    updated_entry = pydash.omit({**bibliography_entry, "title": "New Title"}, "PMID")
    bibliography_repository.create(bibliography_entry)
    bibliography_repository.update(updated_entry)

    assert (
        bibliography_repository.query_by_id(bibliography_entry["id"]) == updated_entry
    )
コード例 #9
0
def test_reference() -> None:
    bibliography_entry = BibliographyEntryFactory.build()
    reference_with_document = create_reference_with_document(bibliography_entry)

    assert reference_with_document.id == bibliography_entry["id"]
    assert reference_with_document.type == TYPE
    assert reference_with_document.pages == PAGES
    assert reference_with_document.notes == NOTES
    assert reference_with_document.lines_cited == LINES_CITED
    assert reference_with_document.document == bibliography_entry
コード例 #10
0
def test_search_author_title_year(bibliography, bibliography_repository, when):
    bibliography_entry = BibliographyEntryFactory.build()
    author = bibliography_entry["author"][0]["family"]
    year = bibliography_entry["issued"]["date-parts"][0][0]
    title = bibliography_entry["title"]
    query = f"{author} {year} {title}"
    (when(bibliography_repository).query_by_author_year_and_title(
        author, year, title).thenReturn([bibliography_entry]))
    (verify(bibliography_repository,
            0).query_by_container_title_and_collection_number(
                author, str(year)))
    assert [bibliography_entry] == bibliography.search(query)
コード例 #11
0
def test_from_dict_with_document() -> None:
    bibliography_entry = BibliographyEntryFactory.build()
    reference_with_document = create_reference_with_document(bibliography_entry)

    result = ReferenceSchema().load(
        {
            **SERIALIZED_REFERENCE,
            "id": reference_with_document.id,
            "document": bibliography_entry,
        }
    )

    assert result == reference_with_document
コード例 #12
0
def test_create_entry(client):
    bibliography_entry = BibliographyEntryFactory.build()
    id_ = bibliography_entry["id"]
    body = json.dumps(bibliography_entry)
    post_result = client.simulate_post("/bibliography", body=body)

    assert post_result.status == falcon.HTTP_CREATED
    assert post_result.headers["Location"] == f"/bibliography/{id_}"
    assert post_result.json == bibliography_entry

    get_result = client.simulate_get(f"/bibliography/{id_}")

    assert get_result.json == bibliography_entry
コード例 #13
0
def test_search_container_short_collection_number(bibliography,
                                                  bibliography_repository,
                                                  when):
    bibliography_entry = BibliographyEntryFactory.build()
    container_title = bibliography_entry["container-title-short"]
    collection_number = bibliography_entry["collection-number"]
    query = f"{container_title} {collection_number}"
    (when(bibliography_repository).query_by_author_year_and_title(
        container_title, int(collection_number), None).thenReturn([]))
    (when(bibliography_repository
          ).query_by_container_title_and_collection_number(
              container_title,
              collection_number).thenReturn([bibliography_entry]))
    assert [bibliography_entry] == bibliography.search(query)
コード例 #14
0
def test_update(
    bibliography,
    bibliography_repository,
    user,
    when,
    changelog,
    create_mongo_bibliography_entry,
):
    bibliography_entry = BibliographyEntryFactory.build()
    (when(bibliography_repository).query_by_id(
        bibliography_entry["id"]).thenReturn(bibliography_entry))
    (when(changelog).create(
        COLLECTION,
        user.profile,
        create_mongo_bibliography_entry(),
        create_mongo_bibliography_entry(),
    ).thenReturn())
    (when(bibliography_repository).update(bibliography_entry).thenReturn())
    bibliography.update(bibliography_entry, user)
コード例 #15
0
def test_create(
    bibliography,
    bibliography_repository,
    user,
    changelog,
    when,
    create_mongo_bibliography_entry,
):
    bibliography_entry = BibliographyEntryFactory.build()
    (when(changelog).create(
        COLLECTION,
        user.profile,
        {
            "_id": bibliography_entry["id"]
        },
        create_mongo_bibliography_entry(),
    ).thenReturn())
    (when(bibliography_repository).create(bibliography_entry).thenReturn())

    bibliography.create(bibliography_entry, user)
コード例 #16
0
def test_create_duplicate(
    bibliography,
    user,
    when,
    changelog,
    bibliography_repository,
    create_mongo_bibliography_entry,
):
    bibliography_entry = BibliographyEntryFactory.build()
    (when(changelog).create(
        COLLECTION,
        user.profile,
        {
            "_id": bibliography_entry["id"]
        },
        create_mongo_bibliography_entry(),
    ).thenReturn())
    (when(bibliography_repository).create(bibliography_entry).thenRaise(
        DuplicateError))
    with pytest.raises(DuplicateError):
        bibliography.create(bibliography_entry, user)
コード例 #17
0
def test_entry_not_found(bibliography, bibliography_repository, when):
    bibliography_entry = BibliographyEntryFactory.build()
    (when(bibliography_repository).query_by_id(
        bibliography_entry["id"]).thenRaise(NotFoundError))
    with pytest.raises(NotFoundError):
        bibliography.find(bibliography_entry["id"])
def test_update_not_found(bibliography_repository):
    bibliography_entry = BibliographyEntryFactory.build()
    with pytest.raises(NotFoundError):
        bibliography_repository.update(bibliography_entry)
コード例 #19
0
 def _from_bibliography_entry(
         bibliography_entry: Union[dict, None] = None) -> dict:
     if not bibliography_entry:
         bibliography_entry = BibliographyEntryFactory.build()
     return pydash.map_keys(bibliography_entry, lambda _, key: "_id"
                            if key == "id" else key)
def test_create_duplicate(bibliography_repository):
    bibliography_entry = BibliographyEntryFactory.build()
    bibliography_repository.create(bibliography_entry)
    with pytest.raises(DuplicateError):
        bibliography_repository.create(bibliography_entry)
コード例 #21
0
def saved_entry(bibliography, user):
    bibliography_entry = BibliographyEntryFactory.build()
    bibliography.create(bibliography_entry, user)
    return bibliography_entry
コード例 #22
0
def test_find(bibliography, bibliography_repository, when):
    bibliography_entry = BibliographyEntryFactory.build()
    (when(bibliography_repository).query_by_id(
        bibliography_entry["id"]).thenReturn(bibliography_entry))
    assert bibliography.find(bibliography_entry["id"]) == bibliography_entry