~tieong/notjinja2

b11390b09a11b61e76b5f72ba5d37dd79a0f9156 — Thomas Ieong 1 year, 7 months ago b7e77fb
Test another template
1 files changed, 34 insertions(+), 12 deletions(-)

M tests/test_templates.py
M tests/test_templates.py => tests/test_templates.py +34 -12
@@ 3,21 3,23 @@ from pathlib import Path
import unittest


class TestTemplates(unittest.TestCase):
    def test_template1(self):
        class User:
            def __init__(self, is_logged_in, name):
                self.is_logged_in = is_logged_in
                self.name = name
class User:
    def __init__(self, is_logged_in, name):
        self.is_logged_in = is_logged_in
        self.name = name


        class Product:
            def __init__(self, name, price):
                self.name = name
                self.price = price
class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

            def format_price(self, price):
                return str(price) + "0"
    def format_price(self, price):
        return str(price) + "0"


class TestTemplates(unittest.TestCase):
    def test_template1(self):
        path = Path.cwd() / "templates" / "hello_world.html"
        with path.open(encoding="utf-8") as f:
            template = Template(


@@ 36,6 38,26 @@ class TestTemplates(unittest.TestCase):
            "    <p>Welcome, Donald!</p><p>Products:</p><ul>    <li>foo: 100</li>    <li>boo: 200</li>    <p>Priority: HIGH</p></ul>",
        )

    def test_template2(self):
        path = Path.cwd() / "templates" / "depth.html"
        with path.open(encoding="utf-8") as f:
            template = Template(
                f.read(),
                {
                    "user": User(True, "Donald"),
                    "product_list": [Product("foo", 10), Product("boo", 20)],
                    "format_price": Product.format_price,
                    "priority": "HIGH",
                    "priority_low": "LOW",
                    "data": {"HIGH": 1, "MID": 2, "LOW": 3},
                },
            )
        result = template.render()
        self.assertEqual(
            result,
            "    <p>Welcome, Donald!</p><p>Products:</p><ul>    <li>foo: 100</li>    <ul>                         <li>foo: 100</li>                                 <li>boo: 200</li>                  </ul>       <li>boo: 200</li>    <ul>                         <li>foo: 100</li>                                 <li>boo: 200</li>                  </ul>       <p>Priority: HIGH</p></ul>",
        )


if __name__ == "__main__":
    unittest.main()