Django 1.8 support / Project revamp #13

Merged
rafaelcaricio merged 20 commits from django-1.8-support into master 2015-07-20 07:57:38 +00:00
5 changed files with 0 additions and 286 deletions
Showing only changes of commit 4e450da041 - Show all commits

View file

@ -1,12 +0,0 @@
#!/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 models import * # NOQA
from templates import * # NOQA

View file

@ -1,119 +0,0 @@
#!/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 pyvows import Vows
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
if isinstance(topic, Model):
topic = topic.model
if isinstance(topic, models.Model):
topic = topic.__class__
if not field_class:
field_class = models.Field
field = topic._meta.get_field(field_name)
assert isinstance(field, field_class), "The '%s.%s' is not an instance of '%s'" % (topic.__name__, field_name, field_class.__name__)
if kwargs:
for attr, value in kwargs.items():
field_value = getattr(field, attr)
assert field_value == value, "The field option '%s' should be equal to '%s', but it's equal to '%s'" % (attr, value, field_value)
@Vows.assertion
def to_be_cruddable(topic, defaults={}):
import django.db.models.fields as fields
instance = __create_or_update_instance(topic, None, defaults)
assert instance, "An instance could not be created for model %s" % topic.model.__name__
retrieved = topic.model.objects.get(id=instance.id)
assert retrieved.id == instance.id, "An instance could not be retrieved for model %s with id %d" % (topic.model.__name__, instance.id)
for key, value in defaults.iteritems():
assert value == getattr(retrieved, key), "The default specified value of '%s' should have been set in the '%s' property of the instance but it was not" % (value, key)
updated = __create_or_update_instance(topic, retrieved, defaults)
for field, value in topic.model._meta._field_cache:
if field.__class__ == fields.AutoField:
continue
if field.name in defaults:
continue
assert getattr(updated, field.name) != getattr(instance, field.name), "The instance should have been updated but the field %s is the same in both the original instance and the updated one (%s)." % (field.name, getattr(updated, field.name))
instance.delete()
object_count = topic.model.objects.count()
assert object_count == 0, "Object should have been deleted, but it wasn't (count: %d)" % object_count
def __create_or_update_instance(topic, instance, defaults):
import django.db.models.fields as fields
arguments = {}
for field, value in topic.model._meta._field_cache:
if field.__class__ == fields.AutoField:
continue
if field.name in defaults:
arguments[field.name] = defaults[field.name]
continue
if field.__class__ == fields.CharField:
__add_char_value_for(field, instance, arguments)
if instance:
for key, value in arguments.iteritems():
setattr(instance, key, value)
instance.save()
return instance
return topic.model.objects.create(**arguments)
def __add_char_value_for(field, instance, arguments):
value = "monty python"
if instance:
value = getattr(instance, field.name) + '2'
if field.max_length:
if instance:
value = value[:len(value) - 2] + '2'
value = (value * field.max_length)[:field.max_length]
arguments[field.name] = value

View file

@ -1,57 +0,0 @@
#!/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 lxml.etree import fromstring
from lxml.cssselect import CSSSelector
from pyvows import Vows, expect
from django.template.loader import render_to_string
class Template(object):
def __init__(self, template_name, context):
self.template_name = template_name
self.context = context
self.doc = None
def load(self):
if self.doc is None:
self.doc = fromstring(render_to_string(self.template_name, self.context))
return self.doc
def select_element(self, selector):
sel = CSSSelector(selector)
return sel(self.load())
def _to_contain(self, selector):
return len(self.select_element(selector)) > 0
def get_text(self, selector):
return "".join((c.text for c in self.select_element(selector)))
def __unicode__(self):
return self.template_name
@Vows.assertion
def to_have_contents_of(topic, expected):
expect(topic.content).to_be_like(expected)
@Vows.assertion
def to_contain(topic, selector):
assert isinstance(topic, Template), "Only django_pyvows.Template items can be verified for mapping"
assert topic._to_contain(selector), "Expected template(%s) to have an element(%s), but it don't have" % \
(unicode(topic), selector)
@Vows.assertion
def not_to_contain(topic, selector):
assert isinstance(topic, Template), "Only django_pyvows.Template items can be verified for mapping"
assert not topic._to_contain(selector), "Expected template(%s) to not have an element(%s), but it have" % \
(unicode(topic), selector)

View file

@ -1,56 +0,0 @@
#!/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 pyvows import expect
from django_pyvows.context import DjangoContext
DjangoContext.start_environment("sandbox.sandbox.settings")
from django.db import models # NOQA
from sandbox.main.models import StringModel # NOQA
class ModelVows(DjangoContext):
class MainModel(DjangoContext):
def topic(self):
return self.model(StringModel)
def should_be_cruddable_when_model_only_has_a_string(self, topic):
expect(topic).to_be_cruddable()
def should_be_cruddable_when_string_passed(self, topic):
expect(topic).to_be_cruddable({
'name': 'something'
})
def should_be_possible_to_use_the_assertion_in_model_instance(self, topic):
expect(topic).to_have_field('name')
def should_have_a_method_to_call(self, topic):
expect(hasattr(topic, '__call__')).to_be_true()
class WhenICreateANewInstance(DjangoContext):
def topic(self, model):
return model()
def should_be_an_instance_of_django_model_class(self, topic):
expect(isinstance(topic, models.Model)).to_be_true()
def should_have_a_name_field(self, topic):
expect(topic).to_have_field('name')
def should_have_a_name_field_as_charfield(self, topic):
expect(topic).to_have_field('name', models.CharField)
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)

View file

@ -1,42 +0,0 @@
#!/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 pyvows import Vows, expect
from django_pyvows.context import DjangoContext
from django_pyvows.assertions import * # NOQA
@Vows.batch
class TemplateVows(DjangoContext):
class IndexTemplate(DjangoContext):
def topic(self):
return self.template('index.html', {
'some': 'thing'
})
def should_have_container_div(self, topic):
expect(topic).to_contain('div.container')
def should_not_have_a_hello_div(self, topic):
expect(topic).Not.to_contain('div.hello')
def should_be_index_file(self, topic):
expect(unicode(topic)).to_equal('index.html')
class Paragraph(DjangoContext):
def topic(self, template):
return template.get_text('p.my-text')
def should_have_paragraph_with_text(self, topic):
expect(topic).to_be_like('some text')