Merge pull request #398 from eReuse/bugfix/4028-placeholder-in-new-components

Bugfix/4028 placeholder in new components
This commit is contained in:
cayop 2022-11-07 11:57:03 +01:00 committed by GitHub
commit 3d658a9051
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 0 deletions

View File

@ -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)

View File

@ -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(

View File

@ -1380,3 +1380,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