Esempio n. 1
0
    def test_force_update(self):
        c = Counter.objects.create(name="one", value=1)

        # The normal case
        c.value = 2
        c.save()
        # Same thing, via an update
        c.value = 3
        c.save(force_update=True)

        # Won't work because force_update and force_insert are mutually
        # exclusive
        c.value = 4
        self.assertRaises(ValueError, c.save, force_insert=True, force_update=True)

        # Try to update something that doesn't have a primary key in the first
        # place.
        c1 = Counter(name="two", value=2)
        self.assertRaises(ValueError, c1.save, force_update=True)
        c1.save(force_insert=True)

        # Won't work because we can't insert a pk of the same value.
        sid = transaction.savepoint()
        c.value = 5
        self.assertRaises(IntegrityError, c.save, force_insert=True)
        transaction.savepoint_rollback(sid)

        # Trying to update should still fail, even with manual primary keys, if
        # the data isn't in the database already.
        obj = WithCustomPK(name=1, value=1)
        self.assertRaises(DatabaseError, obj.save, force_update=True)
Esempio n. 2
0
 def test_required_pk(self):
     # The primary key must be specified, so an error is raised if you
     # try to create an object without it.
     sid = transaction.savepoint()
     self.assertRaises(IntegrityError,
         Employee.objects.create, first_name="Tom", last_name="Smith"
     )
     transaction.savepoint_rollback(sid)
Esempio n. 3
0
 def work():
     mod = Mod.objects.create(fld=1)
     pk = mod.pk
     sid = transaction.savepoint()
     mod1 = Mod.objects.filter(pk=pk).update(fld=20)
     transaction.savepoint_rollback(sid)
     mod2 = Mod.objects.get(pk=pk)
     transaction.commit()
     self.assertEqual(mod2.fld, 1)
Esempio n. 4
0
 def test_unique_pk(self):
     # The primary key must also obviously be unique, so trying to create a
     # new object with the same primary key will fail.
     e = Employee.objects.create(
         employee_code=123, first_name="Frank", last_name="Jones"
     )
     sid = transaction.savepoint()
     self.assertRaises(IntegrityError,
         Employee.objects.create, employee_code=123, first_name="Fred", last_name="Jones"
     )
     transaction.savepoint_rollback(sid)
Esempio n. 5
0
    def test_multiple_o2o(self):
        # One-to-one fields still work if you create your own primary key
        o1 = ManualPrimaryKey(primary_key="abc123", name="primary")
        o1.save()
        o2 = RelatedModel(link=o1, name="secondary")
        o2.save()

        # You can have multiple one-to-one fields on a model, too.
        x1 = MultiModel(link1=self.p1, link2=o1, name="x1")
        x1.save()
        self.assertEqual(repr(o1.multimodel), '<MultiModel: Multimodel x1>')
        # This will fail because each one-to-one field must be unique (and
        # link2=o1 was used for x1, above).
        sid = transaction.savepoint()
        mm = MultiModel(link1=self.p2, link2=o1, name="x1")
        self.assertRaises(IntegrityError, mm.save)
        transaction.savepoint_rollback(sid)
Esempio n. 6
0
 def save(self, must_create=False):
     """
     Saves the current session data to the database. If 'must_create' is
     True, a database error will be raised if the saving operation doesn't
     create a *new* entry (as opposed to possibly updating an existing
     entry).
     """
     obj = Session(
         session_key=self._get_or_create_session_key(),
         session_data=self.encode(self._get_session(no_load=must_create)),
         expire_date=self.get_expiry_date(),
     )
     using = router.db_for_write(Session, instance=obj)
     sid = transaction.savepoint(using=using)
     try:
         obj.save(force_insert=must_create, using=using)
     except IntegrityError:
         if must_create:
             transaction.savepoint_rollback(sid, using=using)
             raise CreateError
         raise