Exemplo n.º 1
0
    def test_where_clause_rendering(self):
        """ tests that where clauses are rendered properly """
        wc = WhereClause('a', EqualsOperator(), 'c')
        wc.set_context_id(5)

        self.assertEqual('"a" = %(5)s', six.text_type(wc), six.text_type(wc))
        self.assertEqual('"a" = %(5)s', str(wc), type(wc))
    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)
    def _check_partition_value_generation(self, model, state, reverse=False):
        """
        This generates a some statements based on the partition_key_index of the model.
        It then validates that order of the partition key values in the statement matches the index
        specified in the models partition_key_index
        """
        # Setup some unique values for statement generation
        uuid = uuid4()
        values = {
            'k': 5,
            'v': 3,
            'partition': uuid,
            'cluster': 6,
            'count': 42,
            'text': 'text',
            'float': 3.1415,
            'text_2': 'text_2'
        }
        res = dict((v, k) for k, v in values.items())
        items = list(model._partition_key_index.items())
        if (reverse):
            items.reverse()
        # Add where clauses for each partition key
        for partition_key, position in items:
            wc = WhereClause(partition_key, EqualsOperator(),
                             values.get(partition_key))
            state._add_where_clause(wc)

        # Iterate over the partition key values check to see that their index matches
        # Those specified in the models partition field
        for indx, value in enumerate(
                state.partition_key_values(model._partition_key_index)):
            name = res.get(value)
            self.assertEqual(indx, model._partition_key_index.get(name))
    def test_mintimeuuid_function(self):
        """
        Tests that queries with helper functions are generated properly
        """
        now = datetime.now()
        where = WhereClause('time', EqualsOperator(),
                            functions.MinTimeUUID(now))
        where.set_context_id(5)

        self.assertEqual(str(where), '"time" = MinTimeUUID(%(5)s)')
        ctx = {}
        where.update_context(ctx)
        self.assertEqual(ctx, {'5': columns.DateTime().to_database(now)})
 def test_delete_conditional(self):
     where = [WhereClause('id', EqualsOperator(), 1)]
     conditionals = [
         ConditionalClause('f0', 'value0'),
         ConditionalClause('f1', 'value1')
     ]
     ds = DeleteStatement('table', where=where, conditionals=conditionals)
     self.assertEqual(len(ds.conditionals), len(conditionals))
     self.assertEqual(
         six.text_type(ds),
         'DELETE FROM table WHERE "id" = %(0)s IF "f0" = %(1)s AND "f1" = %(2)s',
         six.text_type(ds))
     fields = ['one', 'two']
     ds = DeleteStatement('table',
                          fields=fields,
                          where=where,
                          conditionals=conditionals)
     self.assertEqual(
         six.text_type(ds),
         'DELETE "one", "two" FROM table WHERE "id" = %(0)s IF "f0" = %(1)s AND "f1" = %(2)s',
         six.text_type(ds))
Exemplo n.º 6
0
 def test_equality_method(self):
     """ tests that 2 identical where clauses evaluate as == """
     wc1 = WhereClause('a', EqualsOperator(), 'c')
     wc2 = WhereClause('a', EqualsOperator(), 'c')
     assert wc1 == wc2
Exemplo n.º 7
0
 def test_operator_check(self):
     """ tests that creating a where statement with a non BaseWhereOperator object fails """
     with self.assertRaises(StatementException):
         WhereClause('a', 'b', 'c')