1. from django.db import DatabaseError, IntegrityError, transaction
    
  2. from django.test import TestCase
    
  3. 
    
  4. from .models import Counter, InheritedCounter, ProxyCounter, SubCounter, WithCustomPK
    
  5. 
    
  6. 
    
  7. class ForceTests(TestCase):
    
  8.     def test_force_update(self):
    
  9.         c = Counter.objects.create(name="one", value=1)
    
  10. 
    
  11.         # The normal case
    
  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.         msg = "Cannot force both insert and updating in model saving."
    
  22.         with self.assertRaisesMessage(ValueError, msg):
    
  23.             c.save(force_insert=True, force_update=True)
    
  24. 
    
  25.         # Try to update something that doesn't have a primary key in the first
    
  26.         # place.
    
  27.         c1 = Counter(name="two", value=2)
    
  28.         msg = "Cannot force an update in save() with no primary key."
    
  29.         with self.assertRaisesMessage(ValueError, msg):
    
  30.             with transaction.atomic():
    
  31.                 c1.save(force_update=True)
    
  32.         c1.save(force_insert=True)
    
  33. 
    
  34.         # Won't work because we can't insert a pk of the same value.
    
  35.         c.value = 5
    
  36.         with self.assertRaises(IntegrityError):
    
  37.             with transaction.atomic():
    
  38.                 c.save(force_insert=True)
    
  39. 
    
  40.         # Trying to update should still fail, even with manual primary keys, if
    
  41.         # the data isn't in the database already.
    
  42.         obj = WithCustomPK(name=1, value=1)
    
  43.         msg = "Forced update did not affect any rows."
    
  44.         with self.assertRaisesMessage(DatabaseError, msg):
    
  45.             with transaction.atomic():
    
  46.                 obj.save(force_update=True)
    
  47. 
    
  48. 
    
  49. class InheritanceTests(TestCase):
    
  50.     def test_force_update_on_inherited_model(self):
    
  51.         a = InheritedCounter(name="count", value=1, tag="spam")
    
  52.         a.save()
    
  53.         a.save(force_update=True)
    
  54. 
    
  55.     def test_force_update_on_proxy_model(self):
    
  56.         a = ProxyCounter(name="count", value=1)
    
  57.         a.save()
    
  58.         a.save(force_update=True)
    
  59. 
    
  60.     def test_force_update_on_inherited_model_without_fields(self):
    
  61.         """
    
  62.         Issue 13864: force_update fails on subclassed models, if they don't
    
  63.         specify custom fields.
    
  64.         """
    
  65.         a = SubCounter(name="count", value=1)
    
  66.         a.save()
    
  67.         a.value = 2
    
  68.         a.save(force_update=True)