(self)
| 16 | |
| 17 | class ForceTests(TestCase): |
| 18 | def test_force_update(self): |
| 19 | c = Counter.objects.create(name="one", value=1) |
| 20 | |
| 21 | # The normal case |
| 22 | c.value = 2 |
| 23 | c.save() |
| 24 | # Same thing, via an update |
| 25 | c.value = 3 |
| 26 | c.save(force_update=True) |
| 27 | |
| 28 | # Won't work because force_update and force_insert are mutually |
| 29 | # exclusive |
| 30 | c.value = 4 |
| 31 | msg = "Cannot force both insert and updating in model saving." |
| 32 | with self.assertRaisesMessage(ValueError, msg): |
| 33 | c.save(force_insert=True, force_update=True) |
| 34 | |
| 35 | # Try to update something that doesn't have a primary key in the first |
| 36 | # place. |
| 37 | c1 = Counter(name="two", value=2) |
| 38 | msg = "Cannot force an update in save() with no primary key." |
| 39 | with self.assertRaisesMessage(ValueError, msg): |
| 40 | with transaction.atomic(): |
| 41 | c1.save(force_update=True) |
| 42 | c1.save(force_insert=True) |
| 43 | |
| 44 | # Won't work because we can't insert a pk of the same value. |
| 45 | c.value = 5 |
| 46 | with self.assertRaises(IntegrityError): |
| 47 | with transaction.atomic(): |
| 48 | c.save(force_insert=True) |
| 49 | |
| 50 | # Trying to update should still fail, even with manual primary keys, if |
| 51 | # the data isn't in the database already. |
| 52 | obj = WithCustomPK(name=1, value=1) |
| 53 | msg = "Forced update did not affect any rows." |
| 54 | # Make sure backward compatibility with DatabaseError is preserved. |
| 55 | exceptions = [DatabaseError, ObjectNotUpdated, WithCustomPK.NotUpdated] |
| 56 | for exception in exceptions: |
| 57 | with ( |
| 58 | self.subTest(exception), |
| 59 | self.assertRaisesMessage(DatabaseError, msg), |
| 60 | transaction.atomic(), |
| 61 | ): |
| 62 | obj.save(force_update=True) |
| 63 | |
| 64 | |
| 65 | class InheritanceTests(TestCase): |
nothing calls this directly
no test coverage detected