def test_insert(self): io = self.conn.__getitem__.return_value.insert_many sql = 'INSERT INTO "table" ("col1", "col2") VALUES (%s, %s)' params = [1, 2] result = Result(self.db, self.conn, self.conn_prop, sql, params) io.assert_any_call([{'col1': 1, 'col2': 2}], ordered=False) self.conn.reset_mock() sql = 'INSERT INTO "table" ("col1", "col2") VALUES (%s, NULL)' params = [1] result = Result(self.db, self.conn, self.conn_prop, sql, params) io.assert_any_call([{'col1': 1, 'col2': None}], ordered=False) self.conn.reset_mock() sql = 'INSERT INTO "table" ("col") VALUES (%s)' params = [1] result = Result(self.db, self.conn, self.conn_prop, sql, params) io.assert_any_call([{'col': 1}], ordered=False) self.conn.reset_mock() #INSERT INTO "m2m_regress_post" ("id") VALUES (DEFAULT) sql = 'INSERT INTO "table" ("id") VALUES (DEFAULT)' params = [] aid = MagicMock() auto = {'auto': {'field_names': ['id'], 'seq': 1}} self.conn.__getitem__().find_one_and_update.return_value = auto result = Result(self.db, self.conn, self.conn_prop, sql, params) io.assert_any_call([{'id': 1}], ordered=False) self.conn.reset_mock()
def test_update(self): um = self.conn.__getitem__.return_value.update_many sql = 'UPDATE "table" SET "col1" = %s, "col2" = NULL WHERE "table"."col2" = %s' params = [1, 2] result = Result(self.db, self.conn, self.conn_prop, sql, params) um.assert_any_call(filter={'col2': {'$eq': 2}}, update={'$set': {'col1': 1, 'col2': None}}) self.conn.reset_mock() sql = 'UPDATE "table" SET "col" = %s WHERE "table"."col" = %s' params = [1,2] result = Result(self.db, self.conn, self.conn_prop, sql, params) um.assert_any_call(filter={'col': {'$eq': 2}}, update={'$set': {'col': 1}}) self.conn.reset_mock() sql = 'UPDATE "table" SET "col1" = %s WHERE "table"."col2" = %s' params = [1,2] result = Result(self.db, self.conn, self.conn_prop, sql, params) um.assert_any_call(filter={'col2': {'$eq': 2}}, update={'$set': {'col1': 1}}) self.conn.reset_mock() sql = 'UPDATE "table" SET "col1" = %s, "col2" = %s WHERE "table"."col2" = %s' params = [1, 2, 3] result = Result(self.db, self.conn, self.conn_prop, sql, params) um.assert_any_call(filter={'col2': {'$eq': 3}}, update={'$set': {'col1': 1, 'col2': 2}}) self.conn.reset_mock()
def test_insert(self): io = self.conn.__getitem__.return_value.insert_many sql = 'INSERT INTO "table" ("col1", "col2") VALUES (%s, %s)' params = [1, 2] result = Result(self.db, self.conn, self.conn_prop, sql, params) io.assert_any_call([{'col1':1, 'col2': 2}], ordered=False) self.conn.reset_mock() sql = 'INSERT INTO "table" ("col1", "col2") VALUES (%s, NULL)' params = [1] result = Result(self.db, self.conn, self.conn_prop, sql, params) io.assert_any_call([{'col1': 1, 'col2': None}], ordered=False) self.conn.reset_mock() sql = 'INSERT INTO "table" ("col") VALUES (%s)' params = [1] result = Result(self.db, self.conn, self.conn_prop, sql, params) io.assert_any_call([{'col': 1}], ordered=False) self.conn.reset_mock() #INSERT INTO "m2m_regress_post" ("id") VALUES (DEFAULT) sql = 'INSERT INTO "table" ("id") VALUES (DEFAULT)' params = [] result = Result(self.db, self.conn, self.conn_prop, sql, params) io.assert_any_call([], ordered=False) self.conn.reset_mock()
def test_insert(self): io = self.conn.__getitem__.return_value.insert_one sql = 'INSERT INTO "table" ("col1", "col2") VALUES (%s, %s)' params = [1, 2] result = Result(self.db, self.conn, sql, params) io.assert_any_call({'col1': 1, 'col2': 2}) self.conn.reset_mock() sql = 'INSERT INTO "table" ("col1", "col2") VALUES (%s, NULL)' params = [1] result = Result(self.db, self.conn, sql, params) io.assert_any_call({'col1': 1, 'col2': None}) self.conn.reset_mock() sql = 'INSERT INTO "table" ("col") VALUES (%s)' params = [1] result = Result(self.db, self.conn, sql, params) io.assert_any_call({'col': 1}) self.conn.reset_mock()
def aggregate_mock(self, pipeline, iter_return_value=None, ans=None): if iter_return_value: self.agg_iter.return_value = iter_return_value result = list(Result(self.db, self.conn, self.conn_prop, self.sql, self.params)) self.aggregate.assert_any_call(pipeline) if self.params == self.params_none: self.params.assert_not_called() if ans: self.assertEqual(result, ans) self.conn.reset_mock()
def find_mock(self): result = Result(self.db, self.conn, self.conn_prop, self.sql, self.params) return list(result)
def exe(self): result = Result(self.db, self.conn, self.conn_prop, self.sql, self.params)