-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5658270
commit 2a87139
Showing
4 changed files
with
118 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,115 @@ | ||
from enum import auto, nonmember | ||
|
||
from django.db.models import IntegerChoices | ||
from django.test import SimpleTestCase | ||
from dsfr.utils import generate_random_id, generate_summary_items | ||
|
||
from dsfr.enums import ExtendedChoices | ||
|
||
|
||
class ExtendedChoicesTestCase(SimpleTestCase): | ||
def test_create_dict_enum(self): | ||
... | ||
class TestExtendedChoices(ExtendedChoices, IntegerChoices): | ||
TEST_1 = { | ||
"value": auto(), | ||
"label": "Test 1", | ||
} | ||
TEST_2 = { | ||
"value": auto(), | ||
"label": "Test 2", | ||
} | ||
|
||
self.assertEqual( | ||
{(1, "Test 1"), (2, "Test 2")}, set(TestExtendedChoices.choices) | ||
) | ||
self.assertEqual({"Test 1", "Test 2"}, set(TestExtendedChoices.labels)) | ||
self.assertEqual({"TEST_1", "TEST_2"}, set(TestExtendedChoices.names)) | ||
self.assertEqual({1, 2}, set(TestExtendedChoices.values)) | ||
|
||
def test_additional_attributes(self): | ||
class TestExtendedChoices(ExtendedChoices, IntegerChoices): | ||
TEST_1 = { | ||
"value": auto(), | ||
"additionnal_attribute": {"lorem": "ipsum 1"}, | ||
} | ||
TEST_2 = { | ||
"value": auto(), | ||
"additionnal_attribute": {"lorem": "ipsum 2"}, | ||
} | ||
|
||
self.assertEqual( | ||
{"additionnal_attribute"}, | ||
set(TestExtendedChoices.additional_attributes), | ||
) | ||
self.assertEqual( | ||
[{"lorem": "ipsum 1"}, {"lorem": "ipsum 2"}], | ||
[it.additionnal_attribute for it in TestExtendedChoices], | ||
) | ||
|
||
def test_nonmember_attributes(self): | ||
class TestExtendedChoices(ExtendedChoices, IntegerChoices): | ||
TEST_1 = {"value": auto()} | ||
TEST_2 = {"value": auto()} | ||
|
||
additional_attribute = nonmember({"lorem": "ipsum 1"}) | ||
|
||
self.assertEqual( | ||
{(1, "Test 1"), (2, "Test 2")}, set(TestExtendedChoices.choices) | ||
) | ||
|
||
def test_absent_value_key_raises_error(self): | ||
... | ||
with self.assertRaises(ValueError) as e: | ||
|
||
class TestExtendedChoices(ExtendedChoices, IntegerChoices): | ||
TEST_1 = {"label": "Test 1"} | ||
TEST_2 = {"label": "Test 2"} | ||
|
||
self.assertEqual( | ||
"enum value for TEST_1 should contain member 'value' " | ||
"when using a dict as value; got TEST_1 = {'label': 'Test 1'}", | ||
str(e.exception), | ||
) | ||
|
||
def test_absent_label_computes_default(self): | ||
... | ||
class TestExtendedChoices(ExtendedChoices, IntegerChoices): | ||
TEST_1 = {"value": auto()} | ||
TEST_2 = auto(), "Autre label" | ||
|
||
self.assertEqual({"Test 1", "Autre label"}, set(TestExtendedChoices.labels)) | ||
|
||
def test_absent_additionnal_key_calls_dynamic_attribute_value_method(self): | ||
... | ||
class TestExtendedChoices(ExtendedChoices, IntegerChoices): | ||
TEST_1 = { | ||
"value": auto(), | ||
"additionnal_attribute": {"lorem": "ipsum 1"}, | ||
} | ||
TEST_2 = auto() | ||
|
||
def dynamic_attribute_value(self, name): | ||
match name: | ||
case "additionnal_attribute": | ||
return {"lorem": "ipsum {}".format(self.value)} | ||
case _: | ||
return {"lorem": "ipsum x"} | ||
|
||
self.assertEqual( | ||
[{"lorem": "ipsum 1"}, {"lorem": "ipsum 2"}], | ||
[it.additionnal_attribute for it in TestExtendedChoices], | ||
) | ||
|
||
def test_absent_additionnal_key_no_dynamic_attribute_value_method(self): | ||
... | ||
class TestExtendedChoices(ExtendedChoices, IntegerChoices): | ||
TEST_1 = { | ||
"value": auto(), | ||
"additionnal_attribute": {"lorem": "ipsum 1"}, | ||
} | ||
TEST_2 = auto() | ||
|
||
def test_additional_attributes(self): | ||
... | ||
with self.assertRaises(AttributeError) as e: | ||
TestExtendedChoices.TEST_2.additionnal_attribute | ||
|
||
self.assertEqual( | ||
"TestExtendedChoices.TEST_2 does not contain key 'additionnal_attribute'. " | ||
"Please add key or implement a 'dynamic_attribute_value(self, name)' " | ||
"method in you enum to provide a value", | ||
str(e.exception), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters