|
| 1 | +"""Testes de v1.37.0: |
| 2 | +
|
| 3 | + - Exclusão permanente de rolo (db.delete_spool + rota admin-only). |
| 4 | + - Cadastro em lote (campo Quantidade cria N rolos idênticos). |
| 5 | +""" |
| 6 | +from conftest import ADMIN_USER, ADMIN_PASS |
| 7 | + |
| 8 | + |
| 9 | +def _make_filament(db): |
| 10 | + return db.create_filament(brand="Acme", material="PLA", family="PLA", |
| 11 | + color_hex="#ffffff") |
| 12 | + |
| 13 | + |
| 14 | +def _make_spool(db, fid, nominal=1000.0): |
| 15 | + return db.create_spool(filament_id=fid, spool_model_id=None, custom_tare_g=200.0, |
| 16 | + nominal_weight_g=nominal, location="", purchase_date="", |
| 17 | + purchase_price=None, notes="") |
| 18 | + |
| 19 | + |
| 20 | +# ── Exclusão permanente (cascade) ──────────────────────────────────────────── |
| 21 | + |
| 22 | +def test_delete_spool_cascades(db): |
| 23 | + """delete_spool apaga o rolo, o histórico de pesagens e a entrada na fila.""" |
| 24 | + fid = _make_filament(db) |
| 25 | + sid = _make_spool(db, fid) |
| 26 | + db.add_weight_reading(sid, gross_weight_g=800.0, tare_weight_g=200.0) |
| 27 | + db.queue_add(sid) |
| 28 | + assert db.get_spool(sid) is not None |
| 29 | + assert db.list_weight_readings(spool_id=sid) |
| 30 | + assert db.is_in_queue(sid) |
| 31 | + |
| 32 | + db.delete_spool(sid) |
| 33 | + |
| 34 | + assert db.get_spool(sid) is None |
| 35 | + assert db.list_weight_readings(spool_id=sid) == [] |
| 36 | + assert not db.is_in_queue(sid) |
| 37 | + |
| 38 | + |
| 39 | +def test_delete_route_admin_only(viewer_client, auth_client, db): |
| 40 | + """POST /spools/<id>/delete é 403 p/ viewer e remove o rolo p/ admin.""" |
| 41 | + fid = _make_filament(db) |
| 42 | + sid = _make_spool(db, fid) |
| 43 | + |
| 44 | + assert viewer_client.post(f"/spools/{sid}/delete", data={}).status_code == 403 |
| 45 | + assert db.get_spool(sid) is not None # viewer não conseguiu apagar |
| 46 | + |
| 47 | + resp = auth_client.post(f"/spools/{sid}/delete", data={}) |
| 48 | + assert resp.status_code == 302 |
| 49 | + assert db.get_spool(sid) is None |
| 50 | + |
| 51 | + |
| 52 | +def test_delete_unknown_spool_404(auth_client): |
| 53 | + assert auth_client.post("/spools/999999/delete", data={}).status_code == 404 |
| 54 | + |
| 55 | + |
| 56 | +# ── Cadastro em lote (Quantidade) ──────────────────────────────────────────── |
| 57 | + |
| 58 | +def test_bulk_create_makes_n_spools(auth_client, db): |
| 59 | + fid = _make_filament(db) |
| 60 | + resp = auth_client.post("/spools/new", data={ |
| 61 | + "filament_id": str(fid), "nominal_weight_g": "1000", "quantity": "3", |
| 62 | + }) |
| 63 | + assert resp.status_code == 302 |
| 64 | + assert "/spools?created=" in resp.headers["Location"] |
| 65 | + assert len(db.list_spools()) == 3 |
| 66 | + |
| 67 | + |
| 68 | +def test_single_create_keeps_detail_flow(auth_client, db): |
| 69 | + """Quantidade 1 (default) mantém o fluxo antigo: vai ao detalhe c/ queue_prompt.""" |
| 70 | + fid = _make_filament(db) |
| 71 | + resp = auth_client.post("/spools/new", data={ |
| 72 | + "filament_id": str(fid), "nominal_weight_g": "1000", "quantity": "1", |
| 73 | + }) |
| 74 | + assert resp.status_code == 302 |
| 75 | + loc = resp.headers["Location"] |
| 76 | + sid = db.list_spools()[0]["id"] |
| 77 | + assert f"/spools/{sid}" in loc and "queue_prompt=1" in loc |
| 78 | + |
| 79 | + |
| 80 | +def test_bulk_create_clamps_quantity(auth_client, db): |
| 81 | + """Quantidade acima do teto (50) é limitada; entrada inválida vira 1.""" |
| 82 | + fid = _make_filament(db) |
| 83 | + auth_client.post("/spools/new", data={ |
| 84 | + "filament_id": str(fid), "nominal_weight_g": "1000", "quantity": "999", |
| 85 | + }) |
| 86 | + assert len(db.list_spools()) == 50 |
| 87 | + |
| 88 | + |
| 89 | +# ── Renderização da UI ─────────────────────────────────────────────────────── |
| 90 | + |
| 91 | +def test_list_shows_delete_and_finish_for_admin(auth_client, db): |
| 92 | + fid = _make_filament(db) |
| 93 | + sid = _make_spool(db, fid) |
| 94 | + html = auth_client.get("/spools").get_data(as_text=True) |
| 95 | + assert f"/spools/{sid}/delete" in html # botão excluir (admin) |
| 96 | + assert f"/spools/{sid}/deactivate" in html # botão finalizar (escrita) |
| 97 | + |
| 98 | + |
| 99 | +def test_list_hides_delete_for_viewer(viewer_client, auth_client, db): |
| 100 | + fid = _make_filament(db) |
| 101 | + sid = _make_spool(db, fid) |
| 102 | + html = viewer_client.get("/spools").get_data(as_text=True) |
| 103 | + assert f"/spools/{sid}/delete" not in html # excluir é só admin |
| 104 | + assert f"/spools/{sid}/deactivate" not in html # finalizar exige escrita |
| 105 | + |
| 106 | + |
| 107 | +def test_new_form_has_quantity_field(auth_client): |
| 108 | + html = auth_client.get("/spools/new").get_data(as_text=True) |
| 109 | + assert 'name="quantity"' in html |
| 110 | + |
| 111 | + |
| 112 | +def test_created_modal_renders(auth_client, db): |
| 113 | + fid = _make_filament(db) |
| 114 | + a, b = _make_spool(db, fid), _make_spool(db, fid) |
| 115 | + html = auth_client.get(f"/spools?created={a},{b}").get_data(as_text=True) |
| 116 | + assert "createdQueueModal" in html |
| 117 | + assert html.count('name="spool_ids"') >= 2 |
0 commit comments