-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2aa74f4
commit 6a26b19
Showing
7 changed files
with
82 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,90 +22,105 @@ def setUpTestData(cls): | |
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite specific test") | ||
def test_create_user_in_sqlite(self): | ||
test_cases = [ | ||
({"tenant": self.tenant, "id": 2412}, 2412), | ||
({"tenant_id": self.tenant.id, "id": 5316}, 5316), | ||
({"pk": (self.tenant.id, 7424)}, 7424), | ||
{"tenant": self.tenant, "id": 2412, "email": "[email protected]"}, | ||
{"tenant_id": self.tenant.id, "id": 5316, "email": "[email protected]"}, | ||
{"pk": (self.tenant.id, 7424), "email": "[email protected]"}, | ||
] | ||
|
||
for fields, user_id in test_cases: | ||
with self.subTest(fields=fields, user_id=user_id): | ||
for fields in test_cases: | ||
user = User(**fields) | ||
self.assertIsNotNone(user.id) | ||
self.assertIsNotNone(user.email) | ||
|
||
with self.subTest(fields=fields): | ||
with CaptureQueriesContext(connection) as context: | ||
obj = User.objects.create(**fields) | ||
|
||
self.assertEqual(obj.tenant_id, self.tenant.id) | ||
self.assertEqual(obj.id, user_id) | ||
self.assertEqual(obj.pk, (self.tenant.id, user_id)) | ||
self.assertEqual(obj.id, user.id) | ||
self.assertEqual(obj.pk, (self.tenant.id, user.id)) | ||
self.assertEqual(obj.email, user.email) | ||
self.assertEqual(len(context.captured_queries), 1) | ||
u = User._meta.db_table | ||
self.assertEqual( | ||
context.captured_queries[0]["sql"], | ||
f'INSERT INTO "{u}" ("tenant_id", "id") ' | ||
f"VALUES ({self.tenant.id}, {user_id})", | ||
f'INSERT INTO "{u}" ("tenant_id", "id", "email") ' | ||
f"VALUES ({self.tenant.id}, {user.id}, '{user.email}')", | ||
) | ||
|
||
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific test") | ||
def test_create_user_in_postgresql(self): | ||
test_cases = [ | ||
({"tenant": self.tenant, "id": 5231}, 5231), | ||
({"tenant_id": self.tenant.id, "id": 6123}, 6123), | ||
({"pk": (self.tenant.id, 3513)}, 3513), | ||
{"tenant": self.tenant, "id": 5231, "email": "[email protected]"}, | ||
{"tenant_id": self.tenant.id, "id": 6123, "email": "[email protected]"}, | ||
{"pk": (self.tenant.id, 3513), "email": "[email protected]"}, | ||
] | ||
|
||
for fields, user_id in test_cases: | ||
with self.subTest(fields=fields, user_id=user_id): | ||
for fields in test_cases: | ||
user = User(**fields) | ||
self.assertIsNotNone(user.id) | ||
self.assertIsNotNone(user.email) | ||
|
||
with self.subTest(fields=fields): | ||
with CaptureQueriesContext(connection) as context: | ||
obj = User.objects.create(**fields) | ||
|
||
self.assertEqual(obj.tenant_id, self.tenant.id) | ||
self.assertEqual(obj.id, user_id) | ||
self.assertEqual(obj.pk, (self.tenant.id, user_id)) | ||
self.assertEqual(obj.id, user.id) | ||
self.assertEqual(obj.pk, (self.tenant.id, user.id)) | ||
self.assertEqual(obj.email, user.email) | ||
self.assertEqual(len(context.captured_queries), 1) | ||
u = User._meta.db_table | ||
self.assertEqual( | ||
context.captured_queries[0]["sql"], | ||
f'INSERT INTO "{u}" ("tenant_id", "id") ' | ||
f"VALUES ({self.tenant.id}, {user_id}) " | ||
f'INSERT INTO "{u}" ("tenant_id", "id", "email") ' | ||
f"VALUES ({self.tenant.id}, {user.id}, '{user.email}') " | ||
f'RETURNING "{u}"."id"', | ||
) | ||
|
||
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific test") | ||
def test_create_user_with_autofield_in_postgresql(self): | ||
test_cases = [ | ||
{"tenant": self.tenant}, | ||
{"tenant_id": self.tenant.id}, | ||
{"tenant": self.tenant, "email": "[email protected]"}, | ||
{"tenant_id": self.tenant.id, "email": "[email protected]"}, | ||
] | ||
|
||
for fields in test_cases: | ||
user = User(**fields) | ||
self.assertIsNotNone(user.email) | ||
|
||
with CaptureQueriesContext(connection) as context: | ||
obj = User.objects.create(**fields) | ||
|
||
self.assertEqual(obj.tenant_id, self.tenant.id) | ||
self.assertIsInstance(obj.id, int) | ||
self.assertGreater(obj.id, 0) | ||
self.assertEqual(obj.pk, (self.tenant.id, obj.id)) | ||
self.assertEqual(obj.email, user.email) | ||
self.assertEqual(len(context.captured_queries), 1) | ||
u = User._meta.db_table | ||
self.assertEqual( | ||
context.captured_queries[0]["sql"], | ||
f'INSERT INTO "{u}" ("tenant_id") ' | ||
f"VALUES ({self.tenant.id}) " | ||
f'INSERT INTO "{u}" ("tenant_id", "email") ' | ||
f"VALUES ({self.tenant.id}, '{user.email}') " | ||
f'RETURNING "{u}"."id"', | ||
) | ||
|
||
def test_save_user(self): | ||
user = User(tenant=self.tenant, id=9241) | ||
user = User(tenant=self.tenant, id=9241, email="[email protected]") | ||
user.save() | ||
self.assertEqual(user.tenant_id, self.tenant.id) | ||
self.assertEqual(user.tenant, self.tenant) | ||
self.assertEqual(user.id, 9241) | ||
self.assertEqual(user.pk, (self.tenant.id, 9241)) | ||
self.assertEqual(user.email, "[email protected]") | ||
|
||
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite specific test") | ||
def test_bulk_create_users_in_sqlite(self): | ||
objs = [ | ||
User(tenant=self.tenant, id=8291), | ||
User(tenant_id=self.tenant.id, id=4021), | ||
User(pk=(self.tenant.id, 8214)), | ||
User(tenant=self.tenant, id=8291, email="[email protected]"), | ||
User(tenant_id=self.tenant.id, id=4021, email="[email protected]"), | ||
User(pk=(self.tenant.id, 8214), email="[email protected]"), | ||
] | ||
|
||
with CaptureQueriesContext(connection) as context: | ||
|
@@ -125,19 +140,20 @@ def test_bulk_create_users_in_sqlite(self): | |
u = User._meta.db_table | ||
self.assertEqual( | ||
context.captured_queries[0]["sql"], | ||
f'INSERT INTO "{u}" ("tenant_id", "id") ' | ||
f"VALUES ({self.tenant.id}, 8291), ({self.tenant.id}, 4021), " | ||
f"({self.tenant.id}, 8214)", | ||
f'INSERT INTO "{u}" ("tenant_id", "id", "email") ' | ||
f"VALUES ({self.tenant.id}, 8291, '[email protected]'), " | ||
f"({self.tenant.id}, 4021, '[email protected]'), " | ||
f"({self.tenant.id}, 8214, '[email protected]')", | ||
) | ||
|
||
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific test") | ||
def test_bulk_create_users_in_postgresql(self): | ||
objs = [ | ||
User(tenant=self.tenant, id=8361), | ||
User(tenant_id=self.tenant.id, id=2819), | ||
User(pk=(self.tenant.id, 9136)), | ||
User(tenant=self.tenant), | ||
User(tenant_id=self.tenant.id), | ||
User(tenant=self.tenant, id=8361, email="[email protected]"), | ||
User(tenant_id=self.tenant.id, id=2819, email="[email protected]"), | ||
User(pk=(self.tenant.id, 9136), email="[email protected]"), | ||
User(tenant=self.tenant, email="[email protected]"), | ||
User(tenant_id=self.tenant.id, email="[email protected]"), | ||
] | ||
|
||
with CaptureQueriesContext(connection) as context: | ||
|
@@ -165,15 +181,17 @@ def test_bulk_create_users_in_postgresql(self): | |
u = User._meta.db_table | ||
self.assertEqual( | ||
context.captured_queries[0]["sql"], | ||
f'INSERT INTO "{u}" ("tenant_id", "id") ' | ||
f"VALUES ({self.tenant.id}, 8361), ({self.tenant.id}, 2819), " | ||
f"({self.tenant.id}, 9136) " | ||
f'INSERT INTO "{u}" ("tenant_id", "id", "email") ' | ||
f"VALUES ({self.tenant.id}, 8361, '[email protected]'), " | ||
f"({self.tenant.id}, 2819, '[email protected]'), " | ||
f"({self.tenant.id}, 9136, '[email protected]') " | ||
f'RETURNING "{u}"."id"', | ||
) | ||
self.assertEqual( | ||
context.captured_queries[1]["sql"], | ||
f'INSERT INTO "{u}" ("tenant_id") ' | ||
f"VALUES ({self.tenant.id}), ({self.tenant.id}) " | ||
f'INSERT INTO "{u}" ("tenant_id", "email") ' | ||
f"VALUES ({self.tenant.id}, '[email protected]'), " | ||
f"({self.tenant.id}, '[email protected]') " | ||
f'RETURNING "{u}"."id"', | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters