Skip to content

Commit 4e77c0a

Browse files
NRL-1215 Add tests for duplicates of each field in DocumentReference
1 parent 2d38b6a commit 4e77c0a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import json
2+
import pytest
3+
from json_duplicate_checker import check_duplicate_keys
4+
from layer.nrlf.tests.data import load_document_reference_data
5+
6+
7+
def get_all_fields_from_json(json_str):
8+
def extract_fields(data, parent_key=''):
9+
fields = []
10+
if isinstance(data, dict):
11+
for k, v in data.items():
12+
full_key = f"{parent_key}.{k}" if parent_key else k
13+
fields.append(full_key)
14+
fields.extend(extract_fields(v, full_key))
15+
elif isinstance(data, list):
16+
for i, item in enumerate(data):
17+
full_key = f"{parent_key}[{i}]"
18+
fields.extend(extract_fields(item, full_key))
19+
return fields
20+
21+
data = json.loads(json_str)
22+
return extract_fields(data)
23+
24+
def duplicate_field_in_json(json_str, field_path):
25+
data = json.loads(json_str)
26+
path = field_path.replace(']', '').replace('[', '.').split('.')
27+
current = data
28+
for key in path[:-1]:
29+
current = current[int(key) if key.isdigit() else key]
30+
field = path[-1]
31+
32+
if field in current:
33+
duplicate_field = f"{field}_duplicate"
34+
current[duplicate_field] = current[field]
35+
modified_json_str = json.dumps(data)
36+
# Replace the duplicate field name with the original field name to simulate duplication
37+
duplicated_json_str = modified_json_str.replace(f'"{duplicate_field}":', f'"{field}":', 1)
38+
return duplicated_json_str
39+
return json_str
40+
41+
def load_document_reference_data_with_all_fields():
42+
docref_body = load_document_reference_data("Y05868-736253002-Valid")
43+
return get_all_fields_from_json(docref_body)
44+
45+
@pytest.mark.parametrize("field", load_document_reference_data_with_all_fields())
46+
def test_parse_body_valid_docref_with_duplicate_keys(field):
47+
docref_body = load_document_reference_data("Y05868-736253002-Valid")
48+
49+
docref_body = duplicate_field_in_json(docref_body, field)
50+
51+
result = check_duplicate_keys(docref_body)
52+
53+
node = field.split(".")[-1]
54+
assert result[0] == [node]
55+
assert result[1] == [f"root.{field}"]

0 commit comments

Comments
 (0)