test_models.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from django.contrib.contenttypes.models import ContentType
  2. from django.core.exceptions import ValidationError
  3. from django.test import TestCase
  4. from .models import Comment, Tenant, Token, User
  5. class CompositePKModelsTests(TestCase):
  6. @classmethod
  7. def setUpTestData(cls):
  8. cls.tenant_1 = Tenant.objects.create()
  9. cls.tenant_2 = Tenant.objects.create()
  10. cls.user_1 = User.objects.create(
  11. tenant=cls.tenant_1,
  12. id=1,
  13. email="user0001@example.com",
  14. )
  15. cls.user_2 = User.objects.create(
  16. tenant=cls.tenant_1,
  17. id=2,
  18. email="user0002@example.com",
  19. )
  20. cls.user_3 = User.objects.create(
  21. tenant=cls.tenant_2,
  22. id=3,
  23. email="user0003@example.com",
  24. )
  25. cls.comment_1 = Comment.objects.create(id=1, user=cls.user_1)
  26. cls.comment_2 = Comment.objects.create(id=2, user=cls.user_1)
  27. cls.comment_3 = Comment.objects.create(id=3, user=cls.user_2)
  28. cls.comment_4 = Comment.objects.create(id=4, user=cls.user_3)
  29. def test_fields(self):
  30. # tenant_1
  31. self.assertSequenceEqual(
  32. self.tenant_1.user_set.order_by("pk"),
  33. [self.user_1, self.user_2],
  34. )
  35. self.assertSequenceEqual(
  36. self.tenant_1.comments.order_by("pk"),
  37. [self.comment_1, self.comment_2, self.comment_3],
  38. )
  39. # tenant_2
  40. self.assertSequenceEqual(self.tenant_2.user_set.order_by("pk"), [self.user_3])
  41. self.assertSequenceEqual(
  42. self.tenant_2.comments.order_by("pk"), [self.comment_4]
  43. )
  44. # user_1
  45. self.assertEqual(self.user_1.id, 1)
  46. self.assertEqual(self.user_1.tenant_id, self.tenant_1.id)
  47. self.assertEqual(self.user_1.tenant, self.tenant_1)
  48. self.assertEqual(self.user_1.pk, (self.tenant_1.id, self.user_1.id))
  49. self.assertSequenceEqual(
  50. self.user_1.comments.order_by("pk"), [self.comment_1, self.comment_2]
  51. )
  52. # user_2
  53. self.assertEqual(self.user_2.id, 2)
  54. self.assertEqual(self.user_2.tenant_id, self.tenant_1.id)
  55. self.assertEqual(self.user_2.tenant, self.tenant_1)
  56. self.assertEqual(self.user_2.pk, (self.tenant_1.id, self.user_2.id))
  57. self.assertSequenceEqual(self.user_2.comments.order_by("pk"), [self.comment_3])
  58. # comment_1
  59. self.assertEqual(self.comment_1.id, 1)
  60. self.assertEqual(self.comment_1.user_id, self.user_1.id)
  61. self.assertEqual(self.comment_1.user, self.user_1)
  62. self.assertEqual(self.comment_1.tenant_id, self.tenant_1.id)
  63. self.assertEqual(self.comment_1.tenant, self.tenant_1)
  64. self.assertEqual(self.comment_1.pk, (self.tenant_1.id, self.user_1.id))
  65. def test_full_clean_success(self):
  66. test_cases = (
  67. # 1, 1234, {}
  68. ({"tenant": self.tenant_1, "id": 1234}, {}),
  69. ({"tenant_id": self.tenant_1.id, "id": 1234}, {}),
  70. ({"pk": (self.tenant_1.id, 1234)}, {}),
  71. # 1, 1, {"id"}
  72. ({"tenant": self.tenant_1, "id": 1}, {"id"}),
  73. ({"tenant_id": self.tenant_1.id, "id": 1}, {"id"}),
  74. ({"pk": (self.tenant_1.id, 1)}, {"id"}),
  75. # 1, 1, {"tenant", "id"}
  76. ({"tenant": self.tenant_1, "id": 1}, {"tenant", "id"}),
  77. ({"tenant_id": self.tenant_1.id, "id": 1}, {"tenant", "id"}),
  78. ({"pk": (self.tenant_1.id, 1)}, {"tenant", "id"}),
  79. )
  80. for kwargs, exclude in test_cases:
  81. with self.subTest(kwargs):
  82. kwargs["email"] = "user0004@example.com"
  83. User(**kwargs).full_clean(exclude=exclude)
  84. def test_full_clean_failure(self):
  85. e_tenant_and_id = "User with this Tenant and Id already exists."
  86. e_id = "User with this Id already exists."
  87. test_cases = (
  88. # 1, 1, {}
  89. ({"tenant": self.tenant_1, "id": 1}, {}, (e_tenant_and_id, e_id)),
  90. ({"tenant_id": self.tenant_1.id, "id": 1}, {}, (e_tenant_and_id, e_id)),
  91. ({"pk": (self.tenant_1.id, 1)}, {}, (e_tenant_and_id, e_id)),
  92. # 2, 1, {}
  93. ({"tenant": self.tenant_2, "id": 1}, {}, (e_id,)),
  94. ({"tenant_id": self.tenant_2.id, "id": 1}, {}, (e_id,)),
  95. ({"pk": (self.tenant_2.id, 1)}, {}, (e_id,)),
  96. # 1, 1, {"tenant"}
  97. ({"tenant": self.tenant_1, "id": 1}, {"tenant"}, (e_id,)),
  98. ({"tenant_id": self.tenant_1.id, "id": 1}, {"tenant"}, (e_id,)),
  99. ({"pk": (self.tenant_1.id, 1)}, {"tenant"}, (e_id,)),
  100. )
  101. for kwargs, exclude, messages in test_cases:
  102. with self.subTest(kwargs):
  103. with self.assertRaises(ValidationError) as ctx:
  104. kwargs["email"] = "user0004@example.com"
  105. User(**kwargs).full_clean(exclude=exclude)
  106. self.assertSequenceEqual(ctx.exception.messages, messages)
  107. def test_field_conflicts(self):
  108. test_cases = (
  109. ({"pk": (1, 1), "id": 2}, (1, 1)),
  110. ({"id": 2, "pk": (1, 1)}, (1, 1)),
  111. ({"pk": (1, 1), "tenant_id": 2}, (1, 1)),
  112. ({"tenant_id": 2, "pk": (1, 1)}, (1, 1)),
  113. ({"pk": (2, 2), "tenant_id": 3, "id": 4}, (2, 2)),
  114. ({"tenant_id": 3, "id": 4, "pk": (2, 2)}, (2, 2)),
  115. )
  116. for kwargs, pk in test_cases:
  117. with self.subTest(kwargs=kwargs):
  118. user = User(**kwargs)
  119. self.assertEqual(user.pk, pk)
  120. def test_validate_unique(self):
  121. user = User.objects.get(pk=self.user_1.pk)
  122. user.id = None
  123. with self.assertRaises(ValidationError) as ctx:
  124. user.validate_unique()
  125. self.assertSequenceEqual(
  126. ctx.exception.messages, ("User with this Email already exists.",)
  127. )
  128. def test_permissions(self):
  129. token = ContentType.objects.get_for_model(Token)
  130. user = ContentType.objects.get_for_model(User)
  131. comment = ContentType.objects.get_for_model(Comment)
  132. self.assertEqual(4, token.permission_set.count())
  133. self.assertEqual(4, user.permission_set.count())
  134. self.assertEqual(4, comment.permission_set.count())