Skip to content

Commit 794920b

Browse files
committed
add tests for project clean, resource clean
1 parent c8ff4fa commit 794920b

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

coldfront/core/project/tests/tests.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: AGPL-3.0-or-later
44

55
import logging
6+
import sys
67
from unittest.mock import patch
78

89
from django.core.exceptions import ValidationError
@@ -379,3 +380,52 @@ def test_determine_automated_institution_choice_does_not_save_to_database(self):
379380
self.assertEqual(original_db_project.institution, "Default")
380381

381382
self.assertNotEqual(project.institution, current_db_project.institution)
383+
384+
385+
class ProjectAttributeModelCleanMethodTests(TestCase):
386+
def _test_clean(
387+
self, project_attribute_type_name: str, project_attribute_values: list, expect_validation_error: bool
388+
):
389+
attribute_type = PAttributeTypeFactory(name=project_attribute_type_name)
390+
project_attribute_type = ProjectAttributeTypeFactory(attribute_type=attribute_type)
391+
project_attribute = ProjectAttributeFactory(project_attribute_type=project_attribute_type)
392+
for value in project_attribute_values:
393+
with self.subTest(value=value):
394+
if not isinstance(value, str):
395+
raise TypeError("project attribute value must be a string")
396+
project_attribute.value = value
397+
if expect_validation_error:
398+
with self.assertRaises(ValidationError):
399+
project_attribute.clean()
400+
else:
401+
project_attribute.clean()
402+
403+
def test_expect_int_given_int(self):
404+
self._test_clean("Int", ["-1", "0", "1", str(sys.maxsize)], False)
405+
406+
def test_expect_int_given_float(self):
407+
self._test_clean("Int", ["-1.0", "0.0", "1.0", "2e30"], True)
408+
409+
def test_expect_int_given_garbage(self):
410+
self._test_clean("Int", ["foobar", "", " ", "\0", "1j"], True)
411+
412+
def test_expect_float_given_int(self):
413+
self._test_clean("Float", ["-1", "0", "1", str(sys.maxsize)], False)
414+
415+
def test_expect_float_given_float(self):
416+
self._test_clean("Float", ["-1.0", "0.0", "1.0", "2e30"], False)
417+
418+
def test_expect_float_given_garbage(self):
419+
self._test_clean("Float", ["foobar", "", " ", "\0", "1j"], True)
420+
421+
def test_expect_yes_no_given_yes_no(self):
422+
self._test_clean("Yes/No", ["Yes", "No"], False)
423+
424+
def test_expect_yes_no_given_garbage(self):
425+
self._test_clean("Yes/No", ["foobar", "", " ", "\0", "1", "1.0", "2e30", "1j", "yes", "no", "YES", "NO"], True)
426+
427+
def test_expect_date_given_date(self):
428+
self._test_clean("Date", ["1970-01-01"], False)
429+
430+
def test_expect_date_given_garbage(self):
431+
self._test_clean("Date", ["foobar", "", " ", "\0", "1", "1.0", "2e30", "1j"], True)

coldfront/core/resource/tests/tests.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,71 @@
33
# SPDX-License-Identifier: AGPL-3.0-or-later
44

