mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-12-15 02:57:09 +01:00
Enhancement: better monetary field with currency code (#5858)
This commit is contained in:
parent
84721b001f
commit
bf11dc8d1b
13 changed files with 283 additions and 38 deletions
|
|
@ -10,7 +10,7 @@ from documents.models import Document
|
|||
from documents.tests.utils import DirectoriesMixin
|
||||
|
||||
|
||||
class TestCustomField(DirectoriesMixin, APITestCase):
|
||||
class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
||||
ENDPOINT = "/api/custom_fields/"
|
||||
|
||||
def setUp(self):
|
||||
|
|
@ -127,6 +127,10 @@ class TestCustomField(DirectoriesMixin, APITestCase):
|
|||
name="Test Custom Field Monetary",
|
||||
data_type=CustomField.FieldDataType.MONETARY,
|
||||
)
|
||||
custom_field_monetary2 = CustomField.objects.create(
|
||||
name="Test Custom Field Monetary 2",
|
||||
data_type=CustomField.FieldDataType.MONETARY,
|
||||
)
|
||||
custom_field_documentlink = CustomField.objects.create(
|
||||
name="Test Custom Field Doc Link",
|
||||
data_type=CustomField.FieldDataType.DOCUMENTLINK,
|
||||
|
|
@ -164,7 +168,11 @@ class TestCustomField(DirectoriesMixin, APITestCase):
|
|||
},
|
||||
{
|
||||
"field": custom_field_monetary.id,
|
||||
"value": 11.10,
|
||||
"value": "EUR11.10",
|
||||
},
|
||||
{
|
||||
"field": custom_field_monetary2.id,
|
||||
"value": 11.10, # Legacy format
|
||||
},
|
||||
{
|
||||
"field": custom_field_documentlink.id,
|
||||
|
|
@ -188,13 +196,14 @@ class TestCustomField(DirectoriesMixin, APITestCase):
|
|||
{"field": custom_field_boolean.id, "value": True},
|
||||
{"field": custom_field_url.id, "value": "https://example.com"},
|
||||
{"field": custom_field_float.id, "value": 12.3456},
|
||||
{"field": custom_field_monetary.id, "value": 11.10},
|
||||
{"field": custom_field_monetary.id, "value": "EUR11.10"},
|
||||
{"field": custom_field_monetary2.id, "value": "11.1"},
|
||||
{"field": custom_field_documentlink.id, "value": [doc2.id]},
|
||||
],
|
||||
)
|
||||
|
||||
doc.refresh_from_db()
|
||||
self.assertEqual(len(doc.custom_fields.all()), 8)
|
||||
self.assertEqual(len(doc.custom_fields.all()), 9)
|
||||
|
||||
def test_change_custom_field_instance_value(self):
|
||||
"""
|
||||
|
|
@ -458,7 +467,7 @@ class TestCustomField(DirectoriesMixin, APITestCase):
|
|||
GIVEN:
|
||||
- Document & custom field exist
|
||||
WHEN:
|
||||
- API request to set a field value to something not a valid monetary decimal
|
||||
- API request to set a field value to something not a valid monetary decimal (legacy) or not a new monetary format e.g. USD12.34
|
||||
THEN:
|
||||
- HTTP 400 is returned
|
||||
- No field instance is created or attached to the document
|
||||
|
|
@ -488,6 +497,54 @@ class TestCustomField(DirectoriesMixin, APITestCase):
|
|||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
resp = self.client.patch(
|
||||
f"/api/documents/{doc.id}/",
|
||||
data={
|
||||
"custom_fields": [
|
||||
{
|
||||
"field": custom_field_money.id,
|
||||
# Too few places past decimal
|
||||
"value": "GBP12.1",
|
||||
},
|
||||
],
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
resp = self.client.patch(
|
||||
f"/api/documents/{doc.id}/",
|
||||
data={
|
||||
"custom_fields": [
|
||||
{
|
||||
"field": custom_field_money.id,
|
||||
# Too many places past decimal
|
||||
"value": "GBP12.123",
|
||||
},
|
||||
],
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
resp = self.client.patch(
|
||||
f"/api/documents/{doc.id}/",
|
||||
data={
|
||||
"custom_fields": [
|
||||
{
|
||||
"field": custom_field_money.id,
|
||||
# Not a 3-letter currency code
|
||||
"value": "G12.12",
|
||||
},
|
||||
],
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertEqual(CustomFieldInstance.objects.count(), 0)
|
||||
self.assertEqual(len(doc.custom_fields.all()), 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue