Beispiel #1
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 #2
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 #3
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 #4
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 #5
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')
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 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)