Skip to content

Commit

Permalink
add decimal field updater
Browse files Browse the repository at this point in the history
  • Loading branch information
aidangannon committed Jun 24, 2024
1 parent 0d28799 commit 19d28fe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
35 changes: 35 additions & 0 deletions autofixture/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
import decimal
import typing
import uuid
from decimal import Decimal
from enum import Enum
import random as rand

Expand Down Expand Up @@ -87,6 +89,9 @@ def create(self, dto: typing.Type[T],
if _type is float:
self.__generate_float_field(is_predictable_data, key, new_value, num)

if _type is Decimal:
self.__generate_decimal_field(is_predictable_data, key, new_value, num)

if _type == list[str]:
self.__generate_str_list_field(is_predictable_data, key, new_value, num, seed, list_limit)

Expand All @@ -102,6 +107,9 @@ def create(self, dto: typing.Type[T],
if _type == list[float]:
self.__generate_float_list_field(is_predictable_data, key, new_value, num, list_limit)

if _type == list[decimal.Decimal]:
self.__generate_decimal_list_field(is_predictable_data, key, new_value, num, list_limit)

if type(_type) is type(Enum):
self.__generate_random_enum_field(_type, is_predictable_data, key, new_value, num)

Expand Down Expand Up @@ -217,6 +225,22 @@ def __generate_float_list_field(is_predictable_data, key, new_value, num, list_l
value_for_given_member.append(rand.uniform(0, 100))
setattr(new_value, key, value_for_given_member)

@staticmethod
def __generate_decimal_list_field(is_predictable_data, key, new_value, num, list_limit):
if is_predictable_data:
value_for_given_member = []
for i in range(0, num):
trailing_decimals = ""
for i in range(0, num):
trailing_decimals = f"{trailing_decimals}{num}"
value_for_given_member_item = float(f"{num}.{trailing_decimals}")
value_for_given_member.append(Decimal(str(value_for_given_member_item)))
else:
value_for_given_member = []
for i in range(0, rand.randint(0, list_limit)):
value_for_given_member.append(Decimal(str(rand.uniform(0, 100))))
setattr(new_value, key, value_for_given_member)

@staticmethod
def __generate_int_list_field(is_predictable_data, key, new_value, num, list_limit):
if is_predictable_data:
Expand Down Expand Up @@ -266,6 +290,17 @@ def __generate_float_field(is_predictable_data, key, new_value, num):
value_for_given_member = rand.uniform(0, 100)
setattr(new_value, key, value_for_given_member)

@staticmethod
def __generate_decimal_field(is_predictable_data, key, new_value, num):
if is_predictable_data:
trailing_decimals = ""
for i in range(0, num):
trailing_decimals = f"{trailing_decimals}{num}"
value_for_given_member = float(f"{num}.{trailing_decimals}")
else:
value_for_given_member = rand.uniform(0, 100)
setattr(new_value, key, Decimal(str(value_for_given_member)))

@staticmethod
def __generate_int_field(is_predictable_data, key, new_value, num):
if is_predictable_data:
Expand Down
5 changes: 4 additions & 1 deletion tests/test_autofixture.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
from dataclasses import dataclass, field
from decimal import Decimal
from enum import Enum
from unittest import TestCase

Expand Down Expand Up @@ -55,10 +56,12 @@ class TestDto(InheritedDto):
list_of_strings: list[str] = None
list_of_ints: list[int] = None
list_of_floats: list[float] = None
list_of_decimals: list[Decimal] = None
list_of_bools: list[bool] = None
date: datetime.datetime = None
list_of_dates: list[datetime.datetime] = None
decimal_num: float = None
float_num: float = None
decimal_num: Decimal = None
nested: NestedTestDto = None
nested_list: list[NestedTestDto] = None

Expand Down

0 comments on commit 19d28fe

Please sign in to comment.