diff --git a/Makefile b/Makefile index 68472d1..8041e69 100644 --- a/Makefile +++ b/Makefile @@ -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/ diff --git a/django_pyvows/__init__.py b/django_pyvows/__init__.py index d3c1c5f..43d9932 100644 --- a/django_pyvows/__init__.py +++ b/django_pyvows/__init__.py @@ -9,3 +9,4 @@ # Copyright (c) 2011 Rafael Caricio rafael@caricio.com from context import DjangoContext # NOQA +from assertions import * # NOQA diff --git a/django_pyvows/assertions.py b/django_pyvows/assertions.py new file mode 100644 index 0000000..e918f0a --- /dev/null +++ b/django_pyvows/assertions.py @@ -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) diff --git a/vows/assertions_vows.py b/vows/assertions_vows.py new file mode 100644 index 0000000..067274c --- /dev/null +++ b/vows/assertions_vows.py @@ -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.') diff --git a/vows/sandbox/main/forms.py b/vows/sandbox/main/forms.py new file mode 100644 index 0000000..3fb866d --- /dev/null +++ b/vows/sandbox/main/forms.py @@ -0,0 +1,5 @@ +from django import forms + + +class NameForm(forms.Form): + your_name = forms.CharField(label='Your name', max_length=100, required=True) diff --git a/vows/sandbox/main/views.py b/vows/sandbox/main/views.py index e334a90..39e10b8 100644 --- a/vows/sandbox/main/views.py +++ b/vows/sandbox/main/views.py @@ -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}) diff --git a/vows/sandbox/sandbox/urls.py b/vows/sandbox/sandbox/urls.py index 6a48525..2ecfaf2 100644 --- a/vows/sandbox/sandbox/urls.py +++ b/vows/sandbox/sandbox/urls.py @@ -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'), ] diff --git a/vows/sandbox/templates/name.html b/vows/sandbox/templates/name.html new file mode 100644 index 0000000..bd1b89c --- /dev/null +++ b/vows/sandbox/templates/name.html @@ -0,0 +1,5 @@ +
+ {% csrf_token %} + {{ form }} + +