Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
csirmazbendeguz committed Mar 29, 2024
1 parent f3d3eac commit 656df05
Showing 1 changed file with 157 additions and 144 deletions.
301 changes: 157 additions & 144 deletions tests/composite_pk/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 656df05

Please sign in to comment.