From be5b841d42e42dbd86c9b0cea6922008d7c5c035 Mon Sep 17 00:00:00 2001 From: marcin-koziol-iteo Date: Tue, 21 Nov 2023 14:17:54 +0100 Subject: [PATCH 1/2] Do not use random to generate example values for DecimalField --- audoma/examples.py | 9 ++++++++- audoma/tests/test_openapi.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/audoma/examples.py b/audoma/examples.py index fac32f9..a70ea48 100644 --- a/audoma/examples.py +++ b/audoma/examples.py @@ -1,5 +1,6 @@ import datetime import random +from decimal import Decimal from typing import ( Any, Type, @@ -49,7 +50,13 @@ def generate_value(self) -> float: """ min_val = float(getattr(self.field, "min_value", 1) or 1) max_val = float(getattr(self.field, "max_value", 1000) or 1000) - return random.uniform(min_val, max_val) + max_digits = getattr(self.field, "max_digits", None) + decimal_places = getattr(self.field, "decimal_places", None) + if max_digits and decimal_places: + fmt = f".{decimal_places}f" + return Decimal(f"{(max_val):{fmt}}") + ret = random.uniform(min_val, max_val) + return ret class RegexExample(Example): diff --git a/audoma/tests/test_openapi.py b/audoma/tests/test_openapi.py index d617bee..4e7fc2f 100644 --- a/audoma/tests/test_openapi.py +++ b/audoma/tests/test_openapi.py @@ -356,6 +356,23 @@ def test_map_serializer_field_choicefield_xchoices_success(self): {"choices": {"SMALL": "small", "MEDIUM": "medium", "LARGE": "large"}}, ) + def test_decimal_examples_are_generated_correctly(self): + fields_config = {"order_value": audoma_fields.DecimalField(max_digits=5, decimal_places=2, min_value=999.98, max_value=999.99)} + serializer = create_serializer( + fields_config=fields_config, serializer_base_classes=[Serializer] + ) + serializer_fields = serializer.fields + + request = self.factory.get("/example/") + view = create_basic_view(view_properties={"serializer_class": type(serializer)}) + view.request = request + view.action = "retrieve" + view.schema = AudomaAutoSchema() + mapped_field = view.schema._map_serializer_field( + serializer_fields["order_value"], direction="response" + ) + self.assertEqual(mapped_field['example'], '999.99') + def test_map_serializer_field_audoma_choicefield_xchoices_success(self): fields_config = { "company_name": audoma_fields.CharField(max_length=255), From abb0d3455fcfeef083f58e70c92e9ad7424c70a6 Mon Sep 17 00:00:00 2001 From: marcin-koziol-iteo Date: Tue, 21 Nov 2023 14:26:59 +0100 Subject: [PATCH 2/2] remove unused var --- audoma/examples.py | 3 +-- audoma/tests/test_openapi.py | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/audoma/examples.py b/audoma/examples.py index a70ea48..c22585c 100644 --- a/audoma/examples.py +++ b/audoma/examples.py @@ -50,9 +50,8 @@ def generate_value(self) -> float: """ min_val = float(getattr(self.field, "min_value", 1) or 1) max_val = float(getattr(self.field, "max_value", 1000) or 1000) - max_digits = getattr(self.field, "max_digits", None) decimal_places = getattr(self.field, "decimal_places", None) - if max_digits and decimal_places: + if decimal_places: fmt = f".{decimal_places}f" return Decimal(f"{(max_val):{fmt}}") ret = random.uniform(min_val, max_val) diff --git a/audoma/tests/test_openapi.py b/audoma/tests/test_openapi.py index 4e7fc2f..c1c68a0 100644 --- a/audoma/tests/test_openapi.py +++ b/audoma/tests/test_openapi.py @@ -357,7 +357,11 @@ def test_map_serializer_field_choicefield_xchoices_success(self): ) def test_decimal_examples_are_generated_correctly(self): - fields_config = {"order_value": audoma_fields.DecimalField(max_digits=5, decimal_places=2, min_value=999.98, max_value=999.99)} + fields_config = { + "order_value": audoma_fields.DecimalField( + max_digits=5, decimal_places=2, min_value=999.98, max_value=999.99 + ) + } serializer = create_serializer( fields_config=fields_config, serializer_base_classes=[Serializer] ) @@ -371,7 +375,7 @@ def test_decimal_examples_are_generated_correctly(self): mapped_field = view.schema._map_serializer_field( serializer_fields["order_value"], direction="response" ) - self.assertEqual(mapped_field['example'], '999.99') + self.assertEqual(mapped_field["example"], "999.99") def test_map_serializer_field_audoma_choicefield_xchoices_success(self): fields_config = {