From b0103d2597a18c10edbf637da80832541e831355 Mon Sep 17 00:00:00 2001 From: Matthieu Rigal Date: Thu, 24 Jun 2021 12:37:50 +0200 Subject: [PATCH] tests: Add tests for trait and maybe --- tests/test_circular.py | 16 ++++++++ tests/test_trait_maybe.py | 80 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 tests/test_trait_maybe.py diff --git a/tests/test_circular.py b/tests/test_circular.py index ee6d13d..1f63f78 100644 --- a/tests/test_circular.py +++ b/tests/test_circular.py @@ -36,6 +36,17 @@ class Meta: book = factory.RelatedFactory("tests.test_circular.BookFactory", "author") +class AuthorBookTraitFactory(factory.Factory): + class Meta: + model = Author + + name = "Charles Dickens" + + class Params: + with_book = factory.Trait( + book = factory.RelatedFactory("tests.test_circular.BookFactory", "author")) + + class BookFactory(factory.Factory): class Meta: model = Book @@ -46,8 +57,13 @@ class Meta: register(AuthorFactory) +register(AuthorBookTraitFactory) register(BookFactory) def test_circular(author, factoryboy_request, request): assert author.books + + +def test_circular_with_trait(author_book_trait): + assert author_book_trait.name == "Charles Dickens" diff --git a/tests/test_trait_maybe.py b/tests/test_trait_maybe.py new file mode 100644 index 0000000..2218ed0 --- /dev/null +++ b/tests/test_trait_maybe.py @@ -0,0 +1,80 @@ +import datetime +from typing import NamedTuple, Any + +import factory +from pytest_factoryboy import register + + +class User(NamedTuple): + is_active: bool + deactivation_date: datetime.datetime + + +class UserFactory(factory.Factory): + class Meta: + model = User + + is_active = True + deactivation_date = factory.Maybe( + 'is_active', + yes_declaration=None, + no_declaration=factory.fuzzy.FuzzyDateTime( + datetime.datetime.now(tz=datetime.timezone.utc) - datetime.timedelta(days=10)), + ) + + +class Package(NamedTuple): + box: bool + packed_by: Any + + +class PackageFactory(factory.Factory): + class Meta: + model = Package + + box = False + packed_by = None + + class Params: + packed = factory.Trait( + box=True, + packed_by=factory.SubFactory(UserFactory), + ) + + +class Order(NamedTuple): + state: str + shipped_on: datetime.datetime + shipped_by: Any + + +class OrderFactory(factory.Factory): + class Meta: + model = Order + + state = 'pending' + shipped_on = None + shipped_by = None + + class Params: + shipped = factory.Trait( + state='shipped', + shipped_on=datetime.date.today(), + shipped_by=factory.RelatedFactory(UserFactory), + ) + +register(UserFactory) +register(PackageFactory) +register(OrderFactory) + +def test_maybe(user, factoryboy_request, request): + # TODO: Factory.Maybe is completely ignored/mishandled + assert user.deactivation_date is None + +def test_trait_subfactory(package, factoryboy_request, request): + assert not package.box + +def test_trait_related_factory(order, factoryboy_request, request): + # TODO: although defined as None, value of shipped_by is not passed during creation + # which causes create() to fail missing an argument + assert order.state == 'pending'