Permalink
Browse files

Add CharField.

  • Loading branch information...
1 parent 3382504 commit 6f65bdebd1737581408a498d34dbf6d9efe368b2 @iamteem iamteem committed Jun 17, 2010
Showing with 73 additions and 37 deletions.
  1. +23 −1 redisco/models/attributes.py
  2. +3 −1 tests/__init__.py
  3. +47 −35 tests/models.py
View
24 redisco/models/attributes.py
@@ -7,7 +7,7 @@
from redisco.containers import List
from exceptions import FieldValidationError
-__all__ = ['Attribute', 'ListField', 'DateTimeField',
+__all__ = ['Attribute', 'CharField', 'ListField', 'DateTimeField',
'DateField', 'ReferenceField', 'IntegerField',
'FloatField', 'BooleanField', 'Counter', 'ZINDEXABLE']
@@ -109,6 +109,28 @@ def validate_uniqueness(self, instance, val):
return (self.name, 'not unique',)
+class CharField(Attribute):
+
+ def __init__(self, max_length=255, **kwargs):
+ super(CharField, self).__init__(**kwargs)
+ self.max_length = max_length
+
+ def validate(self, instance):
+ errors = []
+ try:
+ super(CharField, self).validate(instance)
+ except FieldValidationError as err:
+ errors.extend(err.errors)
+
+ val = getattr(instance, self.name)
+
+ if val and len(val) > self.max_length:
+ errors.append((self.name, 'exceeds max length'))
+
+ if errors:
+ raise FieldValidationError(errors)
+
+
class BooleanField(Attribute):
def typecast_for_read(self, value):
return bool(int(value))
View
4 tests/__init__.py
@@ -3,7 +3,8 @@
HashTestCase)
from models import (ModelTestCase, DateFieldTestCase, FloatFieldTestCase,
BooleanFieldTestCase, ListFieldTestCase, ReferenceFieldTestCase,
- DateTimeFieldTestCase, CounterFieldTestCase, MutexTestCase,)
+ DateTimeFieldTestCase, CounterFieldTestCase, CharFieldTestCase,
+ MutexTestCase,)
import redisco
redisco.connection_setup(host="localhost", port=6380, db=10)
@@ -23,4 +24,5 @@ def all_tests():
suite.addTest(unittest.makeSuite(CounterFieldTestCase))
suite.addTest(unittest.makeSuite(MutexTestCase))
suite.addTest(unittest.makeSuite(HashTestCase))
+ suite.addTest(unittest.makeSuite(CharFieldTestCase))
return suite
View
82 tests/models.py
@@ -10,8 +10,8 @@
from redisco.models.base import Mutex
class Person(models.Model):
- first_name = models.Attribute()
- last_name = models.Attribute()
+ first_name = models.CharField()
+ last_name = models.CharField()
def full_name(self):
return "%s %s" % (self.first_name, self.last_name,)
@@ -38,7 +38,7 @@ def test_is_new(self):
p = Person(first_name="Darken", last_name="Rahl")
self.assertTrue(p.is_new())
- def test_attributes(self):
+ def test_CharFields(self):
person = Person(first_name="Granny", last_name="Goose")
self.assertEqual("Granny", person.first_name)
self.assertEqual("Goose", person.last_name)
@@ -88,7 +88,7 @@ def test_update(self):
self.assertEqual("Morgan", p.first_name)
self.assertEqual(None, p.last_name)
- def test_default_attribute_val(self):
+ def test_default_CharField_val(self):
class User(models.Model):
views = models.IntegerField(default=199)
liked = models.BooleanField(default=True)
@@ -151,7 +151,7 @@ def test_delete(self):
self.assertEqual(0, self.client.scard('Person:all'))
class Event(models.Model):
- name = models.Attribute(required=True)
+ name = models.CharField(required=True)
created_on = models.DateField(required=True)
from datetime import date
@@ -283,7 +283,7 @@ def test_limit(self):
def test_integer_field(self):
class Character(models.Model):
n = models.IntegerField()
- m = models.Attribute()
+ m = models.CharField()
Character.objects.create(n=1998, m="A")
Character.objects.create(n=3100, m="b")
@@ -321,7 +321,7 @@ def test_filter_date(self):
from datetime import datetime
class Post(models.Model):
- name = models.Attribute()
+ name = models.CharField()
date = models.DateTimeField()
dates = (
@@ -350,7 +350,7 @@ class Post(models.Model):
def test_validation(self):
class Person(models.Model):
- name = models.Attribute(required=True)
+ name = models.CharField(required=True)
p = Person(name="Kokoy")
self.assertTrue(p.is_valid())
@@ -390,10 +390,10 @@ def validate(self):
def test_load_object_from_key(self):
class Schedule(models.Model):
- att = models.Attribute()
+ att = models.CharField()
class PaperType(models.Model):
- att = models.Attribute()
+ att = models.CharField()
assert Schedule.objects.create(att="dinuguan")
assert Schedule.objects.create(att="chicharon")
@@ -430,7 +430,7 @@ def boom():
def test_uniqueness_validation(self):
class Student(models.Model):
- student_id = models.Attribute(unique=True)
+ student_id = models.CharField(unique=True)
student = Student.objects.create(student_id="042231")
self.assert_(student)
@@ -502,17 +502,17 @@ def test_get_or_create(self):
class Event(models.Model):
- name = models.Attribute(required=True)
+ name = models.CharField(required=True)
date = models.DateField(required=True)
class DateFieldTestCase(RediscoTestCase):
- def test_attribute(self):
+ def test_CharField(self):
event = Event(name="Legend of the Seeker Premiere",
date=date(2008, 11, 12))
self.assertEqual(date(2008, 11, 12), event.date)
- def test_saved_attribute(self):
+ def test_saved_CharField(self):
instance = Event.objects.create(name="Legend of the Seeker Premiere",
date=date(2008, 11, 12))
assert instance
@@ -540,7 +540,7 @@ def test_indexes(self):
def test_auto_now(self):
class Report(models.Model):
- title = models.Attribute()
+ title = models.CharField()
created_on = models.DateField(auto_now_add=True)
updated_on = models.DateField(auto_now=True)
@@ -552,16 +552,28 @@ class Report(models.Model):
self.assertEqual(date.today(), r.created_on)
+class CharFieldTestCase(RediscoTestCase):
+
+ def test_max_length(self):
+ class Person(models.Model):
+ name = models.CharField(max_length=20, required=True)
+
+ p = Person(name='The quick brown fox jumps over the lazy dog.')
+
+ self.assertFalse(p.is_valid())
+ self.assert_(('name', 'exceeds max length') in p.errors)
+
+
class Student(models.Model):
- name = models.Attribute(required=True)
+ name = models.CharField(required=True)
average = models.FloatField(required=True)
class FloatFieldTestCase(RediscoTestCase):
- def test_attribute(self):
+ def test_CharField(self):
s = Student(name="Richard Cypher", average=86.4)
self.assertEqual(86.4, s.average)
- def test_saved_attribute(self):
+ def test_saved_CharField(self):
s = Student.objects.create(name="Richard Cypher",
average=3.14159)
assert s
@@ -582,16 +594,16 @@ def test_indexing(self):
class Task(models.Model):
- name = models.Attribute()
+ name = models.CharField()
done = models.BooleanField()
class BooleanFieldTestCase(RediscoTestCase):
- def test_attribute(self):
+ def test_CharField(self):
t = Task(name="Cook dinner", done=False)
assert t.save()
self.assertFalse(t.done)
- def test_saved_attribute(self):
+ def test_saved_CharField(self):
t = Task(name="Cook dinner", done=False)
assert t.save()
@@ -623,7 +635,7 @@ def test_indexing(self):
class ListFieldTestCase(RediscoTestCase):
def test_basic(self):
class Cake(models.Model):
- name = models.Attribute()
+ name = models.CharField()
ingredients = models.ListField(str)
sizes = models.ListField(int)
@@ -657,11 +669,11 @@ class Cake(models.Model):
def test_list_of_reference_fields(self):
class Book(models.Model):
- title = models.Attribute(required=True)
+ title = models.CharField(required=True)
date_published = models.DateField(required=True)
class Author(models.Model):
- name = models.Attribute(required=True)
+ name = models.CharField(required=True)
books = models.ListField(Book)
book = Book.objects.create(
@@ -693,7 +705,7 @@ class Author(models.Model):
def test_lazy_reference_field(self):
class User(models.Model):
- name = models.Attribute()
+ name = models.CharField()
likes = models.ListField('Link')
def likes_link(self, link):
@@ -706,7 +718,7 @@ def likes_link(self, link):
self.save()
class Link(models.Model):
- url = models.Attribute()
+ url = models.CharField()
user = User.objects.create(name="Lion King")
assert Link.objects.create(url="http://google.com")
@@ -729,11 +741,11 @@ class Link(models.Model):
class ReferenceFieldTestCase(RediscoTestCase):
def test_basic(self):
class Word(models.Model):
- placeholder = models.Attribute()
+ placeholder = models.CharField()
class Character(models.Model):
n = models.IntegerField()
- m = models.Attribute()
+ m = models.CharField()
word = models.ReferenceField(Word)
Word.objects.create()
@@ -753,13 +765,13 @@ class Character(models.Model):
def test_lazy_reference_field(self):
class User(models.Model):
- name = models.Attribute()
+ name = models.CharField()
address = models.ReferenceField('Address')
class Address(models.Model):
- street_address = models.Attribute()
- city = models.Attribute()
- zipcode = models.Attribute()
+ street_address = models.CharField()
+ city = models.CharField()
+ zipcode = models.CharField()
address = Address.objects.create(street_address="32/F Redisville",
city="NoSQL City", zipcode="1.3.18")
@@ -779,7 +791,7 @@ def test_basic(self):
from datetime import datetime
n = datetime(2009, 12, 31)
class Post(models.Model):
- title = models.Attribute()
+ title = models.CharField()
date_posted = models.DateTimeField()
created_at = models.DateTimeField(auto_now_add=True)
post = Post(title="First!", date_posted=n)
@@ -793,8 +805,8 @@ class CounterFieldTestCase(RediscoTestCase):
def test_basic(self):
class Post(models.Model):
- title = models.Attribute()
- body = models.Attribute(indexed=False)
+ title = models.CharField()
+ body = models.CharField(indexed=False)
liked = models.Counter()
post = Post.objects.create(title="First!",

0 comments on commit 6f65bde

Please sign in to comment.