Beispiel #1
0
    def test_execute_query(self):
        label = data_mocking.create_label({
            'name': 'apple',
        }, self.session)
        label_file = data_mocking.create_label_file(
            {
                'label': label,
                'project_id': self.project.id
            }, self.session)
        with self.app.test_request_context():
            common_actions.add_auth_to_session(flask.session,
                                               self.project.users[0])
            query_string2 = 'labels.apple > 3'
            query_creator = QueryCreator(session=self.session,
                                         project=self.project,
                                         member=self.member)
            # Correct case
            diffgram_query_obj = query_creator.create_query(
                query_string=query_string2)
            executor = SqlAlchemyQueryExecutor(
                session=self.session, diffgram_query=diffgram_query_obj)
            sql_alchemy_query, execution_log = executor.execute_query()

            self.assertEqual(type(sql_alchemy_query), Query)
            self.assertEqual(len(executor.log['error'].keys()), 0)
Beispiel #2
0
 def test_create_query(self):
     label = data_mocking.create_label({
         'name': 'apple',
     }, self.session)
     label_file = data_mocking.create_label_file(
         {
             'label': label,
             'project_id': self.project.id
         }, self.session)
     query_str = 'labels.apple > 0'
     with self.app.test_request_context():
         common_actions.add_auth_to_session(flask.session,
                                            self.project.users[0])
         creator = QueryCreator(session=self.session,
                                project=self.project,
                                member=self.member)
         result = creator.create_query(query_string=query_str)
         self.assertTrue(type(result) == DiffgramQuery)
         self.assertEqual(result.tree.data, 'start')
         self.assertEqual(result.tree.children[0].data, 'expr')
         self.assertEqual(result.tree.children[0].children[0].data, 'term')
         self.assertEqual(
             result.tree.children[0].children[0].children[0].data, 'factor')
         self.assertEqual(
             result.tree.children[0].children[0].children[0].children[0].
             data, 'compare_expr')
Beispiel #3
0
 def test_compare_expr(self):
     label = data_mocking.create_label({
         'name': 'apple',
     }, self.session)
     label_file = data_mocking.create_label_file(
         {
             'label': label,
             'project_id': self.project.id
         }, self.session)
     with self.app.test_request_context():
         common_actions.add_auth_to_session(flask.session,
                                            self.project.users[0])
         query_string2 = 'labels.apple > 3'
         query_creator = QueryCreator(session=self.session,
                                      project=self.project,
                                      member=self.member)
         # Correct case
         diffgram_query_obj = query_creator.create_query(
             query_string=query_string2)
         executor = SqlAlchemyQueryExecutor(
             session=self.session, diffgram_query=diffgram_query_obj)
         sql_alchemy_query, execution_log = executor.execute_query()
         result = executor.compare_expr(diffgram_query_obj.tree.children[0].
                                        children[0].children[0].children[0])
         self.assertEqual(len(execution_log['error'].keys()), 0)
         self.assertIsNotNone(result)
         self.assertIsNotNone(result.query_condition)
         self.assertTrue(executor.valid)
Beispiel #4
0
    def test__parse_value(self):
        label = data_mocking.create_label({
            'name': 'apple',
        }, self.session)
        label_file = data_mocking.create_label_file(
            {
                'label': label,
                'project_id': self.project.id
            }, self.session)
        with self.app.test_request_context():
            common_actions.add_auth_to_session(flask.session,
                                               self.project.users[0])
            query_string = 'labels.x > 5'  # dummy query
            query_creator = QueryCreator(session=self.session,
                                         project=self.project,
                                         member=self.member)
            diffgram_query_obj = query_creator.create_query(
                query_string=query_string)
            executor = SqlAlchemyQueryExecutor(
                session=self.session, diffgram_query=diffgram_query_obj)

            token = Token(value='labels.apple', type_='dummy')
            value = executor._SqlAlchemyQueryExecutor__parse_value(token)
            self.assertEqual(type(value), ScalarSelect)

            token = Token(value='file.something', type_='dummy')
            value = executor._SqlAlchemyQueryExecutor__parse_value(token)
            self.assertEqual(type(value), BinaryExpression)
