def test_dump_to_neo4j_cache( self, override_cache, expected_number_courses, mock_graph_constructor, mock_selector_class, ): """ Tests the caching mechanism and override to make sure we only publish recently updated courses. """ mock_graph = MockGraph() mock_graph_constructor.return_value = mock_graph mock_selector_class.return_value = MockNodeSelector(mock_graph) mock_credentials = mock.Mock() # run once to warm the cache self.mss.dump_courses_to_neo4j(mock_credentials, override_cache=override_cache) # when run the second time, only dump courses if the cache override # is enabled submitted, __ = self.mss.dump_courses_to_neo4j( mock_credentials, override_cache=override_cache) self.assertEqual(len(submitted), expected_number_courses)
def test_dump_to_neo4j_published(self, mock_graph_constructor, mock_selector_class): """ Tests that we only dump those courses that have been published after the last time the command was been run. """ mock_graph = MockGraph() mock_graph_constructor.return_value = mock_graph mock_selector_class.return_value = MockNodeSelector(mock_graph) # mocking is thorwing error in kombu serialzier and its not require here any more. credentials = {} # run once to warm the cache submitted, skipped = self.mss.dump_courses_to_neo4j(credentials) self.assertEqual(len(submitted), len(self.course_strings)) # simulate one of the courses being published with block_structure_config.waffle().override( block_structure_config.STORAGE_BACKING_FOR_CACHE): update_block_structure_on_course_publish(None, self.course.id) # make sure only the published course was dumped submitted, __ = self.mss.dump_courses_to_neo4j(credentials) self.assertEqual(len(submitted), 1) self.assertEqual(submitted[0], six.text_type(self.course.id))
def test_dump_to_neo4j(self, mock_graph_constructor, mock_selector_class): """ Tests the dump_to_neo4j method works against a mock py2neo Graph """ mock_graph = MockGraph() mock_graph_constructor.return_value = mock_graph mock_selector_class.return_value = MockNodeSelector(mock_graph) # mocking is thorwing error in kombu serialzier and its not require here any more. credentials = {} submitted, skipped = self.mss.dump_courses_to_neo4j(credentials) self.assertCourseDump( mock_graph, number_of_courses=2, number_commits=2, number_rollbacks=0, ) # 9 nodes + 7 relationships from the first course # 2 nodes and no relationships from the second self.assertEqual(len(mock_graph.nodes), 11) six.assertCountEqual(self, submitted, self.course_strings)
def test_dump_to_neo4j_cache( self, override_cache, expected_number_courses, mock_graph_constructor, mock_selector_class, ): """ Tests the caching mechanism and override to make sure we only publish recently updated courses. """ mock_graph = MockGraph() mock_graph_constructor.return_value = mock_graph mock_selector_class.return_value = MockNodeSelector(mock_graph) # mocking is thorwing error in kombu serialzier and its not require here any more. credentials = {} # run once to warm the cache self.mss.dump_courses_to_neo4j( credentials, override_cache=override_cache ) # when run the second time, only dump courses if the cache override # is enabled submitted, __ = self.mss.dump_courses_to_neo4j( credentials, override_cache=override_cache ) assert len(submitted) == expected_number_courses
def test_dump_to_neo4j_published(self, mock_graph_constructor, mock_matcher_class): """ Tests that we only dump those courses that have been published after the last time the command was been run. """ mock_graph = MockGraph() mock_graph_constructor.return_value = mock_graph mock_matcher_class.return_value = MockNodeMatcher(mock_graph) # mocking is thorwing error in kombu serialzier and its not require here any more. credentials = {} # run once to warm the cache submitted, skipped = self.mss.dump_courses_to_neo4j(credentials) # lint-amnesty, pylint: disable=unused-variable assert len(submitted) == len(self.course_strings) # simulate one of the courses being published with override_waffle_switch( block_structure_config.STORAGE_BACKING_FOR_CACHE, True): update_block_structure_on_course_publish(None, self.course.id) # make sure only the published course was dumped submitted, __ = self.mss.dump_courses_to_neo4j(credentials) assert len(submitted) == 1 assert submitted[0] == str(self.course.id)
def test_dump_to_neo4j_rollback(self, mock_selector_class): """ Tests that the the dump_to_neo4j method handles the case where there's an exception trying to write to the neo4j database. """ mock_graph = MockGraph(transaction_errors=True) mock_selector_class.return_value = MockNodeSelector(mock_graph) submitted, skipped = self.mss.dump_courses_to_neo4j(mock_graph) self.assertCourseDump( mock_graph, number_of_courses=0, number_commits=0, number_rollbacks=2, ) self.assertItemsEqual(submitted, self.course_strings)
def test_dump_to_neo4j_published(self, mock_selector_class): """ Tests that we only dump those courses that have been published after the last time the command was been run. """ mock_graph = MockGraph() mock_selector_class.return_value = MockNodeSelector(mock_graph) # run once to warm the cache submitted, skipped = self.mss.dump_courses_to_neo4j(mock_graph) self.assertEqual(len(submitted), len(self.course_strings)) # simulate one of the courses being published listen_for_course_publish(None, self.course.id) # make sure only the published course was dumped submitted, __ = self.mss.dump_courses_to_neo4j(mock_graph) self.assertEqual(len(submitted), 1) self.assertEqual(submitted[0], unicode(self.course.id))
def test_dump_to_neo4j_cache(self, override_cache, expected_number_courses, mock_selector_class): """ Tests the caching mechanism and override to make sure we only publish recently updated courses. """ mock_graph = MockGraph() mock_selector_class.return_value = MockNodeSelector(mock_graph) # run once to warm the cache self.mss.dump_courses_to_neo4j( mock_graph, override_cache=override_cache ) # when run the second time, only dump courses if the cache override # is enabled successful, unsuccessful = self.mss.dump_courses_to_neo4j( mock_graph, override_cache=override_cache ) self.assertEqual(len(successful + unsuccessful), expected_number_courses)
def setup_mock_graph(mock_selector_class, mock_graph_class, transaction_errors=False): """ Replaces the py2neo Graph object with a MockGraph; similarly replaces NodeSelector with MockNodeSelector. Arguments: mock_selector_class: a mocked NodeSelector class mock_graph_class: a mocked Graph class transaction_errors: a bool for whether we should get errors when transactions try to commit Returns: an instance of MockGraph """ mock_graph = MockGraph(transaction_errors=transaction_errors) mock_graph_class.return_value = mock_graph mock_node_selector = MockNodeSelector(mock_graph) mock_selector_class.return_value = mock_node_selector return mock_graph
def test_dump_to_neo4j_rollback(self, mock_graph_constructor, mock_selector_class): """ Tests that the the dump_to_neo4j method handles the case where there's an exception trying to write to the neo4j database. """ mock_graph = MockGraph(transaction_errors=True) mock_graph_constructor.return_value = mock_graph mock_selector_class.return_value = MockNodeSelector(mock_graph) # mocking is thorwing error in kombu serialzier and its not require here any more. credentials = {} submitted, skipped = self.mss.dump_courses_to_neo4j(credentials) # lint-amnesty, pylint: disable=unused-variable self.assertCourseDump( mock_graph, number_of_courses=0, number_commits=0, number_rollbacks=2, ) self.assertCountEqual(submitted, self.course_strings)
def test_dump_to_neo4j(self, mock_selector_class): """ Tests the dump_to_neo4j method works against a mock py2neo Graph """ mock_graph = MockGraph() mock_selector_class.return_value = MockNodeSelector(mock_graph) submitted, skipped = self.mss.dump_courses_to_neo4j(mock_graph) self.assertCourseDump( mock_graph, number_of_courses=2, number_commits=2, number_rollbacks=0, ) # 9 nodes + 7 relationships from the first course # 2 nodes and no relationships from the second self.assertEqual(len(mock_graph.nodes), 11) self.assertItemsEqual(submitted, self.course_strings)