def test_distinct(self):
        ss = SelectStatement('table', distinct_fields=['field2'])
        ss.add_where_clause(WhereClause('field1', EqualsOperator(), 'b'))
        self.assertEqual(six.text_type(ss), 'SELECT DISTINCT "field2" FROM table WHERE "field1" = %(0)s', six.text_type(ss))

        ss = SelectStatement('table', distinct_fields=['field1', 'field2'])
        self.assertEqual(six.text_type(ss), 'SELECT DISTINCT "field1", "field2" FROM table')
 def test_count(self):
     ss = SelectStatement('table', count=True, limit=10, order_by='d')
     ss.add_where(Column(db_field='a'), EqualsOperator(), 'b')
     self.assertEqual(
         six.text_type(ss),
         'SELECT COUNT(*) FROM table WHERE "a" = %(0)s LIMIT 10',
         six.text_type(ss))
     self.assertIn('LIMIT', six.text_type(ss))
     self.assertNotIn('ORDER', six.text_type(ss))
    def test_distinct(self):
        ss = SelectStatement('table', distinct_fields=['field2'])
        ss.add_where(Column(db_field='field1'), EqualsOperator(), 'b')
        self.assertEqual(six.text_type(ss), 'SELECT DISTINCT "field2" FROM table WHERE "field1" = %(0)s', six.text_type(ss))

        ss = SelectStatement('table', distinct_fields=['field1', 'field2'])
        self.assertEqual(six.text_type(ss), 'SELECT DISTINCT "field1", "field2" FROM table')

        ss = SelectStatement('table', distinct_fields=['field1'], count=True)
        self.assertEqual(six.text_type(ss), 'SELECT DISTINCT COUNT("field1") FROM table')
    def test_limit_rendering(self):
        ss = SelectStatement('table', None, limit=10)
        qstr = six.text_type(ss)
        self.assertIn('LIMIT 10', qstr)

        ss = SelectStatement('table', None, limit=0)
        qstr = six.text_type(ss)
        self.assertNotIn('LIMIT', qstr)

        ss = SelectStatement('table', None, limit=None)
        qstr = six.text_type(ss)
        self.assertNotIn('LIMIT', qstr)
 def test_field_rendering(self):
     """ tests that fields are properly added to the select statement """
     ss = SelectStatement('table', ['f1', 'f2'])
     self.assertTrue(
         six.text_type(ss).startswith('SELECT "f1", "f2"'),
         six.text_type(ss))
     self.assertTrue(str(ss).startswith('SELECT "f1", "f2"'), str(ss))
Esempio n. 6
0
    def _verify_statement(self, original):
        st = SelectStatement(self.table_name)
        result = execute(st)
        response = result[0]

        for assignment in original.assignments:
            self.assertEqual(response[assignment.field], assignment.value)
        self.assertEqual(len(response), 7)
    def test_distinct(self):
        ss = SelectStatement('table', distinct_fields=['field2'])
        ss.add_where(Column(db_field='field1'), EqualsOperator(), 'b')
        self.assertEqual(six.text_type(ss), 'SELECT DISTINCT "field2" FROM table WHERE "field1" = %(0)s', six.text_type(ss))

        ss = SelectStatement('table', distinct_fields=['field1', 'field2'])
        self.assertEqual(six.text_type(ss), 'SELECT DISTINCT "field1", "field2" FROM table')

        ss = SelectStatement('table', distinct_fields=['field1'], count=True)
        self.assertEqual(six.text_type(ss), 'SELECT DISTINCT COUNT("field1") FROM table')
    def test_context_id_update(self):
        """ tests that the right things happen the the context id """
        ss = SelectStatement('table')
        ss.add_where_clause(WhereClause('a', EqualsOperator(), 'b'))
        self.assertEqual(ss.get_context(), {'0': 'b'})
        self.assertEqual(str(ss), 'SELECT * FROM table WHERE "a" = %(0)s')

        ss.update_context_id(5)
        self.assertEqual(ss.get_context(), {'5': 'b'})
        self.assertEqual(str(ss), 'SELECT * FROM table WHERE "a" = %(5)s')
 def test_additional_rendering(self):
     ss = SelectStatement('table',
                          None,
                          order_by=['x', 'y'],
                          limit=15,
                          allow_filtering=True)
     qstr = six.text_type(ss)
     self.assertIn('LIMIT 15', qstr)
     self.assertIn('ORDER BY x, y', qstr)
     self.assertIn('ALLOW FILTERING', qstr)
