from unittest import TestCase
from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause
from cqlengine.operators import *


class UpdateStatementTests(TestCase):

    def test_table_rendering(self):
        """ tests that fields are properly added to the select statement """
        us = UpdateStatement('table')
        self.assertTrue(unicode(us).startswith('UPDATE table SET'), unicode(us))
        self.assertTrue(str(us).startswith('UPDATE table SET'), str(us))

    def test_rendering(self):
        us = UpdateStatement('table')
        us.add_assignment_clause(AssignmentClause('a', 'b'))
        us.add_assignment_clause(AssignmentClause('c', 'd'))
        us.add_where_clause(WhereClause('a', EqualsOperator(), 'x'))
        self.assertEqual(unicode(us), 'UPDATE table SET "a" = :0, "c" = :1 WHERE "a" = :2', unicode(us))

    def test_context(self):
        us = UpdateStatement('table')
        us.add_assignment_clause(AssignmentClause('a', 'b'))
        us.add_assignment_clause(AssignmentClause('c', 'd'))
        us.add_where_clause(WhereClause('a', EqualsOperator(), 'x'))
        self.assertEqual(us.get_context(), {'0': 'b', '1': 'd', '2': 'x'})

    def test_context_update(self):
        us = UpdateStatement('table')
        us.add_assignment_clause(AssignmentClause('a', 'b'))
        us.add_assignment_clause(AssignmentClause('c', 'd'))
        us.add_where_clause(WhereClause('a', EqualsOperator(), 'x'))
        us.update_context_id(3)
        self.assertEqual(unicode(us), 'UPDATE table SET "a" = :4, "c" = :5 WHERE "a" = :3')
        self.assertEqual(us.get_context(), {'4': 'b', '5': 'd', '3': 'x'})

    def test_additional_rendering(self):
        us = UpdateStatement('table', ttl=60)
        us.add_assignment_clause(AssignmentClause('a', 'b'))
        us.add_where_clause(WhereClause('a', EqualsOperator(), 'x'))
        self.assertIn('USING TTL 60', unicode(us))


[((10, 13, 10, 37), 'cqlengine.statements.UpdateStatement', 'UpdateStatement', ({(10, 29, 10, 36): '"""table"""'}, {}), "('table')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((15, 13, 15, 37), 'cqlengine.statements.UpdateStatement', 'UpdateStatement', ({(15, 29, 15, 36): '"""table"""'}, {}), "('table')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((22, 13, 22, 37), 'cqlengine.statements.UpdateStatement', 'UpdateStatement', ({(22, 29, 22, 36): '"""table"""'}, {}), "('table')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((29, 13, 29, 37), 'cqlengine.statements.UpdateStatement', 'UpdateStatement', ({(29, 29, 29, 36): '"""table"""'}, {}), "('table')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((38, 13, 38, 45), 'cqlengine.statements.UpdateStatement', 'UpdateStatement', (), '', False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((16, 33, 16, 59), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', ({(16, 50, 16, 53): '"""a"""', (16, 55, 16, 58): '"""b"""'}, {}), "('a', 'b')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((17, 33, 17, 59), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', ({(17, 50, 17, 53): '"""c"""', (17, 55, 17, 58): '"""d"""'}, {}), "('c', 'd')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((23, 33, 23, 59), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', ({(23, 50, 23, 53): '"""a"""', (23, 55, 23, 58): '"""b"""'}, {}), "('a', 'b')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((24, 33, 24, 59), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', ({(24, 50, 24, 53): '"""c"""', (24, 55, 24, 58): '"""d"""'}, {}), "('c', 'd')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((30, 33, 30, 59), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', ({(30, 50, 30, 53): '"""a"""', (30, 55, 30, 58): '"""b"""'}, {}), "('a', 'b')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((31, 33, 31, 59), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', ({(31, 50, 31, 53): '"""c"""', (31, 55, 31, 58): '"""d"""'}, {}), "('c', 'd')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((39, 33, 39, 59), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', ({(39, 50, 39, 53): '"""a"""', (39, 55, 39, 58): '"""b"""'}, {}), "('a', 'b')", False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n')]