def test_read_then_write_produces_the_same_result(): filepath = os.path.join('tests', 'data', 'huge.bib') bib1 = pybibs.read_file(filepath) out1 = pybibs.write_string(bib1) bib2 = pybibs.read_string(out1) out2 = pybibs.write_string(bib2) assert out1 == out2
def test_search_a_key_with_colon(): data = pybibs.read_string(""" @article{Gurion:2019, } """) results = list(query.search(data, ["Gurion:"])) assert len(results) == 1
def test_read_string(raw): bib = pybibs.read_string(raw) assert len(bib) == 2 entry = bib[0] assert entry["key"] == "israel" assert entry["type"] == "article" assert entry["fields"]["author"] == "Israel, Moshe" assert entry["fields"]["year"] == "2008"
def test_destination_heuristic_empty(): data = pybibs.read_string(""" @article{a, } """) with pytest.raises(click.ClickException, match=".*no paths in the database") as e: internals.destination_heuristic(data)
def test_read_string(raw): bib = pybibs.read_string(raw) assert len(bib) == 2 entry = bib[0] assert entry['key'] == 'israel' assert entry['type'] == 'article' assert entry['fields']['author'] == 'Israel, Moshe' assert entry['fields']['year'] == '2008'
def test_read_bens_multiline(bens_multiline_entry): bib = pybibs.read_string(bens_multiline_entry) fields = bib[0]["fields"] expected_title = "{Radio Time-Domain Signatures of Magnetar Birth}" expected_keywords = "Astrophysics - High Energy Astrophysical Phenomena" assert fields["title"] == expected_title assert fields["keywords"] == expected_keywords assert fields["year"] == "2019"
def test_set_file_exists_already(tmpdir): data = pybibs.read_string(""" @book{key, title=title, } """) entry = data[0] existing_file = tmpdir / "key.txt" existing_file.write("content") with pytest.raises(click.ClickException, match=".* already exists"): internals.set_file(data, entry, existing_file.strpath, tmpdir)
def test_destination_heuristic(tmpdir): data = pybibs.read_string(""" @article{a, file = {~/some/path/a.pdf}, } @article{b, file = {~/some/path/b.pdf}, } """) assert internals.destination_heuristic(data) == "~/some/path"
def test_format_entry(): data = pybibs.read_string(""" @book{tolkien1937hobit, year = {1937}, title = {The Hobbit}, author = {Tolkien, John R. R.}, } """) entry = data[0] assert internals.format_entry(entry, "$year") == "1937" assert internals.format_entry(entry, "$year: $title") == "1937: The Hobbit"
def test_search_with_capitalized_search_term(): """Issue #28""" data = pybibs.read_string(""" @book{asimov1951foundation, year = {1951}, title = {Foundation}, author = {Asimov, Izaac} } """) results = list(query.search(data, ["ASIMOV"])) assert len(results) == 1 assert results[0].entry["fields"]["title"] == "Foundation"
def test_add_duplicate_key(runner, database): # Use pybibs to get the first entry in the DB to re-add args = ["--database", database, "list", "--raw"] result = runner.invoke(bibo.cli, args) data = pybibs.read_string(result.output) to_add = pybibs.write_string([data[0]]) with mock.patch("click.edit") as edit_mock: edit_mock.return_value = to_add result = runner.invoke(bibo.cli, ["--database", database, "add"]) assert result.exit_code == 1 assert "duplicate" in result.output.lower()
def test_search_match_details(): data = pybibs.read_string(""" @book{tolkien1937hobit, year = {1937}, title = {The Hobbit}, author = {Tolkien, John R. R.}, } """) results = list(query.search(data, ["tolkien", "hobbit"])) assert "tolkien" in results[0].match["key"] assert ("author", set(["Tolkien"])) in results[0].match["fields"].items() assert ("title", set(["Hobbit"])) in results[0].match["fields"].items()
def test_search_specific_field_with_capital_letter(): """Issue #27""" data = pybibs.read_string(""" @book{asimov1951foundation, year = {1951}, title = {Foundation}, author = {Asimov, Izaac} } """) results = list(query.search(data, ["author:asimov"])) assert len(results) == 1 assert results[0].entry["fields"]["title"] == "Foundation"
def test_open_multiple_entries_one_exact_match(): data = pybibs.read_string(""" @article{abc, title = {Partial key of abcd}, } @article{abcd, title = {A key that contains a shorter key}, } """) with pytest.raises(click.ClickException): query.get(data, ["ab"]) query.get(data, ["abc"])
def test_set_file_with_destination(example_pdf, tmpdir): data = pybibs.read_string(""" @book{tolkien1937hobit, year = {1937}, title = {The Hobbit}, author = {Tolkien, John R. R.}, } """) entry = data[0] destination = tmpdir / "somewhere_else" os.mkdir(str(destination)) internals.set_file(data, entry, example_pdf, str(destination)) assert os.path.exists(str(destination / entry["key"] + ".pdf"))
def test_search_specific_field(): data = pybibs.read_string(""" @book{tolkien1937hobit, year = {1937}, title = {The Hobbit}, author = {Tolkien, John R. R.}, } @article{1937history, title = {What happened in 1937?}, } """) results = list(query.search(data, ["year:1937"])) assert len(results) == 1 assert results[0].entry["fields"]["title"] == "The Hobbit"
def test_destination_heuristic_multiple_equaly_valid_paths(): data = pybibs.read_string(""" @article{a, file = {~/some/path/a.pdf}, } @article{b, file = {~/other/path/b.pdf}, } """) with pytest.raises( click.ClickException, match=".*there are multiple equally valid paths in the database", ) as e: print(internals.destination_heuristic(data))
def test_add_file_no_copy(runner, database): # Note that this is the actual test pdf. Not the temp copy! filepath = "tests/bibo/example.pdf" with mock.patch("click.edit") as edit_mock: edit_mock.return_value = TO_ADD args = ["--database", database, "add", "--file", filepath, "--no-copy"] result = runner.invoke(bibo.cli, args) with open(database) as f: data = pybibs.read_string(f.read()) for item in data: if item.get("key") == "haidt2001emotional": assert item["fields"]["file"] == os.path.abspath(filepath) break else: raise AssertionError("Item not found")
def test_search_multiple_search_terms(): data = pybibs.read_string(""" @book{tolkien1937hobit, year = {1937}, title = {The Hobbit}, author = {Tolkien, John R. R.}, } @trilogy{tolkien1954lord, title={The Lord of the Rings}, author={Tolkien, John Ronald Reuel}, year={1954}, } """) results = list(query.search(data, ["tolkien", "hobbit"])) assert len(results) == 1 assert results[0].entry["fields"]["title"] == "The Hobbit"
def test_search_single_term(): data = pybibs.read_string(""" @book{tolkien1937hobit, year = {1937}, title = {The Hobbit}, author = {Tolkien, John R. R.}, } @book{asimov1951foundation, year = {1951}, title = {Foundation}, author = {Asimov, Izaac} } """) results = list(query.search(data, ["asimov"])) assert len(results) == 1 assert results[0].entry["fields"]["title"] == "Foundation"
def test_edit_multiple_fields(runner, database): args = [ "--database", database, "edit", "asimov1951foundation", "title=I, robot", "year=1950", ] result = runner.invoke(bibo.cli, args) assert result.exit_code == 0 with open(database) as f: data = pybibs.read_string(f.read()) entry = bibo.query.get(data, "asimov").entry assert entry["fields"]["title"] == "I, robot" assert entry["fields"]["year"] == "1950"
def test_search_or_get_key_with_many_colons(): """Issue #66""" data = pybibs.read_string(""" @article{key:with:many:colons, title = {Key with many colons}, } @book{tolkien1937hobit, year = {1937}, title = {The Hobbit}, author = {Tolkien, John R. R.}, } """) results = list(query.search(data, "key:with:many:colons")) assert len(results) == 1 search_result = query.get(data, "key:with:many:colons") assert search_result.entry["fields"]["title"] == "Key with many colons"
def test_search_multiple_terms_are_anded(): data = pybibs.read_string(""" @book{tolkien1937hobit, year = {1937}, title = {The Hobbit}, author = {Tolkien, John R. R.}, } @trilogy{tolkien1954lord, title={The Lord of the Rings}, author={Tolkien, John Ronald Reuel}, year={1954}, } @book{asimov1951foundation, year = {1951}, title = {Foundation}, author = {Asimov, Izaac} } """) results = list(query.search(data, ["tolkien", "type:book"])) assert len(results) == 1 assert results[0].entry["fields"]["title"] == "The Hobbit"
def test_match(): data = pybibs.read_string(""" @book{tolkien1937hobit, year = {1937}, title = {The Hobbit}, author = {Tolkien, John R. R.}, } """) entry = data[0] assert query._match(entry, "Tolkien") == { "key": {"tolkien"}, "fields": { "author": set(["Tolkien"]) }, } assert query._match(entry, "article") == {} assert query._match(entry, "book") == {"type": set(["book"])} assert query._match(entry, "hobbit") == { "fields": { "title": set(["Hobbit"]) } } assert query._match(entry, "year:193") == { "fields": { "year": set(["193"]) } } assert query._match(entry, "year:1937") == { "fields": { "year": set(["1937"]) } } # Issue 68: A match with field only, no key should return empty set, # not a set with empty string. It breaks the highlighting assert query._match(entry, "year:") == {"fields": {"year": set()}}
def test_read_then_write_produces_the_same_result(database): bib1 = pybibs.read_file(database) out1 = pybibs.write_string(bib1) bib2 = pybibs.read_string(out1) out2 = pybibs.write_string(bib2) assert out1 == out2
def test_preamble_type_entry(): raw = r'@preamble{"Some \latex code"}' bib = pybibs.read_string(raw) assert bib[0] == {"type": "preamble", "body": r'"Some \latex code"'}
def test_comment_type_entry(): raw = "@comment{This is a comment}" bib = pybibs.read_string(raw) assert bib[0] == {"type": "comment", "body": "This is a comment"}
def test_entry_contains_key_and_type(raw): bib = pybibs.read_string(raw) assert bib[0]["key"] == "israel" assert bib[0].get("type") == "article"
def test_string_type_entry(): raw = '@string{foo = "Mrs. Foo"}' bib = pybibs.read_string(raw) assert bib[0]["type"] == "string" assert bib[0]["key"] == "foo" assert bib[0]["val"] == "Mrs. Foo"
def test_read_multiline(multiline_entry): bib = pybibs.read_string(multiline_entry) assert bib[0]["fields"]["author"] == "Israel, Moshe and Yosef, Shlomo"