Esempio n. 10
0
 def _select_query(self):
     """
     Returns a select clause based on the given filter args
     """
     if self._where:
         self._validate_select_where()
     return SelectStatement(self.column_family_name,
                            fields=self._select_fields(),
                            where=self._where,
                            order_by=self._order,
                            limit=self._limit,
                            allow_filtering=self._allow_filtering)
    def test_context_id_update(self):
        """ tests that the right things happen the the context id """
        ss = SelectStatement('table')
        ss.add_where(Column(db_field='a'), EqualsOperator(), 'b')
        self.assertEqual(ss.get_context(), {'0': 'b'})
        self.assertEqual(str(ss), 'SELECT * FROM table WHERE "a" = %(0)s')

        ss.update_context_id(5)
        self.assertEqual(ss.get_context(), {'5': 'b'})
        self.assertEqual(str(ss), 'SELECT * FROM table WHERE "a" = %(5)s')
Esempio n. 12
0
    def test_like_operator(self):
        """
        Test to verify the like operator works appropriately

        @since 3.13
        @jira_ticket PYTHON-512
        @expected_result the expected row is read using LIKE

        @test_category data_types:object_mapper
        """
        cluster = TestCluster()
        session = cluster.connect()
        self.addCleanup(cluster.shutdown)

        session.execute("""CREATE CUSTOM INDEX text_index ON {} (text)
                                    USING 'org.apache.cassandra.index.sasi.SASIIndex';"""
                        .format(self.table_name))
        self.addCleanup(session.execute,
                        "DROP INDEX {}.text_index".format(DEFAULT_KEYSPACE))

        partition = uuid4()
        cluster = 1
        self._insert_statement(partition, cluster)

        ss = SelectStatement(self.table_name)
        like_clause = "text_for_%"
        ss.add_where(Column(db_field='text'), LikeOperator(), like_clause)
        self.assertEqual(
            six.text_type(ss),
            'SELECT * FROM {} WHERE "text" LIKE %(0)s'.format(self.table_name))

        result = execute(ss)
        self.assertEqual(result[0]["text"], self.text)

        q = TestQueryUpdateModel.objects.filter(
            text__like=like_clause).allow_filtering()
        self.assertEqual(q[0].text, self.text)

        q = TestQueryUpdateModel.objects.filter(text__like=like_clause)
        self.assertEqual(q[0].text, self.text)
    def test_like_operator(self):
        """
        Test to verify the like operator works appropriately

        @since 3.13
        @jira_ticket PYTHON-512
        @expected_result the expected row is read using LIKE

        @test_category data_types:object_mapper
        """
        cluster = Cluster()
        session = cluster.connect()
        self.addCleanup(cluster.shutdown)

        session.execute("""CREATE CUSTOM INDEX text_index ON {} (text)
                                    USING 'org.apache.cassandra.index.sasi.SASIIndex';""".format(self.table_name))
        self.addCleanup(session.execute, "DROP INDEX {}.text_index".format(DEFAULT_KEYSPACE))

        partition = uuid4()
        cluster = 1
        self._insert_statement(partition, cluster)

        ss = SelectStatement(self.table_name)
        like_clause = "text_for_%"
        ss.add_where(Column(db_field='text'), LikeOperator(), like_clause)
        self.assertEqual(six.text_type(ss),
                         'SELECT * FROM {} WHERE "text" LIKE %(0)s'.format(self.table_name))

        result = execute(ss)
        self.assertEqual(result[0]["text"], self.text)

        q = TestQueryUpdateModel.objects.filter(text__like=like_clause).allow_filtering()
        self.assertEqual(q[0].text, self.text)

        q = TestQueryUpdateModel.objects.filter(text__like=like_clause)
        self.assertEqual(q[0].text, self.text)
