Пример #1
0
    def test_create_skip_cdms(self):
        """
        obj = ChildClass(parent_fk=...)
        obj.save(skip_cdms=True)

        should not hit cdms.
        """
        obj = SimpleObj(
            name='simple obj',
            fk_obj=self.parent_obj
        )
        obj.save(skip_cdms=True)

        self.assertNoAPICalled()
Пример #2
0
    def test_success(self):
        """
        When calling obj.save(skip_cdms=True), changes should only happen in local, not in cdms.
        """
        obj = SimpleObj()
        obj.name = "simple obj"

        self.assertEqual(obj.cdms_pk, "")
        self.assertEqual(SimpleObj.objects.skip_cdms().count(), 0)
        obj.save(skip_cdms=True)
        self.assertEqual(SimpleObj.objects.skip_cdms().count(), 1)
        self.assertEqual(obj.cdms_pk, "")

        self.assertNoAPICalled()
Пример #3
0
    def test_exception_triggers_rollback(self):
        """
        In case of exceptions during cdms calls, no changes should be reflected in the db.
        """
        self.mocked_cdms_api.create.side_effect = Exception

        obj = SimpleObj()
        obj.name = "simple obj"

        self.assertEqual(SimpleObj.objects.skip_cdms().count(), 0)
        self.assertRaises(Exception, obj.save)
        self.assertEqual(SimpleObj.objects.skip_cdms().count(), 0)

        self.assertAPINotCalled(["list", "update", "delete", "get"])
Пример #4
0
    def test_create(self):
        """
        obj = ChildClass(parent_fk=...)
        obj.save()

        should create the object in local and in cdms with the related `parent_fk` set.
        """
        self.mocked_cdms_api.create.side_effect = mocked_cdms_create(
            create_data={
                'SimpleId': 'simple id',
                'ModifiedOn': timezone.now()
            }
        )

        obj = SimpleObj(
            name='simple obj',
            fk_obj=self.parent_obj
        )
        obj.save()
        self.assertEqual(SimpleObj.objects.skip_cdms().count(), 1)

        self.assertAPICreateCalled(
            SimpleObj, kwargs={
                'data': {
                    'Name': 'simple obj',
                    'DateTimeField': None,
                    'IntField': None,
                    'FKField': {
                        'Id': self.parent_obj.cdms_pk
                    }
                }
            }
        )
        self.assertAPINotCalled(['list', 'update', 'delete', 'get'])

        # reload obj and check cdms_pk and modified
        obj = SimpleObj.objects.skip_cdms().get(pk=obj.pk)
        self.assertEqual(obj.fk_obj.cdms_pk, self.parent_obj.cdms_pk)
Пример #5
0
    def test_success(self):
        """
        obj.save() should create a new obj in local and cdms if it doesn't exist.
        """
        modified_on = (timezone.now() - datetime.timedelta(days=1)).replace(microsecond=0)
        cdms_id = "brand new id"
        self.mocked_cdms_api.create.side_effect = mocked_cdms_create(
            create_data={"SimpleId": cdms_id, "ModifiedOn": modified_on}
        )

        obj = SimpleObj()
        obj.name = "simple obj"
        obj.dt_field = datetime.datetime(2016, 1, 1).replace(tzinfo=datetime.timezone.utc)
        obj.int_field = 10

        self.assertEqual(obj.cdms_pk, "")
        self.assertEqual(SimpleObj.objects.skip_cdms().count(), 0)
        obj.save()
        self.assertEqual(SimpleObj.objects.skip_cdms().count(), 1)
        self.assertEqual(obj.cdms_pk, cdms_id)
        self.assertEqual(obj.modified, modified_on)

        self.assertAPICreateCalled(
            SimpleObj,
            kwargs={
                "data": {
                    "Name": "simple obj",
                    "DateTimeField": "/Date(1451606400000)/",
                    "IntField": 10,
                    "FKField": None,
                }
            },
        )
        self.assertAPINotCalled(["list", "update", "delete", "get"])

        # reload obj and check cdms_pk and modified
        obj = SimpleObj.objects.skip_cdms().get(pk=obj.pk)
        self.assertEqual(obj.cdms_pk, cdms_id)
        self.assertEqual(obj.modified, modified_on)