fixing bug

This commit is contained in:
Cayo Puigdefabregas 2022-11-03 18:19:56 +01:00
parent c14f40fccb
commit 09a0a30386
2 changed files with 28 additions and 2 deletions

View file

@ -310,7 +310,25 @@ class Sync:
def create_placeholder(device: Device): def create_placeholder(device: Device):
"""If the device is new, we need create automaticaly a new placeholder""" """If the device is new, we need create automaticaly a new placeholder"""
if device.binding: 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 return
dict_device = copy.copy(device.__dict__) dict_device = copy.copy(device.__dict__)
dict_device.pop('_sa_instance_state') dict_device.pop('_sa_instance_state')
dict_device.pop('id', None) dict_device.pop('id', None)

View file

@ -1390,6 +1390,7 @@ def test_bug_4028_components(user: UserClient):
s = yaml2json('real-eee-1001pxd.snapshot.12') s = yaml2json('real-eee-1001pxd.snapshot.12')
snap1, _ = user.post(s, res=Snapshot) snap1, _ = user.post(s, res=Snapshot)
dev1 = m.Device.query.filter_by(id=snap1['device']['id']).one() 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] components1 = [c for c in dev1.components]
for c in s['components']: for c in s['components']:
if c['type'] == 'HardDrive': 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() dev2 = m.Device.query.filter_by(id=snap2['device']['id']).one()
components2 = [c for c in dev2.components] 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 components1]
assert '' not in [c.phid() for c in components2] 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