"""A card has no machine until pull, and a proof gives its machine to its parent.""" from uuid import uuid4 from psycopg.types.json import Jsonb from app.core import db from tests.smoke_test import Client from tests.users_test import order_fixture def final_stub(c, oid, owner): uid = uuid4() c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,complete, expires_at,scan_state) VALUES(%s,%s,'machine-test.png',1,%s,'',true,now()+interval '1 day','clean')''', (uid, owner, f'machine-test/{uid}')) c.execute('''INSERT INTO dtf_local.order_files(id,order_id,upload_id,item_index,kind,note,created_by) VALUES(%s,%s,%s,0,'final','machine test','test')''', (uuid4(), oid, uid)) def run(): client = Client() client.call('/session') tag = uuid4().hex[:6] machine = client.call('/operator/machines', {'name': f'Pull {tag}', 'products': ['textil']}, operator=True)['id'] other = client.call('/operator/machines', {'name': f'Other {tag}', 'products': ['textil']}, operator=True)['id'] owner = uuid4() with db.connect() as c: _, oid = order_fixture(c, owner, 'file') final_stub(c, oid, owner) c.execute("UPDATE dtf_local.orders SET state='tra' WHERE id=%s", (oid,)) board = next(o for o in client.call('/operator/board', operator=True)['orders'] if o['id'] == str(oid)) assert board['machine'] is None, board['machine'] moved = client.call(f'/operator/orders/{oid}/move', {'state': 'imp', 'version': 0, 'machine_id': machine}, operator=True) assert str(moved['machine_id']) == machine shown = next(o for o in client.call('/operator/board', operator=True)['orders'] if o['id'] == str(oid)) assert shown['machine_pulled']['by'] and shown['machine_pulled']['at'], shown assert client.call(f'/operator/orders/{oid}/history', operator=True)[-1]['machine'] == f'Pull {tag}' with db.connect() as c: _, parent = order_fixture(c, owner, 'file') parent_row = c.execute('SELECT snapshot FROM dtf_local.orders WHERE id=%s', (parent,)).fetchone() child = uuid4() c.execute('''INSERT INTO dtf_local.orders(id,owner,snapshot,payment,origin,parent_id,state) VALUES(%s,%s,%s,%s,'site',%s,'tra')''', (child, owner, Jsonb({**parent_row['snapshot'], 'proof_of': 1}), Jsonb({'provider': 'prova'}), parent)) final_stub(c, child, owner) shown = {o['id']: o for o in client.call('/operator/board', operator=True)['orders']} assert shown[str(parent)]['machine'] is None and shown[str(child)]['machine'] is None client.call(f'/operator/orders/{child}/move', {'state': 'imp', 'version': 0, 'machine_id': machine}, operator=True) shown = {o['id']: o for o in client.call('/operator/board', operator=True)['orders']} assert shown[str(parent)]['machine']['id'] == machine == shown[str(child)]['machine']['id'] with db.connect() as c: c.execute("UPDATE dtf_local.orders SET state='rev' WHERE id=%s", (child,)) client.call(f'/operator/orders/{child}/proof-answer', {'result': 'ok'}, operator=True) locked = client.call(f'/operator/orders/{parent}/machine', {'machine_id': other}, operator=True, expected=409) assert 'mesma máquina' in locked['detail'], locked print('PASS: no machine at creation; pull records operator and time; proof and parent share one machine') if __name__ == '__main__': run()