Update code to use the new version of pyvows.
This commit is contained in:
parent
f25ae6ebc2
commit
47c55f38d4
9 changed files with 98 additions and 78 deletions
|
@ -9,52 +9,16 @@
|
||||||
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
||||||
|
|
||||||
import os
|
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 sys
|
||||||
import urllib2
|
import urllib2
|
||||||
from BaseHTTPServer import HTTPServer
|
|
||||||
from BaseHTTPServer import BaseHTTPRequestHandler
|
|
||||||
from threading import Thread
|
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.core.handlers.wsgi import WSGIHandler
|
||||||
|
|
||||||
|
from django_pyvows.assertions import Url, Model, Template
|
||||||
from django_pyvows import version
|
from django_pyvows import version
|
||||||
|
|
||||||
class WSGIRequestHandler(BaseHTTPRequestHandler):
|
class WSGIRequestHandler(BaseHTTPRequestHandler):
|
||||||
|
@ -159,41 +123,88 @@ class WSGIRequestHandler(BaseHTTPRequestHandler):
|
||||||
if self.parse_request():
|
if self.parse_request():
|
||||||
return self.run_wsgi()
|
return self.run_wsgi()
|
||||||
|
|
||||||
def log_request(self, code='-', size='-'):
|
def log_request(self, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def log_error(self, *args):
|
def log_error(self, *args):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def log_message(self, format, *args):
|
def log_message(self, *args):
|
||||||
pass
|
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)
|
HTTPServer.__init__(self, (host, int(port)), WSGIRequestHandler)
|
||||||
self.app = app
|
self.app = WSGIHandler()
|
||||||
|
|
||||||
class DjangoHttpContext(DjangoContext):
|
|
||||||
|
|
||||||
def _start_server(self):
|
class DjangoContext(Vows.Context):
|
||||||
self.server = PyVowsDjangoServer(self._get_http_host(), self._get_http_port(), WSGIHandler())
|
|
||||||
|
@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()
|
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():
|
def make_response():
|
||||||
|
while True:
|
||||||
self.server.handle_request()
|
self.server.handle_request()
|
||||||
Thread(target=make_response).start()
|
self.thr = Thread(target=make_response)
|
||||||
return urllib2.urlopen(self._get_url(path))
|
self.thr.daemon = True
|
||||||
|
self.thr.start()
|
||||||
|
|
||||||
def _post(self, url, params):
|
def __init__(self, parent):
|
||||||
pass
|
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)
|
||||||
|
|
|
@ -16,7 +16,7 @@ from django_pyvows.context import DjangoContext
|
||||||
class ContextTest(Vows.Context):
|
class ContextTest(Vows.Context):
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
return DjangoContext._start_environment(None)
|
return DjangoContext.start_environment(None)
|
||||||
|
|
||||||
def should_be_an_error(self, topic):
|
def should_be_an_error(self, topic):
|
||||||
expect(topic).to_be_an_error()
|
expect(topic).to_be_an_error()
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
from pyvows import Vows, expect
|
from pyvows import Vows, expect
|
||||||
from django_pyvows.context import DjangoContext
|
from django_pyvows.context import DjangoContext
|
||||||
|
|
||||||
DjangoContext._start_environment("sandbox.settings")
|
DjangoContext.start_environment("sandbox.settings")
|
||||||
|
|
||||||
from sandbox.main.models import StringModel
|
from sandbox.main.models import StringModel
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ class ModelVows(DjangoContext):
|
||||||
class MainModel(DjangoContext):
|
class MainModel(DjangoContext):
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
return self._model(StringModel)
|
return self.model(StringModel)
|
||||||
|
|
||||||
def should_be_cruddable_when_model_only_has_a_string(self, topic):
|
def should_be_cruddable_when_model_only_has_a_string(self, topic):
|
||||||
expect(topic).to_be_cruddable()
|
expect(topic).to_be_cruddable()
|
||||||
|
|
|
@ -11,4 +11,5 @@
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
def home(request):
|
def home(request):
|
||||||
|
#print request
|
||||||
return HttpResponse('hello world')
|
return HttpResponse('hello world')
|
||||||
|
|
|
@ -8,18 +8,18 @@
|
||||||
# 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 django_pyvows.context import DjangoContext, DjangoHttpContext
|
from django_pyvows.context import DjangoContext, DjangoHTTPContext
|
||||||
from django_pyvows.assertions import *
|
from django_pyvows.assertions import *
|
||||||
|
|
||||||
@Vows.batch
|
@Vows.batch
|
||||||
class HttpContextVows(DjangoHttpContext):
|
class HttpContextVows(DjangoHTTPContext):
|
||||||
|
|
||||||
def _get_settings(self):
|
def get_settings(self):
|
||||||
return "sandbox.settings"
|
return "sandbox.settings"
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
self._start_server()
|
self.start_server()
|
||||||
return self._get("/")
|
return self.get(self.get_url("/"))
|
||||||
|
|
||||||
def the_return_code_should_be_200(self, topic):
|
def the_return_code_should_be_200(self, topic):
|
||||||
expect(topic.getcode()).to_equal(200)
|
expect(topic.getcode()).to_equal(200)
|
||||||
|
@ -27,3 +27,11 @@ class HttpContextVows(DjangoHttpContext):
|
||||||
def should_return_the_success_response(self, topic):
|
def should_return_the_success_response(self, topic):
|
||||||
expect(topic.read()).to_equal("hello world")
|
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)
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ class DefaultsVows(DjangoContext):
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
del os.environ['DJANGO_SETTINGS_MODULE']
|
del os.environ['DJANGO_SETTINGS_MODULE']
|
||||||
return self._get_settings()
|
return self.get_settings()
|
||||||
|
|
||||||
def should_be_using_the_default_settings(self, topic):
|
def should_be_using_the_default_settings(self, topic):
|
||||||
expect(topic).to_equal('settings')
|
expect(topic).to_equal('settings')
|
||||||
|
|
|
@ -22,7 +22,7 @@ class TemplateVows(DjangoContext):
|
||||||
class IndexTemplate(DjangoContext):
|
class IndexTemplate(DjangoContext):
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
return self._template('index.html', {
|
return self.template('index.html', {
|
||||||
'some': 'thing'
|
'some': 'thing'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ from pyvows import Vows, expect
|
||||||
from django_pyvows.context import DjangoContext
|
from django_pyvows.context import DjangoContext
|
||||||
from django_pyvows.assertions import *
|
from django_pyvows.assertions import *
|
||||||
|
|
||||||
DjangoContext._start_environment("sandbox.settings")
|
DjangoContext.start_environment("sandbox.settings")
|
||||||
|
|
||||||
from sandbox.main.views import home
|
from sandbox.main.views import home
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ class UrlVows(DjangoContext):
|
||||||
class Home(DjangoContext):
|
class Home(DjangoContext):
|
||||||
|
|
||||||
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()
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
from pyvows import Vows, expect
|
from pyvows import Vows, expect
|
||||||
from django_pyvows.context import DjangoContext
|
from django_pyvows.context import DjangoContext
|
||||||
|
|
||||||
DjangoContext._start_environment("sandbox.settings")
|
DjangoContext.start_environment("sandbox.settings")
|
||||||
|
|
||||||
from sandbox.main.views import home
|
from sandbox.main.views import home
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ class ViewVows(DjangoContext):
|
||||||
class Home(DjangoContext):
|
class Home(DjangoContext):
|
||||||
|
|
||||||
def topic(self):
|
def topic(self):
|
||||||
return home(self._request())
|
return home(self.request())
|
||||||
|
|
||||||
def should_be_instance_of_http_response(self, topic):
|
def should_be_instance_of_http_response(self, topic):
|
||||||
expect(topic).to_be_http_response()
|
expect(topic).to_be_http_response()
|
||||||
|
|
Reference in a new issue