Beispiel #5
0
    def test_get_suggestions(self):
        query_str = ''
        with self.app.test_request_context():
            common_actions.add_auth_to_session(flask.session,
                                               self.project.users[0])
            creator = QueryCreator(self.session, self.project, self.member)
            result, type = creator.get_suggestions(query_string=query_str)
            self.assertTrue('labels' in result)
            self.assertTrue('file' in result)
            self.assertEqual(type, 'entities')
            label = data_mocking.create_label({
                'name': 'apple',
            }, self.session)
            label_file = data_mocking.create_label_file(
                {
                    'label': label,
                    'project_id': self.project.id
                }, self.session)
            label2 = data_mocking.create_label({
                'name': 'car',
            }, self.session)
            label_file2 = data_mocking.create_label_file(
                {
                    'label': label2,
                    'project_id': self.project.id
                }, self.session)
            label3 = data_mocking.create_label({
                'name': 'person',
            }, self.session)
            label_file3 = data_mocking.create_label_file(
                {
                    'label': label3,
                    'project_id': self.project.id
                }, self.session)

            query_str = 'labels.'
            result, type = creator.get_suggestions(query_string=query_str)

            self.assertEqual(type, 'labels')
            self.assertTrue('apple' in result)
            self.assertTrue('car' in result)
            self.assertTrue('person' in result)

            query_str = 'file.'
            result, type = creator.get_suggestions(query_string=query_str)

            self.assertEqual(type, 'labels')
Beispiel #6
0
    def test__validate_expression(self):
        label = data_mocking.create_label({
            'name': 'apple',
        }, self.session)
        label_file = data_mocking.create_label_file(
            {
                'label': label,
                'project_id': self.project.id
            }, self.session)
        with self.app.test_request_context():
            common_actions.add_auth_to_session(flask.session,
                                               self.project.users[0])
            query_string = 'labels.x > 5'  # dummy query
            query_creator = QueryCreator(session=self.session,
                                         project=self.project,
                                         member=self.member)
            diffgram_query_obj = query_creator.create_query(
                query_string=query_string)
            executor = SqlAlchemyQueryExecutor(
                session=self.session, diffgram_query=diffgram_query_obj)

            token = Token(value='something', type_='dummy')
            token2 = Token(value='5', type_='dummy')
            operator = Token(value='>', type_='dummy')
            value = executor._SqlAlchemyQueryExecutor__validate_expression(
                token, token2, operator)
            self.assertFalse(value, False)
            self.assertIsNotNone(executor.log['error'].get('compare_expr'))

            token = Token(value='labels.apple', type_='dummy')
            token2 = Token(value='5', type_='dummy')
            operator = Token(value='>', type_='dummy')
            executor.log['error'] = {}
            value = executor._SqlAlchemyQueryExecutor__validate_expression(
                token, token2, operator)
            self.assertTrue(value)
            self.assertEqual(len(executor.log['error'].keys()), 0)

            token = Token(value='file.sensor', type_='dummy')
            token2 = Token(value='sensorA', type_='dummy')
            operator = Token(value='>', type_='dummy')
            executor.log['error'] = {}
            value = executor._SqlAlchemyQueryExecutor__validate_expression(
                token, token2, operator)
            self.assertFalse(value)
            self.assertEqual(len(executor.log['error'].keys()), 1)
Beispiel #7
0
def query_suggest_core(session: object,
                       log: dict = regular_log.default(),
                       project: Project = None,
                       query: str = None,
                       member: object = None):
    """
        Returns suggestions based on query provided.

    :param session:
    :param log:
    :param project_id:
    :param query:
    :return:
    """
    if project is None:
        log['error']['project'] = 'Provide project'
        return None, log

    query_creator = QueryCreator(session, project, member)
    suggestions, type = query_creator.get_suggestions(query)
    return {'suggestions': suggestions, 'type': type}, log
