Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
138 lines
4.7 KiB
Python
138 lines
4.7 KiB
Python
import pytest
|
|
from src.core.templating import TemplatingEngine
|
|
|
|
|
|
class TestTemplatingEngine:
|
|
def setup_method(self):
|
|
self.engine = TemplatingEngine()
|
|
|
|
def test_render_simple_template(self):
|
|
template = "Hello, {{name}}!"
|
|
context = {"name": "World"}
|
|
result = self.engine.render_template(template, context)
|
|
assert result == "Hello, World!"
|
|
|
|
def test_render_template_with_path_param(self):
|
|
template = "User ID: {{request.path.id}}"
|
|
context = {"request": {"path": {"id": "123"}}}
|
|
result = self.engine.render_template(template, context)
|
|
assert result == "User ID: 123"
|
|
|
|
def test_render_template_with_query_param(self):
|
|
template = "Page: {{request.query.page}}"
|
|
context = {"request": {"query": {"page": "5"}}}
|
|
result = self.engine.render_template(template, context)
|
|
assert result == "Page: 5"
|
|
|
|
def test_render_dict_simple(self):
|
|
data = {
|
|
"message": "Hello {{name}}",
|
|
"count": 42
|
|
}
|
|
context = {"name": "World"}
|
|
result = self.engine.render_dict(data, context)
|
|
assert result["message"] == "Hello World"
|
|
assert result["count"] == 42
|
|
|
|
def test_render_dict_nested(self):
|
|
data = {
|
|
"user": {
|
|
"name": "{{name}}",
|
|
"id": "{{uuid}}"
|
|
}
|
|
}
|
|
context = {"name": "John", "uuid": "abc123"}
|
|
result = self.engine.render_dict(data, context)
|
|
assert result["user"]["name"] == "John"
|
|
assert result["user"]["id"] == "abc123"
|
|
|
|
def test_render_dict_list(self):
|
|
data = {
|
|
"items": [
|
|
{"name": "{{name}}"},
|
|
"static"
|
|
]
|
|
}
|
|
context = {"name": "Test"}
|
|
result = self.engine.render_dict(data, context)
|
|
assert result["items"][0]["name"] == "Test"
|
|
assert result["items"][1] == "static"
|
|
|
|
def test_render_dict_non_string(self):
|
|
data = {
|
|
"count": 100,
|
|
"active": True,
|
|
"items": [1, 2, 3]
|
|
}
|
|
context = {}
|
|
result = self.engine.render_dict(data, context)
|
|
assert result["count"] == 100
|
|
assert result["active"] is True
|
|
assert result["items"] == [1, 2, 3]
|
|
|
|
def test_build_context(self):
|
|
context = self.engine.build_context(
|
|
path_params={"id": "123"},
|
|
query_params={"page": "1"},
|
|
headers={"Authorization": "Bearer token"},
|
|
body={"name": "test"},
|
|
extra={"custom": "value"}
|
|
)
|
|
assert context["request"]["path"]["id"] == "123"
|
|
assert context["request"]["query"]["page"] == "1"
|
|
assert context["request"]["headers"]["Authorization"] == "Bearer token"
|
|
assert context["request"]["body"]["name"] == "test"
|
|
assert context["custom"] == "value"
|
|
assert "uuid" in context
|
|
assert "timestamp" in context
|
|
|
|
def test_build_context_defaults(self):
|
|
context = self.engine.build_context()
|
|
assert context["request"]["path"] == {}
|
|
assert context["request"]["query"] == {}
|
|
assert context["request"]["headers"] == {}
|
|
assert context["request"]["body"] == {}
|
|
|
|
def test_template_filters(self):
|
|
template = "{{name|upper}}"
|
|
context = {"name": "hello"}
|
|
result = self.engine.render_template(template, context)
|
|
assert result == "HELLO"
|
|
|
|
def test_template_default_filter(self):
|
|
template = "{{value|default('N/A')}}"
|
|
context = {"value": None}
|
|
result = self.engine.render_template(template, context)
|
|
assert result == "N/A"
|
|
|
|
def test_template_length_filter(self):
|
|
template = "{{items|length}}"
|
|
context = {"items": [1, 2, 3]}
|
|
result = self.engine.render_template(template, context)
|
|
assert result == "3"
|
|
|
|
def test_template_globals(self):
|
|
template = "ID: {{uuid}}, Time: {{timestamp}}"
|
|
context = self.engine.build_context()
|
|
result = self.engine.render_template(template, context)
|
|
assert "ID:" in result
|
|
assert "Time:" in result
|
|
|
|
def test_template_datetime(self):
|
|
template = "Now: {{datetime}}"
|
|
context = self.engine.build_context()
|
|
result = self.engine.render_template(template, context)
|
|
assert "Now:" in result
|
|
|
|
def test_template_random_int(self):
|
|
template = "Random: {{random_int(1, 10)}}"
|
|
context = {}
|
|
result = self.engine.render_template(template, context)
|
|
assert "Random:" in result
|
|
|
|
def test_template_random_string(self):
|
|
template = "String: {{random_string(8)}}"
|
|
context = {}
|
|
result = self.engine.render_template(template, context)
|
|
assert "String:" in result
|