Skip to content

Commit

Permalink
YDA-5584: support header column suffixes in some situations
Browse files Browse the repository at this point in the history
  • Loading branch information
claravox authored Jan 31, 2024
1 parent 3a70b3e commit 3706218
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
5 changes: 5 additions & 0 deletions unit-tests/files/csv-import-test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
category,subcategory,groupname,manager,member,member,viewer
test-automation,csv-test,csv-test-group1,[email protected],[email protected],[email protected],[email protected]
test-automation,csv-test,csv-test-group2,[email protected],[email protected],[email protected],[email protected]
test-automation,csv-test,csv-test-group3,[email protected],[email protected],[email protected],[email protected]
test-automation,csv-test,csv-test-group4,[email protected],[email protected],[email protected],[email protected]
3 changes: 3 additions & 0 deletions unit-tests/files/header-with-suffixes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category,subcategory,groupname,manager:alice,member:bob,member:charlie,viewer:dan
test-automation,csv-test,csv-test-group1,[email protected],[email protected],[email protected],[email protected]
test-automation,csv-test,csv-test-group2,[email protected],[email protected],[email protected],[email protected]
44 changes: 42 additions & 2 deletions unit-tests/test_importgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_fully_filled_csv_line_1_9(self):
"manager": ["[email protected]"],
"member": ["[email protected]"],
"viewer": ["[email protected]"],
"expiration_date": ["2025-01-01"],
"expiration_date": ["2030-01-01"],
"schema_id": ["default-3"],
}
expected = (
Expand All @@ -42,7 +42,34 @@ def test_fully_filled_csv_line_1_9(self):
["[email protected]"],
["[email protected]"],
"default-3",
"2025-01-01",
"2030-01-01",
)
result, error_msg = _process_csv_line(d, args, "1.9")
self.assertTupleEqual(expected, result)
self.assertIsNone(error_msg)

def test_fully_filled_csv_line_1_9_with_suffixes(self):
# Confirm support the old csv header format still (with ":nicknameofuser")
args = {"offline_check": True}
d = {
"category": ["test-automation"],
"subcategory": ["initial"],
"groupname": ["groupteama"],
"manager:alice": ["[email protected]"],
"member:bob": ["[email protected]"],
"viewer:eve": ["[email protected]"],
"expiration_date": ["2030-01-01"],
"schema_id": ["default-3"],
}
expected = (
"test-automation",
"initial",
"research-groupteama",
["[email protected]"],
["[email protected]"],
["[email protected]"],
"default-3",
"2030-01-01",
)
result, error_msg = _process_csv_line(d, args, "1.9")
self.assertTupleEqual(expected, result)
Expand Down Expand Up @@ -129,6 +156,19 @@ def test_error_fields_1_8(self):
self.assertIsNone(result)
self.assertIn("1.9", error_msg)

def test_parse_csv(self):
args = {"offline_check": True}
parse_csv_file("files/csv-import-test.csv", args, "1.9")

@patch('sys.stderr', new_callable=StringIO)
def test_parse_csv_with_header_suffixes(self, mock_stderr):
args = {"offline_check": True}
parse_csv_file("files/header-with-suffixes.csv", args, "1.8")

# This header format not supported in 1.9 and higher
with self.assertRaises(SystemExit):
parse_csv_file("files/header-with-suffixes.csv", args, "1.9")

@patch('sys.stderr', new_callable=StringIO)
def test_parse_invalid_csv_file(self, mock_stderr):
# csv that has an unlabeled header
Expand Down
21 changes: 16 additions & 5 deletions yclienttools/importgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,21 @@ def parse_csv_file(input_file, args, yoda_version):

# Check that all header names are valid
possible_labels = _get_csv_possible_labels(yoda_version)
labels_with_optional_suffix = ['viewer', 'member', 'manager']
for label in header:
if label not in possible_labels:
_exit_with_error(
'CSV header contains unknown field "{}"'.format(label))
found_match = False
for opt_label in labels_with_optional_suffix:
if label.startswith('{}:'.format(opt_label)):
found_match = True

if found_match and yoda_version not in ('1.7', '1.8'):
_exit_with_error(
'This script does not support headers with suffixes in '
'Yoda version 1.9 and higher. Field with suffix: "{}"'.format(label))
elif not found_match:
_exit_with_error(
'CSV header contains unknown field "{}"'.format(label))

duplicate_columns = _get_duplicate_columns(header, yoda_version)
if (len(duplicate_columns) > 0):
Expand Down Expand Up @@ -146,11 +157,11 @@ def _process_csv_line(line, args, yoda_version):
if not is_valid:
return None, '"{}" is not a valid username.'.format(item_list[i])

if column_name.lower() == 'manager':
if column_name.lower() == 'manager' or column_name.lower().startswith('manager:'):
managers = item_list
elif column_name.lower() == 'member':
elif column_name.lower() == 'member' or column_name.lower().startswith('member:'):
members = item_list
elif column_name.lower() == 'viewer':
elif column_name.lower() == 'viewer' or column_name.lower().startswith('viewer:'):
viewers = item_list

# perform additional data validations
Expand Down

0 comments on commit 3706218

Please sign in to comment.