Migrate Jinja to Vue Template Rendering to pytest
This commit is contained in:
parent
0682411954
commit
506d53f862
@ -1,50 +0,0 @@
|
|||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
|
||||||
sys.path.append(parent_dir)
|
|
||||||
|
|
||||||
from jinja2 import Environment, FileSystemLoader
|
|
||||||
from wtforms.widgets import CheckboxInput, FileInput
|
|
||||||
from wtforms.fields import StringField, FileField
|
|
||||||
from wtforms.validators import InputRequired
|
|
||||||
from wtforms import Form
|
|
||||||
|
|
||||||
from atst.filters import iconSvg
|
|
||||||
|
|
||||||
env = Environment(loader=FileSystemLoader("templates"))
|
|
||||||
|
|
||||||
env.filters["iconSvg"] = iconSvg
|
|
||||||
|
|
||||||
# override tojson as identity function to prevent
|
|
||||||
# wrapping strings in extra quotes
|
|
||||||
env.filters["tojson"] = lambda x: x
|
|
||||||
|
|
||||||
|
|
||||||
class InitialValueForm(Form):
|
|
||||||
datafield = StringField(label="initialvalue value", default="initialvalue")
|
|
||||||
|
|
||||||
errorfield = StringField(
|
|
||||||
label="error", validators=[InputRequired(message="Test Error Message")]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
checkbox_template = env.get_template("components/checkbox_input.html")
|
|
||||||
ci_macro = getattr(checkbox_template.module, "CheckboxInput")
|
|
||||||
checkbox_input_form = InitialValueForm()
|
|
||||||
checkbox_input_form.datafield.widget = CheckboxInput()
|
|
||||||
rendered_checkbox_macro = ci_macro(checkbox_input_form.datafield)
|
|
||||||
with open("js/test_templates/checkbox_input_template.html", "w") as fh:
|
|
||||||
fh.write(rendered_checkbox_macro)
|
|
||||||
|
|
||||||
upload_template = env.get_template("components/upload_input.html")
|
|
||||||
up_macro = getattr(upload_template.module, "UploadInput")
|
|
||||||
rendered_upload_macro = up_macro(InitialValueForm().datafield)
|
|
||||||
with open("js/test_templates/upload_input_template.html", "w") as fh:
|
|
||||||
fh.write(rendered_upload_macro)
|
|
||||||
|
|
||||||
erroredform = InitialValueForm()
|
|
||||||
erroredform.validate()
|
|
||||||
rendered_upload_error_macro = up_macro(erroredform.errorfield)
|
|
||||||
with open("js/test_templates/upload_input_error_template.html", "w") as fh:
|
|
||||||
fh.write(rendered_upload_error_macro)
|
|
52
tests/render_vue_component.py
Normal file
52
tests/render_vue_component.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from wtforms.widgets import CheckboxInput
|
||||||
|
from wtforms.fields import StringField
|
||||||
|
from wtforms.validators import InputRequired
|
||||||
|
from wtforms import Form
|
||||||
|
|
||||||
|
class InitialValueForm(Form):
|
||||||
|
datafield = StringField(label="initialvalue value", default="initialvalue")
|
||||||
|
|
||||||
|
errorfield = StringField(
|
||||||
|
label="error", validators=[InputRequired(message="Test Error Message")]
|
||||||
|
)
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def env(app, scope="function"):
|
||||||
|
return app.jinja_env
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def upload_input_macro(env):
|
||||||
|
# override tojson as identity function to prevent
|
||||||
|
# wrapping strings in extra quotes
|
||||||
|
env.filters["tojson"] = lambda x: x
|
||||||
|
upload_template = env.get_template("components/upload_input.html")
|
||||||
|
return getattr(upload_template.module, "UploadInput")
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def checkbox_input_macro(env):
|
||||||
|
checkbox_template = env.get_template("components/checkbox_input.html")
|
||||||
|
return getattr(checkbox_template.module, "CheckboxInput")
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def initial_value_form(scope="function"):
|
||||||
|
return InitialValueForm()
|
||||||
|
|
||||||
|
def write_template(content, name):
|
||||||
|
with open("js/test_templates/{}".format(name), "w") as fh:
|
||||||
|
fh.write(content)
|
||||||
|
|
||||||
|
def test_make_checkbox_input_template(checkbox_input_macro, initial_value_form):
|
||||||
|
initial_value_form.datafield.widget = CheckboxInput()
|
||||||
|
rendered_checkbox_macro = checkbox_input_macro(initial_value_form.datafield)
|
||||||
|
write_template(rendered_checkbox_macro, "checkbox_input_template.html")
|
||||||
|
|
||||||
|
def test_make_upload_input_template(upload_input_macro, initial_value_form):
|
||||||
|
rendered_upload_macro = upload_input_macro(initial_value_form.datafield)
|
||||||
|
write_template(rendered_upload_macro, "upload_input_template.html")
|
||||||
|
|
||||||
|
def test_make_upload_input_error_template(upload_input_macro, initial_value_form):
|
||||||
|
initial_value_form.validate()
|
||||||
|
rendered_upload_macro = upload_input_macro(initial_value_form.errorfield)
|
||||||
|
write_template(rendered_upload_macro, "upload_input_error_template.html")
|
Loading…
x
Reference in New Issue
Block a user