def test_dump_entities_with_filter(self, mock_get_session, mock_get_table_and_model): mock_table = type('MockTable', (object, ), {'_id': '9204940'}) mock_model = {} mock_filter = mock.MagicMock() mock_get_table_and_model.return_value = mock_table, mock_model result = dump_entities('catalog_name', 'collection_name', filter=mock_filter) mock_get_session.return_value.query.assert_called_with(mock_table) # Assert filter is called, and used on entities mock_filter.assert_called_with(mock_table) mock_get_session.return_value.query.return_value.filter.assert_called_with( mock_filter.return_value) entities = mock_get_session.return_value.query.return_value.filter.return_value # Assert catalog/collection are set on entities entities.set_catalog_collection.assert_called_with( 'catalog_name', 'collection_name') mock_model['catalog'] = 'catalog_name' mock_model['collection'] = 'collection_name' self.assertEqual((entities.yield_per.return_value, mock_model), result)
def _dump_entities(self, **kwargs): """ Dump entities ordered by last_event ascending Optionally provide a filter argument to partial dump entities :param kwargs: :return: """ return dump_entities(self.catalog_name, self.collection_name, **kwargs, order_by=FIELD.LAST_EVENT)
def test_dump_entities_order_by(self, mock_get_session, mock_get_table_and_model): mock_table = type('MockTable', (object, ), { '_id': '9204940', '_order_by_column': 'orderval' }) mock_model = {} order_by_column = '_order_by_column' mock_get_table_and_model.return_value = mock_table, mock_model dump_entities('catalog_name', 'collection_name', order_by=order_by_column) mock_get_session.return_value.query.assert_called_with(mock_table) # Assert order_by is added and used mock_get_session.return_value.query.return_value.order_by.assert_called_with( 'orderval')
def _dump(catalog_name, collection_name): """ Dump all entities in the requested format. Currently only csv :param catalog_name: :param collection_name: :return: Streaming response of all entities in csv format with header """ method = request.method if method == 'GET': format = request.args.get('format') exclude_deleted = request.args.get('exclude_deleted') == 'true' filter = (lambda table: getattr(table, FIELD.DATE_DELETED).is_(None) ) if exclude_deleted else None entities, model = dump_entities(catalog_name, collection_name, filter=filter) if format == "csv": result = csv_entities(entities, model) return WorkerResponse.stream_with_context(result, mimetype='text/csv') elif format == "sql": return Response(sql_entities(catalog_name, collection_name, model), mimetype='application/sql') else: return f"Unrecognised format parameter '{format}'" if format else "Format parameter not set", 400 elif method == 'POST': content_type = request.content_type if content_type == 'application/json': config = json.loads(request.data) result = dump_to_db(catalog_name, collection_name, config) return WorkerResponse.stream_with_context(result, mimetype='text/plain') else: return f"Unrecognised content type '{content_type}'", 400
def test_dump_entities(self): result = storage.dump_entities("any catalog", "any collection") self.assertEqual(result, ('any table', {'catalog': 'any catalog', 'collection': 'any collection'}))