Files
dtf-system/tests/machine_transfer_test.py

86 lines
5.2 KiB
Python

"""An order in Imprimindo changes machine: the new one resumes from the part
the old was ripping, with the reason kept for the Revisor (Rodada 5, D4)."""
from uuid import uuid4
from app.core import db
from tests.smoke_test import Client
from tests.users_test import PASSWORD, order_fixture, signed_in
def run():
client = Client()
client.call('/session')
tag = uuid4().hex[:6]
machine = client.call('/operator/machines', {'name': f'Transfere A {tag}', 'products': ['textil']},
operator=True)['id']
other = client.call('/operator/machines', {'name': f'Transfere B {tag}', 'products': ['textil']},
operator=True)['id']
email = f'transfere-{tag}@example.test'
operator_id = client.call('/operator/users', {'name': 'Operador', 'email': email, 'role': 'operador',
'password': PASSWORD}, operator=True)['id']
owner = uuid4()
quote = order = None
try:
with db.connect() as c:
quote, order = order_fixture(c, owner, 'file')
upload = uuid4()
c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,complete,
expires_at,scan_state) VALUES(%s,%s,'transfer-test.png',1,%s,'',true,now()+interval '1 day','clean')''',
(upload, owner, f'originals/transfer-test-{upload}'))
c.execute('''UPDATE dtf_local.orders SET state='imp', machine_id=%s, machine_chosen=true
WHERE id=%s''', (machine, order))
for seq, state in enumerate(('consumed', 'consumed', 'written', 'queued'), 1):
c.execute('''INSERT INTO dtf_local.hot_parts(order_id,round,seq,item_index,upload_id,name,state)
VALUES(%s,1,%s,0,%s,%s,%s)''', (order, seq, upload, f'parte {seq}.png', state))
path = f'/operator/orders/{order}/transfer'
# The reason is required, even if only spaces.
answer = client.call(path, {'machine_id': other, 'reason': ' '}, operator=True, expected=422)
assert answer['detail'] == 'Escreva o motivo da transferência.', answer
# It only goes to another machine.
answer = client.call(path, {'machine_id': machine, 'reason': 'quebrou'}, operator=True, expected=409)
assert answer['detail'] == 'O pedido já está nessa máquina.', answer
# With the reason, the old machine unloads what was not taken and the
# new round resumes from part 2, renumbered.
assert client.call(path, {'machine_id': other, 'reason': ' quebrou a cabeça '},
operator=True) == {'ok': True}
with db.connect() as c:
parts = c.execute('''SELECT round,seq,state,name FROM dtf_local.hot_parts
WHERE order_id=%s ORDER BY round,seq''', (order,)).fetchall()
old = {row['seq']: row['state'] for row in parts if row['round'] == 1}
assert old == {1: 'consumed', 2: 'consumed', 3: 'withdraw', 4: 'gone'}, old
new = [row for row in parts if row['round'] == 2]
assert [row['seq'] for row in new] == [1, 2, 3], parts
assert [row['name'] for row in new] == ['parte 2.png', 'parte 3.png', 'parte 4.png'], new
assert all(row['state'] == 'queued' for row in new), new
moved = c.execute('SELECT machine_id,machine_chosen FROM dtf_local.orders WHERE id=%s',
(order,)).fetchone()
assert str(moved['machine_id']) == other and moved['machine_chosen'], moved
row = c.execute('SELECT * FROM dtf_local.machine_transfers WHERE order_id=%s', (order,)).fetchone()
assert row['resumed_from'] == 2 and row['reason'] == 'quebrou a cabeça', row
assert (row['from_name'], row['to_name']) == (f'Transfere A {tag}', f'Transfere B {tag}'), row
mine = next(o for o in client.call('/operator/board', operator=True)['orders'] if o['id'] == str(order))
assert mine['transfers'][0]['resumed_from'] == 2, mine.get('transfers')
# Only Gestão and the administrators move a card between machines.
op = signed_in(email)
op.call(path, {'machine_id': machine, 'reason': 'outra tentativa'}, expected=403)
# Outside Imprimindo there is no transfer.
with db.connect() as c:
c.execute("UPDATE dtf_local.orders SET state='tra' WHERE id=%s", (order,))
answer = client.call(path, {'machine_id': machine, 'reason': 'fora de hora'}, operator=True, expected=409)
assert answer['detail'] == 'Só um pedido em Imprimindo muda de máquina por aqui.', answer
print('PASS: an order in Imprimindo transfers machine with its reason, '
'the old machine unloads what was not taken and the new resumes from part 2')
finally:
if order is not None:
with db.connect() as c:
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (order,))
c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote,))
if operator_id:
client.call(f'/operator/users/{operator_id}/delete', {}, operator=True)
for machine_id in (machine, other):
client.call(f'/operator/machines/{machine_id}/delete', {}, operator=True)
if __name__ == '__main__':
run()