Update code to use the new version of pyvows.

This commit is contained in:
Rafael Caricio 2011-08-18 18:44:43 -03:00
parent f25ae6ebc2
commit 47c55f38d4
9 changed files with 98 additions and 78 deletions

View file

@ -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)

View file

@ -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()

View file

@ -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()

View file

@ -11,4 +11,5 @@
from django.http import HttpResponse
def home(request):
#print request
return HttpResponse('hello world')

View file

@ -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)

View file

@ -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')

View file

@ -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'
})

View file

@ -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()

View file

@ -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()