From 4fb843a6ddc204a9a4d149e6932ed65f2e75df6e Mon Sep 17 00:00:00 2001 From: Claudius Ellsel Date: Wed, 15 Jul 2026 18:24:29 +0200 Subject: [PATCH] Test duplicate key namespaces * Cover strict duplicate detection for string blocks. * Verify entries and strings may share a key without conflict. --- tests/test_library.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_library.py b/tests/test_library.py index e0668d0..d94efca 100644 --- a/tests/test_library.py +++ b/tests/test_library.py @@ -4,6 +4,7 @@ from bibtexparser.model import Entry from bibtexparser.model import Field from bibtexparser.model import ImplicitComment +from bibtexparser.model import String def get_dummy_entry(): @@ -17,6 +18,10 @@ def get_dummy_entry(): ) +def get_dummy_string(): + return String(key="duplicateKey", value='"A value"') + + def test_replace_with_duplicates(): """Test that replace() works when there are duplicate values. See issue 404.""" library = Library() @@ -97,6 +102,26 @@ def test_constructor_fails_on_duplicate_by_default(): assert len(library.failed_blocks) == 1 +def test_add_string_fails_on_duplicate_by_default(): + """Strict duplicate handling applies to string blocks as well as entries.""" + library = Library(blocks=[get_dummy_string()]) + + with pytest.raises(ValueError, match="duplicateKey"): + library.add(get_dummy_string()) + + assert len(library.strings) == 1 + assert len(library.failed_blocks) == 0 + + +def test_entry_and_string_keys_use_separate_namespaces(): + """An entry and string may share a key without becoming duplicate blocks.""" + library = Library(blocks=[get_dummy_entry(), get_dummy_string()]) + + assert len(library.entries) == 1 + assert len(library.strings) == 1 + assert len(library.failed_blocks) == 0 + + def test_remove_prefers_identical_instance_over_equal_block(): """With two equal blocks, remove() must remove the passed instance. See issue 537.""" first_comment = ImplicitComment("#same")