Skip to content

Commit 5b2ec25

Browse files
committed
[1.2.X] Migrated the force_insert_update tests. Thanks to Alex Gaynor.
Backport of r13782 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13799 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent ae26534 commit 5b2ec25

File tree

2 files changed

+38
-51
lines changed

2 files changed

+38
-51
lines changed

tests/modeltests/force_insert_update/models.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,3 @@ class Counter(models.Model):
1111
class WithCustomPK(models.Model):
1212
name = models.IntegerField(primary_key=True)
1313
value = models.IntegerField()
14-
15-
__test__ = {"API_TESTS": """
16-
>>> c = Counter.objects.create(name="one", value=1)
17-
18-
# The normal case
19-
>>> c.value = 2
20-
>>> c.save()
21-
22-
# Same thing, via an update
23-
>>> c.value = 3
24-
>>> c.save(force_update=True)
25-
26-
# Won't work because force_update and force_insert are mutually exclusive
27-
>>> c.value = 4
28-
>>> c.save(force_insert=True, force_update=True)
29-
Traceback (most recent call last):
30-
...
31-
ValueError: Cannot force both insert and updating in model saving.
32-
33-
# Try to update something that doesn't have a primary key in the first place.
34-
>>> c1 = Counter(name="two", value=2)
35-
>>> c1.save(force_update=True)
36-
Traceback (most recent call last):
37-
...
38-
ValueError: Cannot force an update in save() with no primary key.
39-
40-
>>> c1.save(force_insert=True)
41-
42-
# Won't work because we can't insert a pk of the same value.
43-
>>> sid = transaction.savepoint()
44-
>>> c.value = 5
45-
>>> try:
46-
... c.save(force_insert=True)
47-
... except Exception, e:
48-
... if isinstance(e, IntegrityError):
49-
... print "Pass"
50-
... else:
51-
... print "Fail with %s" % type(e)
52-
Pass
53-
>>> transaction.savepoint_rollback(sid)
54-
55-
# Trying to update should still fail, even with manual primary keys, if the
56-
# data isn't in the database already.
57-
>>> obj = WithCustomPK(name=1, value=1)
58-
>>> obj.save(force_update=True)
59-
Traceback (most recent call last):
60-
...
61-
DatabaseError: ...
62-
63-
"""
64-
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from django.db import transaction, IntegrityError, DatabaseError
2+
from django.test import TestCase
3+
4+
from models import Counter, WithCustomPK
5+
6+
7+
class 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)

0 commit comments

Comments
 (0)