From 25b4a6e20729dc99e85c5ef68986ec3921b4122b Mon Sep 17 00:00:00 2001 From: Adam Palay Date: Mon, 13 Nov 2017 17:32:56 -0500 Subject: [PATCH] addresses issue where branch information was not being stripped from child blocks when serializing courses (PLAT-1794) --- .../commands/tests/test_dump_to_neo4j.py | 22 ++++++++++++++++++- openedx/core/djangoapps/coursegraph/tasks.py | 16 +++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/openedx/core/djangoapps/coursegraph/management/commands/tests/test_dump_to_neo4j.py b/openedx/core/djangoapps/coursegraph/management/commands/tests/test_dump_to_neo4j.py index b22c7405c3..2606df6c11 100644 --- a/openedx/core/djangoapps/coursegraph/management/commands/tests/test_dump_to_neo4j.py +++ b/openedx/core/djangoapps/coursegraph/management/commands/tests/test_dump_to_neo4j.py @@ -25,6 +25,7 @@ from openedx.core.djangoapps.coursegraph.tasks import ( serialize_course, coerce_types, should_dump_course, + strip_branch_and_version, ) from openedx.core.djangoapps.content.course_structures.signals import ( listen_for_course_publish @@ -262,6 +263,25 @@ class TestModuleStoreSerializer(TestDumpToNeo4jCommandBase): # the course has 7 "PARENT_OF" relationships and 3 "PRECEDES" self.assertEqual(len(relationships), 10) + def test_strip_version_and_branch(self): + """ + Tests that the _strip_version_and_branch function strips the version + and branch from a location + """ + location = self.course.id.make_usage_key( + 'test_block_type', 'test_block_id' + ).for_branch( + 'test_branch' + ).for_version('test_version') + + self.assertIsNotNone(location.branch) + self.assertIsNotNone(location.version) + + stripped_location = strip_branch_and_version(location) + + self.assertIsNone(stripped_location.branch) + self.assertIsNone(stripped_location.version) + @staticmethod def _extract_relationship_pairs(relationships, relationship_type): """ @@ -503,7 +523,7 @@ class TestModuleStoreSerializer(TestDumpToNeo4jCommandBase): """ mock_get_command_last_run.return_value = last_command_run mock_get_course_last_published.return_value = last_course_published - mock_course_key = mock.Mock + mock_course_key = mock.Mock() mock_graph = mock.Mock() self.assertEqual( should_dump_course(mock_course_key, mock_graph), diff --git a/openedx/core/djangoapps/coursegraph/tasks.py b/openedx/core/djangoapps/coursegraph/tasks.py index 137b75646a..09545576b1 100644 --- a/openedx/core/djangoapps/coursegraph/tasks.py +++ b/openedx/core/djangoapps/coursegraph/tasks.py @@ -144,6 +144,16 @@ def get_course_last_published(course_key): return course_last_published_date +def strip_branch_and_version(location): + """ + Removes the branch and version information from a location. + Args: + location: an xblock's location. + Returns: that xblock's location without branch and version information. + """ + return location.for_branch(None) + + def serialize_course(course_id): """ Serializes a course into py2neo Nodes and Relationships @@ -170,15 +180,15 @@ def serialize_course(course_id): fields[field_name] = coerce_types(value) node = Node(block_type, 'item', **fields) - location_to_node[item.location.version_agnostic()] = node + location_to_node[strip_branch_and_version(item.location)] = node # create relationships relationships = [] for item in items: previous_child_node = None for index, child in enumerate(item.get_children()): - parent_node = location_to_node.get(item.location.version_agnostic()) - child_node = location_to_node.get(child.location.version_agnostic()) + parent_node = location_to_node.get(strip_branch_and_version(item.location)) + child_node = location_to_node.get(strip_branch_and_version(child.location)) if parent_node is not None and child_node is not None: child_node["index"] = index