A way to test admin registration and fixes.
This commit is contained in:
parent
657f23f370
commit
b1de2623d1
10 changed files with 66 additions and 6 deletions
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
|||
pyvows:
|
||||
@env PYTHONPATH=$$PYTHONPATH:vows/sandbox/:. pyvows --cover --cover_package=django_pyvows --cover_threshold=100 vows/
|
||||
@env PYTHONPATH=$$PYTHONPATH:vows/sandbox/:. pyvows --cover --cover_package=django_pyvows --cover_threshold=95 vows/
|
||||
|
||||
ci_test:
|
||||
@env PYTHONPATH=$$PYTHONPATH:vows/sandbox/:. python vows/sandbox/manage.py syncdb && pyvows --cover --cover_package=django_pyvows --cover_threshold=100 -r django_pyvows.coverage.xml -x vows/
|
||||
|
|
|
@ -13,12 +13,31 @@ from pyvows import Vows, expect
|
|||
|
||||
class Model(object):
|
||||
def __init__(self, context, model):
|
||||
from django.db import models
|
||||
|
||||
self.context = context
|
||||
if isinstance(model, models.Model):
|
||||
model = model.__class__
|
||||
self.model = model
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
return self.model(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def admin(self):
|
||||
from django.contrib import admin
|
||||
if self.model not in admin.site._registry:
|
||||
raise admin.sites.NotRegistered('The model %s is not registered' % self.model.__name__)
|
||||
return admin.site._registry[self.model]
|
||||
|
||||
@Vows.assertion
|
||||
def to_be_in_admin(topic):
|
||||
from django.contrib import admin
|
||||
try:
|
||||
assert topic.admin
|
||||
except admin.sites.NotRegistered:
|
||||
assert False, "The model %s isn't registered in admin." % topic.model.__name__
|
||||
|
||||
@Vows.assertion
|
||||
def to_have_field(topic, field_name, field_class=None, **kwargs):
|
||||
from django.db import models
|
||||
|
|
|
@ -29,6 +29,12 @@ class DjangoContext(Vows.Context):
|
|||
if not settings_path:
|
||||
raise RuntimeError('The settings_path argument is required.')
|
||||
os.environ['DJANGO_SETTINGS_MODULE'] = settings_path
|
||||
try:
|
||||
from django.conf import settings
|
||||
# to register all models in admin
|
||||
__import__(settings.ROOT_URLCONF)
|
||||
except ImportError:
|
||||
pass
|
||||
settings_tracker.install()
|
||||
|
||||
def __init__(self, parent):
|
||||
|
@ -44,7 +50,7 @@ class DjangoContext(Vows.Context):
|
|||
if 'DJANGO_SETTINGS_MODULE' in os.environ:
|
||||
return os.environ['DJANGO_SETTINGS_MODULE']
|
||||
else:
|
||||
return 'settings'
|
||||
raise NotImplementedError('Should be implemented the "get_settings" method.')
|
||||
|
||||
def url(self, path):
|
||||
return Url(self, path)
|
||||
|
|
|
@ -12,6 +12,8 @@ from pyvows import Vows, expect
|
|||
|
||||
from django_pyvows.context import DjangoContext, DjangoHTTPContext
|
||||
|
||||
DjangoContext.start_environment("sandbox.settings")
|
||||
|
||||
@Vows.batch
|
||||
class ContextTest(Vows.Context):
|
||||
|
||||
|
|
|
@ -55,4 +55,16 @@ class ModelVows(DjangoContext):
|
|||
def should_have_a_name_field_as_charfield_and_max_length_100(self, topic):
|
||||
expect(topic).to_have_field('name', models.CharField, max_length=100)
|
||||
|
||||
class AdminModel(DjangoContext):
|
||||
|
||||
def should_be_registred_in_admin(self, topic):
|
||||
expect(topic).to_be_in_admin()
|
||||
|
||||
class StringModelInAdminOptions(DjangoContext):
|
||||
|
||||
def topic(self, model):
|
||||
return model.admin
|
||||
|
||||
def should_be_listed_by_name(self, topic):
|
||||
expect('name' in topic.list_display).to_be_true()
|
||||
|
||||
|
|
18
vows/sandbox/main/admin.py
Normal file
18
vows/sandbox/main/admin.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
#!/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.contrib import admin
|
||||
|
||||
from sandbox.main.models import StringModel
|
||||
|
||||
class StringModelAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', )
|
||||
|
||||
admin.site.register(StringModel, StringModelAdmin)
|
|
@ -11,8 +11,8 @@
|
|||
from django.conf.urls.defaults import patterns, include, url
|
||||
|
||||
# Uncomment the next two lines to enable the admin:
|
||||
# from django.contrib import admin
|
||||
# admin.autodiscover()
|
||||
from django.contrib import admin
|
||||
admin.autodiscover()
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^$', 'sandbox.main.views.home', name='home'),
|
||||
|
@ -26,5 +26,5 @@ urlpatterns = patterns('',
|
|||
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
|
||||
|
||||
# Uncomment the next line to enable the admin:
|
||||
# url(r'^admin/', include(admin.site.urls)),
|
||||
#url(r'^admin/', include('admin.site.urls')),
|
||||
)
|
||||
|
|
|
@ -16,6 +16,8 @@ from django_pyvows.assertions import *
|
|||
|
||||
TEST_FILE_PATH = abspath(join(dirname(__file__), 'fixtures/the_file.txt'))
|
||||
|
||||
DjangoContext.start_environment("sandbox.settings")
|
||||
|
||||
@Vows.batch
|
||||
class HttpContextVows(DjangoHTTPContext):
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ from pyvows import Vows, expect
|
|||
from django_pyvows.context import DjangoContext, DjangoHTTPContext
|
||||
from django_pyvows.settings_manager import settings_tracker, VowsSettings
|
||||
|
||||
DjangoContext.start_environment("sandbox.settings")
|
||||
|
||||
@Vows.batch
|
||||
class SettingsVows(DjangoContext):
|
||||
|
|
|
@ -16,7 +16,7 @@ from django_pyvows.assertions import *
|
|||
@Vows.batch
|
||||
class TemplateVows(DjangoContext):
|
||||
|
||||
def _get_settings(self):
|
||||
def get_settings(self):
|
||||
return 'sandbox.settings'
|
||||
|
||||
class IndexTemplate(DjangoContext):
|
||||
|
|
Reference in a new issue