2020-07-11 19:15:00 +03:00
|
|
|
"""Dependency injector Flask extension unit tests."""
|
|
|
|
|
2021-03-06 04:17:28 +03:00
|
|
|
import unittest
|
2020-07-14 05:45:15 +03:00
|
|
|
from flask import Flask, url_for
|
2020-07-11 19:15:00 +03:00
|
|
|
from flask.views import MethodView
|
|
|
|
|
2020-07-29 02:19:05 +03:00
|
|
|
from dependency_injector import containers
|
2020-07-11 19:15:00 +03:00
|
|
|
from dependency_injector.ext import flask
|
|
|
|
|
|
|
|
|
|
|
|
def index():
|
2021-10-01 03:09:42 +03:00
|
|
|
return "Hello World!"
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test():
|
2021-10-01 03:09:42 +03:00
|
|
|
return "Test!"
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Test(MethodView):
|
|
|
|
def get(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
return "Test class-based!"
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
|
2020-07-14 05:45:15 +03:00
|
|
|
class ApplicationContainer(containers.DeclarativeContainer):
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2020-07-14 05:45:15 +03:00
|
|
|
app = flask.Application(Flask, __name__)
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2020-07-14 05:45:15 +03:00
|
|
|
index_view = flask.View(index)
|
|
|
|
test_view = flask.View(test)
|
|
|
|
test_class_view = flask.ClassBasedView(Test)
|
|
|
|
|
|
|
|
|
|
|
|
def create_app():
|
|
|
|
container = ApplicationContainer()
|
|
|
|
app = container.app()
|
|
|
|
app.container = container
|
2021-10-01 03:09:42 +03:00
|
|
|
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-class", view_func=container.test_class_view.as_view("test-class"))
|
2020-07-14 05:45:15 +03:00
|
|
|
return app
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ApplicationTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2020-07-14 05:45:15 +03:00
|
|
|
self.app = create_app()
|
2021-10-01 03:09:42 +03:00
|
|
|
self.app.config["SERVER_NAME"] = "test-server.com"
|
2020-07-11 19:15:00 +03:00
|
|
|
self.client = self.app.test_client()
|
|
|
|
self.client.__enter__()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.__exit__(None, None, None)
|
|
|
|
|
|
|
|
def test_index(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
response = self.client.get("/")
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(response.data, b"Hello World!")
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
def test_test(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
response = self.client.get("/test")
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(response.data, b"Test!")
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
def test_test_class_based(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
response = self.client.get("/test-class")
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(response.data, b"Test class-based!")
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
def test_endpoints(self):
|
|
|
|
with self.app.app_context():
|
2021-10-01 03:09:42 +03:00
|
|
|
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")
|