Exemple #1
0
    def test_diff_operations__init(self):
        differ = SnapshotDiffer(last_snapshot=None, snapshot=self.snapshot)
        return_token = 'TOKEN'
        differ.init_diff_operations = MagicMock(return_value=return_token)
        operations = differ.diff_operations()

        differ.init_diff_operations.assert_called_once()
        self.assertEqual(operations, return_token)
Exemple #2
0
    def test_diff_operations__resource_delete(self):
        new_snapshot = {
            'system': [
                {
                    "resource_type_name":
                    "资源A",
                    "actions": [{
                        "action_name": "查看",
                        "is_related_resource": True,
                        "action_id": "view"
                    }, {
                        "action_name": "编辑",
                        "is_related_resource": True,
                        "action_id": "edit"
                    }, {
                        "action_name": "删除",
                        "is_related_resource": True,
                        "action_id": "delete"
                    }],
                    "parent_resource_type":
                    "project",
                    "resource_type":
                    "resource_a"
                },
            ],
            'business': [
                {
                    "resource_type_name":
                    "资源C",
                    "actions": [{
                        "action_name": "查看",
                        "is_related_resource": True,
                        "action_id": "view"
                    }, {
                        "action_name": "删除",
                        "is_related_resource": True,
                        "action_id": "delete"
                    }],
                    "parent_resource_type":
                    "",
                    "resource_type":
                    "resource_c"
                },
            ]
        }
        expect = [{
            'operation': 'delete_resource_type',
            'data': {
                'scope_type': 'system',
                'resource_type': 'resource_b'
            }
        }]

        differ = SnapshotDiffer(last_snapshot=self.snapshot,
                                snapshot=new_snapshot)
        operations = differ.diff_operations()

        self.assertEqual(expect, operations)
Exemple #3
0
def forward_func(apps, schema_editor):
    reader = ResourceSnapshotReader(Migration.snapshot_json)
    snapshot = reader.read()
    last_snapshot = None

    if Migration.dependencies:
        loader = MigrationLoader(None, ignore_no_migrations=True)
        last_migration = loader.disk_migrations[Migration.dependencies[0]]
        last_reader = ResourceSnapshotReader(last_migration.snapshot_json)
        last_snapshot = last_reader.read()

    differ = SnapshotDiffer(last_snapshot, snapshot)
    operations = differ.diff_operations()

    migration = ResourceMigration.get_migration(operations)

    migration.apply()
Exemple #4
0
    def test_diff_operations__new_resource(self):
        new_snapshot = {
            'system': [
                {
                    "resource_type_name":
                    "资源A",
                    "actions": [{
                        "action_name": "查看",
                        "is_related_resource": True,
                        "action_id": "view"
                    }, {
                        "action_name": "编辑",
                        "is_related_resource": True,
                        "action_id": "edit"
                    }, {
                        "action_name": "删除",
                        "is_related_resource": True,
                        "action_id": "delete"
                    }],
                    "parent_resource_type":
                    "project",
                    "resource_type":
                    "resource_a"
                },
                {
                    "resource_type_name":
                    "资源B",
                    "actions": [{
                        "action_name": "查看",
                        "is_related_resource": True,
                        "action_id": "view"
                    }, {
                        "action_name": "删除",
                        "is_related_resource": True,
                        "action_id": "delete"
                    }],
                    "parent_resource_type":
                    "project",
                    "resource_type":
                    "resource_b"
                },
                {
                    "resource_type_name":
                    "资源D",
                    "actions": [{
                        "action_name": "查看",
                        "is_related_resource": True,
                        "action_id": "view"
                    }, {
                        "action_name": "删除",
                        "is_related_resource": True,
                        "action_id": "delete"
                    }],
                    "parent_resource_type":
                    "project",
                    "resource_type":
                    "resource_d"
                },
            ],
            'business': [
                {
                    "resource_type_name":
                    "资源C",
                    "actions": [{
                        "action_name": "查看",
                        "is_related_resource": True,
                        "action_id": "view"
                    }, {
                        "action_name": "删除",
                        "is_related_resource": True,
                        "action_id": "delete"
                    }],
                    "parent_resource_type":
                    "",
                    "resource_type":
                    "resource_c"
                },
                {
                    "resource_type_name":
                    "资源E",
                    "actions": [{
                        "action_name": "查看",
                        "is_related_resource": True,
                        "action_id": "view"
                    }, {
                        "action_name": "删除",
                        "is_related_resource": True,
                        "action_id": "delete"
                    }],
                    "parent_resource_type":
                    "",
                    "resource_type":
                    "resource_e"
                },
            ]
        }
        expect = [{
            'data': {
                'resource_types': [{
                    'actions': [{
                        'action_id': 'view',
                        'action_name': '查看',
                        'is_related_resource': True
                    }, {
                        'action_id': 'delete',
                        'action_name': '删除',
                        'is_related_resource': True
                    }],
                    'parent_resource_type':
                    'project',
                    'resource_type':
                    'resource_d',
                    'resource_type_name':
                    '资源D'
                }],
                'scope_type':
                'system'
            },
            'operation': 'batch_upsert_resource_types'
        }, {
            'data': {
                'resource_types': [{
                    'actions': [{
                        'action_id': 'view',
                        'action_name': '查看',
                        'is_related_resource': True
                    }, {
                        'action_id': 'delete',
                        'action_name': '删除',
                        'is_related_resource': True
                    }],
                    'parent_resource_type':
                    '',
                    'resource_type':
                    'resource_e',
                    'resource_type_name':
                    '资源E'
                }],
                'scope_type':
                'business'
            },
            'operation': 'batch_upsert_resource_types'
        }]

        differ = SnapshotDiffer(last_snapshot=self.snapshot,
                                snapshot=new_snapshot)
        operations = differ.diff_operations()

        self.assertEqual(expect, operations)
Exemple #5
0
 def test_diff_operations__resource_not_change(self):
     differ = SnapshotDiffer(last_snapshot=self.snapshot,
                             snapshot=self.snapshot)
     expect = []
     operations = differ.diff_operations()
     self.assertEqual(expect, operations)