~kkubasik/django/aggregation-branch

« back to all changes in this revision

Viewing changes to tests/modeltests/transactions/models.py

  • Committer: adrian
  • Date: 2006-05-02 01:31:56 UTC
  • Revision ID: vcs-imports@canonical.com-20060502013156-2941fcd40d080649
MERGED MAGIC-REMOVAL BRANCH TO TRUNK. This change is highly backwards-incompatible. Please read http://code.djangoproject.com/wiki/RemovingTheMagic for upgrade instructions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
XXX. Transactions
 
3
 
 
4
Django handles transactions in three different ways. The default is to commit
 
5
each transaction upon a write, but you can decorate a function to get
 
6
commit-on-success behavior. Alternatively, you can manage the transaction
 
7
manually.
 
8
"""
 
9
 
 
10
from django.db import models
 
11
 
 
12
class Reporter(models.Model):
 
13
    first_name = models.CharField(maxlength=30)
 
14
    last_name = models.CharField(maxlength=30)
 
15
    email = models.EmailField()
 
16
 
 
17
    def __repr__(self):
 
18
        return "%s %s" % (self.first_name, self.last_name)
 
19
 
 
20
API_TESTS = """
 
21
>>> from django.db import connection, transaction
 
22
 
 
23
# the default behavior is to autocommit after each save() action
 
24
>>> def create_a_reporter_then_fail(first, last):
 
25
...     a = Reporter(first_name=first, last_name=last)
 
26
...     a.save()
 
27
...     raise Exception("I meant to do that")
 
28
...
 
29
>>> create_a_reporter_then_fail("Alice", "Smith")
 
30
Traceback (most recent call last):
 
31
    ...
 
32
Exception: I meant to do that
 
33
 
 
34
# The object created before the exception still exists
 
35
>>> Reporter.objects.all()
 
36
[Alice Smith]
 
37
 
 
38
# the autocommit decorator works exactly the same as the default behavior
 
39
>>> autocomitted_create_then_fail = transaction.autocommit(create_a_reporter_then_fail)
 
40
>>> autocomitted_create_then_fail("Ben", "Jones")
 
41
Traceback (most recent call last):
 
42
    ...
 
43
Exception: I meant to do that
 
44
 
 
45
# Same behavior as before
 
46
>>> Reporter.objects.all()
 
47
[Alice Smith, Ben Jones]
 
48
 
 
49
# With the commit_on_success decorator, the transaction is only comitted if the
 
50
# function doesn't throw an exception
 
51
>>> committed_on_success = transaction.commit_on_success(create_a_reporter_then_fail)
 
52
>>> committed_on_success("Carol", "Doe")
 
53
Traceback (most recent call last):
 
54
    ...
 
55
Exception: I meant to do that
 
56
 
 
57
# This time the object never got saved
 
58
>>> Reporter.objects.all()
 
59
[Alice Smith, Ben Jones]
 
60
 
 
61
# If there aren't any exceptions, the data will get saved
 
62
>>> def remove_a_reporter():
 
63
...     r = Reporter.objects.get(first_name="Alice")
 
64
...     r.delete()
 
65
...
 
66
>>> remove_comitted_on_success = transaction.commit_on_success(remove_a_reporter)
 
67
>>> remove_comitted_on_success()
 
68
>>> Reporter.objects.all()
 
69
[Ben Jones]
 
70
 
 
71
# You can manually manage transactions if you really want to, but you
 
72
# have to remember to commit/rollback
 
73
>>> def manually_managed():
 
74
...     r = Reporter(first_name="Carol", last_name="Doe")
 
75
...     r.save()
 
76
...     transaction.commit()
 
77
>>> manually_managed = transaction.commit_manually(manually_managed)
 
78
>>> manually_managed()
 
79
>>> Reporter.objects.all()
 
80
[Ben Jones, Carol Doe]
 
81
 
 
82
# If you forget, you'll get bad errors
 
83
>>> def manually_managed_mistake():
 
84
...     r = Reporter(first_name="David", last_name="Davidson")
 
85
...     r.save()
 
86
...     # oops, I forgot to commit/rollback!
 
87
>>> manually_managed_mistake = transaction.commit_manually(manually_managed_mistake)
 
88
>>> manually_managed_mistake()
 
89
Traceback (most recent call last):
 
90
    ...
 
91
TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK
 
92
"""
 
 
b'\\ No newline at end of file'