Skip to content

Commit 20ab3ae

Browse files
committed
Merge branch 'EIR-11-duplicate' into 'master'
EIR-11: Fix duplicate entry checker for composite primary keys See merge request voereir/pre-commit-hooks!4
2 parents 468cda3 + ee7cee9 commit 20ab3ae

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

pre_commit_hooks/notify_duplicate_entry.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@
44
from typing import Sequence
55
from pathlib import Path
66

7-
def _check_duplicate_entry(json_contents, key):
8-
json_dict = {}
9-
duplicate_uuids = set()
10-
for row in json_contents:
11-
if row[key] not in json_dict:
12-
json_dict[row[key]] = row
7+
8+
def _check_duplicate_entry(json_entries, pkeys):
9+
""" Check duplicate entry based on pkey criteria.
10+
11+
:param json_entries: List of json entries
12+
:param pkeys: List of Primary keys
13+
:return: list of duplicated entry pkey value tuples
14+
"""
15+
unique_entries = set()
16+
duplicate_entries = set()
17+
for entry in json_entries:
18+
pkey_value_tuple = tuple(entry[pkey] for pkey in pkeys)
19+
if pkey_value_tuple not in unique_entries:
20+
unique_entries.add(pkey_value_tuple)
1321
else:
14-
duplicate_uuids.add(row[key])
15-
return duplicate_uuids, len(duplicate_uuids)
22+
duplicate_entries.add(pkey_value_tuple)
23+
return duplicate_entries, len(duplicate_entries)
1624

1725

1826
def main(argv: Optional[Sequence[str]] = None) -> int:
@@ -21,19 +29,27 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
2129
help='Names of the JSON files to check duplicate entries'
2230
)
2331
table_uuid_mapping = {
24-
'action': 'uuid', 'env_property_group': 'uuid',
25-
'environment': 'uuid', 'environment_property': 'code',
26-
'report_summary': 'uuid',
27-
'runner': 'uuid', 'scenario': 'uuid',
28-
'sla': 'uuid', 'sla_scenario_association': 'sla', 'tag': 'uuid',
29-
'tag_action_association': 'tag_uuid',
30-
'tag_case_association': 'test_case_uuid',
31-
'teams': 'uuid',
32-
'test_case': 'uuid',
33-
'test_suit': 'uuid', 'test_supported_version': 'test_case_uuid',
34-
'testcase_workload_association': 'uuid', 'user': 'uuid',
35-
'user_tokens': 'user_token', 'workflow_task': 'workflow_id',
36-
'context': 'uuid',
32+
'action': ['uuid'],
33+
'env_property_group': ['uuid'],
34+
'environment': ['uuid'],
35+
'environment_property': ['code'],
36+
'report_summary': ['uuid'],
37+
'runner': ['uuid'],
38+
'scenario': ['uuid'],
39+
'sla': ['uuid'],
40+
'sla_scenario_association': ['sla', 'scenario'],
41+
'tag': ['uuid'],
42+
'tag_action_association': ['tag_uuid', 'action_uuid'],
43+
'tag_case_association': ['test_case_uuid', 'tag_uuid'],
44+
'teams': ['uuid'],
45+
'test_case': ['uuid'],
46+
'test_suit': ['uuid'],
47+
'test_supported_version': ['test_case_uuid', 'version'],
48+
'testcase_workload_association': ['uuid'],
49+
'user': ['uuid'],
50+
'user_tokens': ['user_token'],
51+
'workflow_task': ['workflow_id'],
52+
'context': ['uuid'],
3753
}
3854

3955
args = vars(parser.parse_args(argv))
@@ -43,13 +59,13 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
4359
for i in range(len(filenames)):
4460
json_file = filenames[i]
4561
file_name = Path(filenames[i]).stem
46-
key = table_uuid_mapping[file_name]
62+
pkeys = table_uuid_mapping[file_name]
4763
with open(json_file, encoding='UTF-8') as f:
48-
contents = json.load(f)
49-
duplicate_uuids, status = _check_duplicate_entry(contents, key)
64+
json_entries = json.load(f)
65+
duplicate_entries, status = _check_duplicate_entry(json_entries, pkeys)
5066

5167
if status:
52-
print(f"Duplicate UUIDs found - {duplicate_uuids} in file "
68+
print(f"Duplicate entries found - {duplicate_entries} in file "
5369
f"{json_file}")
5470
flag = True
5571

0 commit comments

Comments
 (0)