From 656df05780eed340f0063aea27596455ad5fce95 Mon Sep 17 00:00:00 2001 From: Bendeguz Csirmaz Date: Fri, 29 Mar 2024 13:41:36 +0800 Subject: [PATCH] Fix tests --- tests/composite_pk/tests.py | 301 +++++++++++++++++++----------------- 1 file changed, 157 insertions(+), 144 deletions(-) diff --git a/tests/composite_pk/tests.py b/tests/composite_pk/tests.py index e96733f2d872..48739efb7fdc 100644 --- a/tests/composite_pk/tests.py +++ b/tests/composite_pk/tests.py @@ -125,36 +125,37 @@ def test_delete_tenant_by_pk(self): self.assertFalse(Comment.objects.filter(id=self.comment.id).exists()) self.assertEqual(len(context.captured_queries), 6) - self.assertEqual( - context.captured_queries[0]["sql"], - f'SELECT "{t}"."id" FROM "{t}" WHERE "{t}"."id" = {self.tenant.id}', - ) - self.assertEqual( - context.captured_queries[1]["sql"], - f'SELECT "{u}"."tenant_id", "{u}"."id" ' - f'FROM "{u}" ' - f'WHERE "{u}"."tenant_id" IN ({self.tenant.id})', - ) - self.assertEqual( - context.captured_queries[2]["sql"], - f'DELETE FROM "{c}" ' - f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' - f'AND "{c}"."user_id" = {self.user.id})', - ) - self.assertEqual( - context.captured_queries[3]["sql"], - f'DELETE FROM "{c}" WHERE "{c}"."tenant_id" IN ({self.tenant.id})', - ) - self.assertEqual( - context.captured_queries[4]["sql"], - f'DELETE FROM "{u}" ' - f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' - f'AND "{u}"."id" = {self.user.id})', - ) - self.assertEqual( - context.captured_queries[5]["sql"], - f'DELETE FROM "{t}" WHERE "{t}"."id" IN ({self.tenant.id})', - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'SELECT "{t}"."id" FROM "{t}" WHERE "{t}"."id" = {self.tenant.id}', + ) + self.assertEqual( + context.captured_queries[1]["sql"], + f'SELECT "{u}"."tenant_id", "{u}"."id" ' + f'FROM "{u}" ' + f'WHERE "{u}"."tenant_id" IN ({self.tenant.id})', + ) + self.assertEqual( + context.captured_queries[2]["sql"], + f'DELETE FROM "{c}" ' + f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' + f'AND "{c}"."user_id" = {self.user.id})', + ) + self.assertEqual( + context.captured_queries[3]["sql"], + f'DELETE FROM "{c}" WHERE "{c}"."tenant_id" IN ({self.tenant.id})', + ) + self.assertEqual( + context.captured_queries[4]["sql"], + f'DELETE FROM "{u}" ' + f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' + f'AND "{u}"."id" = {self.user.id})', + ) + self.assertEqual( + context.captured_queries[5]["sql"], + f'DELETE FROM "{t}" WHERE "{t}"."id" IN ({self.tenant.id})', + ) def test_delete_user_by_id(self): u = User._meta.db_table @@ -172,24 +173,25 @@ def test_delete_user_by_id(self): self.assertFalse(Comment.objects.filter(id=self.comment.id).exists()) self.assertEqual(len(context.captured_queries), 3) - self.assertEqual( - context.captured_queries[0]["sql"], - f'SELECT "{u}"."tenant_id", "{u}"."id" ' - f'FROM "{u}" ' - f'WHERE "{u}"."id" = {self.user.id}', - ) - self.assertEqual( - context.captured_queries[1]["sql"], - f'DELETE FROM "{c}" ' - f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' - f'AND "{c}"."user_id" = {self.user.id})', - ) - self.assertEqual( - context.captured_queries[2]["sql"], - f'DELETE FROM "{u}" ' - f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' - f'AND "{u}"."id" = {self.user.id})', - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'SELECT "{u}"."tenant_id", "{u}"."id" ' + f'FROM "{u}" ' + f'WHERE "{u}"."id" = {self.user.id}', + ) + self.assertEqual( + context.captured_queries[1]["sql"], + f'DELETE FROM "{c}" ' + f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' + f'AND "{c}"."user_id" = {self.user.id})', + ) + self.assertEqual( + context.captured_queries[2]["sql"], + f'DELETE FROM "{u}" ' + f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' + f'AND "{u}"."id" = {self.user.id})', + ) def test_delete_user_by_pk(self): u = User._meta.db_table @@ -207,25 +209,26 @@ def test_delete_user_by_pk(self): self.assertFalse(Comment.objects.filter(id=self.comment.id).exists()) self.assertEqual(len(context.captured_queries), 3) - self.assertEqual( - context.captured_queries[0]["sql"], - f'SELECT "{u}"."tenant_id", "{u}"."id" ' - f'FROM "{u}" ' - f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' - f'AND "{u}"."id" = {self.user.id})', - ) - self.assertEqual( - context.captured_queries[1]["sql"], - f'DELETE FROM "{c}" ' - f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' - f'AND "{c}"."user_id" = {self.user.id})', - ) - self.assertEqual( - context.captured_queries[2]["sql"], - f'DELETE FROM "{u}" ' - f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' - f'AND "{u}"."id" = {self.user.id})', - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'SELECT "{u}"."tenant_id", "{u}"."id" ' + f'FROM "{u}" ' + f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' + f'AND "{u}"."id" = {self.user.id})', + ) + self.assertEqual( + context.captured_queries[1]["sql"], + f'DELETE FROM "{c}" ' + f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' + f'AND "{c}"."user_id" = {self.user.id})', + ) + self.assertEqual( + context.captured_queries[2]["sql"], + f'DELETE FROM "{u}" ' + f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' + f'AND "{u}"."id" = {self.user.id})', + ) class CompositePKGetTests(BaseTestCase): @@ -247,13 +250,14 @@ def test_get_tenant_by_pk(self): self.assertEqual(obj, self.tenant) self.assertEqual(len(context.captured_queries), 1) - self.assertEqual( - context.captured_queries[0]["sql"], - f'SELECT "{t}"."id" ' - f'FROM "{t}" ' - f'WHERE "{t}"."id" = {self.tenant.id} ' - f"LIMIT {MAX_GET_RESULTS}", - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'SELECT "{t}"."id" ' + f'FROM "{t}" ' + f'WHERE "{t}"."id" = {self.tenant.id} ' + f"LIMIT {MAX_GET_RESULTS}", + ) def test_get_user_by_pk(self): u = User._meta.db_table @@ -269,14 +273,15 @@ def test_get_user_by_pk(self): self.assertEqual(obj, self.user) self.assertEqual(len(context.captured_queries), 1) - self.assertEqual( - context.captured_queries[0]["sql"], - f'SELECT "{u}"."tenant_id", "{u}"."id" ' - f'FROM "{u}" ' - f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' - f'AND "{u}"."id" = {self.user.id}) ' - f"LIMIT {MAX_GET_RESULTS}", - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'SELECT "{u}"."tenant_id", "{u}"."id" ' + f'FROM "{u}" ' + f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' + f'AND "{u}"."id" = {self.user.id}) ' + f"LIMIT {MAX_GET_RESULTS}", + ) def test_get_user_by_field(self): u = User._meta.db_table @@ -295,13 +300,14 @@ def test_get_user_by_field(self): self.assertEqual(obj, self.user) self.assertEqual(len(context.captured_queries), 1) - self.assertEqual( - context.captured_queries[0]["sql"], - f'SELECT "{u}"."tenant_id", "{u}"."id" ' - f'FROM "{u}" ' - f'WHERE "{u}"."{column}" = {value} ' - f"LIMIT {MAX_GET_RESULTS}", - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'SELECT "{u}"."tenant_id", "{u}"."id" ' + f'FROM "{u}" ' + f'WHERE "{u}"."{column}" = {value} ' + f"LIMIT {MAX_GET_RESULTS}", + ) def test_get_comment_by_pk(self): c = Comment._meta.db_table @@ -311,14 +317,15 @@ def test_get_comment_by_pk(self): self.assertEqual(obj, self.comment) self.assertEqual(len(context.captured_queries), 1) - self.assertEqual( - context.captured_queries[0]["sql"], - f'SELECT "{c}"."tenant_id", "{c}"."id", "{c}"."user_id" ' - f'FROM "{c}" ' - f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' - f'AND "{c}"."id" = {self.comment.id}) ' - f"LIMIT {MAX_GET_RESULTS}", - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'SELECT "{c}"."tenant_id", "{c}"."id", "{c}"."user_id" ' + f'FROM "{c}" ' + f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' + f'AND "{c}"."id" = {self.comment.id}) ' + f"LIMIT {MAX_GET_RESULTS}", + ) def test_get_comment_by_field(self): c = Comment._meta.db_table @@ -339,13 +346,14 @@ def test_get_comment_by_field(self): self.assertEqual(obj, self.comment) self.assertEqual(len(context.captured_queries), 1) - self.assertEqual( - context.captured_queries[0]["sql"], - f'SELECT "{c}"."tenant_id", "{c}"."id", "{c}"."user_id" ' - f'FROM "{c}" ' - f'WHERE "{c}"."{column}" = {value} ' - f"LIMIT {MAX_GET_RESULTS}", - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'SELECT "{c}"."tenant_id", "{c}"."id", "{c}"."user_id" ' + f'FROM "{c}" ' + f'WHERE "{c}"."{column}" = {value} ' + f"LIMIT {MAX_GET_RESULTS}", + ) def test_get_comment_by_user(self): c = Comment._meta.db_table @@ -355,14 +363,15 @@ def test_get_comment_by_user(self): self.assertEqual(obj, self.comment) self.assertEqual(len(context.captured_queries), 1) - self.assertEqual( - context.captured_queries[0]["sql"], - f'SELECT "{c}"."tenant_id", "{c}"."id", "{c}"."user_id" ' - f'FROM "{c}" ' - f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' - f'AND "{c}"."user_id" = {self.user.id}) ' - f"LIMIT {MAX_GET_RESULTS}", - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'SELECT "{c}"."tenant_id", "{c}"."id", "{c}"."user_id" ' + f'FROM "{c}" ' + f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' + f'AND "{c}"."user_id" = {self.user.id}) ' + f"LIMIT {MAX_GET_RESULTS}", + ) def test_get_comment_by_user_pk(self): c = Comment._meta.db_table @@ -373,16 +382,17 @@ def test_get_comment_by_user_pk(self): self.assertEqual(obj, self.comment) self.assertEqual(len(context.captured_queries), 1) - self.assertEqual( - context.captured_queries[0]["sql"], - f'SELECT "{c}"."tenant_id", "{c}"."id", "{c}"."user_id" ' - f'FROM "{c}" ' - f'INNER JOIN "{u}" ON ("{c}"."tenant_id" = "{u}"."tenant_id" ' - f'AND "{c}"."user_id" = "{u}"."id") ' - f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' - f'AND "{u}"."id" = {self.user.id}) ' - f"LIMIT {MAX_GET_RESULTS}", - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'SELECT "{c}"."tenant_id", "{c}"."id", "{c}"."user_id" ' + f'FROM "{c}" ' + f'INNER JOIN "{u}" ON ("{c}"."tenant_id" = "{u}"."tenant_id" ' + f'AND "{c}"."user_id" = "{u}"."id") ' + f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' + f'AND "{u}"."id" = {self.user.id}) ' + f"LIMIT {MAX_GET_RESULTS}", + ) class CompositePKCreateTests(TestCase): @@ -574,13 +584,14 @@ def test_update_user(self): self.assertEqual(user.tenant_id, self.tenant.id) self.assertEqual(user.id, 8341) self.assertEqual(len(context.captured_queries), 1) - self.assertEqual( - context.captured_queries[0]["sql"], - f'UPDATE "{u}" ' - 'SET "id" = 8341 ' - f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' - f'AND "{u}"."id" = {self.user.id})', - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'UPDATE "{u}" ' + 'SET "id" = 8341 ' + f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' + f'AND "{u}"."id" = {self.user.id})', + ) def test_save_comment(self): c = Comment._meta.db_table @@ -592,14 +603,15 @@ def test_save_comment(self): self.assertEqual(Comment.objects.all().count(), 1) self.assertEqual(len(context.captured_queries), 1) - self.assertEqual( - context.captured_queries[0]["sql"], - f'UPDATE "{c}" ' - f'SET "tenant_id" = {self.tenant.id}, "id" = {self.comment.id}, ' - f'"user_id" = 8214 ' - f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' - f'AND "{c}"."id" = {self.comment.id})', - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + f'UPDATE "{c}" ' + f'SET "tenant_id" = {self.tenant.id}, "id" = {self.comment.id}, ' + f'"user_id" = 8214 ' + f'WHERE ("{c}"."tenant_id" = {self.tenant.id} ' + f'AND "{c}"."id" = {self.comment.id})', + ) @unittest.skipUnless(connection.vendor == "sqlite", "SQLite specific test") def test_bulk_update_comments_in_sqlite(self): @@ -655,13 +667,14 @@ def test_filter_and_count_user_by_pk(self): self.assertEqual(result, 1) self.assertEqual(len(context.captured_queries), 1) - self.assertEqual( - context.captured_queries[0]["sql"], - 'SELECT COUNT(*) AS "__count" ' - f'FROM "{u}" ' - f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' - f'AND "{u}"."id" = {self.user.id})', - ) + if connection.vendor in ("sqlite", "postgresql"): + self.assertEqual( + context.captured_queries[0]["sql"], + 'SELECT COUNT(*) AS "__count" ' + f'FROM "{u}" ' + f'WHERE ("{u}"."tenant_id" = {self.tenant.id} ' + f'AND "{u}"."id" = {self.user.id})', + ) class NamesToPathTests(TestCase):