Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.rej
*.py[cod]
/.env
/.env3
*.orig

# C extensions
Expand Down
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.black]
line-length = 120
target-version = ['py27', 'py35', 'py36', 'py37', 'py38']
2 changes: 1 addition & 1 deletion pytest_factoryboy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""pytest-factoryboy public API."""
from .fixture import register, LazyFixture

__version__ = '2.0.3'
__version__ = "2.0.3"


__all__ = [
Expand Down
20 changes: 9 additions & 11 deletions pytest_factoryboy/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def get_model_name(factory_class):
"""Get model fixture name by factory."""
return (
inflection.underscore(factory_class._meta.model.__name__)
if not isinstance(factory_class._meta.model, str) else factory_class._meta.model)
if not isinstance(factory_class._meta.model, str)
else factory_class._meta.model
)


def get_factory_name(factory_class):
Expand All @@ -164,17 +166,15 @@ def is_dep(value):
if isinstance(value, factory.RelatedFactory):
return False
if isinstance(value, factory.SubFactory) and get_model_name(value.get_factory()) == parent_model_name:
return False
return False
if isinstance(value, factory.declarations.PostGeneration):
# Dependency on extracted value
return True

return True

return [
SEPARATOR.join((model_name, attr))
for attr, value in factory_class._meta.declarations.items()
if is_dep(value)
SEPARATOR.join((model_name, attr)) for attr, value in factory_class._meta.declarations.items() if is_dep(value)
]


Expand All @@ -199,7 +199,8 @@ class Factory(factory_class):
pass

Factory._meta.base_declarations = dict(
(k, v) for k, v in Factory._meta.base_declarations.items()
(k, v)
for k, v in Factory._meta.base_declarations.items()
if not isinstance(v, factory.declarations.PostGenerationDeclaration)
)
Factory._meta.post_declarations = factory.builder.DeclarationSet()
Expand Down Expand Up @@ -233,7 +234,7 @@ class Factory(factory_class):
argname = "".join((prefix, attr))
extra = {}
for k, v in factory_class._meta.post_declarations.contexts[attr].items():
if k == '':
if k == "":
continue
post_attr = SEPARATOR.join((argname, k))

Expand Down Expand Up @@ -339,10 +340,7 @@ def __init__(self, fixture):
self.fixture = fixture
if callable(self.fixture):
params = signature(self.fixture).parameters.values()
self.args = [
param.name for param in params
if param.kind == param.POSITIONAL_OR_KEYWORD
]
self.args = [param.name for param in params if param.kind == param.POSITIONAL_OR_KEYWORD]
else:
self.args = [self.fixture]

Expand Down
7 changes: 4 additions & 3 deletions pytest_factoryboy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def defer(self, functions):
self.deferred.append(functions)

def get_deps(self, request, fixture, deps=None):
request = request.getfixturevalue('request')
request = request.getfixturevalue("request")

if deps is None:
deps = set([fixture])
if fixture == 'request':
if fixture == "request":
return deps

for fixturedef in request._fixturemanager.getfixturedefs(fixture, request._pyfuncitem.parent.nodeid) or []:
Expand All @@ -43,7 +43,7 @@ def get_deps(self, request, fixture, deps=None):

def get_current_deps(self, request):
deps = set()
while hasattr(request, '_parent_request'):
while hasattr(request, "_parent_request"):
if request.fixturename and request.fixturename not in getattr(request, "_fixturedefs", {}):
deps.add(request.fixturename)
request = request._parent_request
Expand Down Expand Up @@ -113,6 +113,7 @@ def pytest_runtest_call(item):
def pytest_addhooks(pluginmanager):
"""Register plugin hooks."""
from pytest_factoryboy import hooks

pluginmanager.add_hookspecs(hooks)


Expand Down
4 changes: 0 additions & 4 deletions requirements-testing.txt
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
mock
pytest-pep8
pytest-cov
pytest-cache
12 changes: 7 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
dirname = os.path.dirname(__file__)

long_description = (
codecs.open(os.path.join(dirname, "README.rst"), encoding="utf-8").read() + "\n" +
codecs.open(os.path.join(dirname, "AUTHORS.rst"), encoding="utf-8").read() + "\n" +
codecs.open(os.path.join(dirname, "CHANGES.rst"), encoding="utf-8").read()
codecs.open(os.path.join(dirname, "README.rst"), encoding="utf-8").read()
+ "\n"
+ codecs.open(os.path.join(dirname, "AUTHORS.rst"), encoding="utf-8").read()
+ "\n"
+ codecs.open(os.path.join(dirname, "CHANGES.rst"), encoding="utf-8").read()
)

with codecs.open(os.path.join(dirname, 'pytest_factoryboy', '__init__.py'), encoding='utf-8') as fd:
VERSION = re.compile(r".*__version__ = '(.*?)'", re.S).match(fd.read()).group(1)
with codecs.open(os.path.join(dirname, "pytest_factoryboy", "__init__.py"), encoding="utf-8") as fd:
VERSION = re.compile(r'.*__version__ = "(.*?)"', re.S).match(fd.read()).group(1)

setup(
name="pytest-factoryboy",
Expand Down
30 changes: 0 additions & 30 deletions tests/test_attributes_field.py

This file was deleted.

4 changes: 1 addition & 3 deletions tests/test_circular.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ def __init__(self, name):


class AuthorFactory(factory.Factory):

class Meta:
model = Author

name = "Charles Dickens"

book = factory.RelatedFactory('tests.test_circular.BookFactory', 'author')
book = factory.RelatedFactory("tests.test_circular.BookFactory", "author")


class BookFactory(factory.Factory):

class Meta:
model = Book

Expand Down
2 changes: 1 addition & 1 deletion tests/test_factory_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def test_lazy_fixture_callable(book, another_author):
("author__register_user", "author__register_user__password"),
[
(LazyFixture(lambda: "lazyfixture"), LazyFixture(lambda: "asdasd")),
]
],
)
def test_lazy_fixture_post_generation(author):
"""Test that post-generation values are replaced with lazy fixtures."""
Expand Down
11 changes: 4 additions & 7 deletions tests/test_postgen_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@


class Foo(object):

def __init__(self, value, expected):
self.value = value
self.expected = expected


class Bar(object):

def __init__(self, foo):
self.foo = foo

Expand Down Expand Up @@ -66,23 +64,23 @@ def test_postgen_invoked(foo):
register(BarFactory)


@pytest.mark.parametrize('foo__value', [3])
@pytest.mark.parametrize('foo__expected', [1])
@pytest.mark.parametrize("foo__value", [3])
@pytest.mark.parametrize("foo__expected", [1])
def test_depends_on(bar):
"""Test that post-generation hooks are done and the value is 1."""
assert bar.foo.value == 1


def test_getfixturevalue(request, factoryboy_request):
"""Test post-generation declarations via the getfixturevalue."""
foo = request.getfixturevalue('foo')
foo = request.getfixturevalue("foo")
assert not factoryboy_request.deferred
assert foo.value == 1


def test_after_postgeneration(foo):
"""Test _after_postgeneration is called."""
assert foo._postgeneration_results == {'set1': None}
assert foo._postgeneration_results == {"set1": None}
assert foo._create is True


Expand All @@ -92,7 +90,6 @@ class Ordered(object):

@register
class OrderedFactory(factory.Factory):

class Meta:
model = Ordered

Expand Down
12 changes: 10 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
[tox]
distshare = {homedir}/.tox/distshare
envlist = py27-pytest{43,44,45,46},
py38-pytest{43,44,45,46,50,51,52,latest},
py38-pytest{43,44,45,46,50,51,52,53,54,60,latest},
py38-pytestlatest-linters,
py{35,36,37}-pytestlatest

[testenv]
commands = pytest --junitxml={envlogdir}/junit-{envname}.xml {posargs:tests}
deps =
pytestlatest: pytest
pytest60: pytest~=6.0.0
pytest54: pytest~=5.4.0
pytest53: pytest~=5.3.0
pytest52: pytest~=5.2.0
pytest51: pytest~=5.1.0
pytest50: pytest~=5.0.0
Expand All @@ -18,6 +22,10 @@ deps =

-r{toxinidir}/requirements-testing.txt

[testenv:py38-pytestlatest-linters]
deps = black
commands = black --check --verbose setup.py pytest_factoryboy tests

[pytest]
addopts = -vv -l --pep8
addopts = -vv -l
pep8maxlinelength = 120