#coding: utf8
from sweet._tests import TestCase
from sweet.database.recordset import MySQLRecordset


class TestRecordsetUpdateMySQL(TestCase):

    def get_recordset(self):
        class FakeDB(object): pass
        return MySQLRecordset(db=FakeDB(), tbname="users")

    def test_update(self):
        def _(sql, *params):
            self.assertEqual('UPDATE `users` SET `age` = %s, `name` = %s WHERE `age` >= %s OR `name` = %s', sql)
            self.assertEqual([20, 'nothing', 40, 'Ryan'], list(params))
            return 3
        tb = self.get_recordset().where(age__gte=40).or_where(name="Ryan")
        tb.db.execute_rowcount = _
        self.assertEqual(3, tb.update(age=20, name='nothing'))

    def test_update_with_join(self):
        def _(sql, *params):
            self.assertEqual('UPDATE `users` INNER JOIN `cars` ON `users`.`id` = `cars`.`user_id` SET `name` = %s WHERE `id` IN (%s, %s, %s) OR `name` = %s', sql)
            self.assertEqual([ 'nothing', 1, 2, 3, 'Ryan'], list(params))
            return 3
        tb = self.get_recordset()
        tb.db.execute_rowcount = _

        r = tb.where(id=[1,2,3]).or_where(name="Ryan").join('cars', "users.id=cars.user_id").update(name='nothing')
        self.assertEqual(3, r)

    def test_increase(self):
        def _(sql, *params):
            self.assertEqual('UPDATE `users` SET `age` = `age` + %s, `score` = `score` + %s WHERE `age` >= %s OR `name` = %s', sql)
            self.assertEqual([10, 20, 40, 'Ryan'], list(params))
            return 3
        tb = self.get_recordset().where(age__gte=40).or_where(name="Ryan")
        tb.db.execute_rowcount = _
        self.assertEqual(3, tb.increase(age=10, score=20))

    def test_decrease(self):
        def _(sql, *params):
            self.assertEqual('UPDATE `users` SET `age` = `age` - %s, `score` = `score` - %s WHERE `age` >= %s OR `name` = %s', sql)
            self.assertEqual([10, 20, 40, 'Ryan'], list(params))
            return 3
        tb = self.get_recordset().where(age__gte=40).or_where(name="Ryan")
        tb.db.execute_rowcount = _
        self.assertEqual(3, tb.decrease(age=10, score=20))


if __name__ == '__main__':
    import unittest
    unittest.main()


#coding: utf8
from sweet._tests import TestCase
from sweet.database.recordset import MySQLRecordset
from unittest import mock


class TestRecordsetUpdateMySQL(TestCase):

    def test_update(self):
        db = mock.MagicMock('db')
        db.execute_rowcount = mock.MagicMock(return_value=3)
        tb = MySQLRecordset(db=db, tbname='users').where(age__gte=40).or_where(name="Ryan")
        tb.update(age=20, name='nothing')
        db.execute_rowcount.assert_called_once_with('UPDATE `users` SET `age` = %s, `name` = %s WHERE `age` >= %s OR `name` = %s', *[20, 'nothing', 40, 'Ryan'])

    def test_update_with_join(self):
        db = mock.MagicMock('db')
        db.execute_rowcount = mock.MagicMock(return_value=3)
        tb = MySQLRecordset(db=db, tbname='users').where(id=[1,2,3]).or_where(name="Ryan").join('cars', "users.id=cars.user_id")
        tb.update(name='nothing')
        db.execute_rowcount.assert_called_once_with('UPDATE `users` INNER JOIN `cars` ON `users`.`id` = `cars`.`user_id` SET `name` = %s WHERE `id` IN (%s, %s, %s) OR `name` = %s', *['nothing', 1, 2, 3, 'Ryan'])

    def test_increase(self):
        db = mock.MagicMock('db')
        db.execute_rowcount = mock.MagicMock(return_value=3)
        tb = MySQLRecordset(db=db, tbname='users').where(age__gte=40).or_where(name="Ryan")
        tb.increase(age=10, score=20)
        db.execute_rowcount.assert_called_once_with('UPDATE `users` SET `age` = `age` + %s, `score` = `score` + %s WHERE `age` >= %s OR `name` = %s', *[10, 20, 40, 'Ryan'])

    def test_decrease(self):
        db = mock.MagicMock('db')
        db.execute_rowcount = mock.MagicMock(return_value=3)
        tb = MySQLRecordset(db=db, tbname='users').where(age__gte=40).or_where(name="Ryan")
        tb.decrease(age=10, score=20)
        db.execute_rowcount.assert_called_once_with('UPDATE `users` SET `age` = `age` - %s, `score` = `score` - %s WHERE `age` >= %s OR `name` = %s', *[10, 20, 40, 'Ryan'])


if __name__ == '__main__':
    import unittest
    unittest.main()


use mock