Now you can test like #4
This commit is contained in:
parent
a956a7856f
commit
b87184dca0
5 changed files with 106 additions and 2 deletions
|
@ -8,6 +8,11 @@
|
||||||
# http://www.opensource.org/licenses/mit-license
|
# http://www.opensource.org/licenses/mit-license
|
||||||
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
||||||
|
|
||||||
|
from lxml.cssselect import CSSSelector
|
||||||
|
from lxml.etree import fromstring
|
||||||
|
|
||||||
|
from django.template.loader import render_to_string
|
||||||
|
|
||||||
from pyvows import Vows
|
from pyvows import Vows
|
||||||
|
|
||||||
class Url(object):
|
class Url(object):
|
||||||
|
@ -15,6 +20,42 @@ class Url(object):
|
||||||
self.context = context
|
self.context = context
|
||||||
self.path = path
|
self.path = path
|
||||||
|
|
||||||
|
class Template(object):
|
||||||
|
def __init__(self, template_name, context):
|
||||||
|
self.template_name = template_name
|
||||||
|
self.context = context
|
||||||
|
self.doc = None
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
if not self.doc:
|
||||||
|
self.doc = fromstring(render_to_string(self.template_name, self.context))
|
||||||
|
return self.doc
|
||||||
|
|
||||||
|
def select_element(self, selector):
|
||||||
|
sel = CSSSelector(selector)
|
||||||
|
return sel(self.load())
|
||||||
|
|
||||||
|
def _to_contain(self, selector):
|
||||||
|
return len(self.select_element(selector)) > 0
|
||||||
|
|
||||||
|
def get_text(self, selector):
|
||||||
|
return "".join((c.text for c in self.select_element(selector)))
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self.template_name
|
||||||
|
|
||||||
|
@Vows.assertion
|
||||||
|
def to_contain(topic, selector):
|
||||||
|
assert isinstance(topic, Template), "Only django_pyvows.Template items can be verified for mapping"
|
||||||
|
assert topic._to_contain(selector), "Expected template(%s) to have an element(%s), but it don't have" % \
|
||||||
|
(unicode(topic), selector)
|
||||||
|
|
||||||
|
@Vows.assertion
|
||||||
|
def not_to_contain(topic, selector):
|
||||||
|
assert isinstance(topic, Template), "Only django_pyvows.Template items can be verified for mapping"
|
||||||
|
assert not topic._to_contain(selector), "Expected template(%s) to not have an element(%s), but it have" % \
|
||||||
|
(unicode(topic), selector)
|
||||||
|
|
||||||
@Vows.assertion
|
@Vows.assertion
|
||||||
def to_be_mapped(topic):
|
def to_be_mapped(topic):
|
||||||
verify_url_is_mapped_to_method(topic)
|
verify_url_is_mapped_to_method(topic)
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from pyvows import Vows
|
from pyvows import Vows
|
||||||
from django_pyvows.assertions import Url
|
from django_pyvows.assertions import Url, Template
|
||||||
|
|
||||||
class DjangoContext(Vows.Context):
|
class DjangoContext(Vows.Context):
|
||||||
|
|
||||||
|
@ -31,8 +31,14 @@ class DjangoContext(Vows.Context):
|
||||||
def _url(self, path):
|
def _url(self, path):
|
||||||
return Url(self, path)
|
return Url(self, path)
|
||||||
|
|
||||||
|
def _template(self, template_name, context):
|
||||||
|
return Template(template_name, context)
|
||||||
|
|
||||||
class DjangoSubContext(Vows.Context):
|
class DjangoSubContext(Vows.Context):
|
||||||
|
|
||||||
def _url(self, path):
|
def _url(self, path):
|
||||||
return self.parent._url(path)
|
return self.parent._url(path)
|
||||||
|
|
||||||
|
def _template(self, template_name, context={}):
|
||||||
|
return self.parent._template(template_name, context)
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
# Django settings for sandbox project.
|
# Django settings for sandbox project.
|
||||||
|
import os.path
|
||||||
|
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
TEMPLATE_DEBUG = DEBUG
|
TEMPLATE_DEBUG = DEBUG
|
||||||
|
|
||||||
|
PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
|
||||||
|
|
||||||
ADMINS = (
|
ADMINS = (
|
||||||
# ('Your Name', 'your_email@example.com'),
|
# ('Your Name', 'your_email@example.com'),
|
||||||
)
|
)
|
||||||
|
@ -106,6 +109,7 @@ TEMPLATE_DIRS = (
|
||||||
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
|
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
|
||||||
# Always use forward slashes, even on Windows.
|
# Always use forward slashes, even on Windows.
|
||||||
# Don't forget to use absolute paths, not relative paths.
|
# Don't forget to use absolute paths, not relative paths.
|
||||||
|
os.path.join(PROJECT_ROOT, "templates"),
|
||||||
)
|
)
|
||||||
|
|
||||||
INSTALLED_APPS = (
|
INSTALLED_APPS = (
|
||||||
|
|
11
vows/sandbox/templates/index.html
Normal file
11
vows/sandbox/templates/index.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>test</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<p class="my-text">some text</p>
|
||||||
|
<p>Some {{ some }}!</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
42
vows/template_vows.py
Normal file
42
vows/template_vows.py
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# django-pyvows extensions
|
||||||
|
# https://github.com/rafaelcaricio/django-pyvows
|
||||||
|
|
||||||
|
# Licensed under the MIT license:
|
||||||
|
# http://www.opensource.org/licenses/mit-license
|
||||||
|
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
||||||
|
|
||||||
|
from pyvows import Vows, expect
|
||||||
|
from django_pyvows.context import DjangoContext, DjangoSubContext
|
||||||
|
import django_pyvows.assertions
|
||||||
|
|
||||||
|
@Vows.batch
|
||||||
|
class TemplateVows(DjangoContext):
|
||||||
|
|
||||||
|
def _get_settings(self):
|
||||||
|
return 'sandbox.settings'
|
||||||
|
|
||||||
|
class IndexTemplate(DjangoSubContext):
|
||||||
|
|
||||||
|
def topic(self):
|
||||||
|
return self._template('index.html', {
|
||||||
|
'some': 'thing'
|
||||||
|
})
|
||||||
|
|
||||||
|
def should_have_container_div(self, topic):
|
||||||
|
expect(topic).to_contain('div.container')
|
||||||
|
|
||||||
|
def should_not_have_a_hello_div(self, topic):
|
||||||
|
expect(topic).Not.to_contain('div.hello')
|
||||||
|
|
||||||
|
class Paragraph(DjangoSubContext):
|
||||||
|
|
||||||
|
def topic(self, template):
|
||||||
|
return template.get_text('p.my-text')
|
||||||
|
|
||||||
|
def should_have_paragraph_with_text(self, topic):
|
||||||
|
expect(topic).to_be_like('some text')
|
||||||
|
|
||||||
|
|
Reference in a new issue