예제 #1
0
class SearchTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        config = configparser.ConfigParser()
        config.read(os.path.join(os.path.dirname(__file__), 'test.ini'))
        settings = config.items('app:oe_utils')
        cls.settings = dict((s[0], s[1]) for s in settings)
        cls.object_type = 'dossier'
        cls.user_acls = ["test_user", "test_role"]
        cls.engine = engine_from_config(cls.settings, prefix='sqlalchemy.')
        cls.session_maker = sessionmaker(
            bind=cls.engine,
            extension=ZopeTransactionExtension()
        )

    def setUp(self):
        self.search_engine = SearchEngine(self.settings['searchengine.baseurl'],
                                          self.settings['searchengine.index'],
                                          'dossier')
        self.system_token = 'test_token'
        self.search_engine.remove_index('test_token')
        self.search_engine.create_index('test_token', data=dummy_index)
        self.search_engine.add_type_mapping('actor', dummy_mapping, 'test_token')
        self.session = self.session_maker()

    def tearDown(self):
        self.session.close()
        del self.search_engine

    def test_add_to_index(self):
        object_id = 'test1'
        assert_no_exception_raised(self.search_engine.add_to_index, self.system_token, self.object_type, object_id,
                                   json.dumps(testdata))

    def test_add_to_index_KO(self):
        object_id = 'test2'
        self.assertRaises(HTTPError, self.search_engine.add_to_index, self.system_token, self.object_type, object_id,
                          incorrect_testdata)

    def test_remove_from_index_KO(self):
        object_id = 'test2'
        self.assertRaises(HTTPError, self.search_engine.remove_from_index, self.system_token, self.object_type,
                          object_id)

    def test_remove_from_index(self):
        index_db(self.session, self.search_engine, self.system_token, self.object_type)
        object_id = '1'
        assert_no_exception_raised(self.search_engine.remove_from_index, self.system_token, self.object_type, object_id)

    def test_remove_from_index_no_system_token(self):
        index_db(self.session, self.search_engine, self.system_token, self.object_type)
        object_id = '1'
        assert_no_exception_raised(self.search_engine.remove_from_index, None, self.object_type, object_id)

    def test_remove_index(self):
        assert_no_exception_raised(self.search_engine.remove_index, 'test_token')

    def test_remove_index_KO(self):
        searchengine = SearchEngine(self.settings['searchengine.baseurl'], 'testing', 'testing')
        assert_no_exception_raised(searchengine.remove_index, 'test_token')

    def test_create_index(self):
        searchengine = SearchEngine(self.settings['searchengine.baseurl'], 'test', 'test')
        assert_no_exception_raised(searchengine.remove_index, 'test_token')
        assert_no_exception_raised(searchengine.create_index, 'test_token', data=dummy_index)

    def test_create_index_KO(self):
        self.assertRaises(HTTPError, self.search_engine.create_index, 'test_token', data=dummy_index)

    def test_add_type_mapping(self):
        assert_no_exception_raised(self.search_engine.add_type_mapping, self.object_type, dummy_mapping, 'test_token')

    def test_add_type_mapping_KO(self):
        object_type = 'test'
        self.assertRaises(HTTPError, self.search_engine.add_type_mapping, object_type, {}, 'test_token')

    def test_query(self):
        index_db(self.session, self.search_engine, self.system_token, self.object_type)
        query = MultiDict({
            'query': 'test'
        })
        result_dto = self.search_engine.query(self.system_token,
                                              query_params=query,
                                              user_acls=self.user_acls)
        self.assertTrue(isinstance(result_dto, ResultDTO))
        self.assertEqual(2, result_dto.total)
        self.assertIsNotNone(result_dto.data)

    def test_query_ranged(self):
        index_db(self.session, self.search_engine, self.system_token, self.object_type)
        object_type = 'dossier'
        query = MultiDict({
            'query': 'test'
        })
        result_range = Range(start=0, end=4)
        result_dto = self.search_engine.query(self.system_token, object_type=object_type,
                                              query_params=query,
                                              user_acls=self.user_acls,
                                              result_range=result_range)
        self.assertTrue(isinstance(result_dto, ResultDTO))
        self.assertEqual(2, result_dto.total)
        self.assertIsNotNone(result_dto.data)

    def test_query_min_score(self):
        index_db(self.session, self.search_engine, self.system_token, self.object_type)
        object_type = 'dossier'
        query = MultiDict({
            'query': 'test'
        })
        result_dto = self.search_engine.query(self.system_token, object_type=object_type,
                                              query_params=query,
                                              user_acls=self.user_acls,
                                              min_score=1)
        self.assertTrue(isinstance(result_dto, ResultDTO))
        self.assertEqual(2, result_dto.total)
        self.assertIsNotNone(result_dto.data)

    def test_query_no_hits(self):
        index_db(self.session, self.search_engine, self.system_token, self.object_type)
        object_type = 'testing'
        query = MultiDict({
            'query': 'test'
        })
        result_dto = self.search_engine.query(self.system_token, object_type=object_type,
                                              query_params=query,
                                              user_acls=self.user_acls)
        self.assertTrue(isinstance(result_dto, ResultDTO))
        self.assertEqual(0, result_dto.total)
        self.assertIsNotNone(result_dto.data)
