diff --git a/openedx/core/djangoapps/coursegraph/management/commands/dump_to_neo4j.py b/openedx/core/djangoapps/coursegraph/management/commands/dump_to_neo4j.py index 1471db7c85..bd43eda637 100644 --- a/openedx/core/djangoapps/coursegraph/management/commands/dump_to_neo4j.py +++ b/openedx/core/djangoapps/coursegraph/management/commands/dump_to_neo4j.py @@ -27,7 +27,6 @@ log = logging.getLogger(__name__) bolt_log = logging.getLogger('neo4j.bolt') # pylint: disable=invalid-name bolt_log.setLevel(logging.ERROR) -ITERABLE_NEO4J_TYPES = (tuple, list, set, frozenset) PRIMITIVE_NEO4J_TYPES = (integer, string, neo4j_unicode, float, bool) COMMAND_LAST_RUN_CACHE = CommandLastRunCache() @@ -140,16 +139,11 @@ class ModuleStoreSerializer(object): value: the value of an xblock's field Returns: either the value, a text version of the value, or, if the - value is iterable, the value with each element being converted to text + value is a list, a list where each element is converted to text. """ - coerced_value = value - if isinstance(value, ITERABLE_NEO4J_TYPES): - coerced_value = [] - for element in value: - coerced_value.append(six.text_type(element)) - # convert coerced_value back to its original type - coerced_value = type(value)(coerced_value) + if isinstance(value, list): + coerced_value = [six.text_type(element) for element in coerced_value] # if it's not one of the types that neo4j accepts, # just convert it to text 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 1f4b9c1281..61980de024 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 @@ -15,7 +15,6 @@ from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory from openedx.core.djangoapps.coursegraph.management.commands.dump_to_neo4j import ( ModuleStoreSerializer, - ITERABLE_NEO4J_TYPES, ) from openedx.core.djangoapps.coursegraph.signals import _listen_for_course_publish @@ -131,21 +130,6 @@ class TestModuleStoreSerializer(TestDumpToNeo4jCommandBase): self.assertEqual(len(nodes), 9) self.assertEqual(len(relationships), 7) - @ddt.data(*ITERABLE_NEO4J_TYPES) - def test_coerce_types_iterable(self, iterable_type): - """ - Tests the coerce_types helper method for iterable types - """ - example_iterable = iterable_type([object, object, object]) - - # each element in the iterable is not unicode: - self.assertFalse(any(isinstance(tab, six.text_type) for tab in example_iterable)) - # but after they are coerced, they are: - coerced = ModuleStoreSerializer().coerce_types(example_iterable) - self.assertTrue(all(isinstance(tab, six.text_type) for tab in coerced)) - # finally, make sure we haven't changed the type: - self.assertEqual(type(coerced), iterable_type) - @ddt.data( (1, 1), (object, ""), @@ -154,11 +138,15 @@ class TestModuleStoreSerializer(TestDumpToNeo4jCommandBase): (b"plain string", b"plain string"), (True, True), (None, "None"), + ((1,), "(1,)"), + # list of elements should be coerced into a list of the + # string representations of those elements + ([object, object], ["", ""]) ) @ddt.unpack - def test_coerce_types_base(self, original_value, coerced_expected): + def test_coerce_types(self, original_value, coerced_expected): """ - Tests the coerce_types helper for the neo4j base types + Tests the coerce_types helper """ coerced_value = self.mss.coerce_types(original_value) self.assertEqual(coerced_value, coerced_expected)