Files
temp/tests/features/steps/template_renderer_errors_steps.py
T

163 lines
5.1 KiB
Python

import json
import sys
from unittest.mock import patch
from behave import given
from behave import then
from behave import when
from cleveragents.core.exceptions import TemplateError
from cleveragents.templates.renderer import TemplateEngine
from cleveragents.templates.renderer import TemplateRenderer
@given('I try to initialize a TemplateRenderer with the "{engine}" engine')
def step_impl(context, engine):
context.engine_type_str = engine
if engine == "jinja2":
context.engine_type_enum = TemplateEngine.JINJA2
elif engine == "mustache":
context.engine_type_enum = TemplateEngine.MUSTACHE
@when('the required package "{package}" is not installed')
def step_impl(context, package):
with patch.dict(sys.modules, {package: None}):
try:
TemplateRenderer(context.engine_type_enum)
except TemplateError as e:
context.error = e
@then('a TemplateError should be raised with a message containing "{message}"')
def step_impl(context, message):
assert hasattr(context, "error"), "TemplateError was not raised"
assert isinstance(
context.error, TemplateError
), f"Expected TemplateError, but got {type(context.error)}"
assert message in str(context.error), f"'{message}' not in '{str(context.error)}'"
@given("I have an unsupported template engine type")
def step_impl(context):
context.unsupported_engine = "unsupported_engine_type"
@when("I try to initialize a TemplateRenderer with this engine type")
def step_impl(context):
try:
# We need to bypass the Enum type check for this test
renderer = TemplateRenderer()
renderer.engine_type = context.unsupported_engine
renderer._initialize_engine()
except TemplateError as e:
context.error = e
@given('a TemplateRenderer with the "{engine}" engine')
def step_impl(context, engine):
# This step assumes packages are installed for successful initialization
context.renderer = TemplateRenderer(TemplateEngine(engine))
@when("I attempt to register a template with an empty name")
def step_impl(context):
try:
context.renderer.register_template("", "some content")
except TemplateError as e:
context.error = e
@when('I try to register a template with invalid Jinja2 syntax "{syntax}"')
def step_impl(context, syntax):
try:
context.renderer.register_template("invalid", syntax)
except TemplateError as e:
context.error = e
@given('a template named "{name}" with content "{content}"')
def step_impl(context, name, content):
context.renderer.register_template(name, content)
@when('I try to render the "{template_name}" template with an empty context')
def step_impl(context, template_name):
try:
context.renderer.render(template_name, {})
except TemplateError as e:
context.error = e
@when('I try to render the string "{template_str}" with an empty context')
def step_impl(context, template_str):
try:
context.renderer.render_string(template_str, {})
except TemplateError as e:
context.error = e
@when("I render the \"{template_name}\" template with context '{json_context}'")
def step_impl(context, template_name, json_context):
ctx = json.loads(json_context)
context.result = context.renderer.render(template_name, ctx)
@then('the result should be "{expected_result}"')
def step_impl(context, expected_result):
assert (
context.result == expected_result
), f"Expected '{expected_result}', got '{context.result}'"
@when('I try to render a template named "{template_name}" with any context')
@when('I try to render the "{template_name}" template with any context')
def step_impl(context, template_name):
try:
context.renderer.render(template_name, {"foo": "bar"})
except TemplateError as e:
context.error = e
@when('I try to render the string "{template_str}" with any context')
def step_impl(context, template_str):
try:
context.renderer.render_string(template_str, {"foo": "bar"})
except TemplateError as e:
context.error = e
@when('I try to get a template named "{template_name}"')
def step_impl(context, template_name):
try:
context.renderer.get_template(template_name)
except TemplateError as e:
context.error = e
@given("I have manually modified the engine to be invalid")
def step_impl(context):
# Replace engine with an object that doesn't have the required methods
context.renderer.engine = object()
@when('I try to register a template named "{name}" with content "{content}"')
def step_impl(context, name, content):
try:
context.renderer.register_template(name, content)
except TemplateError as e:
context.error = e
@given(
'I have manually registered a template named "{template_name}" that is not a valid Jinja2 template object'
)
def step_impl(context, template_name):
# Manually insert a non-template object into the templates dict
context.renderer.templates[template_name] = "just a string"
@given("I have manually set the engine type to an unsupported value")
def step_impl(context):
context.renderer.engine_type = "unsupported"