Assertions passing

This commit is contained in:
Bernardo Heynemann 2011-05-09 17:20:46 -03:00
parent e3c4b9c548
commit 536eb1f255
4 changed files with 78 additions and 14 deletions

View file

@ -0,0 +1,48 @@
#!/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
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)
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"

View file

@ -8,15 +8,31 @@
# 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
import os
from pyvows import Vows from pyvows import Vows
from django_pyvows.assertions import Url
from django.test.utils import setup_test_environment, teardown_test_environment
class DjangoContext(Vows.Context): class DjangoContext(Vows.Context):
def _set_up_enviroment(self): def __init__(self, parent):
super(DjangoContext, self).__init__(parent)
os.environ['DJANGO_SETTINGS_MODULE'] = self._get_settings()
#Gotta set settings environment variable first
from django.test.utils import setup_test_environment #, teardown_test_environment
setup_test_environment() setup_test_environment()
def __call__(self):
self._setup_enviroment() def _get_settings(self):
return 'settings'
def _url(self, path):
return Url(self, path)
class DjangoSubContext(Vows.Context):
def _url(self, path):
return self.parent._url(path)

View file

@ -15,7 +15,7 @@ from django.conf.urls.defaults import patterns, include, url
# admin.autodiscover() # admin.autodiscover()
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^$', 'sandbox.views.home', name='home'), url(r'^$', 'sandbox.main.views.home', name='home'),
# url(r'^sandbox/', include('sandbox.foo.urls')), # url(r'^sandbox/', include('sandbox.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation: # Uncomment the admin/doc line below to enable admin documentation:

View file

@ -8,22 +8,22 @@
# 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 os.path import abspath, join, dirname
from pyvows import Vows, expect from pyvows import Vows, expect
from django_pyvows import DjangoContext, Assertions from django_pyvows.context import DjangoContext, DjangoSubContext
import django_pyvows.assertions
from sandbox.main.views import home from sandbox.main.views import home
@Vows.batch
class UrlVows(DjangoContext): class UrlVows(DjangoContext):
def settings_path(self): def _get_settings(self):
return abspath(join(dirname(__file__), 'sandbox', 'settings.py')) return 'sandbox.settings'
class Home(Vows.Context): class Home(DjangoSubContext):
def topic(self): def topic(self):
return self._url('/') return self._url('^$')
def should_have_home_url_mapped(self, topic): def should_have_home_url_mapped(self, topic):
expect(topic).to_be_mapped() expect(topic).to_be_mapped()