Skip to content

Commit 94aca21

Browse files
authored
Pytest migration (ets-labs#519)
* Add pytest and pytest-asyncio to the requirements * Update aiohttp ext test * Update setup.cfg * Update tox.ini * Add pytest to the tox requirements * Update tox.ini * Move configuration to tox.ini * Add pytest configs * Rename pytest-py34-py35.ini -> pytest-py35.ini * Update config file paths * Update makefile * Migrate common tests to pytest * Migrate FastAPI and Flask wiring tests * Rename flask and fastapi wiring test files * Move wiring autoloader tests * Add pytest-asyncio to the tox.ini * Migrate wiring async injection tests * Migrate main wiring tests * Migrate wiring string module and package names tests * Migrate wiring config tests * Migrate misc wiring tests * Update tests structure * Migrate misc wiring tests * Refactor container.from_schema() API tests * Migrate container.from_schema() integration tests * Rename schema samples * Update sample imports * Migrate container tests * Refactor container tests * Migrate container self tests * Migrate container instance tests * Migrate container custom string attribute name tests * Migrate container async resource tests * Fix py2 container tests * Migrate container cls tests * Migrate container class custom string cls as atrribute name tests * Migrate ext.aiohttp tests * Migrate ext.flasks tests * Update ext package tests doc block * Migrate provider utils tests * Migrate Factory async mode tests * Migrate async tests * Rename common test module * Refactor asserts in provider tests * Migrate factory tests * Migrate selector provider tests * Migrate object provider tests * Migrate self provider tests * Migrate delegate provider tests * Migrate provider tests * Migrate dependency provider tests * Migrate dependencies container provider tests * Fix warnings * Migrate list provider tests * Migrate dict provider tests * Migrate callable tests * Migrate injection tests * Migrate container provider tests * Migrate coroutine providers * Migrate traversal tests * Migrate resource tests * Migrate configuration tests * Migrate provided instance provider tests * Update doc blocks and imports * Migrate singleton tests * Update changelog and cosmetic fixes
1 parent 4cc4ca9 commit 94aca21

File tree

197 files changed

+11464
-11210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

197 files changed

+11464
-11210
lines changed

Makefile

+2-9
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,10 @@ install: uninstall clean cythonize
4545
uninstall:
4646
- pip uninstall -y -q dependency-injector 2> /dev/null
4747

48-
test-py2: build
48+
test:
4949
# Unit tests with coverage report
5050
coverage erase
51-
coverage run --rcfile=./.coveragerc -m unittest discover -s tests/unit/ -p test_*_py2_py3.py
52-
coverage report --rcfile=./.coveragerc
53-
coverage html --rcfile=./.coveragerc
54-
55-
test: build
56-
# Unit tests with coverage report
57-
coverage erase
58-
coverage run --rcfile=./.coveragerc -m unittest discover -s tests/unit/ -p test_*py3*.py
51+
coverage run --rcfile=./.coveragerc -m pytest -c tests/.configs/pytest.ini
5952
coverage report --rcfile=./.coveragerc
6053
coverage html --rcfile=./.coveragerc
6154

docs/main/changelog.rst

+2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Develop
1515
- Add support of ``with`` statement for ``container.override_providers()`` method.
1616
- Drop support of Python 3.4. There are no immediate breaking changes, but Dependency Injector
1717
will no longer be tested on Python 3.4 and any bugs will not be fixed.
18+
- Fix ``Dependency.is_defined`` attribute to always return boolean value.
1819
- Update documentation and fix typos.
20+
- Migrate tests to ``pytest``.
1921

2022
4.36.2
2123
------

requirements-dev.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
cython==0.29.22
2+
pytest
3+
pytest-asyncio
24
tox
35
coverage
46
flake8

src/dependency_injector/containers.pyi

+9-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Container:
4949
def __delattr__(self, name: str) -> None: ...
5050
def set_providers(self, **providers: Provider): ...
5151
def set_provider(self, name: str, provider: Provider) -> None: ...
52-
def override(self, overriding: C_Base) -> None: ...
52+
def override(self, overriding: Union[Container, Type[Container]]) -> None: ...
5353
def override_providers(self, **overriding_providers: Union[Provider, Any]) -> ProvidersOverridingContext[C_Base]: ...
5454
def reset_last_overriding(self) -> None: ...
5555
def reset_override(self) -> None: ...
@@ -88,6 +88,14 @@ class DeclarativeContainer(Container):
8888
cls_providers: ClassVar[Dict[str, Provider]]
8989
inherited_providers: ClassVar[Dict[str, Provider]]
9090
def __init__(self, **overriding_providers: Union[Provider, Any]) -> None: ...
91+
@classmethod
92+
def override(cls, overriding: Union[Container, Type[Container]]) -> None: ...
93+
@classmethod
94+
def override_providers(cls, **overriding_providers: Union[Provider, Any]) -> ProvidersOverridingContext[C_Base]: ...
95+
@classmethod
96+
def reset_last_overriding(cls) -> None: ...
97+
@classmethod
98+
def reset_override(cls) -> None: ...
9199

92100

93101
class ProvidersOverridingContext(Generic[T]):

src/dependency_injector/providers.c

+17-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dependency_injector/providers.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ cdef class Dependency(Provider):
771771
@property
772772
def is_defined(self):
773773
"""Return True if dependency is defined."""
774-
return self.__last_overriding or self.__default
774+
return self.__last_overriding is not None or self.__default is not None
775775

776776
def provided_by(self, provider):
777777
"""Set external dependency provider.

tests/.configs/pytest-py27.ini

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[pytest]
2+
testpaths = tests/unit
3+
python_files = test_*_py2_py3.py
4+
filterwarnings =
5+
ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning
6+
ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning

tests/.configs/pytest-py35.ini

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[pytest]
2+
testpaths = tests/unit
3+
python_files = test_*_py3.py
4+
filterwarnings =
5+
ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning
6+
ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning

tests/.configs/pytest.ini

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[pytest]
2+
testpaths = tests/unit/
3+
python_files = test_*_py3*.py
4+
filterwarnings =
5+
ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning
6+
ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning

tests/unit/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"""Dependency injector unit tests."""
1+
"""Tests package."""

tests/unit/asyncutils.py

-57
This file was deleted.

tests/unit/conftest.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Fixtures module."""
2+
3+
import sys
4+
import os.path
5+
6+
7+
# Add current package to import samples/ dir
8+
sys.path.append(os.path.dirname(__file__))

tests/unit/containers/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"""Dependency injector container unit tests."""
1+
"""Container tests."""

tests/unit/containers/cls/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Container class tests."""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Tests for container cls with custom string classes as attribute names.
2+
3+
See: https://github.com/ets-labs/python-dependency-injector/issues/479
4+
"""
5+
6+
from dependency_injector import containers, providers
7+
from pytest import fixture, raises
8+
9+
10+
class CustomString(str):
11+
pass
12+
13+
14+
class CustomClass:
15+
thing = None
16+
17+
18+
class Container(containers.DeclarativeContainer):
19+
pass
20+
21+
22+
@fixture
23+
def provider():
24+
return providers.Provider()
25+
26+
27+
def test_setattr(provider):
28+
setattr(Container, CustomString("test_attr"), provider)
29+
assert Container.test_attr is provider
30+
31+
32+
def test_delattr():
33+
setattr(Container, CustomString("test_attr"), provider)
34+
delattr(Container, CustomString("test_attr"))
35+
with raises(AttributeError):
36+
Container.test_attr

0 commit comments

Comments
 (0)