Skip to content

Commit 4ff8bc4

Browse files
committed
reorganize monolithic test file into many smaller files fix #70
1 parent 84e1c29 commit 4ff8bc4

31 files changed

+6448
-6312
lines changed

doc/source/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Change Log
55
v2.0.0
66
======
77

8+
* Completed `Reorganize tests <https://github.com/bckohan/django-enum/issues/70>`_
89
* Implemented `Add database constraints on enum fields by default. <https://github.com/bckohan/django-enum/issues/45>`_
910
* Implemented `Provide an optional enum path converter. <https://github.com/bckohan/django-enum/issues/22>`_
1011
* Implemented `Supply a mixin for DRF ModelSerializers that instantiates the provided DRF EnumField type for model EnumFields. <https://github.com/bckohan/django-enum/issues/47>`_

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ sphinx = true
148148
[tool.pytest.ini_options]
149149
# py.test options:
150150
DJANGO_SETTINGS_MODULE = "tests.settings"
151-
python_files = "tests.py"
151+
python_files = "test*.py"
152152
norecursedirs = "*.egg .eggs dist build docs .tox .git __pycache__"
153153

154154
addopts = [

tests/test_admin.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from tests.utils import EnumTypeMixin
2+
from django.test import LiveServerTestCase
3+
from tests.djenum.models import AdminDisplayBug35
4+
from django.urls import reverse
5+
6+
7+
class TestAdmin(EnumTypeMixin, LiveServerTestCase):
8+
9+
BUG35_CLASS = AdminDisplayBug35
10+
11+
def test_admin_list_display_bug35(self):
12+
from django.contrib.auth import get_user_model
13+
14+
get_user_model().objects.create_superuser(
15+
username="admin",
16+
email="admin@django-enum.com",
17+
password="admin_password",
18+
)
19+
self.client.login(username="admin", password="admin_password")
20+
21+
obj = self.BUG35_CLASS.objects.create()
22+
23+
resp = self.client.get(
24+
reverse(
25+
f'admin:{self.BUG35_CLASS._meta.label_lower.replace(".", "_")}_changelist'
26+
)
27+
)
28+
self.assertContains(resp, '<td class="field-int_enum">Value 2</td>')
29+
change_link = reverse(
30+
f'admin:{self.BUG35_CLASS._meta.label_lower.replace(".", "_")}_change',
31+
args=[obj.id],
32+
)
33+
self.assertContains(resp, f'<a href="{change_link}">Value1</a>')
34+
35+
def test_admin_change_display_bug35(self):
36+
from django.contrib.auth import get_user_model
37+
38+
get_user_model().objects.create_superuser(
39+
username="admin",
40+
email="admin@django-enum.com",
41+
password="admin_password",
42+
)
43+
self.client.login(username="admin", password="admin_password")
44+
45+
obj = self.BUG35_CLASS.objects.create()
46+
resp = self.client.get(
47+
reverse(
48+
f'admin:{self.BUG35_CLASS._meta.label_lower.replace(".", "_")}_change',
49+
args=[obj.id],
50+
)
51+
)
52+
self.assertContains(resp, '<div class="readonly">Value1</div>')
53+
self.assertContains(resp, '<div class="readonly">Value 2</div>')

tests/test_admin_ep.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
pytest.importorskip("enum_properties")
4+
5+
from tests.test_admin import TestAdmin
6+
from tests.enum_prop.models import AdminDisplayBug35
7+
8+
class TestEnumPropAdmin(TestAdmin):
9+
10+
BUG35_CLASS = AdminDisplayBug35
11+
12+
TestAdmin = None

tests/test_bulk.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from django.test import TestCase
2+
from tests.utils import EnumTypeMixin
3+
from tests.djenum.models import EnumTester
4+
5+
6+
class TestBulkOperations(EnumTypeMixin, TestCase):
7+
"""Tests bulk insertions and updates"""
8+
9+
MODEL_CLASS = EnumTester
10+
NUMBER = 250
11+
12+
def setUp(self):
13+
self.MODEL_CLASS.objects.all().delete()
14+
15+
@property
16+
def create_params(self):
17+
return {
18+
"small_pos_int": self.SmallPosIntEnum.VAL2,
19+
"small_int": self.SmallIntEnum.VALn1,
20+
"pos_int": 2147483647,
21+
"int": -2147483648,
22+
"big_pos_int": self.BigPosIntEnum.VAL3,
23+
"big_int": self.BigIntEnum.VAL2,
24+
"constant": self.Constants.GOLDEN_RATIO,
25+
"text": self.TextEnum.VALUE2,
26+
"extern": self.ExternEnum.TWO,
27+
"date_enum": self.DateEnum.HUGO,
28+
"datetime_enum": self.DateTimeEnum.KATRINA,
29+
"time_enum": self.TimeEnum.COB,
30+
"duration_enum": self.DurationEnum.FORTNIGHT,
31+
"decimal_enum": self.DecimalEnum.FIVE,
32+
"dj_int_enum": 3,
33+
"dj_text_enum": self.DJTextEnum.A,
34+
"non_strict_int": 15,
35+
"non_strict_text": "arbitrary",
36+
"no_coerce": "0",
37+
}
38+
39+
@property
40+
def update_params(self):
41+
return {
42+
"non_strict_int": 100,
43+
"constant": self.Constants.PI,
44+
"big_int": -2147483649,
45+
"date_enum": self.DateEnum.BRIAN,
46+
"datetime_enum": self.DateTimeEnum.ST_HELENS,
47+
"time_enum": self.TimeEnum.LUNCH,
48+
"duration_enum": self.DurationEnum.WEEK,
49+
"decimal_enum": self.DecimalEnum.TWO,
50+
}
51+
52+
def test_bulk_create(self):
53+
54+
objects = []
55+
for obj in range(0, self.NUMBER):
56+
objects.append(self.MODEL_CLASS(**self.create_params))
57+
58+
self.MODEL_CLASS.objects.bulk_create(objects)
59+
60+
self.assertEqual(
61+
self.MODEL_CLASS.objects.filter(**self.create_params).count(), self.NUMBER
62+
)
63+
64+
def test_bulk_update(self):
65+
objects = []
66+
for obj in range(0, self.NUMBER):
67+
obj = self.MODEL_CLASS.objects.create(**self.create_params)
68+
for param, value in self.update_params.items():
69+
setattr(obj, param, value)
70+
objects.append(obj)
71+
72+
self.assertEqual(len(objects), self.NUMBER)
73+
to_update = ["constant", "non_strict_int"]
74+
self.MODEL_CLASS.objects.bulk_update(objects, to_update)
75+
76+
self.assertEqual(
77+
self.MODEL_CLASS.objects.filter(
78+
**{
79+
**self.create_params,
80+
**{
81+
param: val
82+
for param, val in self.update_params.items()
83+
if param in to_update
84+
},
85+
}
86+
).count(),
87+
self.NUMBER,
88+
)

tests/test_bulk_ep.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
3+
pytest.importorskip("enum_properties")
4+
5+
from tests.test_bulk import TestBulkOperations
6+
from tests.enum_prop.models import EnumTester
7+
8+
9+
class TestBulkOperationsProps(TestBulkOperations):
10+
MODEL_CLASS = EnumTester
11+
12+
@property
13+
def create_params(self):
14+
return {
15+
"small_pos_int": self.SmallPosIntEnum.VAL2,
16+
"small_int": "Value -32768",
17+
"pos_int": 2147483647,
18+
"int": -2147483648,
19+
"big_pos_int": "Value 2147483648",
20+
"big_int": "VAL2",
21+
"constant": "φ",
22+
"text": "V TWo",
23+
"extern": "One",
24+
"dj_int_enum": 3,
25+
"dj_text_enum": self.DJTextEnum.A,
26+
"non_strict_int": 15,
27+
"non_strict_text": "arbitrary",
28+
"no_coerce": "Value 2",
29+
}
30+
31+
@property
32+
def update_params(self):
33+
return {
34+
"non_strict_int": 100,
35+
"non_strict_text": self.TextEnum.VALUE3,
36+
"constant": "π",
37+
"big_int": -2147483649,
38+
"coerce": 2,
39+
}
40+
41+
TestBulkOperations = None

0 commit comments

Comments
 (0)