Skip to content

Commit

Permalink
fix: Nits on import/export functions and on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Jun 21, 2023
1 parent c2e5099 commit a10c6f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
29 changes: 15 additions & 14 deletions openedx_tagging/core/tagging/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,7 @@ def import_tags(taxonomy: Taxonomy, tags: BytesIO, format: TaxonomyDataFormat, r
if taxonomy.allow_free_text:
raise ValueError(
_(
f"Invalid taxonomy ({taxonomy.id}): You can't import free-from tags taxonomies"
)
)
if format not in TaxonomyDataFormat.__members__.values():
raise ValueError(
_(
f"Invalid format: {format}"
f"Invalid taxonomy ({taxonomy.id}): You cannot import into a free-form taxonomy."
)
)

Expand All @@ -148,14 +142,14 @@ def import_tags(taxonomy: Taxonomy, tags: BytesIO, format: TaxonomyDataFormat, r
csv_reader = csv.DictReader(text_tags)
header_fields = csv_reader.fieldnames
if csv_fields != header_fields:
print("AAAAAAAAAAAA")
raise ValueError(
_(
f"Invalid CSV header: {header_fields}. Must be: {csv_fields}."
)
)
tags_data = list(csv_reader)
else:
# TaxonomyDataFormat.JSON
elif format == TaxonomyDataFormat.JSON:
tags_data = json.load(tags)
if 'tags' not in tags_data:
raise ValueError(
Expand All @@ -164,6 +158,12 @@ def import_tags(taxonomy: Taxonomy, tags: BytesIO, format: TaxonomyDataFormat, r
)
)
tags_data = tags_data.get('tags')
else:
raise ValueError(
_(
f"Invalid format: {format}"
)
)
except ValueError as e:
raise e
finally:
Expand Down Expand Up @@ -194,14 +194,14 @@ def create_update_tag(tag):
if tag_id not in new_tags and tag_id not in updated_tags:
try:
# Update tag
tag_instance = Tag.objects.get(external_id=tag_id)
tag_instance = taxonomy.tag_set.get(external_id=tag_id)
tag_instance.value = tag_name

if tag_instance.parent and (not tag_parent_id or not tag_parent_name):
# if there is no parent in the data import
tag_instance.parent = None
updated_tags.append(tag_id)
except ObjectDoesNotExist:
except Tag.DoesNotExist:
# Create tag
tag_instance = Tag(
taxonomy=taxonomy,
Expand All @@ -219,7 +219,7 @@ def create_update_tag(tag):
return tag_instance
else:
# Returns the created/updated tag from history
return Tag.objects.get(external_id=tag_id)
return taxonomy.tag_set.get(external_id=tag_id)

# Create and update tags
with transaction.atomic():
Expand Down Expand Up @@ -249,7 +249,7 @@ def export_tags(taxonomy: Taxonomy, format: TaxonomyDataFormat) -> str:
if taxonomy.allow_free_text:
raise ValueError(
_(
f"Invalid taxonomy ({taxonomy.id}): You can't export free-from tags taxonomies"
f"Invalid taxonomy ({taxonomy.id}): You cannot import into a free-form taxonomy."
)
)
if format not in TaxonomyDataFormat.__members__.values():
Expand All @@ -259,7 +259,7 @@ def export_tags(taxonomy: Taxonomy, format: TaxonomyDataFormat) -> str:
)
)

# Build list of tags in a dictionary format
# Build tags in a dictionary format
tags = get_tags(taxonomy)
result = []
for tag in tags:
Expand All @@ -285,6 +285,7 @@ def export_tags(taxonomy: Taxonomy, format: TaxonomyDataFormat) -> str:
return csv_string
else:
# TaxonomyDataFormat.JSON
# Verification is made at the beginning before bringing and assembling tags data.
json_result = {
'name': taxonomy.name,
'description': taxonomy.description,
Expand Down
11 changes: 7 additions & 4 deletions tests/openedx_tagging/core/tagging/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ def test_import_tags_csv(self):

def test_import_tags_json(self):
json_data = {"tags": [
{"id": "1", "name": "Tag 1"},
{"id": "2", "name": "Tag 2", "parent_id": "1", "parent_name": "Tag 1"}
{"id": "tag_1", "name": "Tag 1"},
{"id": "tag_2", "name": "Tag 2", "parent_id": "tag_1", "parent_name": "Tag 1"}
]}
json_file = BytesIO(json.dumps(json_data).encode())

Expand Down Expand Up @@ -246,17 +246,20 @@ def test_import_tags_update(self):

def test_import_tags_validations(self):
invalid_csv_data = "id,name,tag_parent_id,tag_parent_name\n1,Tag 1,,\n2,Tag 2,1,Tag 1\n"
invalid_csv_file = BytesIO(invalid_csv_data.encode())
taxonomy = tagging_api.create_taxonomy("Validations_Taxonomy")
taxonomy_free_text = tagging_api.create_taxonomy("Free_Taxonomy", allow_free_text=True)

# Open the file in each test since it always closes even if there is an error
with self.assertRaises(ValueError):
invalid_csv_file = BytesIO(invalid_csv_data.encode())
tagging_api.import_tags(taxonomy_free_text, invalid_csv_file, tagging_api.TaxonomyDataFormat.CSV)

with self.assertRaises(ValueError):
tagging_api.import_tags(taxonomy, "", "XML")
invalid_csv_file = BytesIO(invalid_csv_data.encode())
tagging_api.import_tags(taxonomy, invalid_csv_file, "XML")

with self.assertRaises(ValueError):
invalid_csv_file = BytesIO(invalid_csv_data.encode())
tagging_api.import_tags(taxonomy, invalid_csv_file, tagging_api.TaxonomyDataFormat.CSV)

@ddt.data(
Expand Down

0 comments on commit a10c6f6

Please sign in to comment.