예제 #2
0
class SearchEngineTest(unittest.TestCase):
    def setUp(self):
        init_search_responses()
        self.searchengine = SearchEngine(test_base_url, index_name, 'dossier')
        self.system_token = 'test_token'
        self.user_acls = ["test_user", "test_role"]

    def tearDown(self):
        pass

    @responses.activate
    def test_add_to_index(self):
        object_type = 'dossier'
        object_id = 'test1'
        self.searchengine.add_to_index(self.system_token, object_type, object_id, testdata)
        self.assertEqual(responses.calls[0].request.url, 'http://localhost:9200/dossiers/dossier/test1')
        self.assertEqual(responses.calls[0].response.status_code, 201)

    @responses.activate
    def test_add_to_index_KO(self):
        object_type = 'dossier'
        object_id = 'test2'
        self.assertRaises(HTTPError, self.searchengine.add_to_index, self.system_token, object_type, object_id,
                          testdata)

    @responses.activate
    def test_remove_from_index_KO(self):
        object_type = 'dossier'
        object_id = 'test2'
        self.assertRaises(HTTPError, self.searchengine.remove_from_index, self.system_token, object_type, object_id)

    @responses.activate
    def test_remove_from_index(self):
        object_type = 'dossier'
        object_id = 'test1'
        self.searchengine.remove_from_index(self.system_token, object_type, object_id)
        self.assertIn('OpenAmSSOID', responses.calls[0].request.headers)
        self.assertEqual(responses.calls[0].response.status_code, 200)

    @responses.activate
    def test_remove_from_index_no_system_token(self):
        object_type = 'dossier'
        object_id = 'test1'
        self.searchengine.remove_from_index(None, object_type, object_id)
        self.assertNotIn('OpenAmSSOID', responses.calls[0].request.headers)
        self.assertEqual(responses.calls[0].response.status_code, 200)

    @responses.activate
    def test_remove_index(self):
        self.searchengine.remove_index('test_token')
        self.assertEqual(responses.calls[0].response.status_code, 200)
        self.assertEqual(responses.calls[1].response.status_code, 200)

    @responses.activate
    def test_remove_index_KO(self):
        searchengine = SearchEngine(test_base_url, 'testing', 'testing')
        searchengine.remove_index('test_token')
        self.assertEqual(responses.calls[0].response.status_code, 400)
        self.assertEqual(len(responses.calls), 1)

    @responses.activate
    def test_remove_index_KO_delete(self):
        searchengine = SearchEngine(test_base_url, 'test', 'test')
        self.assertRaises(HTTPError, searchengine.remove_index, 'test_token')
        self.assertEqual(len(responses.calls), 2)

    @responses.activate
    def test_create_index(self):
        self.searchengine.create_index('test_token', data='')
        self.assertEqual(responses.calls[0].response.status_code, 200)

    @responses.activate
    def test_create_index_KO(self):
        searchengine = SearchEngine(test_base_url, 'test', 'test')
        self.assertRaises(HTTPError, searchengine.create_index, 'test_token', data='')

    @responses.activate
    def test_add_type_mapping(self):
        object_type = 'dossier'
        self.searchengine.add_type_mapping(object_type, {}, 'test_token')
        self.assertEqual(responses.calls[0].response.status_code, 200)

    @responses.activate
    def test_add_type_mapping_KO(self):
        object_type = 'test'
        self.assertRaises(HTTPError, self.searchengine.add_type_mapping, object_type, {}, 'test_token')

    @responses.activate
    def test_query(self):
        query = MultiDict({
            'query': 'test'
        })
        result_dto = self.searchengine.query(self.system_token,
                                             query_params=query,
                                             user_acls=self.user_acls)
        self.assertTrue(isinstance(result_dto, ResultDTO))
        self.assertEqual(2, result_dto.total)
        self.assertIsNotNone(result_dto.data)

    @responses.activate
    def test_query_ranged(self):
        object_type = 'dossier'
        query = MultiDict({
            'query': 'test'
        })
        result_range = Range(start=0, end=4)
        result_dto = self.searchengine.query(self.system_token, object_type=object_type,
                                             query_params=query,
                                             user_acls=self.user_acls,
                                             result_range=result_range)
        self.assertTrue(isinstance(result_dto, ResultDTO))
        self.assertEqual(2, result_dto.total)
        self.assertIsNotNone(result_dto.data)
        self.assertIn('from=0', responses.calls[0].request.url)
        self.assertIn('size=5', responses.calls[0].request.url)

    @responses.activate
    def test_query_min_score(self):
        object_type = 'dossier'
        query = MultiDict({
            'query': 'test'
        })
        result_dto = self.searchengine.query(self.system_token, object_type=object_type,
                                             query_params=query,
                                             user_acls=self.user_acls,
                                             min_score=5)
        self.assertTrue(isinstance(result_dto, ResultDTO))
        self.assertEqual(2, result_dto.total)
        self.assertIsNotNone(result_dto.data)
        self.assertIn('"min_score": 5', responses.calls[0].request.body)

    @responses.activate
    def test_query_aggregations(self):
        aggregations = {
            'datum': {
                'date_histogram': {
                    'field': 'datum',
                    'interval': 'year',
                    'format': 'yyyy'
                }
            }
        }
        object_type = 'dossier'
        query = MultiDict({
            'query': 'test'
        })
        result_dto = self.searchengine.query(self.system_token, object_type=object_type,
                                             query_params=query,
                                             user_acls=self.user_acls,
                                             aggregations=aggregations)
        self.assertTrue(isinstance(result_dto, ResultDTO))
        self.assertEqual(2, result_dto.total)
        self.assertIsNotNone(result_dto.data)
        self.assertIn('aggregations', responses.calls[0].request.body)

    @responses.activate
    def test_query_no_hits(self):
        object_type = 'testing'
        query = MultiDict({
            'query': 'test'
        })
        result_dto = self.searchengine.query(self.system_token, object_type=object_type,
                                             query_params=query,
                                             user_acls=self.user_acls)
        self.assertTrue(isinstance(result_dto, ResultDTO))
        self.assertEqual(0, result_dto.total)
        self.assertIsNotNone(result_dto.data)

    @responses.activate
    def test_query_KO(self):
        object_type = 'test'
        query = MultiDict({
            'query': 'test'
        })
        self.assertRaises(HTTPError, self.searchengine.query, self.system_token, object_type=object_type,
                          query_params=query,
                          user_acls=self.user_acls)