From 1dac0df15b77e5585f5856288dd11c4077796a2f Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Tue, 14 Mar 2023 17:04:30 -0700 Subject: [PATCH] Adds regression test for and fixes metadata label syncing. --- .../contentcuration/tests/test_sync.py | 55 +++++++++++++++++++ contentcuration/contentcuration/utils/sync.py | 24 ++++++-- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/contentcuration/contentcuration/tests/test_sync.py b/contentcuration/contentcuration/tests/test_sync.py index 0f2459126b..07580b2d48 100644 --- a/contentcuration/contentcuration/tests/test_sync.py +++ b/contentcuration/contentcuration/tests/test_sync.py @@ -6,6 +6,12 @@ from le_utils.constants import content_kinds from le_utils.constants import file_formats from le_utils.constants import format_presets +from le_utils.constants.labels import accessibility_categories +from le_utils.constants.labels import learning_activities +from le_utils.constants.labels import levels +from le_utils.constants.labels import needs +from le_utils.constants.labels import resource_type +from le_utils.constants.labels import subjects from .base import StudioTestCase from .testdata import create_temp_file @@ -270,6 +276,55 @@ def test_sync_tags_add_multiple_tags(self): self.assertTrue(self.derivative_channel.has_changes()) + def test_sync_channel_metadata_labels(self): + """ + Test that calling sync channel will also bring in metadata label updates. + """ + + self.assertFalse(self.channel.has_changes()) + self.assertFalse(self.derivative_channel.has_changes()) + + labels = { + "categories": subjects.MATHEMATICS, + "learner_needs": needs.PRIOR_KNOWLEDGE, + "accessibility_labels": accessibility_categories.CAPTIONS_SUBTITLES, + "grade_levels": levels.LOWER_SECONDARY, + "resource_types": resource_type.LESSON_PLAN, + "learning_activities": learning_activities.LISTEN, + } + + contentnode = ( + self.channel.main_tree.get_descendants() + .exclude(kind_id=content_kinds.TOPIC) + .first() + ) + + target_child = self.derivative_channel.main_tree.get_descendants().get( + source_node_id=contentnode.node_id + ) + + self.assertIsNotNone(target_child) + + for key, value in labels.items(): + setattr(contentnode, key, {value: True}) + contentnode.save() + + sync_channel( + self.derivative_channel, + sync_attributes=True, + sync_tags=False, + sync_files=False, + sync_assessment_items=False, + ) + + self.assertTrue(self.channel.has_changes()) + self.assertTrue(self.derivative_channel.has_changes()) + + target_child.refresh_from_db() + + for key, value in labels.items(): + self.assertEqual(getattr(target_child, key), {value: True}) + class ContentIDTestCase(SyncTestMixin, StudioAPITestCase): def setUp(self): diff --git a/contentcuration/contentcuration/utils/sync.py b/contentcuration/contentcuration/utils/sync.py index 02f44ad3d1..49758d6ddb 100644 --- a/contentcuration/contentcuration/utils/sync.py +++ b/contentcuration/contentcuration/utils/sync.py @@ -75,13 +75,25 @@ def sync_node( return node +synced_fields = [ + "title", + "description", + "license_id", + "copyright_holder", + "author", + "extra_fields", + "categories", + "learner_needs", + "accessibility_labels", + "grade_levels", + "resource_types", + "learning_activities", +] + + def sync_node_data(node, original): - node.title = original.title - node.description = original.description - node.license_id = original.license_id - node.copyright_holder = original.copyright_holder - node.author = original.author - node.extra_fields = original.extra_fields + for field in synced_fields: + setattr(node, field, getattr(original, field)) # Set changed if anything has changed node.on_update()