class IndexesServiceTest(unittest.TestCase): @classmethod def setUpClass(cls): CollectionsSimulator.build_single_col('col', None) @classmethod def tearDownClass(cls): CollectionsSimulator.clean() def setUp(self): self.indexes_service = IndexesService() self.data_service = DataService() def test_create_index(self): col_meta_data = CollectionMetaData('col') docs = self.data_service.find_all(col_meta_data, None) result = self.indexes_service.build_index(col_meta_data, docs, 'id') self.assertEqual(result['status'], 'done') def test_remove_index(self): col_meta_data = CollectionMetaData('col') result = self.indexes_service.remove_index(col_meta_data, 'id') self.assertEqual(result['status'], 'done') def test_update_indexes(self): col_meta_data = CollectionMetaData('col') docs = self.data_service.find_all(col_meta_data, None) self.indexes_service.build_index(col_meta_data, docs, 'id') lines = self.indexes_service.find_all( col_meta_data, 'id', FilterTool({'$filter': { 'id': 2 }})) # this updates the index information, not the document itself self.indexes_service.update_indexes(col_meta_data, [{ 'id': 2 }], [{ 'id': 20 }]) new_lines = self.indexes_service.find_all( col_meta_data, 'id', FilterTool({'$filter': { 'id': 20 }})) self.assertEqual(len(new_lines), 1) self.assertListEqual(lines, new_lines) def suite(): return unittest.TestLoader().loadTestsFromTestCase(IndexesServiceTest)
class CleaningThreadTest(unittest.TestCase): @classmethod def setUpClass(cls): CollectionsSimulator.build_single_col('col', ['id']) def setUp(self): # instanciate the service to test self.data_service = DataService() self.search_service = SearchService() self.indexes_service = IndexesService() @classmethod def tearDownClass(cls): CollectionsSimulator.clean() def test_clean_deleted_items(self): col_meta_data = CollectionMetaData('col') count = len(self.data_service.find_all(col_meta_data, None)) self.data_service.update(col_meta_data, [2], [{}]) CleaningStack.get_instance().push(col_meta_data, {}, 1) docs = self.data_service.find_all(col_meta_data, None) lines = self.indexes_service.find_all(col_meta_data, 'id', FilterTool({'$filter': {'id': 2}})) self.assertEqual(len(CleaningStack.get_instance().stack), 1) self.assertEqual(count, len(docs)) self.assertEqual(len(self.search_service.find_in_docs(docs, SearchContext({'$filter': {'id': 2}}))), 0) self.assertEqual(len(lines), 1) CleaningThread().run() docs = self.data_service.find_all(col_meta_data, None) lines = self.indexes_service.find_all(col_meta_data, 'id', FilterTool({'$filter': {'id': 2}})) self.assertEqual(len(CleaningStack.get_instance().stack), 0) self.assertEqual(count - 1, len(docs)) self.assertEqual(len(self.search_service.find_in_docs(docs, SearchContext({'$filter': {'id': 2}}))), 0) self.assertEqual(len(lines), 0) def suite(): return unittest.TestLoader().loadTestsFromTestCase(CleaningThreadTest)
def build_indexes(col_meta_data, field): indexes_service = IndexesService() data_service = DataService() docs = data_service.find_all(col_meta_data, None) indexes_service.build_index(col_meta_data, docs, field)
def setUp(self): self.indexes_service = IndexesService() self.data_service = DataService()
def setUp(self): self.crud_service = CrudService() self.collections_service = CollectionsService() self.query_manager = QueryManager() self.indexes_service = IndexesService()
class CrudServiceTest(unittest.TestCase): @classmethod def setUpClass(cls): if DatabaseContext.THREADS_MANAGER_CYCLING == False: DatabaseContext.THREADS_MANAGER_CYCLING = True ThreadsManager().start() CollectionsSimulator.build_single_col('col', ['id']) @classmethod def tearDownClass(cls): CollectionsSimulator.clean() def setUp(self): self.crud_service = CrudService() self.collections_service = CollectionsService() self.query_manager = QueryManager() self.indexes_service = IndexesService() def test_bulk_insert_new_docs(self): docs = [{'id': 11}, {'id': 12}, {'id': 13}, {'id': 14}, {'id': 15}] col_meta_data = CollectionMetaData('col') count = self.collections_service.count(col_meta_data) self.crud_service.upsert(col_meta_data, docs) self.assertEqual(self.collections_service.count(col_meta_data), count + 5) self.assertEqual(self.indexes_service.get_lines(col_meta_data, 11), [count]) self.assertEqual(self.indexes_service.get_lines(col_meta_data, 12), [count + 1]) self.assertEqual(self.indexes_service.get_lines(col_meta_data, 13), [count + 2]) self.assertEqual(self.indexes_service.get_lines(col_meta_data, 14), [count + 3]) self.assertEqual(self.indexes_service.get_lines(col_meta_data, 15), [count + 4]) def test_bulk_update_docs(self): docs = [{ 'id': 2, 'first_name': 'Joe', 'last_name': 'Smith' }, { 'id': 5, 'first_name': 'Emmetttttttt', 'last_name': 'Brown' }] col_meta_data = CollectionMetaData('col') count = self.collections_service.count(col_meta_data) self.crud_service.upsert(col_meta_data, docs) self.assertEqual(self.collections_service.count(col_meta_data), count) self.assertEqual( self.query_manager.get_one(col_meta_data.collection, 2), docs[0]) self.assertEqual( self.query_manager.get_one(col_meta_data.collection, 5), docs[1]) def test_bulk_insert_and_update(self): docs = [{ 'id': 21 }, { 'id': 2, 'first_name': 'Jack', 'last_name': 'Smith' }, { 'id': 22 }, { 'id': 5, 'first_name': 'Emmmmmmmmmmmmmet', 'last_name': 'Brown' }, { 'id': 23 }] col_meta_data = CollectionMetaData('col') count = self.collections_service.count(col_meta_data) self.crud_service.upsert(col_meta_data, docs) self.assertEqual(self.collections_service.count(col_meta_data), count + 3) self.assertEqual( self.query_manager.get_one(col_meta_data.collection, 2), docs[1]) self.assertEqual( self.query_manager.get_one(col_meta_data.collection, 5), docs[3]) self.assertEqual(self.indexes_service.get_lines(col_meta_data, 21), [count]) self.assertEqual(self.indexes_service.get_lines(col_meta_data, 22), [count + 1]) self.assertEqual(self.indexes_service.get_lines(col_meta_data, 23), [count + 2]) def test_bulk_delete(self): search_query = {'$filter': {'id': [3, 4]}} col_meta_data = CollectionMetaData('col') count = self.collections_service.count(col_meta_data) self.crud_service.delete(col_meta_data, search_query) while CleaningStack.get_instance().contains_data(): time.sleep(DatabaseContext.THREADS_CYCLE) self.assertEqual(self.collections_service.count(col_meta_data), count - 2) results = self.query_manager.search(col_meta_data.collection, search_query) self.assertEqual(len(results), 0) def suite(): return unittest.TestLoader.loadTestsFromTestCase(SearchServiceTest)
def setUp(self): # instanciate the service to test self.data_service = DataService() self.search_service = SearchService() self.indexes_service = IndexesService()