Browse Source

Added test for Jinja2generation

main
Lakshmi Vyasarajan 14 years ago
parent
commit
0eed3971e3
7 changed files with 68 additions and 22 deletions
  1. +15
    -0
      dev-req.txt
  2. +0
    -14
      hyde/ext/templates/jinja2.py
  3. +19
    -0
      hyde/ext/templates/jinja2Template.py
  4. +6
    -0
      hyde/fs.py
  5. +7
    -0
      hyde/tests/test_fs.py
  6. +16
    -6
      hyde/tests/test_jinja2template.py
  7. +5
    -2
      hyde/tests/util.py

+ 15
- 0
dev-req.txt View File

@@ -0,0 +1,15 @@
Django==1.2.3
Genshi==0.6
Jinja2==2.5.5
Mako==0.3.6
Markdown==2.0.3
MarkupSafe==0.11
PyYAML==3.09
logilab-astng==0.21.0
logilab-common==0.53.0
lxml==2.3beta1
nose==0.11.4
pylint==0.22.0
pyquery==0.6.1
unittest2==0.5.1
wsgiref==0.1.2

+ 0
- 14
hyde/ext/templates/jinja2.py View File

@@ -1,14 +0,0 @@
"""
Hyde Template interface realization for Jinja2
"""
from hyde.template import Template

class Jinja2Template(Template):
def configure(self, config):
"""
Uses the config object to initialize the jinja environment.
"""
pass

def render(self, template_name, context):
pass

+ 19
- 0
hyde/ext/templates/jinja2Template.py View File

@@ -0,0 +1,19 @@
"""
Hyde Template interface realization for Jinja2
"""
from hyde.template import Template
from jinja2 import Environment, FileSystemLoader

class Jinja2Template(Template):
def configure(self, sitepath, config):
"""
Uses the config object to initialize the jinja environment.
"""
self.env = Environment(loader=FileSystemLoader(sitepath))
def render(self, template_name, context):
"""
Renders the given template using the context
"""
t = self.env.get_template(template_name)
return t.render(context)

+ 6
- 0
hyde/fs.py View File

@@ -78,6 +78,12 @@ class File(FS):
read_text = fin.read()
return read_text

def write(self, text, encoding="utf-8"):
"""
Writes the given text to the file using the given encoding.
"""
with codecs.open(self.path, 'w', encoding) as fout:
fout.write(text)

class Folder(FS):
"""


+ 7
- 0
hyde/tests/test_fs.py View File

@@ -58,5 +58,12 @@ def test_read_all():
with codecs.open(path, 'w', 'utf-8') as f:
f.write(utxt)

txt = File(path).read_all()
assert txt == utxt
def test_write():
utxt = u'åßcdeƒ'
path = DATA_ROOT.child('unicode.txt')
File(path).write(utxt)
txt = File(path).read_all()
assert txt == utxt

+ 16
- 6
hyde/tests/test_jinja2template.py View File

@@ -3,9 +3,11 @@
Use nose
`$ pip install nose`
`$ nosetests`

Code borrowed from rwbench.py from the jinja2 examples
"""
from datetime import datetime
from hyde.ext.templates.jinja2 import Jinja2Template
from hyde.ext.templates.jinja2Template import Jinja2Template
from hyde.fs import File, Folder
from jinja2.utils import generate_lorem_ipsum
from random import choice, randrange
@@ -26,6 +28,8 @@ class Article(object):
self.pub_date = datetime.utcfromtimestamp(randrange(10 ** 9, 2 * 10 ** 9))
self.published = True

def dateformat(x):
return x.strftime('%Y-%m-%d')

class User(object):

@@ -49,11 +53,17 @@ context = dict(users=users, articles=articles, page_navigation=navigation)

def test_render():
"""
Uses the real world benchmark code from jinja.
Uses pyquery to test the html structure for validity
"""
t = Jinja2Template()
config = yaml.load(JINJA2.child('config.yaml'))
t.configure(None)
t.configure(JINJA2.path, None)
t.env.filters['dateformat'] = dateformat
html = t.render('index.html', context)
expected = File(JINJA2.child('index_expected.html')).read_all()
assert_html_equals(expected, html)
from pyquery import PyQuery
actual = PyQuery(html)
assert actual(".navigation li").length == 30
assert actual("div.article").length == 20
assert actual("div.article h2").length == 20
assert actual("div.article h2 a").length == 20
assert actual("div.article p.meta").length == 20
assert actual("div.article div.text").length == 20

+ 5
- 2
hyde/tests/util.py View File

@@ -1,6 +1,9 @@
from django.utils.html import strip_spaces_between_tags

def assert_html_equals(expected, actual):
def assert_html_equals(expected, actual, sanitize=None):
expected = strip_spaces_between_tags(expected.strip())
actual = strip_spaces_between_tags(actual.strip())
assert expected == actual
if sanitize:
expected = sanitize(expected)
actual = sanitize(actual)
assert expected == actual

Loading…
Cancel
Save