Ticket #12030: positive_integer.diff

File positive_integer.diff, 3.7 KB (added by elbarto, 14 years ago)
  • django/db/models/fields/__init__.py

     
    978978
    979979class PositiveIntegerField(IntegerField):
    980980    description = _("Integer")
     981    MAX_POSINT = 4294967295
    981982
    982983    def get_internal_type(self):
    983984        return "PositiveIntegerField"
    984985
    985986    def formfield(self, **kwargs):
    986         defaults = {'min_value': 0}
     987        defaults = {'min_value': 0,
     988                    'max_value': PositiveIntegerField.MAX_POSINT}
    987989        defaults.update(kwargs)
    988990        return super(PositiveIntegerField, self).formfield(**defaults)
    989991
  • tests/regressiontests/model_fields/tests.py

     
    88from django.db.models.fields.files import FieldFile
    99from django.utils import unittest
    1010
    11 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document
     11from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, PosInt, Post, NullBooleanModel, BooleanModel, Document
    1212
    1313# If PIL available, do these tests.
    1414if Image:
     
    299299        b = BigInt.objects.get(value = '10')
    300300        self.assertEqual(b.value, 10)
    301301
     302
     303class PositiveIntegerFieldTests(test.TestCase):
     304    def test_limits(self):
     305        # Ensure that values that are right at the limits can be saved
     306        # and then retrieved whitout corruption.
     307        maxval = 4294967295
     308        minval = 0
     309        PosInt.objects.create(value=maxval)
     310        qs = PosInt.objects.filter(value__gte=maxval)
     311        self.assertEqual(qs.count(), 1)
     312        self.assertEqual(qs[0].value, maxval)
     313        PosInt.objects.create(value=minval)
     314        qs = PosInt.objects.filter(value__lte=minval)
     315        self.assertEqual(qs.count(), 1)
     316        self.assertEqual(qs[0].value, minval)
     317    def test_types(self):
     318        p = PosInt(value=0)
     319        self.assertTrue(isinstance(p.value, (int, long)))
     320        p.save()
     321        self.assertTrue(isinstance(p.value, (int, long)))
     322        p = PosInt.objects.all()[0]
     323        self.assertTrue(isinstance(p.value, (int, long)))
     324
     325    def test_coercing(self):
     326        PosInt.objects.create(value='10')
     327        p = PosInt.objects.get(value = '10')
     328        self.assertEqual(p.value, 10)
     329
     330
    302331class TypeCoercionTests(test.TestCase):
    303332    """
    304333    Test that database lookups can accept the wrong types and convert
  • tests/regressiontests/model_fields/models.py

     
    5555    value = models.BigIntegerField()
    5656    null_value = models.BigIntegerField(null = True, blank = True)
    5757
     58class PosInt(models.Model):
     59    value = models.PositiveIntegerField()
     60    null_value = models.PositiveIntegerField(null = True, blank = True)
     61
    5862class Post(models.Model):
    5963    title = models.CharField(max_length=100)
    6064    body = models.TextField()
  • AUTHORS

     
    421421    Manuel Saelices <msaelices@yaco.es>
    422422    Ivan Sagalaev (Maniac) <http://www.softwaremaniacs.org/>
    423423    Vinay Sajip <vinay_sajip@yahoo.co.uk>
     424    Bartolomé Sánchez <aliaselbarto@gmail.com>
    424425    Kadesarin Sanjek
    425426    Massimo Scamarcia <massimo.scamarcia@gmail.com>
    426427    Paulo Scardine <paulo@scardine.com.br>
Back to Top