Test unique ID generation

This commit is contained in:
shamoon 2024-11-13 08:52:11 -08:00
parent 12953eb022
commit a39d79b096
2 changed files with 66 additions and 1 deletions

View File

@ -533,7 +533,7 @@ class CustomFieldSerializer(serializers.ModelSerializer):
if (
"data_type" in attrs
and attrs["data_type"] == CustomField.FieldDataType.SELECT
):
) or self.instance.data_type == CustomField.FieldDataType.SELECT:
if (
"extra_data" not in attrs
or "select_options" not in attrs["extra_data"]

View File

@ -1,5 +1,6 @@
import json
from datetime import date
from unittest.mock import ANY
from django.contrib.auth.models import Permission
from django.contrib.auth.models import User
@ -144,6 +145,70 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
)
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
def test_custom_field_select_unique_ids(self):
"""
GIVEN:
- Nothing
- Existing custom field
WHEN:
- API request to create custom field with select options without id
THEN:
- Unique ids are generated for each option
"""
resp = self.client.post(
self.ENDPOINT,
json.dumps(
{
"data_type": "select",
"name": "Select Field",
"extra_data": {
"select_options": [
{"label": "Option 1"},
{"label": "Option 2"},
],
},
},
),
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
data = resp.json()
self.assertCountEqual(
data["extra_data"]["select_options"],
[
{"label": "Option 1", "id": ANY},
{"label": "Option 2", "id": ANY},
],
)
# Add a new option
resp = self.client.patch(
f"{self.ENDPOINT}{data['id']}/",
json.dumps(
{
"extra_data": {
"select_options": data["extra_data"]["select_options"]
+ [{"label": "Option 3"}],
},
},
),
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
data = resp.json()
self.assertCountEqual(
data["extra_data"]["select_options"],
[
{"label": "Option 1", "id": ANY},
{"label": "Option 2", "id": ANY},
{"label": "Option 3", "id": ANY},
],
)
def test_create_custom_field_monetary_validation(self):
"""
GIVEN: