Beispiel #1
0
    def test_statement(self):
        ist = InsertStatement('table', None)
        ist.add_assignment(Column(db_field='a'), 'b')
        ist.add_assignment(Column(db_field='c'), 'd')

        self.assertEqual(six.text_type(ist),
                         'INSERT INTO table ("a", "c") VALUES (%(0)s, %(1)s)')
Beispiel #2
0
 def test_context_update(self):
     us = UpdateStatement('table')
     us.add_assignment(Column(db_field='a'), 'b')
     us.add_assignment(Column(db_field='c'), 'd')
     us.add_where(Column(db_field='a'), EqualsOperator(), 'x')
     us.update_context_id(3)
     self.assertEqual(
         six.text_type(us),
         'UPDATE table SET "a" = %(4)s, "c" = %(5)s WHERE "a" = %(3)s')
     self.assertEqual(us.get_context(), {'4': 'b', '5': 'd', '3': 'x'})
Beispiel #3
0
    def test_context_update(self):
        ist = InsertStatement('table', None)
        ist.add_assignment(Column(db_field='a'), 'b')
        ist.add_assignment(Column(db_field='c'), 'd')

        ist.update_context_id(4)
        self.assertEqual(six.text_type(ist),
                         'INSERT INTO table ("a", "c") VALUES (%(4)s, %(5)s)')
        ctx = ist.get_context()
        self.assertEqual(ctx, {'4': 'b', '5': 'd'})
Beispiel #4
0
    def test_rendering(self):
        us = UpdateStatement('table')
        us.add_assignment(Column(db_field='a'), 'b')
        us.add_assignment(Column(db_field='c'), 'd')
        us.add_where(Column(db_field='a'), EqualsOperator(), 'x')
        self.assertEqual(
            six.text_type(us),
            'UPDATE table SET "a" = %(0)s, "c" = %(1)s WHERE "a" = %(2)s',
            six.text_type(us))

        us.add_where(Column(db_field='a'), NotEqualsOperator(), 'y')
        self.assertEqual(
            six.text_type(us),
            'UPDATE table SET "a" = %(0)s, "c" = %(1)s WHERE "a" = %(2)s AND "a" != %(3)s',
            six.text_type(us))
    def test_context_update(self):
        ds = DeleteStatement('table', None)
        ds.add_field(MapDeleteClause('d', {1: 2}, {1: 2, 3: 4}))
        ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')

        ds.update_context_id(7)
        self.assertEqual(six.text_type(ds),
                         'DELETE "d"[%(8)s] FROM table WHERE "a" = %(7)s')
        self.assertEqual(ds.get_context(), {'7': 'b', '8': 3})
 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_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')
Beispiel #8
0
    def test_comparisons(self):
        c0 = Column()
        c1 = Column()
        self.assertEqual(c1.position - c0.position, 1)

        # __ne__
        self.assertNotEqual(c0, c1)
        self.assertNotEqual(c0, object())

        # __eq__
        self.assertEqual(c0, c0)
        self.assertFalse(c0 == object())

        # __lt__
        self.assertLess(c0, c1)
        try:
            c0 < object()  # this raises for Python 3
        except TypeError:
            pass

        # __le__
        self.assertLessEqual(c0, c1)
        self.assertLessEqual(c0, c0)
        try:
            c0 <= object()  # this raises for Python 3
        except TypeError:
            pass

        # __gt__
        self.assertGreater(c1, c0)
        try:
            c1 > object()  # this raises for Python 3
        except TypeError:
            pass

        # __ge__
        self.assertGreaterEqual(c1, c0)
        self.assertGreaterEqual(c1, c1)
        try:
            c1 >= object()  # this raises for Python 3
        except TypeError:
            pass
    def test_range_deletion_rendering(self):
        ds = DeleteStatement('table', None)
        ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')
        ds.add_where(Column(db_field='created_at'),
                     GreaterThanOrEqualOperator(), '0')
        ds.add_where(Column(db_field='created_at'), LessThanOrEqualOperator(),
                     '10')
        self.assertEqual(
            six.text_type(ds),
            'DELETE FROM table WHERE "a" = %(0)s AND "created_at" >= %(1)s AND "created_at" <= %(2)s',
            six.text_type(ds))

        ds = DeleteStatement('table', None)
        ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')
        ds.add_where(Column(db_field='created_at'), InOperator(),
                     ['0', '10', '20'])
        self.assertEqual(
            six.text_type(ds),
            'DELETE FROM table WHERE "a" = %(0)s AND "created_at" IN %(1)s',
            six.text_type(ds))

        ds = DeleteStatement('table', None)
        ds.add_where(Column(db_field='a'), NotEqualsOperator(), 'b')
        self.assertEqual(six.text_type(ds),
                         'DELETE FROM table WHERE "a" != %(0)s',
                         six.text_type(ds))
Beispiel #10
0
 def _get_partition_keys(self):
     try:
         table_meta = get_cluster(
             self._get_connection()).metadata.keyspaces[
                 self.keyspace].tables[self.name]
         self.__partition_keys = OrderedDict(
             (pk.name,
              Column(primary_key=True, partition_key=True, db_field=pk.name)
              ) for pk in table_meta.partition_key)
     except Exception as e:
         raise CQLEngineException(
             "Failed inspecting partition keys for {0}."
             "Ensure cqlengine is connected before attempting this with NamedTable."
             .format(self.column_family_name()))
    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_where_clause_rendering(self):
     ds = DeleteStatement('table', None)
     ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')
     self.assertEqual(six.text_type(ds),
                      'DELETE FROM table WHERE "a" = %(0)s',
                      six.text_type(ds))
 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_context(self):
     ds = DeleteStatement('table', None)
     ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')
     self.assertEqual(ds.get_context(), {'0': 'b'})
 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))
Beispiel #16
0
 def test_hash(self):
     c0 = Column()
     self.assertEqual(id(c0), c0.__hash__())
Beispiel #17
0
 def test_additional_rendering(self):
     ist = InsertStatement('table', ttl=60)
     ist.add_assignment(Column(db_field='a'), 'b')
     ist.add_assignment(Column(db_field='c'), 'd')
     self.assertIn('USING TTL 60', six.text_type(ist))
    def test_insert_statement_execute(self):
        """
        Test to verify the execution of BaseCQLStatements using connection.execute

        @since 3.10
        @jira_ticket PYTHON-505
        @expected_result inserts a row in C*, updates the rows and then deletes
        all the rows using BaseCQLStatements

        @test_category data_types:object_mapper
        """
        partition = uuid4()
        cluster = 1

        #Verifying insert statement
        st = InsertStatement(self.table_name)
        st.add_assignment(Column(db_field='partition'), partition)
        st.add_assignment(Column(db_field='cluster'), cluster)

        st.add_assignment(Column(db_field='count'), 1)
        st.add_assignment(Column(db_field='text'), "text_for_db")
        st.add_assignment(Column(db_field='text_set'), set(("foo", "bar")))
        st.add_assignment(Column(db_field='text_list'), ["foo", "bar"])
        st.add_assignment(Column(db_field='text_map'), {
            "foo": '1',
            "bar": '2'
        })

        execute(st)
        self._verify_statement(st)

        # Verifying update statement
        where = [
            WhereClause('partition', EqualsOperator(), partition),
            WhereClause('cluster', EqualsOperator(), cluster)
        ]

        st = UpdateStatement(self.table_name, where=where)
        st.add_assignment(Column(db_field='count'), 2)
        st.add_assignment(Column(db_field='text'), "text_for_db_update")
        st.add_assignment(Column(db_field='text_set'),
                          set(("foo_update", "bar_update")))
        st.add_assignment(Column(db_field='text_list'),
                          ["foo_update", "bar_update"])
        st.add_assignment(Column(db_field='text_map'), {
            "foo": '3',
            "bar": '4'
        })

        execute(st)
        self._verify_statement(st)

        # Verifying delete statement
        execute(DeleteStatement(self.table_name, where=where))
        self.assertEqual(TestQueryUpdateModel.objects.count(), 0)
Beispiel #19
0
 def test_additional_rendering(self):
     us = UpdateStatement('table', ttl=60)
     us.add_assignment(Column(db_field='a'), 'b')
     us.add_where(Column(db_field='a'), EqualsOperator(), 'x')
     self.assertIn('USING TTL 60', six.text_type(us))
Beispiel #20
0
 def test_context(self):
     us = UpdateStatement('table')
     us.add_assignment(Column(db_field='a'), 'b')
     us.add_assignment(Column(db_field='c'), 'd')
     us.add_where(Column(db_field='a'), EqualsOperator(), 'x')
     self.assertEqual(us.get_context(), {'0': 'b', '1': 'd', '2': 'x'})