|
5 | 5 | """Unit tests for the allocation models""" |
6 | 6 |
|
7 | 7 | import datetime |
| 8 | +import sys |
8 | 9 | from unittest.mock import patch |
9 | 10 |
|
10 | 11 | from django.contrib.auth.models import User |
|
18 | 19 | ) |
19 | 20 | from coldfront.core.project.models import Project |
20 | 21 | from coldfront.core.test_helpers.factories import ( |
| 22 | + AAttributeTypeFactory, |
| 23 | + AllocationAttributeFactory, |
| 24 | + AllocationAttributeTypeFactory, |
21 | 25 | AllocationFactory, |
22 | 26 | AllocationStatusChoiceFactory, |
23 | 27 | ProjectFactory, |
@@ -145,6 +149,55 @@ def test_status_is_active_and_start_date_equals_end_date_no_error(self): |
145 | 149 | actual_allocation.full_clean() |
146 | 150 |
|
147 | 151 |
|
| 152 | +class AllocationAttributeModelCleanMethodTests(TestCase): |
| 153 | + def _test_clean( |
| 154 | + self, allocation_attribute_type_name: str, allocation_attribute_values: list, expect_validation_error: bool |
| 155 | + ): |
| 156 | + attribute_type = AAttributeTypeFactory(name=allocation_attribute_type_name) |
| 157 | + allocation_attribute_type = AllocationAttributeTypeFactory(attribute_type=attribute_type) |
| 158 | + allocation_attribute = AllocationAttributeFactory(allocation_attribute_type=allocation_attribute_type) |
| 159 | + for value in allocation_attribute_values: |
| 160 | + with self.subTest(value=value): |
| 161 | + if not isinstance(value, str): |
| 162 | + raise TypeError("allocation attribute value must be a string") |
| 163 | + allocation_attribute.value = value |
| 164 | + if expect_validation_error: |
| 165 | + with self.assertRaises(ValidationError): |
| 166 | + allocation_attribute.clean() |
| 167 | + else: |
| 168 | + allocation_attribute.clean() |
| 169 | + |
| 170 | + def test_expect_int_given_int(self): |
| 171 | + self._test_clean("Int", ["-1", "0", "1", str(sys.maxsize)], False) |
| 172 | + |
| 173 | + def test_expect_int_given_float(self): |
| 174 | + self._test_clean("Int", ["-1.0", "0.0", "1.0", "2e30"], True) |
| 175 | + |
| 176 | + def test_expect_int_given_garbage(self): |
| 177 | + self._test_clean("Int", ["foobar", "", " ", "\0", "1j"], True) |
| 178 | + |
| 179 | + def test_expect_float_given_int(self): |
| 180 | + self._test_clean("Float", ["-1", "0", "1", str(sys.maxsize)], False) |
| 181 | + |
| 182 | + def test_expect_float_given_float(self): |
| 183 | + self._test_clean("Float", ["-1.0", "0.0", "1.0", "2e30"], False) |
| 184 | + |
| 185 | + def test_expect_float_given_garbage(self): |
| 186 | + self._test_clean("Float", ["foobar", "", " ", "\0", "1j"], True) |
| 187 | + |
| 188 | + def test_expect_yes_no_given_yes_no(self): |
| 189 | + self._test_clean("Yes/No", ["Yes", "No"], False) |
| 190 | + |
| 191 | + def test_expect_yes_no_given_garbage(self): |
| 192 | + self._test_clean("Yes/No", ["foobar", "", " ", "\0", "1", "1.0", "2e30", "1j", "yes", "no", "YES", "NO"], True) |
| 193 | + |
| 194 | + def test_expect_date_given_date(self): |
| 195 | + self._test_clean("Date", ["1970-01-01"], False) |
| 196 | + |
| 197 | + def test_expect_date_given_garbage(self): |
| 198 | + self._test_clean("Date", ["foobar", "", " ", "\0", "1", "1.0", "2e30", "1j"], True) |
| 199 | + |
| 200 | + |
148 | 201 | class AllocationModelStrTests(TestCase): |
149 | 202 | """Tests for Allocation.__str__""" |
150 | 203 |
|
|
0 commit comments