Skip to content

Commit

Permalink
tests: Add tests for trait and maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
MRigal committed Jun 25, 2021
1 parent bb6508f commit b8f310a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/test_circular.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ 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
Expand All @@ -46,8 +56,15 @@ class Meta:


register(AuthorFactory)
register(AuthorBookTraitFactory, "author_book_trait")
register(BookFactory)


def test_circular(author, factoryboy_request, request):
assert author.books


def test_circular_with_trait(author_book_trait):
# FIXME: The trait, converted to Maybe, is handled in fixture.make_deferred_postgen
# which tries to run call() on the Maybe class, which was disabled in factory-boy 3.2
assert author_book_trait.name == "Charles Dickens"
85 changes: 85 additions & 0 deletions tests/test_trait_maybe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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):
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):
# FIXME: although defined as None, value of shipped_by is not passed during creation
# which causes create() to fail missing an argument
# Bug hitting all factory-boy versions
assert order.state == "pending"

0 comments on commit b8f310a

Please sign in to comment.