PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/modeltests/force_insert_update/tests.py

https://code.google.com/p/mango-py/
Python | 38 lines | 21 code | 8 blank | 9 comment | 0 complexity | c09541f9af5ff77ae060172a01c3d4e8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.db import transaction, IntegrityError, DatabaseError
  2. from django.test import TestCase
  3. from models import Counter, WithCustomPK
  4. class ForceTests(TestCase):
  5. def test_force_update(self):
  6. c = Counter.objects.create(name="one", value=1)
  7. # The normal case
  8. c.value = 2
  9. c.save()
  10. # Same thing, via an update
  11. c.value = 3
  12. c.save(force_update=True)
  13. # Won't work because force_update and force_insert are mutually
  14. # exclusive
  15. c.value = 4
  16. self.assertRaises(ValueError, c.save, force_insert=True, force_update=True)
  17. # Try to update something that doesn't have a primary key in the first
  18. # place.
  19. c1 = Counter(name="two", value=2)
  20. self.assertRaises(ValueError, c1.save, force_update=True)
  21. c1.save(force_insert=True)
  22. # Won't work because we can't insert a pk of the same value.
  23. sid = transaction.savepoint()
  24. c.value = 5
  25. self.assertRaises(IntegrityError, c.save, force_insert=True)
  26. transaction.savepoint_rollback(sid)
  27. # Trying to update should still fail, even with manual primary keys, if
  28. # the data isn't in the database already.
  29. obj = WithCustomPK(name=1, value=1)
  30. self.assertRaises(DatabaseError, obj.save, force_update=True)