From 47c55f38d41627915767d09205f3de8ef7c847fd Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Thu, 18 Aug 2011 18:44:43 -0300 Subject: [PATCH] Update code to use the new version of pyvows. --- django_pyvows/context.py | 139 ++++++++++++++++++++----------------- vows/context_vows.py | 2 +- vows/model_vows.py | 4 +- vows/sandbox/main/views.py | 1 + vows/server_vows.py | 18 +++-- vows/simple_vows.py | 2 +- vows/template_vows.py | 2 +- vows/url_vows.py | 4 +- vows/view_vows.py | 4 +- 9 files changed, 98 insertions(+), 78 deletions(-) diff --git a/django_pyvows/context.py b/django_pyvows/context.py index 35d333b..752ddb5 100644 --- a/django_pyvows/context.py +++ b/django_pyvows/context.py @@ -9,52 +9,16 @@ # Copyright (c) 2011 Rafael Caricio rafael@caricio.com import os - -from pyvows import Vows - -from django_pyvows.assertions import Url, Model, Template -from django.http import HttpRequest - -class DjangoContext(Vows.Context): - - @classmethod - def _start_environment(cls, settings_path): - if not settings_path: - raise RuntimeError('The settings_path argument is required.') - os.environ['DJANGO_SETTINGS_MODULE'] = settings_path - - def __init__(self, parent): - super(DjangoContext, self).__init__(parent) - if not parent: - DjangoContext._start_environment(self._get_settings()) - - def _get_settings(self): - if 'DJANGO_SETTINGS_MODULE' in os.environ: - return os.environ['DJANGO_SETTINGS_MODULE'] - else: - return 'settings' - - def _url(self, path): - return Url(self, path) - - def _template(self, template_name, context): - return Template(template_name, context) - - def _request(self, **kw): - return HttpRequest(**kw) - - def _model(self, model_class): - return Model(self, model_class) - - import sys import urllib2 -from BaseHTTPServer import HTTPServer -from BaseHTTPServer import BaseHTTPRequestHandler from threading import Thread +from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler +from pyvows import Vows +from django.http import HttpRequest from django.core.handlers.wsgi import WSGIHandler +from django_pyvows.assertions import Url, Model, Template from django_pyvows import version class WSGIRequestHandler(BaseHTTPRequestHandler): @@ -159,41 +123,88 @@ class WSGIRequestHandler(BaseHTTPRequestHandler): if self.parse_request(): return self.run_wsgi() - def log_request(self, code='-', size='-'): + def log_request(self, *args, **kwargs): pass def log_error(self, *args): pass - def log_message(self, format, *args): + def log_message(self, *args): pass -class PyVowsDjangoServer(HTTPServer, object): +class DjangoServer(HTTPServer, object): - def __init__(self, host, port, app): + def __init__(self, host, port): HTTPServer.__init__(self, (host, int(port)), WSGIRequestHandler) - self.app = app + self.app = WSGIHandler() -class DjangoHttpContext(DjangoContext): - def _start_server(self): - self.server = PyVowsDjangoServer(self._get_http_host(), self._get_http_port(), WSGIHandler()) +class DjangoContext(Vows.Context): + + @classmethod + def start_environment(cls, settings_path): + if not settings_path: + raise RuntimeError('The settings_path argument is required.') + os.environ['DJANGO_SETTINGS_MODULE'] = settings_path + + def __init__(self, parent): + super(DjangoContext, self).__init__(parent) + self.port = 8080 + self.host = '127.0.0.1' + self.ignore('get_settings', 'template', 'request', 'model', 'url', 'start_environment', 'get', 'post') + + def setup(self): + DjangoContext.start_environment(self.get_settings()) + + def get_settings(self): + if 'DJANGO_SETTINGS_MODULE' in os.environ: + return os.environ['DJANGO_SETTINGS_MODULE'] + else: + return 'settings' + + def url(self, path): + return Url(self, path) + + def template(self, template_name, context): + return Template(template_name, context) + + def request(self, **kw): + return HttpRequest(**kw) + + def model(self, model_class): + return Model(self, model_class) + + def get(self, url): + return urllib2.urlopen(url) + + def post(self, url, params): + return urllib2.urlopen(url, data=params) + + def get_url(self, path): + ctx = self.parent + while ctx: + if hasattr(ctx, 'get_url'): + return ctx.get_url(path) + ctx = ctx.parent + return None + +class DjangoHTTPContext(DjangoContext): + + def start_server(self): + self.server = DjangoServer(self.host, self.port) self.server.server_activate() - - def _get_http_port(self): - return 8080 - - def _get_http_host(self): - return '127.0.0.1' - - def _get_url(self, path): - return 'http://%s:%d%s' % (self._get_http_host(), self._get_http_port(), path) - - def _get(self, path): def make_response(): - self.server.handle_request() - Thread(target=make_response).start() - return urllib2.urlopen(self._get_url(path)) + while True: + self.server.handle_request() + self.thr = Thread(target=make_response) + self.thr.daemon = True + self.thr.start() - def _post(self, url, params): - pass + def __init__(self, parent): + super(DjangoHTTPContext, self).__init__(parent) + self.port = 8080 + self.host = '127.0.0.1' + self.ignore('start_server', 'port', 'host', 'get_url') + + def get_url(self, path): + return 'http://%s:%d%s' % (self.host, self.port, path) diff --git a/vows/context_vows.py b/vows/context_vows.py index bca8ebd..023a5bd 100644 --- a/vows/context_vows.py +++ b/vows/context_vows.py @@ -16,7 +16,7 @@ from django_pyvows.context import DjangoContext class ContextTest(Vows.Context): def topic(self): - return DjangoContext._start_environment(None) + return DjangoContext.start_environment(None) def should_be_an_error(self, topic): expect(topic).to_be_an_error() diff --git a/vows/model_vows.py b/vows/model_vows.py index b9fc85a..73d78e7 100644 --- a/vows/model_vows.py +++ b/vows/model_vows.py @@ -11,7 +11,7 @@ from pyvows import Vows, expect from django_pyvows.context import DjangoContext -DjangoContext._start_environment("sandbox.settings") +DjangoContext.start_environment("sandbox.settings") from sandbox.main.models import StringModel @@ -21,7 +21,7 @@ class ModelVows(DjangoContext): class MainModel(DjangoContext): def topic(self): - return self._model(StringModel) + return self.model(StringModel) def should_be_cruddable_when_model_only_has_a_string(self, topic): expect(topic).to_be_cruddable() diff --git a/vows/sandbox/main/views.py b/vows/sandbox/main/views.py index dff562b..432d0b8 100644 --- a/vows/sandbox/main/views.py +++ b/vows/sandbox/main/views.py @@ -11,4 +11,5 @@ from django.http import HttpResponse def home(request): + #print request return HttpResponse('hello world') diff --git a/vows/server_vows.py b/vows/server_vows.py index f695523..8a07f07 100644 --- a/vows/server_vows.py +++ b/vows/server_vows.py @@ -8,18 +8,18 @@ # http://www.opensource.org/licenses/mit-license # Copyright (c) 2011 Rafael Caricio rafael@caricio.com -from django_pyvows.context import DjangoContext, DjangoHttpContext +from django_pyvows.context import DjangoContext, DjangoHTTPContext from django_pyvows.assertions import * @Vows.batch -class HttpContextVows(DjangoHttpContext): +class HttpContextVows(DjangoHTTPContext): - def _get_settings(self): + def get_settings(self): return "sandbox.settings" def topic(self): - self._start_server() - return self._get("/") + self.start_server() + return self.get(self.get_url("/")) def the_return_code_should_be_200(self, topic): expect(topic.getcode()).to_equal(200) @@ -27,3 +27,11 @@ class HttpContextVows(DjangoHttpContext): def should_return_the_success_response(self, topic): expect(topic.read()).to_equal("hello world") + class Methods(DjangoContext): + + def topic(self): + return self.get(self.get_url('/?name=rafael')) + + def should_be_possible_to_pass_get_parameters(self, topic): + expect(topic.getcode()).to_equal(200) + diff --git a/vows/simple_vows.py b/vows/simple_vows.py index 81fa9b1..496884a 100644 --- a/vows/simple_vows.py +++ b/vows/simple_vows.py @@ -22,7 +22,7 @@ class DefaultsVows(DjangoContext): def topic(self): del os.environ['DJANGO_SETTINGS_MODULE'] - return self._get_settings() + return self.get_settings() def should_be_using_the_default_settings(self, topic): expect(topic).to_equal('settings') diff --git a/vows/template_vows.py b/vows/template_vows.py index 06ff611..8ee55b6 100644 --- a/vows/template_vows.py +++ b/vows/template_vows.py @@ -22,7 +22,7 @@ class TemplateVows(DjangoContext): class IndexTemplate(DjangoContext): def topic(self): - return self._template('index.html', { + return self.template('index.html', { 'some': 'thing' }) diff --git a/vows/url_vows.py b/vows/url_vows.py index 767fbb0..fdcd053 100644 --- a/vows/url_vows.py +++ b/vows/url_vows.py @@ -13,7 +13,7 @@ from pyvows import Vows, expect from django_pyvows.context import DjangoContext from django_pyvows.assertions import * -DjangoContext._start_environment("sandbox.settings") +DjangoContext.start_environment("sandbox.settings") from sandbox.main.views import home @@ -23,7 +23,7 @@ class UrlVows(DjangoContext): class Home(DjangoContext): def topic(self): - return self._url('^$') + return self.url('^$') def should_have_home_url_mapped(self, topic): expect(topic).to_be_mapped() diff --git a/vows/view_vows.py b/vows/view_vows.py index 73146de..932e888 100644 --- a/vows/view_vows.py +++ b/vows/view_vows.py @@ -11,7 +11,7 @@ from pyvows import Vows, expect from django_pyvows.context import DjangoContext -DjangoContext._start_environment("sandbox.settings") +DjangoContext.start_environment("sandbox.settings") from sandbox.main.views import home @@ -21,7 +21,7 @@ class ViewVows(DjangoContext): class Home(DjangoContext): def topic(self): - return home(self._request()) + return home(self.request()) def should_be_instance_of_http_response(self, topic): expect(topic).to_be_http_response()