Beispiel #8
0
    def build_and_execute_query(self, limit=25, offset=None):
        """
            This functions builds a DiffgramQuery object and executes it with the
            SQLAlchemy executor to get a list of File objects that we can serialized
            and return to the user.
        :return:
        """
        query_string = self.metadata.get('query')
        if not query_string:
            return False, {
                'error': {
                    'query_string': 'Provide query_string'
                }
            }, None
        query_creator = QueryCreator(session=self.session,
                                     project=self.project,
                                     member=self.member,
                                     directory=self.directory)
        diffgram_query_obj = query_creator.create_query(
            query_string=query_string)
        if len(query_creator.log['error'].keys()) > 0:
            return False, query_creator.log, None
        executor = SqlAlchemyQueryExecutor(session=self.session,
                                           diffgram_query=diffgram_query_obj)
        sql_alchemy_query, execution_log = executor.execute_query()

        if sql_alchemy_query:
            count = sql_alchemy_query.count()
            if limit is not None:
                sql_alchemy_query = sql_alchemy_query.limit(limit)

            if offset:
                sql_alchemy_query = sql_alchemy_query.offset(offset)
            file_list = sql_alchemy_query.all()
        else:
            count = None
            return False, execution_log, count
        return file_list, query_creator.log, count
Beispiel #9
0
    def test_get_compare_op(self):
        query_string = 'labels.x > 5'  # dummy query
        with self.app.test_request_context():
            common_actions.add_auth_to_session(flask.session,
                                               self.project.users[0])
            query_creator = QueryCreator(session=self.session,
                                         project=self.project,
                                         member=self.member)
            diffgram_query_obj = query_creator.create_query(
                query_string=query_string)
            executor = SqlAlchemyQueryExecutor(
                session=self.session, diffgram_query=diffgram_query_obj)

            token = Token(value='>=', type_='dummy')
            op = executor.get_compare_op(token)
            self.assertEqual(op, operator.ge)

            token = Token(value='=', type_='dummy')
            op = executor.get_compare_op(token)
            self.assertEqual(op, operator.eq)

            token = Token(value='>=', type_='dummy')
            op = executor.get_compare_op(token)
            self.assertEqual(op, operator.ge)

            token = Token(value='<', type_='dummy')
            op = executor.get_compare_op(token)
            self.assertEqual(op, operator.lt)

            token = Token(value='<=', type_='dummy')
            op = executor.get_compare_op(token)
            self.assertEqual(op, operator.le)

            token = Token(value='!=', type_='dummy')
            op = executor.get_compare_op(token)
            self.assertEqual(op, operator.ne)
Beispiel #10
0
 def test__determine_entity_type(self):
     token = Token(value='labels.apple', type_='dummy')
     token2 = Token(value='files.metadata', type_='dummy')
     token3 = Token(value='issues.count', type_='dummy')
     query_string = 'labels.x > 5'  # dummy query
     with self.app.test_request_context():
         common_actions.add_auth_to_session(flask.session,
                                            self.project.users[0])
         query_creator = QueryCreator(session=self.session,
                                      project=self.project,
                                      member=self.member)
         diffgram_query_obj = query_creator.create_query(
             query_string=query_string)
         executor = SqlAlchemyQueryExecutor(
             session=self.session, diffgram_query=diffgram_query_obj)
         e_type = executor._SqlAlchemyQueryExecutor__determine_entity_type(
             token)
     self.assertEqual(e_type, 'labels')
     e_type = executor._SqlAlchemyQueryExecutor__determine_entity_type(
         token2)
     self.assertEqual(e_type, 'files')
     e_type = executor._SqlAlchemyQueryExecutor__determine_entity_type(
         token3)
     self.assertEqual(e_type, 'issues')