55
# Create your tests here.
6+
7+
import logging
8+
import sys
9+
10+
from django.core.exceptions import ValidationError
11+
from django.test import TestCase
12+
13+
from coldfront.core.test_helpers.factories import (
14+
RAttributeTypeFactory,
15+
ResourceAttributeFactory,
16+
ResourceAttributeTypeFactory,
17+
)
18+
19+
logging.disable(logging.CRITICAL)
20+
21+
22+
class ResourceAttributeModelCleanMethodTests(TestCase):
23+
def _test_clean(
24+
self, resource_attribute_type_name: str, resource_attribute_values: list, expect_validation_error: bool
25+
):
26+
attribute_type = RAttributeTypeFactory(name=resource_attribute_type_name)
27+
resource_attribute_type = ResourceAttributeTypeFactory(attribute_type=attribute_type)
28+
resource_attribute = ResourceAttributeFactory(resource_attribute_type=resource_attribute_type)
29+
for value in resource_attribute_values:
30+
with self.subTest(value=value):
31+
if not isinstance(value, str):
32+
raise TypeError("resource attribute value must be a string")
33+
resource_attribute.value = value
34+
if expect_validation_error:
35+
with self.assertRaises(ValidationError):
36+
resource_attribute.clean()
37+
else:
38+
resource_attribute.clean()
39+
40+
def test_expect_int_given_int(self):
41+
self._test_clean("Int", ["-1", "0", "1", str(sys.maxsize)], False)
42+
43+
def test_expect_int_given_float(self):
44+
self._test_clean("Int", ["-1.0", "0.0", "1.0", "2e30"], True)
45+
46+
def test_expect_int_given_garbage(self):
47+
self._test_clean("Int", ["foobar", "", " ", "\0", "1j"], True)
48+
49+
def test_expect_public_private_given_public_private(self):
50+
self._test_clean("Public/Private", ["Public", "Private"], False)
51+
52+
def test_expect_public_private_given_garbage(self):
53+
self._test_clean(
54+
"Public/Private",
55+
["foobar", "", " ", "\0", "1", "1.0", "2e30", "1j", "public", "private", "PUBLIC", "PRIVATE"],
56+
True,
57+
)
58+
59+
def test_expect_active_inactive_given_active_inactive(self):
60+
self._test_clean("Active/Inactive", ["Active", "Inactive"], False)
61+
62+
def test_expect_active_inactive_given_garbage(self):
63+
self._test_clean(
64+
"Active/Inactive",
65+
["foobar", "", " ", "\0", "1", "1.0", "2e30", "1j", "active", "inactive", "ACTIVE", "INACTIVE"],
66+
True,
67+
)
68+
69+
def test_expect_date_given_date(self):
70+
self._test_clean("Date", ["1970-01-01"], False)
71+
72+
def test_expect_date_given_garbage(self):
73+
self._test_clean("Date", ["foobar", "", " ", "\0", "1", "1.0", "2e30", "1j"], True)

coldfront/core/test_helpers/factories.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@
4141
ProjectUserStatusChoice,
4242
)
4343
from coldfront.core.publication.models import PublicationSource
44-
from coldfront.core.resource.models import Resource, ResourceType
44+
from coldfront.core.resource.models import (
45+
AttributeType as RAttributeType,
46+
)
47+
from coldfront.core.resource.models import Resource, ResourceAttribute, ResourceAttributeType, ResourceType
4548
from coldfront.core.user.models import UserProfile
4649

4750
### Default values and Faker provider setup ###
@@ -240,6 +243,35 @@ class Meta:
240243
resource_type = SubFactory(ResourceTypeFactory)
241244

242245

246+
### Resource Attribute factories ###
247+
248+
249+
class RAttributeTypeFactory(DjangoModelFactory):
250+
class Meta:
251+
model = RAttributeType
252+
django_get_or_create = ("name",)
253+
254+
name = factory.Faker("attr_type")
255+
256+
257+
class ResourceAttributeTypeFactory(DjangoModelFactory):
258+
class Meta:
259+
model = ResourceAttributeType
260+
django_get_or_create = ("name",)
261+
262+
name = "Test attribute type"
263+
attribute_type = SubFactory(RAttributeTypeFactory)
264+
265+
266+
class ResourceAttributeFactory(DjangoModelFactory):
267+
class Meta:
268+
model = ResourceAttribute
269+
270+
resource_attribute_type = SubFactory(ResourceAttributeTypeFactory)
271+
value = "Test attribute value"
272+
resource = SubFactory(RAttributeTypeFactory)
273+
274+
243275
### Allocation factories ###
244276

245277

0 commit comments

Comments
 (0)