2021-10-18 23:19:03 +03:00
|
|
|
"""Flask extension tests."""
|
2020-07-11 19:15:00 +03:00
|
|
|
|
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
|
2021-10-18 23:19:03 +03:00
|
|
|
from flask import Flask, url_for
|
|
|
|
from flask.views import MethodView
|
|
|
|
from pytest import fixture
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
@fixture
|
|
|
|
def app():
|
2020-07-14 05:45:15 +03:00
|
|
|
container = ApplicationContainer()
|
|
|
|
app = container.app()
|
|
|
|
app.container = container
|
2021-10-18 23:19:03 +03:00
|
|
|
app.config["SERVER_NAME"] = "test-server.com"
|
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
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
@fixture
|
|
|
|
def client(app):
|
|
|
|
with app.test_client() as client:
|
|
|
|
yield client
|
|
|
|
|
|
|
|
|
|
|
|
def test_index(client):
|
|
|
|
response = client.get("/")
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.data == b"Hello World!"
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
def test_test(client):
|
|
|
|
response = client.get("/test")
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.data == b"Test!"
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
def test_test_class_based(client):
|
|
|
|
response = client.get("/test-class")
|
2020-07-11 19:15:00 +03:00
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.data == b"Test class-based!"
|
2020-07-11 19:15:00 +03:00
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
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"
|