Ejemplo n.º 1
0
    def test_copymodeldata_no_overwrite(self):
        """ Test the CopyModelData operation with overwrite_existing=False. """

        # Create the TestModel instances, with OtherModel instances with matching PKs only for
        # odd PKs
        for x in range(1, 5):
            TestModel.objects.create(id=x, name="name_which_will_be_copied")
            if x % 2:
                OtherModel.objects.create(id=x, name="original_name")

        # Just for sanity, check that the entities exist
        testmodel_entities = self.get_entities()
        othermodel_entities = self.get_entities(model=OtherModel)
        self.assertEqual(len(testmodel_entities), 4)
        self.assertEqual(len(othermodel_entities), 2)

        operation = operations.CopyModelData("testmodel",
                                             "djangae",
                                             "othermodel",
                                             overwrite_existing=False)
        self.start_operation(operation)
        self.process_task_queues()

        # We now expect there to be 4 OtherModel entities, but only the ones which didn't exist
        # already (i.e. the ones with even PKs) should have the name copied from the TestModel
        othermodel_entities = self.get_entities(model=OtherModel)
        self.assertEqual(len(othermodel_entities), 4)
        for entity in othermodel_entities:
            if entity.key().id() % 2:
                self.assertEqual(entity["name"], "original_name")
            else:
                self.assertEqual(entity["name"], "name_which_will_be_copied")
Ejemplo n.º 2
0
    def test_copymodeldata_overwrite(self):
        """ Test the CopyModelData operation with overwrite_existing=True. """

        # Create the TestModel instances, with OtherModel instances with matching PKs
        for x in range(2):
            instance = TestModel.objects.create(
                name="name_which_will_be_copied")
            OtherModel.objects.create(name="original_name", id=instance.pk)

        # Just for sanity, check that the entities exist
        testmodel_entities = self.get_entities()
        othermodel_entities = self.get_entities(model=OtherModel)
        self.assertEqual(len(testmodel_entities), 2)
        self.assertEqual(len(othermodel_entities), 2)

        operation = operations.CopyModelData("testmodel",
                                             "djangae",
                                             "othermodel",
                                             overwrite_existing=True)
        self.start_operation(operation)
        self.process_task_queues()

        # The OtherModel entities should now all have a name lof "name_which_will_be_copied"
        othermodel_entities = self.get_entities(model=OtherModel)
        self.assertTrue(
            all(entity["name"] == "name_which_will_be_copied"
                for entity in othermodel_entities))