From 74d10e1951b46c959586e1d1df8480edbd684f1b Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 3 Nov 2022 17:49:01 +0100 Subject: [PATCH 1/4] add test --- tests/test_snapshot.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index a4753943..fc754ad9 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -1380,3 +1380,27 @@ 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() + 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] + + # import pdb; pdb.set_trace() + + assert '' not in [c.phid() for c in components1] + assert '' not in [c.phid() for c in components2] \ No newline at end of file From c14f40fccbf6dd04e1401b1cd8eee574c0509196 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 3 Nov 2022 17:49:18 +0100 Subject: [PATCH 2/4] add test --- tests/test_snapshot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index fc754ad9..2231ed08 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -1403,4 +1403,4 @@ def test_bug_4028_components(user: UserClient): # import pdb; pdb.set_trace() assert '' not in [c.phid() for c in components1] - assert '' not in [c.phid() for c in components2] \ No newline at end of file + assert '' not in [c.phid() for c in components2] From 09a0a303863915649a450524a654c6ab667d6231 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 3 Nov 2022 18:19:56 +0100 Subject: [PATCH 3/4] fixing bug --- ereuse_devicehub/resources/device/sync.py | 18 ++++++++++++++++++ tests/test_snapshot.py | 12 ++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) 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 From d8541d917ccf9101ec71ffab7a8279e6da551a9e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 7 Nov 2022 11:25:01 +0100 Subject: [PATCH 4/4] fix orohans storage with parents --- scripts/datastorage_orphans.py | 3 +++ 1 file changed, 3 insertions(+) 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(