diff --git a/ereuse_devicehub/resources/device/sync.py b/ereuse_devicehub/resources/device/sync.py index 90403871..613f0b39 100644 --- a/ereuse_devicehub/resources/device/sync.py +++ b/ereuse_devicehub/resources/device/sync.py @@ -310,7 +310,25 @@ class Sync: def create_placeholder(device: Device): """If the device is new, we need create automaticaly a new placeholder""" if device.binding: + for c in device.components: + if c.phid(): + continue + c_dict = copy.copy(c.__dict__) + c_dict.pop('_sa_instance_state') + c_dict.pop('id', None) + c_dict.pop('devicehub_id', None) + c_dict.pop('actions_multiple', None) + c_dict.pop('actions_one', None) + c_placeholder = c.__class__(**c_dict) + c_placeholder.parent = c.parent.binding.device + c.parent = device + component_placeholder = Placeholder( + device=c_placeholder, binding=c, is_abstract=True + ) + db.session.add(c_placeholder) + db.session.add(component_placeholder) return + dict_device = copy.copy(device.__dict__) dict_device.pop('_sa_instance_state') dict_device.pop('id', None) diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index 2231ed08..7f98e398 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -1390,6 +1390,7 @@ def test_bug_4028_components(user: UserClient): s = yaml2json('real-eee-1001pxd.snapshot.12') snap1, _ = user.post(s, res=Snapshot) dev1 = m.Device.query.filter_by(id=snap1['device']['id']).one() + assert m.Placeholder.query.count() * 2 == m.Device.query.count() components1 = [c for c in dev1.components] for c in s['components']: if c['type'] == 'HardDrive': @@ -1400,7 +1401,14 @@ def test_bug_4028_components(user: UserClient): dev2 = m.Device.query.filter_by(id=snap2['device']['id']).one() components2 = [c for c in dev2.components] - # import pdb; pdb.set_trace() - assert '' not in [c.phid() for c in components1] assert '' not in [c.phid() for c in components2] + assert len(components1) == len(components2) + assert m.Placeholder.query.count() == 16 + assert m.Placeholder.query.count() * 2 == m.Device.query.count() + for c in m.Placeholder.query.filter(): + assert c.binding + assert c.device + + for c in m.Device.query.filter(): + assert c.binding or c.placeholder