Ticket #8669: tests.diff

File tests.diff, 3.2 KB (added by Richard Davies <richard.davies@…>, 16 years ago)

Extra tests (actually test changeset 8670, but that's the base of this ticket too)

  • tests/modeltests/get_or_create/models.py

     
    1616    def __unicode__(self):
    1717        return u'%s %s' % (self.first_name, self.last_name)
    1818
     19class ManualPrimaryKeyTest(models.Model):
     20    id = models.IntegerField(primary_key=True)
     21    data = models.CharField(max_length=100)
     22
    1923__test__ = {'API_TESTS':"""
    2024# Acting as a divine being, create an Person.
    2125>>> from datetime import date
     
    6165...     else:
    6266...         print "Fail with %s" % type(e)
    6367Pass
     68
     69# If you specify an existing primary key, but different other fields, then you
     70# will get an error and data will not be updated.
     71>>> m = ManualPrimaryKeyTest(id=1, data='Original')
     72>>> m.save()
     73>>> try:
     74...    m, created = ManualPrimaryKeyTest.objects.get_or_create(id=1, data='Different')
     75... except Exception, e:
     76...    if isinstance(e, IntegrityError):
     77...        print "Pass"
     78...    else:
     79...        print "Fail with %s" % type(e)
     80Pass
     81>>> ManualPrimaryKeyTest.objects.get(id=1).data == 'Original'
     82True
    6483"""}
  • tests/modeltests/create/models.py

     
     1"""
     2XX. create()
     3
     4``create()`` does what it says: it tries to create a new object with the
     5given parameters.
     6"""
     7
     8from django.db import models, IntegrityError
     9
     10class Person(models.Model):
     11    first_name = models.CharField(max_length=100)
     12    last_name = models.CharField(max_length=100)
     13    birthday = models.DateField()
     14
     15    def __unicode__(self):
     16        return u'%s %s' % (self.first_name, self.last_name)
     17
     18class ManualPrimaryKeyTest(models.Model):
     19    id = models.IntegerField(primary_key=True)
     20    data = models.CharField(max_length=100)
     21
     22__test__ = {'API_TESTS':"""
     23# Acting as a divine being, create an Person.
     24>>> from datetime import date
     25>>> p = Person.objects.create(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
     26
     27# Only one Person is in the database at this point.
     28>>> Person.objects.count()
     291
     30
     31# If you don't specify a value or default value for all required fields, you
     32# will get an error.
     33>>> try:
     34...     Person.objects.create(first_name='Tom', last_name='Smith')
     35... except Exception, e:
     36...     if isinstance(e, IntegrityError):
     37...         print "Pass"
     38...     else:
     39...         print "Fail with %s" % type(e)
     40Pass
     41
     42# If you specify an existing primary key, but different other fields, then you
     43# will get an error and data will not be updated.
     44>>> m = ManualPrimaryKeyTest(id=1, data='Original')
     45>>> m.save()
     46>>> try:
     47...    ManualPrimaryKeyTest.objects.create(id=1, data='Different')
     48... except Exception, e:
     49...    if isinstance(e, IntegrityError):
     50...        print "Pass"
     51...    else:
     52...        print "Fail with %s" % type(e)
     53Pass
     54>>> ManualPrimaryKeyTest.objects.get(id=1).data == 'Original'
     55True
     56"""}
Back to Top