Refactory in assertions modules.
This commit is contained in:
parent
6e6e4ed81e
commit
5816271f50
4 changed files with 124 additions and 87 deletions
13
django_pyvows/assertions/__init__.py
Normal file
13
django_pyvows/assertions/__init__.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
#!/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 urls import *
|
||||
from models import *
|
||||
from templates import *
|
|
@ -10,17 +10,6 @@
|
|||
|
||||
from pyvows import Vows, expect
|
||||
|
||||
from lxml.cssselect import CSSSelector
|
||||
from lxml.etree import fromstring
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
|
||||
class Url(object):
|
||||
def __init__(self, context, path):
|
||||
self.context = context
|
||||
self.path = path
|
||||
|
||||
class Model(object):
|
||||
def __init__(self, context, model):
|
||||
|
@ -30,82 +19,6 @@ class Model(object):
|
|||
def __call__(self, *args, **kwargs):
|
||||
return self.model(*args, **kwargs)
|
||||
|
||||
class Template(object):
|
||||
def __init__(self, template_name, context):
|
||||
self.template_name = template_name
|
||||
self.context = context
|
||||
self.doc = None
|
||||
|
||||
def load(self):
|
||||
if self.doc is None:
|
||||
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_be_mapped(topic):
|
||||
verify_url_is_mapped_to_method(topic)
|
||||
|
||||
@Vows.assertion
|
||||
def to_match_view(topic, view):
|
||||
verify_url_is_mapped_to_method(topic, view, True)
|
||||
|
||||
def verify_url_is_mapped_to_method(topic, method=None, assert_method_as_well=False):
|
||||
assert isinstance(topic, Url), "Only django_pyvows.Url items can be verified for mapping"
|
||||
|
||||
from django.conf import settings
|
||||
project_urls = settings.ROOT_URLCONF
|
||||
|
||||
urlpatterns = __import__(project_urls).urls.urlpatterns
|
||||
|
||||
found = False
|
||||
matches_method = False
|
||||
for urlpattern in urlpatterns:
|
||||
regex = urlpattern.regex
|
||||
pattern = regex.pattern
|
||||
actual_method = urlpattern.callback
|
||||
|
||||
if topic.path == pattern:
|
||||
found = True
|
||||
if method == actual_method:
|
||||
matches_method = True
|
||||
|
||||
assert found, "Expected url(%s) to be mapped but it wasn't"
|
||||
if assert_method_as_well:
|
||||
assert matches_method, "Expected url(%s) to match method(%s), but it didn't"
|
||||
|
||||
@Vows.assertion
|
||||
def to_be_http_response(topic):
|
||||
expect(topic).to_be_instance_of(HttpResponse)
|
||||
|
||||
@Vows.assertion
|
||||
def to_have_contents_of(topic, expected):
|
||||
expect(topic.content).to_be_like(expected)
|
||||
|
||||
@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
|
||||
def to_have_field(topic, field_name, field_class=None, **kwargs):
|
||||
from django.db import models
|
57
django_pyvows/assertions/templates.py
Normal file
57
django_pyvows/assertions/templates.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
#!/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 lxml.etree import fromstring
|
||||
from lxml.cssselect import CSSSelector
|
||||
|
||||
from pyvows import Vows, expect
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
|
||||
class Template(object):
|
||||
def __init__(self, template_name, context):
|
||||
self.template_name = template_name
|
||||
self.context = context
|
||||
self.doc = None
|
||||
|
||||
def load(self):
|
||||
if self.doc is None:
|
||||
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_have_contents_of(topic, expected):
|
||||
expect(topic.content).to_be_like(expected)
|
||||
|
||||
@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)
|
||||
|
54
django_pyvows/assertions/urls.py
Normal file
54
django_pyvows/assertions/urls.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
#!/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.http import HttpResponse
|
||||
|
||||
|
||||
class Url(object):
|
||||
def __init__(self, context, path):
|
||||
self.context = context
|
||||
self.path = path
|
||||
|
||||
@Vows.assertion
|
||||
def to_be_mapped(topic):
|
||||
verify_url_is_mapped_to_method(topic)
|
||||
|
||||
@Vows.assertion
|
||||
def to_match_view(topic, view):
|
||||
verify_url_is_mapped_to_method(topic, view, True)
|
||||
|
||||
@Vows.assertion
|
||||
def to_be_http_response(topic):
|
||||
expect(topic).to_be_instance_of(HttpResponse)
|
||||
|
||||
def verify_url_is_mapped_to_method(topic, method=None, assert_method_as_well=False):
|
||||
assert isinstance(topic, Url), "Only django_pyvows.Url items can be verified for mapping"
|
||||
|
||||
from django.conf import settings
|
||||
project_urls = settings.ROOT_URLCONF
|
||||
|
||||
urlpatterns = __import__(project_urls).urls.urlpatterns
|
||||
|
||||
found = False
|
||||
matches_method = False
|
||||
for urlpattern in urlpatterns:
|
||||
regex = urlpattern.regex
|
||||
pattern = regex.pattern
|
||||
actual_method = urlpattern.callback
|
||||
|
||||
if topic.path == pattern:
|
||||
found = True
|
||||
if method == actual_method:
|
||||
matches_method = True
|
||||
|
||||
assert found, "Expected url(%s) to be mapped but it wasn't"
|
||||
if assert_method_as_well:
|
||||
assert matches_method, "Expected url(%s) to match method(%s), but it didn't"
|
Reference in a new issue