Skip to content

Commit

Permalink
YDA-5578: add duplicate group check
Browse files Browse the repository at this point in the history
Verify that CSV file does not have duplicate group names, since this
can result in unexpected errors while importing (e.g. trying to create
a group that has already been created earlier during the import).
  • Loading branch information
stsnel committed Jan 24, 2024
1 parent 0efcf57 commit 0e2c5a3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ python_irodsclient==1.1.1
enum34
six
humanize>=0.5
iteration_utilities==0.12.0
dnspython>=2.2.0
backports.functools-lru-cache>=1.6.4
typing_extensions==4.1.1
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'enum34',
'six',
'humanize>=0.5',
'iteration_utilities==0.12.0',
'dnspython>=2.2.0',
'backports.functools-lru-cache>=1.6.4',
'PyYaml'
Expand Down
3 changes: 3 additions & 0 deletions unit-tests/files/no-duplicates.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category,subcategory,groupname,manager
test-automation,initial,groupteama,m.manager@yoda.dev
test-automation,initial,groupteamb,m.manager@yoda.dev
5 changes: 5 additions & 0 deletions unit-tests/files/with-duplicates.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
category,subcategory,groupname,manager
test-automation,initial,groupteama,m.manager@yoda.dev
test-automation,initial,groupteamb,m.manager@yoda.dev
test-automation,initial,data-duplicate,m.manager@yoda.dev
test-automation,initial,data-duplicate,m.manager@yoda.dev
12 changes: 11 additions & 1 deletion unit-tests/test_importgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

sys.path.append("../yclienttools")

from importgroups import _get_duplicate_columns, _process_csv_line, parse_csv_file
from importgroups import _get_duplicate_columns, _get_duplicate_groups, _process_csv_line, parse_csv_file


class ImportGroupsTest(TestCase):
Expand Down Expand Up @@ -139,3 +139,13 @@ def test_parse_invalid_csv_file(self, mock_stderr):
# csv that has too many items in the rows compared to the headers
with self.assertRaises(SystemExit):
parse_csv_file("files/more-entries-than-headers.csv", args, "1.9")

@patch('sys.stderr', new_callable=StringIO)
def test_parse_duplicate_groups(self, mock_stderr):
args = {"offline_check": True}

data_no_duplicates = parse_csv_file("files/no-duplicates.csv", args, "1.9")
self.assertEquals(_get_duplicate_groups(data_no_duplicates), [])

data_with_duplicates = parse_csv_file("files/with-duplicates.csv", args, "1.9")
self.assertEquals(_get_duplicate_groups(data_with_duplicates), ["research-data-duplicate"])
12 changes: 12 additions & 0 deletions yclienttools/importgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import csv
import sys

from iteration_utilities import duplicates, unique_everseen

from yclienttools import common_args, common_config

from yclienttools import session as s
Expand Down Expand Up @@ -110,6 +112,11 @@ def _get_duplicate_columns(fields_list, yoda_version):
return duplicate_fields


def _get_duplicate_groups(row_data):
group_names = list(map(lambda r: r[2], row_data))
return list(unique_everseen(duplicates(group_names)))


def _process_csv_line(line, args, yoda_version):
if ('category' not in line or not len(line['category'])
or 'subcategory' not in line or not len(line['subcategory'])
Expand Down Expand Up @@ -387,6 +394,11 @@ def entry():
_exit_with_error(
"The --creator-user and --creator-zone options are only supported with Yoda versions 1.9 and higher.")

duplicate_groups = _get_duplicate_groups(data)
if duplicate_groups:
_exit_with_error(
"The group list has multiple rows with the same group name(s): " + ",".join(duplicate_groups))

if args.offline_check:
sys.exit(0)

Expand Down

0 comments on commit 0e2c5a3

Please sign in to comment.