Files
dtf-system/tests/finished_order_test.py
Cauê Faleiros d69e326125 feat: the proof card, machine pull and print file corrections (Dropstar, 05/11)
The proof card owns its final PNG, keeps its parent locked until the seller
answers, enters the queue first, and shares one machine with the parent.
The board no longer assigns a machine at payment; the puller records it.
A stalled print file times out with a card warning and a retry, finished
orders sort by completion, and finals are stored as PNG. The original
download carries the order number (items 0.1, 0.4 to 0.7 and 0.9).
2026-11-06 10:41:20 -03:00

55 lines
2.9 KiB
Python

"""Finalizado sorts by completion, including when an older order finishes last.
Run against the local stack: python3 -m tests.finished_order_test
"""
from urllib.parse import urlencode
from uuid import uuid4
from psycopg.types.json import Jsonb
from app.core import db
from tests.smoke_test import Client
def run():
client = Client()
owner = uuid4()
older_quote, newer_quote = uuid4(), uuid4()
older_order, newer_order = uuid4(), uuid4()
snapshot = {'customer': {'mail': 'finished-order@example.test', 'cnpj': '11222333000181'},
'items': [{'mode': 'file', 'billed_metres': '1.00'}]}
try:
with db.connect() as c:
for quote_id in (older_quote, newer_quote):
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft)
VALUES(%s,%s,%s,'finished-order-test',%s)''',
(quote_id, owner, uuid4(), Jsonb(snapshot)))
for order_id, quote_id, age in ((older_order, older_quote, 2), (newer_order, newer_quote, 1)):
c.execute('''INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment,origin,state,created_at)
VALUES(%s,%s,%s,%s,%s,'site','fin',now()-%s*interval '1 day')''',
(order_id, quote_id, owner, Jsonb(snapshot), Jsonb({'provider': 'teste'}), age))
# The older purchase finishes later; it must be first in Finalizado.
for order_id, minutes in ((older_order, 2), (newer_order, 1)):
c.execute('''INSERT INTO dtf_local.movements(order_id,from_state,to_state,operator,reason,back,created_at)
VALUES(%s,'rev','fin','finished-order-test','',false,now()+%s*interval '1 minute')''',
(order_id, minutes))
board = client.call('/operator/board', operator=True)['orders']
finished = [o for o in board if o['state'] == 'fin']
ids = [o['id'] for o in finished]
assert ids.index(str(older_order)) < ids.index(str(newer_order)), ids
page = client.call('/operator/orders/finished?limit=2', operator=True)['orders']
assert [o['id'] for o in page] == [str(older_order), str(newer_order)], page
cursor = urlencode({'before_finished_at': page[0]['finished_at'], 'before_id': page[0]['id']})
next_page = client.call('/operator/orders/finished?' + cursor + '&limit=1', operator=True)['orders']
assert [o['id'] for o in next_page] == [str(newer_order)], next_page
print('PASS: Finalizado and its cursor use completion time, not order creation time')
finally:
with db.connect() as c:
c.execute('DELETE FROM dtf_local.movements WHERE order_id=ANY(%s)', ([older_order, newer_order],))
c.execute('DELETE FROM dtf_local.orders WHERE id=ANY(%s)', ([older_order, newer_order],))
c.execute('DELETE FROM dtf_local.quotes WHERE id=ANY(%s)', ([older_quote, newer_quote],))
if __name__ == '__main__':
run()