-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathtest_library.py
68 lines (53 loc) · 2.35 KB
/
test_library.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pytest
from bibtexparser import Library
from bibtexparser.model import Entry
from bibtexparser.model import Field
def get_dummy_entry():
return Entry(
entry_type="article",
key="duplicateKey",
fields=[
Field(key="title", value="A title"),
Field(key="author", value="An author"),
],
)
def test_replace_with_duplicates():
"""Test that replace() works when there are duplicate values. See issue 404."""
library = Library()
library.add(get_dummy_entry())
library.add(get_dummy_entry())
# Test precondition
assert len(library.blocks) == 2
assert len(library.failed_blocks) == 1
replacement_entry = get_dummy_entry()
replacement_entry.fields_dict["title"].value = "A new title"
library.replace(library.failed_blocks[0], replacement_entry, fail_on_duplicate_key=False)
assert len(library.blocks) == 2
assert len(library.failed_blocks) == 1
assert library.failed_blocks[0].ignore_error_block["title"] == "A new title"
replacement_entry_2 = get_dummy_entry()
replacement_entry_2.fields_dict["title"].value = "Another new title"
library.replace(library.entries[0], replacement_entry_2, fail_on_duplicate_key=False)
assert len(library.blocks) == 2
assert len(library.failed_blocks) == 1
# The new block replaces the previous "non-duplicate" and should thus not become a duplicate itself
assert library.entries[0].fields_dict["title"].value == "Another new title"
def test_replace_fail_on_duplicate():
library = Library()
replaceable_entry = get_dummy_entry()
replaceable_entry.key = "Just a regular entry, to be replaced"
future_duplicate_entry = get_dummy_entry()
library.add([replaceable_entry, future_duplicate_entry])
with pytest.raises(ValueError):
library.replace(replaceable_entry, get_dummy_entry(), fail_on_duplicate_key=True)
assert len(library.blocks) == 2
assert len(library.failed_blocks) == 0
assert library.entries[0].key == "Just a regular entry, to be replaced"
assert library.entries[1].key == "duplicateKey"
def test_fail_on_duplicate_add():
library = Library()
library.add(get_dummy_entry())
with pytest.raises(ValueError):
library.add(get_dummy_entry(), fail_on_duplicate_key=True)
assert len(library.blocks) == 2
assert len(library.failed_blocks) == 1