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))


[((311, 335), 'cqlengine.statements.UpdateStatement', 'UpdateStatement', (['"""table"""'], {}), "('table')\n", (326, 335), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((534, 558), 'cqlengine.statements.UpdateStatement', 'UpdateStatement', (['"""table"""'], {}), "('table')\n", (549, 558), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((897, 921), 'cqlengine.statements.UpdateStatement', 'UpdateStatement', (['"""table"""'], {}), "('table')\n", (912, 921), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((1237, 1261), 'cqlengine.statements.UpdateStatement', 'UpdateStatement', (['"""table"""'], {}), "('table')\n", (1252, 1261), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((1707, 1739), 'cqlengine.statements.UpdateStatement', 'UpdateStatement', (['"""table"""'], {'ttl': '(60)'}), "('table', ttl=60)\n", (1722, 1739), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((592, 618), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', (['"""a"""', '"""b"""'], {}), "('a', 'b')\n", (608, 618), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((653, 679), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', (['"""c"""', '"""d"""'], {}), "('c', 'd')\n", (669, 679), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((955, 981), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', (['"""a"""', '"""b"""'], {}), "('a', 'b')\n", (971, 981), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((1016, 1042), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', (['"""c"""', '"""d"""'], {}), "('c', 'd')\n", (1032, 1042), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((1295, 1321), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', (['"""a"""', '"""b"""'], {}), "('a', 'b')\n", (1311, 1321), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((1356, 1382), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', (['"""c"""', '"""d"""'], {}), "('c', 'd')\n", (1372, 1382), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n'), ((1773, 1799), 'cqlengine.statements.AssignmentClause', 'AssignmentClause', (['"""a"""', '"""b"""'], {}), "('a', 'b')\n", (1789, 1799), False, 'from cqlengine.statements import UpdateStatement, WhereClause, AssignmentClause\n')]