diff --git a/ereuse_devicehub/resources/device/sync.py b/ereuse_devicehub/resources/device/sync.py index ecf5690e..df02d451 100644 --- a/ereuse_devicehub/resources/device/sync.py +++ b/ereuse_devicehub/resources/device/sync.py @@ -88,6 +88,7 @@ class Sync: # We only want to perform Add/Remove to not new components actions = self.add_remove(db_device, not_new_components) db_device.components = db_components + self.create_placeholder(db_device) return db_device, actions def execute_register_component( @@ -235,7 +236,6 @@ class Sync: else: # Device is new and tags are not linked to a device device.tags.clear() # We don't want to add the transient dummy tags db.session.add(device) - self.create_placeholder(device) db_device = device db_device.tags |= ( tags # Union of tags the device had plus the (potentially) new ones @@ -283,15 +283,26 @@ class Sync: @staticmethod def create_placeholder(device: Device): """If the device is new, we need create automaticaly a new placeholder""" - # import pdb; pdb.set_trace() + if device.binding: + return dict_device = copy.copy(device.__dict__) dict_device.pop('_sa_instance_state') + dict_device.pop('id', None) + dict_device.pop('devicehub_id', None) + dict_device.pop('actions_multiple', None) + dict_device.pop('actions_one', None) + dict_device.pop('components', None) dev_placeholder = device.__class__(**dict_device) for c in device.components: 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 = dev_placeholder + c.parent = device component_placeholder = Placeholder(device=c_placeholder, binding=c) db.session.add(c_placeholder) db.session.add(component_placeholder) diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index a3183533..a6738711 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -1311,5 +1311,42 @@ def test_snapshot_check_tests_lite(user: UserClient): bodyLite, res = user.post(snapshot_lite, uri="/api/inventory/") assert res.status_code == 201 - SnapshotsLog.query.all() + assert SnapshotsLog.query.count() == 1 + m.Device.query.filter_by(devicehub_id=bodyLite['dhid']).one() + + +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_placeholder(user: UserClient): + """This check the structure of one placeholder generated automatically by a snapshot""" + snapshot_lite = file_json( + 'test_lite/2022-4-13-19-5_user@dhub.com_b27dbf43-b88a-4505-ae27-10de5a95919e.json' + ) + + bodyLite, res = user.post(snapshot_lite, uri="/api/inventory/") + assert res.status_code == 201 dev = m.Device.query.filter_by(devicehub_id=bodyLite['dhid']).one() + assert dev.placeholder is None + assert dev.binding.phid == '12' + assert len(dev.binding.device.components) == 11 + assert len(dev.components) == 11 + assert dev.binding.device.placeholder == dev.binding + assert dev.components != dev.binding.device.components + assert dev.binding.device.actions == [] + + +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_placeholder_actions(user: UserClient): + """This test the actions of a placeholder of one snapshot""" + s = yaml2json('erase-sectors.snapshot') + snap1, _ = user.post(s, res=Snapshot) + + dev = m.Device.query.filter_by(id=snap1['device']['id']).one() + assert dev.placeholder is None + assert dev.binding.phid == '4' + assert len(dev.binding.device.components) == 3 + assert len(dev.components) == 3 + assert dev.binding.device.placeholder == dev.binding + assert dev.components != dev.binding.device.components + assert dev.binding.device.actions == [] + assert len(dev.components[0].actions) == 3 + assert len(dev.binding.device.components[0].actions) == 0