Ticket #8669: 8669_tests.v2.diff

File 8669_tests.v2.diff, 3.4 KB (added by Richard Davies <richard.davies@…>, 16 years ago)

Have learnt that I got it wrong in #8739. Here's a revised set of tests incorporating that learning. There's also good extra tests in the v2.diffs inside tickets #8739 and #8740, which could also go in already

  • 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, transaction, 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...     sid = transaction.savepoint()
     35...     Person.objects.create(first_name='Tom', last_name='Smith')
     36...     transaction.savepoint.commit(sid)
     37... except Exception, e:
     38...     if isinstance(e, IntegrityError):
     39...         transaction.savepoint_rollback(sid)
     40...         print "Pass"
     41...     else:
     42...         print "Fail with %s" % type(e)
     43Pass
     44
     45# If you specify an existing primary key, but different other fields, then you
     46# will get an error and data will not be updated.
     47>>> m = ManualPrimaryKeyTest(id=1, data='Original')
     48>>> m.save()
     49>>> try:
     50...    sid = transaction.savepoint()
     51...    ManualPrimaryKeyTest.objects.create(id=1, data='Different')
     52...    transaction.savepoint_commit(sid)
     53... except Exception, e:
     54...    if isinstance(e, IntegrityError):
     55...        transaction.savepoint_rollback(sid)
     56...        print "Pass"
     57...    else:
     58...        print "Fail with %s" % type(e)
     59Pass
     60>>> ManualPrimaryKeyTest.objects.get(id=1).data == 'Original'
     61True
     62"""}
Back to Top