Commit e4b39c5c by Adam Palay

only pass lists of text as iterable neo4j values

parent d59ab18b
...@@ -27,7 +27,6 @@ log = logging.getLogger(__name__) ...@@ -27,7 +27,6 @@ log = logging.getLogger(__name__)
bolt_log = logging.getLogger('neo4j.bolt') # pylint: disable=invalid-name bolt_log = logging.getLogger('neo4j.bolt') # pylint: disable=invalid-name
bolt_log.setLevel(logging.ERROR) bolt_log.setLevel(logging.ERROR)
ITERABLE_NEO4J_TYPES = (tuple, list, set, frozenset)
PRIMITIVE_NEO4J_TYPES = (integer, string, neo4j_unicode, float, bool) PRIMITIVE_NEO4J_TYPES = (integer, string, neo4j_unicode, float, bool)
COMMAND_LAST_RUN_CACHE = CommandLastRunCache() COMMAND_LAST_RUN_CACHE = CommandLastRunCache()
...@@ -140,16 +139,11 @@ class ModuleStoreSerializer(object): ...@@ -140,16 +139,11 @@ class ModuleStoreSerializer(object):
value: the value of an xblock's field value: the value of an xblock's field
Returns: either the value, a text version of the value, or, if the 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 coerced_value = value
if isinstance(value, ITERABLE_NEO4J_TYPES): if isinstance(value, list):
coerced_value = [] coerced_value = [six.text_type(element) for element in 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 it's not one of the types that neo4j accepts, # if it's not one of the types that neo4j accepts,
# just convert it to text # just convert it to text
......
...@@ -15,7 +15,6 @@ from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory ...@@ -15,7 +15,6 @@ from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
from openedx.core.djangoapps.coursegraph.management.commands.dump_to_neo4j import ( from openedx.core.djangoapps.coursegraph.management.commands.dump_to_neo4j import (
ModuleStoreSerializer, ModuleStoreSerializer,
ITERABLE_NEO4J_TYPES,
) )
from openedx.core.djangoapps.coursegraph.signals import _listen_for_course_publish from openedx.core.djangoapps.coursegraph.signals import _listen_for_course_publish
...@@ -131,21 +130,6 @@ class TestModuleStoreSerializer(TestDumpToNeo4jCommandBase): ...@@ -131,21 +130,6 @@ class TestModuleStoreSerializer(TestDumpToNeo4jCommandBase):
self.assertEqual(len(nodes), 9) self.assertEqual(len(nodes), 9)
self.assertEqual(len(relationships), 7) 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( @ddt.data(
(1, 1), (1, 1),
(object, "<type 'object'>"), (object, "<type 'object'>"),
...@@ -154,11 +138,15 @@ class TestModuleStoreSerializer(TestDumpToNeo4jCommandBase): ...@@ -154,11 +138,15 @@ class TestModuleStoreSerializer(TestDumpToNeo4jCommandBase):
(b"plain string", b"plain string"), (b"plain string", b"plain string"),
(True, True), (True, True),
(None, "None"), (None, "None"),
((1,), "(1,)"),
# list of elements should be coerced into a list of the
# string representations of those elements
([object, object], ["<type 'object'>", "<type 'object'>"])
) )
@ddt.unpack @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) coerced_value = self.mss.coerce_types(original_value)
self.assertEqual(coerced_value, coerced_expected) self.assertEqual(coerced_value, coerced_expected)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment