Assertions from django.test.SimpleTestCase

This commit is contained in:
Rafael Caricio 2015-07-19 00:59:42 +02:00
parent 7dcbf31c5a
commit 89e44803de
8 changed files with 109 additions and 2 deletions

View file

@ -3,8 +3,11 @@ setup: deps db
deps:
@pip install -r requirements.txt
sandbox_run:
@env PYTHONPATH=$$PYTHONPATH:vows:. python vows/sandbox/manage.py runserver
sandbox_shell:
@env PYTHONPATH=$$PYTHONPATH:vows/sandbox/:. python vows/sandbox/manage.py shell
@env PYTHONPATH=$$PYTHONPATH:vows:. python vows/sandbox/manage.py shell
pyvows:
@env PYTHONPATH=$$PYTHONPATH:vows/sandbox/:. pyvows -c -l django_pyvows --profile-threshold 95 vows/

View file

@ -9,3 +9,4 @@
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
from context import DjangoContext # NOQA
from assertions import * # NOQA

View file

@ -0,0 +1,34 @@
#!/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 django.test import SimpleTestCase
from pyvows import Vows
class _Dummy(SimpleTestCase):
def nop():
pass
dummy_testcase = _Dummy('nop')
@Vows.assertion
def redirects_to(*args, **kwargs):
return dummy_testcase.assertRedirects(*args, **kwargs)
@Vows.assertion
def contains(*args, **kwargs):
return dummy_testcase.assertContains(*args, **kwargs)
@Vows.assertion
def with_form_error(*args, **kwargs):
return dummy_testcase.assertFormError(*args, **kwargs)

40
vows/assertions_vows.py Normal file
View file

@ -0,0 +1,40 @@
#!/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
DjangoContext.start_environment("sandbox.sandbox.settings")
@Vows.batch
class DjangoAssertionsVows(DjangoContext):
class RedirectsTo(DjangoContext):
def topic(self):
return self.get("/say")
def should_redirect(self, topic):
expect(topic).redirects_to('/say/', status_code=301)
class Contains(DjangoContext):
def topic(self):
return self.get('/say/')
def should_work(self, topic):
expect(topic).contains('name')
class WithFormError(DjangoContext):
def topic(self):
return self.post('/post-name/', {'another_field': 'filled'})
def should_work(self, topic):
expect(topic).with_form_error('form', 'your_name', 'This field is required.')

View file

@ -0,0 +1,5 @@
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100, required=True)

View file

@ -10,6 +10,10 @@
from django.conf import settings
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import NameForm
def home(request):
@ -33,3 +37,17 @@ def post_it(request):
def post_file(request):
return HttpResponse(request.FILES['the_file'].read().strip())
def post_name(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
return HttpResponseRedirect(
'/say/?name={}'.format(form.cleaned_data['your_name']))
else:
form = NameForm()
return render(request, 'name.html', {'form': form})

View file

@ -14,7 +14,7 @@ Including another URLconf
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import url
from main.views import home, say_hello, post_it, post_file
from main.views import home, say_hello, post_it, post_file, post_name
urlpatterns = [
@ -22,4 +22,5 @@ urlpatterns = [
url(r'^say/$', say_hello, name='say_hello'),
url(r'^post_it/$', post_it, name='post_it'),
url(r'^post_file/$', post_file, name='post_file'),
url(r'^post-name/$', post_name, name='post_name'),
]

View file

@ -0,0 +1,5 @@
<form action="/post-name/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>