mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-14 10:02:23 +03:00
Add tests for flask extension
This commit is contained in:
parent
b927468824
commit
602beced8a
|
@ -6,3 +6,5 @@ flake8
|
||||||
pydocstyle
|
pydocstyle
|
||||||
sphinx_autobuild
|
sphinx_autobuild
|
||||||
pip
|
pip
|
||||||
|
|
||||||
|
-r requirements-ext.txt
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
sphinx
|
sphinx
|
||||||
sphinx_rtd_theme>=0.2.5b2
|
sphinx_rtd_theme>=0.2.5b2
|
||||||
-e git://github.com/rmk135/sphinxcontrib-disqus.git#egg=sphinxcontrib-disqus
|
-e git://github.com/rmk135/sphinxcontrib-disqus.git#egg=sphinxcontrib-disqus
|
||||||
|
|
||||||
|
-r requirements-ext.txt
|
||||||
|
|
1
requirements-ext.txt
Normal file
1
requirements-ext.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
flask
|
1
tests/unit/ext/__init__.py
Normal file
1
tests/unit/ext/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
"""Dependency injector extension unit tests."""
|
75
tests/unit/ext/test_flask_py2_py3.py
Normal file
75
tests/unit/ext/test_flask_py2_py3.py
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
"""Dependency injector Flask extension unit tests."""
|
||||||
|
|
||||||
|
import unittest2 as unittest
|
||||||
|
from flask import Flask, url_for
|
||||||
|
from flask.views import MethodView
|
||||||
|
|
||||||
|
from dependency_injector import containers, providers
|
||||||
|
from dependency_injector.ext import flask
|
||||||
|
|
||||||
|
|
||||||
|
def index():
|
||||||
|
return 'Hello World!'
|
||||||
|
|
||||||
|
|
||||||
|
def test():
|
||||||
|
return 'Test!'
|
||||||
|
|
||||||
|
|
||||||
|
class Test(MethodView):
|
||||||
|
def get(self):
|
||||||
|
return 'Test class-based!'
|
||||||
|
|
||||||
|
|
||||||
|
class Application(containers.DeclarativeContainer):
|
||||||
|
|
||||||
|
index_view = providers.Callable(index)
|
||||||
|
test_view = providers.Callable(test)
|
||||||
|
test_class_view = providers.Factory(Test)
|
||||||
|
|
||||||
|
app = providers.Factory(
|
||||||
|
flask.create_app,
|
||||||
|
name=__name__,
|
||||||
|
routes=[
|
||||||
|
flask.Route('/', view_provider=index_view),
|
||||||
|
flask.Route('/test', 'test-test', test_view),
|
||||||
|
flask.Route('/test-class', 'test-class', test_class_view)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ApplicationTests(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self) -> None:
|
||||||
|
application = Application()
|
||||||
|
self.app: Flask = application.app()
|
||||||
|
self.app.config['SERVER_NAME'] = 'test-server.com'
|
||||||
|
self.client = self.app.test_client()
|
||||||
|
self.client.__enter__()
|
||||||
|
|
||||||
|
def tearDown(self) -> None:
|
||||||
|
self.client.__exit__(None, None, None)
|
||||||
|
|
||||||
|
def test_index(self):
|
||||||
|
response = self.client.get('/')
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.data, b'Hello World!')
|
||||||
|
|
||||||
|
def test_test(self):
|
||||||
|
response = self.client.get('/test')
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.data, b'Test!')
|
||||||
|
|
||||||
|
def test_test_class_based(self):
|
||||||
|
response = self.client.get('/test-class')
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.data, b'Test class-based!')
|
||||||
|
|
||||||
|
def test_endpoints(self):
|
||||||
|
with self.app.app_context():
|
||||||
|
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')
|
Loading…
Reference in New Issue
Block a user