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 24, 2021
1 parent bb6508f commit b0103d2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/test_circular.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
80 changes: 80 additions & 0 deletions tests/test_trait_maybe.py
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit b0103d2

Please sign in to comment.