Skip to content

Commit

Permalink
Parametrize concrete factories, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Feb 7, 2024
1 parent 56f1dd9 commit 937f4c6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
6 changes: 3 additions & 3 deletions factory/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ def __init__(self, **kwargs):
setattr(self, field, value)


class StubFactory(Factory[T]):
class StubFactory(Factory[StubObject]):

class Meta:
strategy = enums.STUB_STRATEGY
Expand Down Expand Up @@ -688,7 +688,7 @@ def _create(cls, model_class, *args, **kwargs):
return cls._build(model_class, *args, **kwargs)


class DictFactory(BaseDictFactory[T]):
class DictFactory(BaseDictFactory[dict]):
class Meta:
model = dict

Expand All @@ -714,7 +714,7 @@ def _create(cls, model_class, *args, **kwargs):
return cls._build(model_class, *args, **kwargs)


class ListFactory(BaseListFactory[T]):
class ListFactory(BaseListFactory[list]):
class Meta:
model = list

Expand Down
24 changes: 20 additions & 4 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import dataclasses
import unittest

from typing_extensions import assert_type

import factory


Expand All @@ -25,7 +27,21 @@ class UserFactory(factory.Factory[User]):
class Meta:
model = User

result: User
result = UserFactory.build()
result = UserFactory.create()
self.assertEqual(result.name, "John Doe")
assert_type(UserFactory.build(), User)
assert_type(UserFactory.create(), User)
assert_type(UserFactory.build_batch(2), list[User])
assert_type(UserFactory.create_batch(2), list[User])
self.assertEqual(UserFactory.create().name, "John Doe")

def test_dict_factory(self) -> None:

class Pet(factory.DictFactory):
species = "dog"
name = "rover"

assert_type(Pet.build(), dict)
assert_type(Pet.create(), dict)

def test_list_factory(self) -> None:
assert_type(factory.ListFactory().build(), list)
assert_type(factory.ListFactory().create(), list)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ passenv =
POSTGRES_DATABASE
deps =
mypy
typing_extensions
alchemy: SQLAlchemy
alchemy: sqlalchemy_utils
mongo: mongoengine
Expand Down

0 comments on commit 937f4c6

Please sign in to comment.