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/scripts/datastorage_orphans.py b/scripts/datastorage_orphans.py index 72148e94..46ce382b 100644 --- a/scripts/datastorage_orphans.py +++ b/scripts/datastorage_orphans.py @@ -30,6 +30,9 @@ def clone_device(device): new_device.devicehub_id = old_devicehub_id device.devicehub_id = None new_device.owner = device.owner + if device.parent and device.parent.binding: + new_device.parent = device.parent.binding.device + db.session.add(new_device) placeholder = Placeholder( diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index a1c711b8..9e408f26 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -1381,3 +1381,35 @@ def test_system_uuid_motherboard(user: UserClient): snap2, _ = user.post(s, res=Snapshot, status=422) txt = "We have detected that a there is a device in your inventory" assert txt in snap2['message'][0] + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_bug_4028_components(user: UserClient): + """Tests when we have one computer and then we change the disk, then + the new disk need to have placeholder too.""" + 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': + c['serialNumber'] = 'E2024242CV86MF' + + s['uuid'] = str(uuid4()) + snap2, _ = user.post(s, res=Snapshot) + dev2 = m.Device.query.filter_by(id=snap2['device']['id']).one() + components2 = [c for c in dev2.components] + + 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