Skip to content

Commit

Permalink
add assertions to verify document state after exceptions
Browse files Browse the repository at this point in the history
- Ensure document state is asserted correctly after exceptions are raised.
- Update test name and add a comment for clarity.
  • Loading branch information
dtrai2 committed Nov 12, 2024
1 parent 4de86e2 commit b6cc4cb
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tests/unit/util/test_helper_add_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ def test_provoke_str_duplicate_in_root_field(self):
document = {"source": {"ip": "8.8.8.8"}, "field": "exists already"}
with pytest.raises(FieldExistsWarning, match=r"could not be written"):
add_field_to(document, "field", "content")
assert document

def test_provoke_str_duplicate_in_dotted_subfield(self):
document = {"source": {"ip": "8.8.8.8"}, "sub": {"field": "exists already"}}
with pytest.raises(FieldExistsWarning, match=r"could not be written"):
add_field_to(document, "sub.field", "content")
assert document

def test_add_dict_content_as_new_root_field(self):
document = {"source": {"ip": "8.8.8.8"}}
Expand All @@ -64,6 +66,7 @@ def test_provoke_dict_duplicate_in_root_field(self):
document = {"source": {"ip": "8.8.8.8"}, "field": {"already_existing": "dict"}}
with pytest.raises(FieldExistsWarning, match=r"could not be written"):
add_field_to(document, "field", {"dict": "content"})
assert document

def test_provoke_dict_duplicate_in_dotted_subfield(self):
document = {"source": {"ip": "8.8.8.8"}, "sub": {"field": {"already_existing": "dict"}}}
Expand Down Expand Up @@ -102,12 +105,14 @@ def test_add_field_to_raises_if_list_should_be_extended_and_overwritten_at_the_s
extends_lists=True,
overwrite_target_field=True,
)
assert document

def test_returns_false_if_dotted_field_value_key_exists(self):
document = {"user": "Franz"}
content = ["user_inlist"]
with pytest.raises(FieldExistsWarning, match=r"could not be written"):
add_field_to(document, "user.in_list", content)
assert document

def test_add_list_with_nested_keys(self):
testdict = {
Expand All @@ -123,9 +128,11 @@ def test_add_list_with_nested_keys(self):
add_field_to(testdict, "key1.key2.key3.key4.key5.list", ["content"], extends_lists=True)
assert testdict == expected

def test_add_value_not_as_list_if_it_is_a_new_value_even_though_extends_lists_is_true(self):
def test_add_field_to_adds_value_not_as_list(self):
# checks if a newly added field is added not as list, even when `extends_list` is True
document = {
"some": "field",
}
add_field_to(document, "new", "list", extends_lists=True)
assert document.get("new") == "list"
assert not isinstance(document.get("new"), list)

0 comments on commit b6cc4cb

Please sign in to comment.