test: the suite follows pull-only machines, the proof lock and 200 DPI
The old tests created orders with an automatic machine and moved them without one. They now choose a machine or pass it to the pull, expect the proof lock before any move, and use the 200 DPI clean grade.
This commit is contained in:
@@ -4,13 +4,11 @@ Run inside the API container: python -m tests.users_test
|
||||
"""
|
||||
import secrets
|
||||
import time
|
||||
from decimal import Decimal
|
||||
from urllib.request import Request, urlopen
|
||||
from uuid import uuid4
|
||||
|
||||
from psycopg.types.json import Jsonb
|
||||
|
||||
from app import machines as routing
|
||||
from app import payments, proof
|
||||
from app.core import db
|
||||
from tests.smoke_test import BASE, Client, with_host
|
||||
@@ -61,34 +59,7 @@ def pickup_photo(client, oid, content):
|
||||
return uid
|
||||
|
||||
|
||||
def expected_machine(su, product, metres=1.0):
|
||||
"""What app/machines.py should pick now: the active machine of the product
|
||||
with the fewest metres open, among those with room, else among all."""
|
||||
# Listed in the order they were registered: min() keeps the first of equals, as the routing does.
|
||||
rows = [m for m in su.call('/operator/machines', operator=True)['machines'] if m['active'] and product in m['products']]
|
||||
room = [m for m in rows if m['limit_metres'] is None or m['open_metres'] + metres <= m['limit_metres']]
|
||||
return min(room or rows, key=lambda m: m['open_metres'])['id']
|
||||
|
||||
|
||||
def picking():
|
||||
"""The choice itself: room first, then the fewest metres, then the first registered."""
|
||||
textil = {'snapshot': {'items': [{'mode': 'file', 'billed_metres': '1'}]}}
|
||||
uv = {'snapshot': {'items': [{'mode': 'uvfile', 'billed_metres': '1'}]}}
|
||||
a = {'id': 'a', 'products': ['textil'], 'active': True, 'limit_metres': Decimal('2')}
|
||||
b = {'id': 'b', 'products': ['textil'], 'active': True, 'limit_metres': None}
|
||||
off = {'id': 'off', 'products': ['textil'], 'active': False, 'limit_metres': None}
|
||||
assert routing.pick([a, b], {}, textil)['id'] == 'a'
|
||||
assert routing.pick([off, a, b], {'a': Decimal('0.5')}, textil)['id'] == 'b'
|
||||
# Full: the one with room takes it, even with more metres open.
|
||||
assert routing.pick([a, b], {'a': Decimal('1.5'), 'b': Decimal('9')}, textil)['id'] == 'b'
|
||||
# All full: the least loaded anyway.
|
||||
b_full = {**b, 'limit_metres': Decimal('3')}
|
||||
assert routing.pick([a, b_full], {'a': Decimal('5'), 'b': Decimal('4')}, textil)['id'] == 'b'
|
||||
assert routing.pick([a, b], {}, uv) is None
|
||||
|
||||
|
||||
def run():
|
||||
picking()
|
||||
tag = secrets.token_hex(3)
|
||||
su = Client()
|
||||
su.call('/session')
|
||||
@@ -131,19 +102,22 @@ def run():
|
||||
expected=409)
|
||||
admin.call(f"/operator/users/{mine['id']}/delete", {}, expected=409)
|
||||
|
||||
# Orders other suites left open get their machines first; then each paid
|
||||
# order goes to the machine of its product with the fewest metres open.
|
||||
# No machine at payment: a card waits for a person to choose one or to
|
||||
# pull it into Imprimindo (Dropstar, 2026-11-05).
|
||||
su.call('/operator/board', operator=True)
|
||||
want = {'textil': expected_machine(su, 'textil'), 'uv': expected_machine(su, 'uv')}
|
||||
with db.connect() as c:
|
||||
textil_q, textil_o = order_fixture(c, owner, 'file')
|
||||
uv_q, uv_o = order_fixture(c, owner, 'uvfile')
|
||||
made += [(textil_q, textil_o), (uv_q, uv_o)]
|
||||
board = {o['id']: o for o in su.call('/operator/board', operator=True)['orders']}
|
||||
assert board[str(textil_o)]['machine']['id'] == want['textil'] and not board[str(textil_o)]['machine']['chosen']
|
||||
assert board[str(uv_o)]['machine']['id'] == want['uv'] and not board[str(uv_o)]['machine']['chosen']
|
||||
# On the UV machine of this test, whatever the others carry.
|
||||
assert board[str(uv_o)]['machine'] is None and not board[str(textil_o)]['machine_chosen']
|
||||
assert board[str(uv_o)]['machine'] is None and not board[str(uv_o)]['machine_chosen']
|
||||
# Chosen before printing, each one stays there.
|
||||
su.call(f'/operator/orders/{textil_o}/machine', {'machine_id': textil}, operator=True)
|
||||
su.call(f'/operator/orders/{uv_o}/machine', {'machine_id': uv}, operator=True)
|
||||
board = {o['id']: o for o in su.call('/operator/board', operator=True)['orders']}
|
||||
assert board[str(textil_o)]['machine']['id'] == textil and board[str(textil_o)]['machine']['chosen']
|
||||
assert board[str(uv_o)]['machine']['id'] == uv and board[str(uv_o)]['machine']['chosen']
|
||||
|
||||
# The operator prints: Arte conferida to Imprimindo to Revisão, nothing
|
||||
# else (Dropstar, 2026-10-23). After signing in they say which machine
|
||||
@@ -338,36 +312,36 @@ def run():
|
||||
listed = {m['id']: m for m in admin.call('/operator/machines')['machines']}
|
||||
assert listed[textil2]['operators'] == [] and [o['id'] for o in listed[uv]['operators']] == [op_id], listed[uv]
|
||||
assert next(u for u in admin.call('/operator/users')['users'] if u['id'] == op_id)['machines'] == [uv]
|
||||
# The next order goes to the one with fewer metres open.
|
||||
target = expected_machine(su, 'textil')
|
||||
# A paid order starts with no machine; choosing one by hand is the
|
||||
# only way it gets one before a pull (Dropstar, 2026-11-05).
|
||||
with db.connect() as c:
|
||||
made.append(order_fixture(c, owner, 'file'))
|
||||
second = str(made[-1][1])
|
||||
board = {o['id']: o for o in su.call('/operator/board', operator=True)['orders']}
|
||||
assert board[second]['machine']['id'] == target
|
||||
# Chosen by hand, the order stays there; back to automatic, it is routed again.
|
||||
assert board[second]['machine'] is None and not board[second]['machine_chosen']
|
||||
su.call(f'/operator/orders/{second}/machine', {'machine_id': textil2}, operator=True)
|
||||
board = {o['id']: o for o in su.call('/operator/board', operator=True)['orders']}
|
||||
assert board[second]['machine']['id'] == textil2 and board[second]['machine']['chosen']
|
||||
listed = {m['id']: m for m in su.call('/operator/machines', operator=True)['machines']}
|
||||
assert listed[textil2]['open_orders'] >= 1 and listed[textil2]['states'].get('rec', 0) >= 1
|
||||
|
||||
# A limit: once full, the next order goes to a machine with room.
|
||||
# The limit is shown on the machine; it no longer routes new orders.
|
||||
open_now = listed[textil2]['open_metres']
|
||||
admin.call(f'/operator/machines/{textil2}', {'name': f'Têxtil B {tag}', 'products': ['textil'],
|
||||
'limit_metres': round(open_now + 0.5, 1)})
|
||||
listed = {m['id']: m for m in su.call('/operator/machines', operator=True)['machines']}
|
||||
assert listed[textil2]['limit_metres'] == round(open_now + 0.5, 1)
|
||||
target = expected_machine(su, 'textil')
|
||||
assert target != textil2
|
||||
with db.connect() as c:
|
||||
made.append(order_fixture(c, owner, 'file'))
|
||||
third = str(made[-1][1])
|
||||
board = {o['id']: o for o in su.call('/operator/board', operator=True)['orders']}
|
||||
assert board[third]['machine']['id'] == target
|
||||
assert board[third]['machine'] is None and not board[third]['machine_chosen']
|
||||
su.call(f'/operator/orders/{third}/machine', {'machine_id': textil}, operator=True)
|
||||
admin.call(f'/operator/machines/{textil2}', {'name': f'Têxtil B {tag}', 'products': ['textil'],
|
||||
'limit_metres': 0}, expected=422)
|
||||
su.call(f'/operator/orders/{second}/machine', {'machine_id': None}, operator=True)
|
||||
board = {o['id']: o for o in su.call('/operator/board', operator=True)['orders']}
|
||||
assert not board[second]['machine']['chosen']
|
||||
assert not board[second]['machine_chosen']
|
||||
|
||||
# Números per machine: an order printed for half an hour on its machine, then
|
||||
# finished. It counts the film printed (0,40 m), not the metre billed.
|
||||
@@ -381,10 +355,10 @@ def run():
|
||||
c.execute(f'''INSERT INTO dtf_local.movements(order_id,from_state,to_state,operator,reason,back,created_at)
|
||||
VALUES(%s,%s,%s,'users-test','',false,{at})''', (third, frm, to))
|
||||
after = {m['id']: m for m in su.call('/operator/stats?days=7', operator=True)['machines']}
|
||||
assert after[target]['orders'] == before[target]['orders'] + 1
|
||||
assert round(after[target]['metres'] - before[target]['metres'], 2) == 0.4, (before[target], after[target])
|
||||
assert after[target]['last_finished'] and after[target]['metres_per_hour'] is not None
|
||||
assert after[target]['printing_hours'] >= before[target]['printing_hours'] + 0.49
|
||||
assert after[textil]['orders'] == before[textil]['orders'] + 1
|
||||
assert round(after[textil]['metres'] - before[textil]['metres'], 2) == 0.4, (before[textil], after[textil])
|
||||
assert after[textil]['last_finished'] and after[textil]['metres_per_hour'] is not None
|
||||
assert after[textil]['printing_hours'] >= before[textil]['printing_hours'] + 0.49
|
||||
# Two orders printing at once on one machine: the half hour they share counts
|
||||
# once, so the machine did 1,50 m in 0,5 h, not in 1 h.
|
||||
with db.connect() as c:
|
||||
@@ -494,12 +468,13 @@ def run():
|
||||
assert {o['name'] for o in on_free} == {'Op', 'Marcus'}, on_free
|
||||
designer.call('/operator/me/machine', {'machine_id': free}, expected=403)
|
||||
|
||||
# Switched off, a machine gives its automatic orders to another one.
|
||||
# Switching a machine off no longer re-routes the cards: a person
|
||||
# chooses another machine (Dropstar, 2026-11-05).
|
||||
with db.connect() as c:
|
||||
c.execute('UPDATE dtf_local.orders SET machine_id=%s, machine_chosen=false WHERE id=%s', (textil2, second))
|
||||
admin.call(f'/operator/machines/{textil2}', {'name': f'Têxtil B {tag}', 'products': ['textil'], 'active': False})
|
||||
board = {o['id']: o for o in su.call('/operator/board', operator=True)['orders']}
|
||||
assert board[second]['machine']['id'] not in (None, textil2)
|
||||
assert board[second]['machine']['id'] == textil2
|
||||
|
||||
# Disabled, the operator is out at once; deleted, the account is gone.
|
||||
op_row = users[emails['operador']]
|
||||
@@ -508,12 +483,11 @@ def run():
|
||||
op.call('/operator/board', expected=401)
|
||||
admin.call(f"/operator/users/{op_row['id']}/delete", {})
|
||||
assert emails['operador'] not in {u['email'] for u in admin.call('/operator/users')['users']}
|
||||
# A deleted machine sends its orders back to the automatic choice.
|
||||
# A deleted machine leaves its cards without a machine; a person picks one.
|
||||
su.call(f'/operator/machines/{uv}/delete', {}, operator=True)
|
||||
machines.remove(uv)
|
||||
board = {o['id']: o for o in su.call('/operator/board', operator=True)['orders']}
|
||||
back = board[str(textil_o)]['machine']
|
||||
assert back and back['id'] != uv and not back['chosen']
|
||||
assert board[str(uv_o)]['machine'] is None
|
||||
# Rework (D-02 to D-04): Gestão opens it with its cause, an Administrador
|
||||
# authorizes it, never their own, and only then it counts in Números.
|
||||
emails['gestao'] = f'gestao-{tag}@example.test'
|
||||
|
||||
Reference in New Issue
Block a user