Exemplo n.º 1
0
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)
Exemplo n.º 2
0
 def __init__(self):
     super().__init__(AppConfig(), DConfigSettings(), DValueSettings())
     self.db: DB = DB(self.database)
     # services
     self.data_service = DataService(self.base_params)
     # scheduler
     self.scheduler.add_job(self.data_service.generate_data, 6000)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
 def setUp(self):
     self.indexes_service = IndexesService()
     self.data_service = DataService()
Exemplo n.º 6
0
 def setUp(self):
     # instanciate the service to test
     self.data_service = DataService()
     self.search_service = SearchService()
     self.indexes_service = IndexesService()
Exemplo n.º 7
0
 def setUp(self):
     # instanciate the service to test
     self.data_service = DataService()
Exemplo n.º 8
0
class DataServiceTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        CollectionsSimulator.build_single_col('col', None)

    def setUp(self):
        # instanciate the service to test
        self.data_service = DataService()

    @classmethod
    def tearDownClass(cls):
        CollectionsSimulator.clean()

    def test_find_by_line_first_file_actual_thread(self):
        docs = self.data_service.find_by_line(CollectionMetaData('col'), [1],
                                              1)
        self.assertEqual(len(docs), 1)
        self.assertEqual(docs[0]['id'], 2)

    def test_find_by_line_first_file_next_thread(self):
        docs = self.data_service.find_by_line(CollectionMetaData('col'), [1],
                                              2)
        self.assertEqual(len(docs), 0)

    def test_find_by_line_second_file_previous_thread(self):
        docs = self.data_service.find_by_line(CollectionMetaData('col'), [4],
                                              1)
        self.assertEqual(len(docs), 0)

    def test_find_by_line_second_file_actual_thread(self):
        docs = self.data_service.find_by_line(CollectionMetaData('col'), [4],
                                              2)
        self.assertEqual(len(docs), 1)
        self.assertEqual(docs[0]['id'], 5)

    def test_find_by_multiple_lines_but_found_first_one(self):
        docs = self.data_service.find_by_line(CollectionMetaData('col'),
                                              [1, 4], 1)
        self.assertEqual(len(docs), 1)
        self.assertEqual(docs[0]['id'], 2)

    def test_find_by_multiple_lines_but_found_second_one(self):
        docs = self.data_service.find_by_line(CollectionMetaData('col'),
                                              [1, 4], 2)
        self.assertEqual(len(docs), 1)
        self.assertEqual(docs[0]['id'], 5)

    def test_find_one_doc_in_file(self):
        filter_tool = FilterTool({'$filter': {'id': 3}})
        result = self.data_service.find_one_in_file('data-test/col/data1.bin',
                                                    filter_tool)
        self.assertEqual(result['id'], 3)

    def test_append_doc_in_file(self):
        DatabaseContext.MAX_DOC_PER_FILE = 10000
        self.data_service.append(CollectionMetaData('col'), [{'id': 123}])
        self.assertEqual(self.data_service.file_len('data-test/col/data2.bin'),
                         4)

    def test_append_doc_to_new_file(self):
        DatabaseContext.MAX_DOC_PER_FILE = 3
        col_meta_data = CollectionMetaData('col')
        self.data_service.append(col_meta_data, [{'id': 123}])
        self.assertEqual(self.data_service.file_len('data-test/col/data3.bin'),
                         1)

        col_meta_data.remove_last_data_file()

    def test_append_bulk(self):
        DatabaseContext.MAX_DOC_PER_FILE = 3
        col_meta_data = CollectionMetaData('col')
        self.data_service.append(col_meta_data, [{
            'id': 201
        }, {
            'id': 202
        }, {
            'id': 203
        }, {
            'id': 204
        }, {
            'id': 205
        }])
        self.assertEqual(self.data_service.file_len('data-test/col/data3.bin'),
                         3)
        self.assertEqual(self.data_service.file_len('data-test/col/data4.bin'),
                         2)

        col_meta_data.remove_last_data_file()
        col_meta_data.remove_last_data_file()

    def test_remove_doc_in_file(self):
        result = self.data_service.update(CollectionMetaData('col'), [6],
                                          [{}])[0]
        self.assertEqual(result, {'line': 5, 'doc': {}})

        filter_tool = FilterTool({'$filter': {'id': 6}})
        result = self.data_service.find_one_in_file('data-test/col/data2.bin',
                                                    filter_tool)
        self.assertIsNone(result)

    def test_read_file_length(self):
        self.assertEqual(self.data_service.file_len('data-test/col/data1.bin'),
                         3)

    def test_update_value(self):
        self.data_service.update(CollectionMetaData('col'), [2],
                                 [{
                                     'id': 2,
                                     'first_name': 'Joooooohn',
                                     'last_name': 'Smith'
                                 }])
        self.assertEqual(self.data_service.file_len('data-test/col/data1.bin'),
                         3)

        filter_tool = FilterTool({'$filter': {'first_name': 'Joooooohn'}})
        result = self.data_service.find_one_in_file('data-test/col/data1.bin',
                                                    filter_tool)
        self.assertEqual(result['id'], 2)
        self.assertEqual(result['first_name'], 'Joooooohn')

        self.data_service.update(CollectionMetaData('col'), [2],
                                 [{
                                     'id': 2,
                                     'first_name': 'John',
                                     'last_name': 'Smith'
                                 }])
        results = self.data_service.find_one_in_file('data-test/col/data1.bin',
                                                     filter_tool)
        self.assertIsNone(results)

    def suite():
        return unittest.TestLoader().loadTestsFromTestCase(DataServiceTest)