1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
"""Test we don't break TurboGears' usage of our Python API."""
from __future__ import annotations
import dataclasses
import wsgiref.util
from pathlib import Path
from typing import Any
import pytest
from kajiki import i18n
DATA = Path(__file__).resolve().parent.parent / "data"
GOLDEN = DATA / "golden"
@pytest.fixture
def tg2():
"""Import tg2 (if available), and cleanup its mess."""
orig_gettext = i18n.gettext
try:
import tg
except ImportError:
pytest.skip("TurboGears not installed")
else:
yield tg
finally:
i18n.gettext = orig_gettext
@pytest.fixture
def tg2_app(tg2):
"""Create a new tg2 app with defaults for testing.
Returns:
A WSGI application.
"""
class RootController(tg2.TGController):
@tg2.expose("kajiki_test_data.kitchensink")
def index(self):
return {}
config = tg2.MinimalApplicationConfigurator()
config.update_blueprint(
{
"root_controller": RootController(),
"renderers": ["kajiki"],
"templating.kajiki.template_extension": ".html",
}
)
return config.make_wsgi_app()
@dataclasses.dataclass
class Response:
"""Encapsulates a response for testing."""
status: str
headers: list[tuple[str, str]]
body: str
def app_request(app: Any, path: str = "/", method: str = "GET") -> Response:
response_status = ""
response_headers = []
def start_response(status, headers):
nonlocal response_status
nonlocal response_headers
response_status = status
response_headers = headers
environ = {}
wsgiref.util.setup_testing_defaults(environ)
environ["REQUEST_METHOD"] = method
environ["PATH_INFO"] = path
response_body = b"".join(app(environ, start_response))
return Response(
status=response_status,
headers=response_headers,
body=response_body,
)
def test_smoke_tg2(tg2_app):
"""Do a basic smoke test of using kajiki renderer on tg2."""
response = app_request(tg2_app, "/")
assert response.status == "200 OK"
assert response.body == (GOLDEN / "kitchensink1.html").read_bytes()
|