Ejemplo n.º 1
0
    def test_clone_objects(self):
        """
        clone_objects should duplicate an object using the same function (method)
        as ClonableMixin.
        """
        from garage.db import clone_objects

        self._msg("test", "clone_objects", first=True)

        class TestObject(DummyObject):
            pass

        obj = TestObject()
        self.assertEqual(obj.pk, 999)
        self.assertEqual(obj.slug, "foo-999")
        self.assertEqual(str(obj), obj.slug)
        self.assertFalse(hasattr(obj, "clone"))

        new_objs = clone_objects(obj)
        cloned = new_objs[0]

        cloned.update_pk(1001)
        self.assertEqual(cloned.pk, 1001)
        self.assertEqual(cloned.slug, "foo-1001")
        self.assertEqual(str(cloned), cloned.slug)
        self.assertFalse(hasattr(cloned, "clone"))

        self._msg("obj", obj)
        self._msg("obj.slug", obj.slug)
        self._msg("cloned", cloned)
        self._msg("cloned.slug", cloned.slug)

        # check dummy m2m_fields and verify they are present
        self.assertEqual(len(obj._meta.many_to_many), len(obj.m2m_fields))

        for field in obj._meta.many_to_many:
            orig_data = getattr(obj, field.attname)
            self._msg(field.attname, orig_data.all())
            self.assertTrue(field.attname in obj.m2m_fields)

            cloned_data = getattr(cloned, field.attname)
            self.assertEqual(orig_data, cloned_data)
            self.assertTrue(field.attname in cloned.m2m_fields)

            self.assertEqual(orig_data.all(), cloned_data.all())
Ejemplo n.º 2
0
    def test_clone_objects_error(self):
        """
        clone_objects should raise ValueError if object pk is None.
        """
        from garage.db import clone_objects

        self._msg("test", "clone_objects error", first=True)

        class TestObject(DummyObject):
            pass

        obj = TestObject()
        obj.update_pk(None)
        self.assertEqual(obj.pk, None)
        self.assertEqual(obj.slug, "foo-None")
        self.assertEqual(str(obj), obj.slug)
        self.assertFalse(hasattr(obj, "clone"))

        with self.assertRaises(ValueError):
            self._msg("clone method error", "ValueError")
            new_objs = clone_objects(obj)