Esempio n. 14
0
    def test_partition_key_index(self):
        """
        Test to ensure that statement partition key generation is in the correct order
        @since 3.2
        @jira_ticket PYTHON-535
        @expected_result .

        @test_category object_mapper
        """
        self._check_partition_value_generation(
            BasicModel, SelectStatement(BasicModel.__table_name__))
        self._check_partition_value_generation(
            BasicModel, DeleteStatement(BasicModel.__table_name__))
        self._check_partition_value_generation(
            BasicModelMulti, SelectStatement(BasicModelMulti.__table_name__))
        self._check_partition_value_generation(
            BasicModelMulti, DeleteStatement(BasicModelMulti.__table_name__))
        self._check_partition_value_generation(
            ComplexModelRouting,
            SelectStatement(ComplexModelRouting.__table_name__))
        self._check_partition_value_generation(
            ComplexModelRouting,
            DeleteStatement(ComplexModelRouting.__table_name__))
        self._check_partition_value_generation(BasicModel,
                                               SelectStatement(
                                                   BasicModel.__table_name__),
                                               reverse=True)
        self._check_partition_value_generation(BasicModel,
                                               DeleteStatement(
                                                   BasicModel.__table_name__),
                                               reverse=True)
        self._check_partition_value_generation(
            BasicModelMulti,
            SelectStatement(BasicModelMulti.__table_name__),
            reverse=True)
        self._check_partition_value_generation(
            BasicModelMulti,
            DeleteStatement(BasicModelMulti.__table_name__),
            reverse=True)
        self._check_partition_value_generation(
            ComplexModelRouting,
            SelectStatement(ComplexModelRouting.__table_name__),
            reverse=True)
        self._check_partition_value_generation(
            ComplexModelRouting,
            DeleteStatement(ComplexModelRouting.__table_name__),
            reverse=True)
Esempio n. 15
0
    async def async_iterate(cls,
                            fetch_size: int,
                            fields: list = None,
                            limit: int = None):
        """Iteration by fetch_size
        """
        statement = SimpleStatement(str(
            SelectStatement(cls.column_family_name(),
                            fields=fields,
                            limit=limit)),
                                    fetch_size=fetch_size)
        connection = conn.get_connection()

        paging_state = None
        while True:
            # Execute query
            result_set = await connection.session.execute_future(
                statement, paging_state=paging_state)
            yield [cls(**result) for result in result_set.current_rows]
            paging_state = result_set.paging_state

            # End
            if paging_state is None:
                break
 def test_context(self):
     ss = SelectStatement('table')
     ss.add_where(Column(db_field='a'), EqualsOperator(), 'b')
     self.assertEqual(ss.get_context(), {'0': 'b'})
 def test_count(self):
     ss = SelectStatement('table', count=True, limit=10, order_by='d')
     ss.add_where(Column(db_field='a'), EqualsOperator(), 'b')
     self.assertEqual(six.text_type(ss), 'SELECT COUNT(*) FROM table WHERE "a" = %(0)s LIMIT 10', six.text_type(ss))
     self.assertIn('LIMIT', six.text_type(ss))
     self.assertNotIn('ORDER', six.text_type(ss))
 def test_single_field_is_listified(self):
     """ tests that passing a string field into the constructor puts it into a list """
     ss = SelectStatement('table', 'field')
     self.assertEqual(ss.fields, ['field'])
 def test_context(self):
     ss = SelectStatement('table')
     ss.add_where_clause(WhereClause('a', EqualsOperator(), 'b'))
     self.assertEqual(ss.get_context(), {'0': 'b'})
 def test_where_clause_rendering(self):
     ss = SelectStatement('table')
     ss.add_where_clause(WhereClause('a', EqualsOperator(), 'b'))
     self.assertEqual(six.text_type(ss), 'SELECT * FROM table WHERE "a" = %(0)s', six.text_type(ss))
 def test_table_rendering(self):
     ss = SelectStatement('table')
     self.assertTrue(six.text_type(ss).startswith('SELECT * FROM table'), six.text_type(ss))
     self.assertTrue(str(ss).startswith('SELECT * FROM table'), str(ss))
 def test_none_fields_rendering(self):
     """ tests that a '*' is added if no fields are passed in """
     ss = SelectStatement('table')
     self.assertTrue(six.text_type(ss).startswith('SELECT *'), six.text_type(ss))
     self.assertTrue(str(ss).startswith('SELECT *'), str(ss))
 def test_where_clause_rendering(self):
     ss = SelectStatement('table')
     ss.add_where(Column(db_field='a'), EqualsOperator(), 'b')
     self.assertEqual(six.text_type(ss), 'SELECT * FROM table WHERE "a" = %(0)s', six.text_type(ss))
 def test_context(self):
     ss = SelectStatement('table')
     ss.add_where_clause(WhereClause('a', EqualsOperator(), 'b'))
     self.assertEqual(ss.get_context(), {'0': 'b'})
 def test_context(self):
     ss = SelectStatement('table')
     ss.add_where(Column(db_field='a'), EqualsOperator(), 'b')
     self.assertEqual(ss.get_context(), {'0': 'b'})