From abb8aa817ff54a4e899f110851a7ff54fb24891e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Sun, 26 Nov 2023 11:48:55 +0000 Subject: [PATCH] :white_check_mark: fix tests --- tests/factories.py | 11 +++++++---- tests/faker_providers.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 tests/faker_providers.py diff --git a/tests/factories.py b/tests/factories.py index fe3a410..99aa3f9 100644 --- a/tests/factories.py +++ b/tests/factories.py @@ -3,7 +3,10 @@ import factory -from . import models +from . import faker_providers, models + + +factory.Faker.add_provider(faker_providers.DateTimeProviders) class AuthorFactory(factory.Factory): @@ -37,7 +40,7 @@ class Meta: boolean_field: factory.Faker = factory.Faker("boolean") char_field: factory.Faker = factory.Faker("text", max_nb_chars=255) date_field: factory.Faker = factory.Faker("date_this_decade") - date_time_field: factory.Faker = factory.Faker("date_time_this_century") + date_time_field: factory.Faker = factory.Faker("date_time_this_century_without_microseconds") decimal_field: factory.Faker = factory.Faker("pydecimal", left_digits=8, right_digits=2) float_field: factory.Faker = factory.Faker("pyfloat", left_digits=8, right_digits=4) integer_field: factory.Faker = factory.Faker("pyint", min_value=-(2**31), max_value=2**31 - 1) @@ -50,10 +53,10 @@ class Meta: small_integer_field: factory.Faker = factory.Faker("pyint", min_value=-(2**15), max_value=2**15 - 1) string_field: factory.Faker = factory.Faker("text", max_nb_chars=255) text_field: factory.Faker = factory.Faker("text", max_nb_chars=1024) - time_field: factory.Faker = factory.Faker("time_object") + time_field: factory.Faker = factory.Faker("time_object_without_microseconds") varbinary_field: factory.Faker = factory.Faker("binary", length=255) varchar_field: factory.Faker = factory.Faker("text", max_nb_chars=255) - timestamp_field: factory.Faker = factory.Faker("date_time_this_century") + timestamp_field: factory.Faker = factory.Faker("date_time_this_century_without_microseconds") class ArticleFactory(factory.Factory): diff --git a/tests/faker_providers.py b/tests/faker_providers.py new file mode 100644 index 0000000..391fb6b --- /dev/null +++ b/tests/faker_providers.py @@ -0,0 +1,22 @@ +import datetime +from typing import Optional + +from faker.providers import BaseProvider, date_time +from faker.typing import DateParseType + + +class DateTimeProviders(BaseProvider): + def time_object_without_microseconds(self, end_datetime: Optional[DateParseType] = None) -> datetime.time: + return date_time.Provider(self.generator).time_object(end_datetime).replace(microsecond=0) + + def date_time_this_century_without_microseconds( + self, + before_now: bool = True, + after_now: bool = False, + tzinfo: Optional[datetime.tzinfo] = None, + ) -> datetime.datetime: + return ( + date_time.Provider(self.generator) + .date_time_this_century(before_now, after_now, tzinfo) + .replace(microsecond=0) + )