Migrate ext.flasks tests

This commit is contained in:
Roman Mogylatov 2021-10-15 10:11:34 -04:00
parent 56c39554e7
commit 31d2c3b992
4 changed files with 33 additions and 31 deletions

View File

@ -3,3 +3,4 @@ testpaths = tests/unit
python_files = test_*_py2_py3.py python_files = test_*_py2_py3.py
filterwarnings = filterwarnings =
ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning
ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning

View File

@ -3,3 +3,4 @@ testpaths = tests/unit
python_files = test_*_py3.py python_files = test_*_py3.py
filterwarnings = filterwarnings =
ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning
ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning

View File

@ -3,3 +3,4 @@ testpaths = tests/unit
python_files = test_*_py3*.py python_files = test_*_py3*.py
filterwarnings = filterwarnings =
ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning
ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning

View File

@ -1,11 +1,10 @@
"""Dependency injector Flask extension unit tests.""" """Flask extension tests."""
import unittest
from flask import Flask, url_for
from flask.views import MethodView
from dependency_injector import containers from dependency_injector import containers
from dependency_injector.ext import flask from dependency_injector.ext import flask
from flask import Flask, url_for
from flask.views import MethodView
from pytest import fixture
def index(): def index():
@ -30,47 +29,47 @@ class ApplicationContainer(containers.DeclarativeContainer):
test_class_view = flask.ClassBasedView(Test) test_class_view = flask.ClassBasedView(Test)
def create_app(): @fixture
def app():
container = ApplicationContainer() container = ApplicationContainer()
app = container.app() app = container.app()
app.container = container app.container = container
app.config["SERVER_NAME"] = "test-server.com"
app.add_url_rule("/", view_func=container.index_view.as_view()) app.add_url_rule("/", view_func=container.index_view.as_view())
app.add_url_rule("/test", "test-test", view_func=container.test_view.as_view()) app.add_url_rule("/test", "test-test", view_func=container.test_view.as_view())
app.add_url_rule("/test-class", view_func=container.test_class_view.as_view("test-class")) app.add_url_rule("/test-class", view_func=container.test_class_view.as_view("test-class"))
return app return app
class ApplicationTests(unittest.TestCase): @fixture
def client(app):
with app.test_client() as client:
yield client
def setUp(self):
self.app = create_app()
self.app.config["SERVER_NAME"] = "test-server.com"
self.client = self.app.test_client()
self.client.__enter__()
def tearDown(self): def test_index(client):
self.client.__exit__(None, None, None) response = client.get("/")
def test_index(self): assert response.status_code == 200
response = self.client.get("/") assert response.data == b"Hello World!"
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, b"Hello World!")
def test_test(self): def test_test(client):
response = self.client.get("/test") response = client.get("/test")
self.assertEqual(response.status_code, 200) assert response.status_code == 200
self.assertEqual(response.data, b"Test!") assert response.data == b"Test!"
def test_test_class_based(self):
response = self.client.get("/test-class")
self.assertEqual(response.status_code, 200) def test_test_class_based(client):
self.assertEqual(response.data, b"Test class-based!") response = client.get("/test-class")
def test_endpoints(self): assert response.status_code == 200
with self.app.app_context(): assert response.data == b"Test class-based!"
self.assertEqual(url_for("index"), "http://test-server.com/")
self.assertEqual(url_for("test-test"), "http://test-server.com/test")
self.assertEqual(url_for("test-class"), "http://test-server.com/test-class") def test_endpoints(app):
with app.app_context():
assert url_for("index") == "http://test-server.com/"
assert url_for("test-test") == "http://test-server.com/test"
assert url_for("test-class") == "http://test-server.com/test-class"