| 1 |
from django.test import TestCase |
| 2 |
|
| 3 |
from .models import Comment, Tenant, TimeStamped, User |
| 4 |
|
| 5 |
|
| 6 |
class CompositePKGetTests(TestCase): |
| 7 |
maxDiff = None |
| 8 |
|
| 9 |
@classmethod |
| 10 |
def setUpTestData(cls): |
| 11 |
cls.tenant_1 = Tenant.objects.create() |
| 12 |
cls.tenant_2 = Tenant.objects.create() |
| 13 |
cls.user_1 = User.objects.create( |
| 14 |
tenant=cls.tenant_1, |
| 15 |
id=1, |
| 16 |
email="[email protected]", |
| 17 |
) |
| 18 |
cls.user_2 = User.objects.create( |
| 19 |
tenant=cls.tenant_1, |
| 20 |
id=2, |
| 21 |
email="[email protected]", |
| 22 |
) |
| 23 |
cls.user_3 = User.objects.create( |
| 24 |
tenant=cls.tenant_2, |
| 25 |
id=3, |
| 26 |
email="[email protected]", |
| 27 |
) |
| 28 |
cls.comment_1 = Comment.objects.create(id=1, user=cls.user_1) |
| 29 |
|
| 30 |
def test_get_user(self): |
| 31 |
test_cases = ( |
| 32 |
{"pk": self.user_1.pk}, |
| 33 |
{"pk": (self.tenant_1.id, self.user_1.id)}, |
| 34 |
{"id": self.user_1.id}, |
| 35 |
) |
| 36 |
|
| 37 |
for lookup in test_cases: |
| 38 |
with self.subTest(lookup=lookup): |
| 39 |
self.assertEqual(User.objects.get(**lookup), self.user_1) |
| 40 |
|
| 41 |
def test_get_comment(self): |
| 42 |
test_cases = ( |
| 43 |
{"pk": self.comment_1.pk}, |
| 44 |
{"pk": (self.tenant_1.id, self.comment_1.id)}, |
| 45 |
{"id": self.comment_1.id}, |
| 46 |
{"user": self.user_1}, |
| 47 |
{"user_id": self.user_1.id}, |
| 48 |
{"user__id": self.user_1.id}, |
| 49 |
{"user__pk": self.user_1.pk}, |
| 50 |
{"tenant": self.tenant_1}, |
| 51 |
{"tenant_id": self.tenant_1.id}, |
| 52 |
{"tenant__id": self.tenant_1.id}, |
| 53 |
{"tenant__pk": self.tenant_1.pk}, |
| 54 |
) |
| 55 |
|
| 56 |
for lookup in test_cases: |
| 57 |
with self.subTest(lookup=lookup): |
| 58 |
self.assertEqual(Comment.objects.get(**lookup), self.comment_1) |
| 59 |
|
| 60 |
def test_get_or_create_user(self): |
| 61 |
test_cases = ( |
| 62 |
{ |
| 63 |
"pk": self.user_1.pk, |
| 64 |
"defaults": {"email": "[email protected]"}, |
| 65 |
}, |
| 66 |
{ |
| 67 |
"pk": (self.tenant_1.id, self.user_1.id), |
| 68 |
"defaults": {"email": "[email protected]"}, |
| 69 |
}, |
| 70 |
{ |
| 71 |
"tenant": self.tenant_1, |
| 72 |
"id": self.user_1.id, |
| 73 |
"defaults": {"email": "[email protected]"}, |
| 74 |
}, |
| 75 |
{ |
| 76 |
"tenant_id": self.tenant_1.id, |
| 77 |
"id": self.user_1.id, |
| 78 |
"defaults": {"email": "[email protected]"}, |
| 79 |
}, |
| 80 |
) |
| 81 |
|
| 82 |
for fields in test_cases: |
| 83 |
with self.subTest(fields=fields): |
| 84 |
count = User.objects.count() |
| 85 |
user, created = User.objects.get_or_create(**fields) |
| 86 |
self.assertIs(created, False) |
| 87 |
self.assertEqual(user.id, self.user_1.id) |
| 88 |
self.assertEqual(user.pk, (self.tenant_1.id, self.user_1.id)) |
| 89 |
self.assertEqual(user.tenant_id, self.tenant_1.id) |
| 90 |
self.assertEqual(user.email, self.user_1.email) |
| 91 |
self.assertEqual(count, User.objects.count()) |
| 92 |
|
| 93 |
def test_lookup_errors(self): |
| 94 |
m_tuple = "'%s' lookup of 'pk' must be a tuple or a list" |
| 95 |
m_2_elements = "'%s' lookup of 'pk' must have 2 elements" |
| 96 |
m_tuple_collection = ( |
| 97 |
"'in' lookup of 'pk' must be a collection of tuples or lists" |
| 98 |
) |
| 99 |
m_2_elements_each = "'in' lookup of 'pk' must have 2 elements each" |
| 100 |
test_cases = ( |
| 101 |
({"pk": 1}, m_tuple % "exact"), |
| 102 |
({"pk": (1, 2, 3)}, m_2_elements % "exact"), |
| 103 |
({"pk__exact": 1}, m_tuple % "exact"), |
| 104 |
({"pk__exact": (1, 2, 3)}, m_2_elements % "exact"), |
| 105 |
({"pk__in": 1}, m_tuple % "in"), |
| 106 |
({"pk__in": (1, 2, 3)}, m_tuple_collection), |
| 107 |
({"pk__in": ((1, 2, 3),)}, m_2_elements_each), |
| 108 |
({"pk__gt": 1}, m_tuple % "gt"), |
| 109 |
({"pk__gt": (1, 2, 3)}, m_2_elements % "gt"), |
| 110 |
({"pk__gte": 1}, m_tuple % "gte"), |
| 111 |
({"pk__gte": (1, 2, 3)}, m_2_elements % "gte"), |
| 112 |
({"pk__lt": 1}, m_tuple % "lt"), |
| 113 |
({"pk__lt": (1, 2, 3)}, m_2_elements % "lt"), |
| 114 |
({"pk__lte": 1}, m_tuple % "lte"), |
| 115 |
({"pk__lte": (1, 2, 3)}, m_2_elements % "lte"), |
| 116 |
) |
| 117 |
|
| 118 |
for kwargs, message in test_cases: |
| 119 |
with ( |
| 120 |
self.subTest(kwargs=kwargs), |
| 121 |
self.assertRaisesMessage(ValueError, message), |
| 122 |
): |
| 123 |
Comment.objects.get(**kwargs) |
| 124 |
|
| 125 |
def test_get_user_by_comments(self): |
| 126 |
self.assertEqual(User.objects.get(comments=self.comment_1), self.user_1) |
| 127 |
|
| 128 |
def test_get_previous_by_field(self): |
| 129 |
stamp_1 = TimeStamped.objects.create(id=1) |
| 130 |
stamp_2 = TimeStamped(id=2) |
| 131 |
msg = "get_next/get_previous cannot be used on unsaved objects." |
| 132 |
with self.assertRaisesMessage(ValueError, msg): |
| 133 |
stamp_2.get_previous_by_created() |
| 134 |
stamp_2.save() |
| 135 |
self.assertEqual(stamp_2.get_previous_by_created(), stamp_1) |