| 1 |
from django.db import DatabaseError, IntegrityError, models, transaction |
| 2 |
from django.test import TestCase |
| 3 |
|
| 4 |
from .models import ( |
| 5 |
Counter, |
| 6 |
DiamondSubSubCounter, |
| 7 |
InheritedCounter, |
| 8 |
OtherSubCounter, |
| 9 |
ProxyCounter, |
| 10 |
SubCounter, |
| 11 |
SubSubCounter, |
| 12 |
WithCustomPK, |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
class ForceTests(TestCase): |
| 17 |
def test_force_update(self): |
| 18 |
c = Counter.objects.create(name="one", value=1) |
| 19 |
|
| 20 |
# The normal case |
| 21 |
c.value = 2 |
| 22 |
c.save() |
| 23 |
# Same thing, via an update |
| 24 |
c.value = 3 |
| 25 |
c.save(force_update=True) |
| 26 |
|
| 27 |
# Won't work because force_update and force_insert are mutually |
| 28 |
# exclusive |
| 29 |
c.value = 4 |
| 30 |
msg = "Cannot force both insert and updating in model saving." |
| 31 |
with self.assertRaisesMessage(ValueError, msg): |
| 32 |
c.save(force_insert=True, force_update=True) |
| 33 |
|
| 34 |
# Try to update something that doesn't have a primary key in the first |
| 35 |
# place. |
| 36 |
c1 = Counter(name="two", value=2) |
| 37 |
msg = "Cannot force an update in save() with no primary key." |
| 38 |
with self.assertRaisesMessage(ValueError, msg): |
| 39 |
with transaction.atomic(): |
| 40 |
c1.save(force_update=True) |
| 41 |
c1.save(force_insert=True) |
| 42 |
|
| 43 |
# Won't work because we can't insert a pk of the same value. |
| 44 |
c.value = 5 |
| 45 |
with self.assertRaises(IntegrityError): |
| 46 |
with transaction.atomic(): |
| 47 |
c.save(force_insert=True) |
| 48 |
|
| 49 |
# Trying to update should still fail, even with manual primary keys, if |
| 50 |
# the data isn't in the database already. |
| 51 |
obj = WithCustomPK(name=1, value=1) |
| 52 |
msg = "Forced update did not affect any rows." |
| 53 |
with self.assertRaisesMessage(DatabaseError, msg): |
| 54 |
with transaction.atomic(): |
| 55 |
obj.save(force_update=True) |
| 56 |
|
| 57 |
|
| 58 |
class InheritanceTests(TestCase): |
| 59 |
def test_force_update_on_inherited_model(self): |
| 60 |
a = InheritedCounter(name="count", value=1, tag="spam") |
| 61 |
a.save() |
| 62 |
a.save(force_update=True) |
| 63 |
|
| 64 |
def test_force_update_on_proxy_model(self): |
| 65 |
a = ProxyCounter(name="count", value=1) |
| 66 |
a.save() |
| 67 |
a.save(force_update=True) |
| 68 |
|
| 69 |
def test_force_update_on_inherited_model_without_fields(self): |
| 70 |
""" |
| 71 |
Issue 13864: force_update fails on subclassed models, if they don't |
| 72 |
specify custom fields. |
| 73 |
""" |
| 74 |
a = SubCounter(name="count", value=1) |
| 75 |
a.save() |
| 76 |
a.value = 2 |
| 77 |
a.save(force_update=True) |
| 78 |
|
| 79 |
|
| 80 |
class ForceInsertInheritanceTests(TestCase): |
| 81 |
def test_force_insert_not_bool_or_tuple(self): |
| 82 |
msg = "force_insert must be a bool or tuple." |
| 83 |
with self.assertRaisesMessage(TypeError, msg), transaction.atomic(): |
| 84 |
Counter().save(force_insert=1) |
| 85 |
with self.assertRaisesMessage(TypeError, msg), transaction.atomic(): |
| 86 |
Counter().save(force_insert="test") |
| 87 |
with self.assertRaisesMessage(TypeError, msg), transaction.atomic(): |
| 88 |
Counter().save(force_insert=[]) |
| 89 |
|
| 90 |
def test_force_insert_not_model(self): |
| 91 |
msg = f"Invalid force_insert member. {object!r} must be a model subclass." |
| 92 |
with self.assertRaisesMessage(TypeError, msg), transaction.atomic(): |
| 93 |
Counter().save(force_insert=(object,)) |
| 94 |
instance = Counter() |
| 95 |
msg = f"Invalid force_insert member. {instance!r} must be a model subclass." |
| 96 |
with self.assertRaisesMessage(TypeError, msg), transaction.atomic(): |
| 97 |
Counter().save(force_insert=(instance,)) |
| 98 |
|
| 99 |
def test_force_insert_not_base(self): |
| 100 |
msg = "Invalid force_insert member. SubCounter must be a base of Counter." |
| 101 |
with self.assertRaisesMessage(TypeError, msg): |
| 102 |
Counter().save(force_insert=(SubCounter,)) |
| 103 |
|
| 104 |
def test_force_insert_false(self): |
| 105 |
with self.assertNumQueries(3): |
| 106 |
obj = SubCounter.objects.create(pk=1, value=0) |
| 107 |
with self.assertNumQueries(2): |
| 108 |
SubCounter(pk=obj.pk, value=1).save() |
| 109 |
obj.refresh_from_db() |
| 110 |
self.assertEqual(obj.value, 1) |
| 111 |
with self.assertNumQueries(2): |
| 112 |
SubCounter(pk=obj.pk, value=2).save(force_insert=False) |
| 113 |
obj.refresh_from_db() |
| 114 |
self.assertEqual(obj.value, 2) |
| 115 |
with self.assertNumQueries(2): |
| 116 |
SubCounter(pk=obj.pk, value=3).save(force_insert=()) |
| 117 |
obj.refresh_from_db() |
| 118 |
self.assertEqual(obj.value, 3) |
| 119 |
|
| 120 |
def test_force_insert_false_with_existing_parent(self): |
| 121 |
parent = Counter.objects.create(pk=1, value=1) |
| 122 |
with self.assertNumQueries(2): |
| 123 |
SubCounter.objects.create(pk=parent.pk, value=2) |
| 124 |
|
| 125 |
def test_force_insert_parent(self): |
| 126 |
with self.assertNumQueries(3): |
| 127 |
SubCounter(pk=1, value=1).save(force_insert=True) |
| 128 |
# Force insert a new parent and don't UPDATE first. |
| 129 |
with self.assertNumQueries(2): |
| 130 |
SubCounter(pk=2, value=1).save(force_insert=(Counter,)) |
| 131 |
with self.assertNumQueries(2): |
| 132 |
SubCounter(pk=3, value=1).save(force_insert=(models.Model,)) |
| 133 |
|
| 134 |
def test_force_insert_with_grandparent(self): |
| 135 |
with self.assertNumQueries(4): |
| 136 |
SubSubCounter(pk=1, value=1).save(force_insert=True) |
| 137 |
# Force insert parents on all levels and don't UPDATE first. |
| 138 |
with self.assertNumQueries(3): |
| 139 |
SubSubCounter(pk=2, value=1).save(force_insert=(models.Model,)) |
| 140 |
with self.assertNumQueries(3): |
| 141 |
SubSubCounter(pk=3, value=1).save(force_insert=(Counter,)) |
| 142 |
# Force insert only the last parent. |
| 143 |
with self.assertNumQueries(4): |
| 144 |
SubSubCounter(pk=4, value=1).save(force_insert=(SubCounter,)) |
| 145 |
|
| 146 |
def test_force_insert_with_existing_grandparent(self): |
| 147 |
# Force insert only the last child. |
| 148 |
grandparent = Counter.objects.create(pk=1, value=1) |
| 149 |
with self.assertNumQueries(4): |
| 150 |
SubSubCounter(pk=grandparent.pk, value=1).save(force_insert=True) |
| 151 |
# Force insert a parent, and don't force insert a grandparent. |
| 152 |
grandparent = Counter.objects.create(pk=2, value=1) |
| 153 |
with self.assertNumQueries(3): |
| 154 |
SubSubCounter(pk=grandparent.pk, value=1).save(force_insert=(SubCounter,)) |
| 155 |
# Force insert parents on all levels, grandparent conflicts. |
| 156 |
grandparent = Counter.objects.create(pk=3, value=1) |
| 157 |
with self.assertRaises(IntegrityError), transaction.atomic(): |
| 158 |
SubSubCounter(pk=grandparent.pk, value=1).save(force_insert=(Counter,)) |
| 159 |
|
| 160 |
def test_force_insert_diamond_mti(self): |
| 161 |
# Force insert all parents. |
| 162 |
with self.assertNumQueries(4): |
| 163 |
DiamondSubSubCounter(pk=1, value=1).save( |
| 164 |
force_insert=(Counter, SubCounter, OtherSubCounter) |
| 165 |
) |
| 166 |
with self.assertNumQueries(4): |
| 167 |
DiamondSubSubCounter(pk=2, value=1).save(force_insert=(models.Model,)) |
| 168 |
# Force insert parents, and don't force insert a common grandparent. |
| 169 |
with self.assertNumQueries(5): |
| 170 |
DiamondSubSubCounter(pk=3, value=1).save( |
| 171 |
force_insert=(SubCounter, OtherSubCounter) |
| 172 |
) |
| 173 |
grandparent = Counter.objects.create(pk=4, value=1) |
| 174 |
with self.assertNumQueries(4): |
| 175 |
DiamondSubSubCounter(pk=grandparent.pk, value=1).save( |
| 176 |
force_insert=(SubCounter, OtherSubCounter), |
| 177 |
) |
| 178 |
# Force insert all parents, grandparent conflicts. |
| 179 |
grandparent = Counter.objects.create(pk=5, value=1) |
| 180 |
with self.assertRaises(IntegrityError), transaction.atomic(): |
| 181 |
DiamondSubSubCounter(pk=grandparent.pk, value=1).save( |
| 182 |
force_insert=(models.Model,) |
| 183 |
) |