Isolate responsabilities

This commit is contained in:
Rafael Caricio 2015-07-19 11:17:55 +02:00
parent 89e44803de
commit c86a0917cb
3 changed files with 38 additions and 27 deletions

View file

@ -13,32 +13,27 @@ import os
import django
from pyvows import Vows
from django_pyvows import http_helpers
from django_pyvows.http_helpers import HttpClientSupport
from django_pyvows.settings_helpers import SettingsOverrideSupport
class DjangoContext(Vows.Context):
class DjangoContext(Vows.Context, HttpClientSupport, SettingsOverrideSupport):
def __init__(self, parent):
super(DjangoContext, self).__init__(parent)
HttpClientSupport.__init__(self)
SettingsOverrideSupport.__init__(self)
self.ignore('start_environment', 'settings_module')
def settings_module(self):
return 'settings'
@classmethod
def start_environment(cls, settings_path):
if not settings_path:
def start_environment(cls, settings_module):
if not settings_module:
raise ValueError('The settings_path argument is required.')
os.environ.update({'DJANGO_SETTINGS_MODULE': settings_path})
os.environ.update({'DJANGO_SETTINGS_MODULE': settings_module})
django.setup()
from django.test.utils import setup_test_environment
setup_test_environment()
def __init__(self, parent):
super(DjangoContext, self).__init__(parent)
self.ignore('start_environment', 'settings', 'get', 'post')
def settings(self, **kwargs):
from django.test.utils import override_settings
return override_settings(**kwargs)
def get(self, *args, **kwargs):
return http_helpers.get(*args, **kwargs)
def post(self, *args, **kwargs):
return http_helpers.post(*args, **kwargs)

View file

@ -2,13 +2,20 @@
# -*- coding: utf-8 -*-
def get(*args, **kwargs):
from django.test.client import Client
client = Client()
return client.get(*args, **kwargs)
class HttpClientSupport(object):
def __init__(self):
self._client = None
self.ignore('get', 'post')
@property
def client(self):
if self._client is None:
from django.test.client import Client # Needs to be lazy loaded due settings config
self._client = Client()
return self._client
def post(*args, **kwargs):
from django.test.client import Client
client = Client()
return client.post(*args, **kwargs)
def get(self, *args, **kwargs):
return self.client.get(*args, **kwargs)
def post(self, *args, **kwargs):
return self.client.post(*args, **kwargs)

View file

@ -0,0 +1,9 @@
class SettingsOverrideSupport(object):
def __init__(self):
self.ignore('settings')
def settings(self, **kwargs):
from django.test.utils import override_settings
return override_settings(**kwargs)