def test_should_push_updated_nodes_to_elasticsearch(self, mock_serialiser, mock_post, mock_get, mock_converter, *_): api_data = '{}' mock_serialiser.return_value = {} mock_converter.return_value = api_data mock_get.return_value = FakeResponse({}, status_code=HTTP_200_OK) run() mock_post.assert_called_with(settings.ELASTIC_SEARCH.BULK, data=api_data)
def test_should_set_sync_end_time_after_posting_to_elasticsearch(self, mock_post, mock_get, mock_now, *_): fake_end_time = FakeDatetime.now() mock_now.return_value = fake_end_time mock_post.return_value = FakeResponse({}, status_code=HTTP_200_OK) mock_get.return_value = FakeResponse({}, status_code=HTTP_200_OK) run() self.assertEqual(SyncInfo.objects.last().end_time, fake_end_time)
def test_should_set_last_sync_status_to_failed_when_post_to_es_raises_and_error(self, mock_post, mock_get, mock_logger, *_): error_message = 'failed to connect to network' mock_post.side_effect = RuntimeError(error_message) mock_get.return_value = FakeResponse({}, status_code=HTTP_200_OK) run() self.assertEqual(SyncInfo.objects.last().status, SyncInfo.STATUS.FAILED) mock_logger.assert_called_with('Sync Failed: %s' % error_message)
def test_should_post_node_mapping_to_elasticsearch_when_mapping_does_not_exist(self, mock_get, mock_post, mock_put, *_): mock_get.return_value = FakeResponse({}, status_code=HTTP_400_BAD_REQUEST) mock_put.return_value = FakeResponse({}, status_code=HTTP_200_OK) mock_post.return_value = FakeResponse({}, status_code=HTTP_200_OK) url = '%s/delivery_node/' % settings.ELASTIC_SEARCH.MAPPING run() mock_get.assert_called_with(url) self.assertEqual(mock_post.call_count, 3) mock_calls = mock_post.mock_calls mock_calls[0].assert_called_with(url, json=DELIVERY_NODE_MAPPING) mock_calls[1].assert_called_with(url, json=DELIVERY_NODE_MAPPING) mock_calls[2].assert_called_with(settings.ELASTIC_SEARCH.BULK, data='')
def test_should_push_deleted_nodes_to_elasticsearch(self, mock_serialise_nodes, mock_post, mock_get, mock_converter, *_): mock_get.return_value = FakeResponse({}, status_code=HTTP_200_OK) node = DeliveryNodeFactory() run() node.delete() api_data = '{}' stub_nodes_to_update = {'id': 10} mock_serialise_nodes.return_value = stub_nodes_to_update mock_converter.return_value = api_data mock_post.return_value = FakeResponse({}, status_code=HTTP_200_OK) run() mock_converter.assert_called_with(stub_nodes_to_update, [node.id]) mock_post.assert_called_with(settings.ELASTIC_SEARCH.BULK, data=api_data) self.assertListEqual(DeleteRecords.objects.first().nodes_to_delete, [])
def test_should_set_last_sync_status_to_successful_when_post_to_elasticsearch_returns_200(self, mock_post, mock_get, *_): mock_post.return_value = FakeResponse({}, status_code=HTTP_200_OK) mock_get.return_value = FakeResponse({}, status_code=HTTP_200_OK) run() self.assertEqual(SyncInfo.objects.last().status, SyncInfo.STATUS.SUCCESSFUL)
def test_should_create_a_sync_info_record_when_sync_starts(self, mock_get, *_): mock_get.return_value = FakeResponse({}, status_code=HTTP_200_OK) self.assertEqual(SyncInfo.objects.count(), 0) run() self.assertEqual(SyncInfo.objects.count(), 1)
def test_run_should_serialise_nodes_that_are_marked_for_sync(self, mock_get, mock_serialiser, mock_generate_nodes, *_): mock_get.return_value = FakeResponse({}, status_code=HTTP_200_OK) mock_nodes = [1, 2] mock_generate_nodes.return_value = mock_nodes run() mock_serialiser.assert_called_with(mock_nodes)
def test_should_set_last_sync_status_to_failed_when_post_to_elasticsearch_returns_non_200(self, mock_post, mock_get, *_): mock_post.return_value = FakeResponse({}, status_code=HTTP_400_BAD_REQUEST) mock_get.return_value = FakeResponse({}, status_code=HTTP_200_OK) run() self.assertEqual(SyncInfo.objects.last().status, SyncInfo.STATUS.FAILED)
def reset_elastic_search_data_source(): SyncInfo.objects.all().delete() run()