PageRenderTime 364ms CodeModel.GetById 161ms app.highlight 4ms RepoModel.GetById 198ms app.codeStats 0ms

/tests/modeltests/force_insert_update/tests.py

https://code.google.com/p/mango-py/
Python | 38 lines | 21 code | 8 blank | 9 comment | 0 complexity | c09541f9af5ff77ae060172a01c3d4e8 MD5 | raw file
 1from django.db import transaction, IntegrityError, DatabaseError
 2from django.test import TestCase
 3
 4from models import Counter, WithCustomPK
 5
 6
 7class ForceTests(TestCase):
 8    def test_force_update(self):
 9        c = Counter.objects.create(name="one", value=1)
10        # The normal case
11
12        c.value = 2
13        c.save()
14        # Same thing, via an update
15        c.value = 3
16        c.save(force_update=True)
17
18        # Won't work because force_update and force_insert are mutually
19        # exclusive
20        c.value = 4
21        self.assertRaises(ValueError, c.save, force_insert=True, force_update=True)
22
23        # Try to update something that doesn't have a primary key in the first
24        # place.
25        c1 = Counter(name="two", value=2)
26        self.assertRaises(ValueError, c1.save, force_update=True)
27        c1.save(force_insert=True)
28
29        # Won't work because we can't insert a pk of the same value.
30        sid = transaction.savepoint()
31        c.value = 5
32        self.assertRaises(IntegrityError, c.save, force_insert=True)
33        transaction.savepoint_rollback(sid)
34
35        # Trying to update should still fail, even with manual primary keys, if
36        # the data isn't in the database already.
37        obj = WithCustomPK(name=1, value=1)
38        self.assertRaises(DatabaseError, obj.save, force_update=True)