feat: Jadlog quotes by the larger of the real and the cubed weight
All checks were successful
Build and deploy / Validate source (push) Successful in 9s
Build and deploy / Integration suite on a real stack (push) Successful in 3m5s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m1s

The client's box is 56 x 14 x 14 cm and weighs 0.30 kg; the film is 0.075 kg
per metre. Jadlog charges the larger of the real and the cubed weight, and this
box is large for its weight, so quoting the real weight alone would price
below what Jadlog charges. JADLOG_CAIXA_CM and JADLOG_CUBAGEM_KG_M3 (both or
neither) give the cubed weight; the quote sends the larger. Until the
contract's factor is set, the Kanban's Frete card says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-01 16:20:44 -03:00
parent b9ebdc0fdb
commit 26c46ccc1d
7 changed files with 89 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ This proves the manual's contract only; app.jadlog_probe must still confirm
it on the client's account. Runs where httpx is installed.
"""
import json
from decimal import Decimal
import os
import unittest
from unittest import mock
@@ -106,6 +107,32 @@ class JadlogFreightTest(unittest.TestCase):
with self.assertRaisesRegex(ValueError, 'CEP destino invalido'):
freight.quote('jadlog', '01310100', '1', 100)
def test_the_box_cubed_weight_wins_when_larger(self):
seen = []
def handler(request):
seen.append(json.loads(request.content)['frete'][0]['peso'])
return httpx.Response(200, json={'frete': [{'vltotal': 30, 'prazo': 3}]})
# 56 x 14 x 14 cm is 0.010976 m³; at 300 kg/m³ that is 3.293 kg.
freight = self.freight(handler, JADLOG_PESO_BASE_KG='0.3', JADLOG_PESO_POR_METRO_KG='0.075',
JADLOG_CAIXA_CM='56 x 14 x 14', JADLOG_CUBAGEM_KG_M3='300')
self.assertEqual(freight.cubed_kg(), Decimal('3.293'))
quote = freight.quote('jadlog', '01310100', '1', 1490)
self.assertEqual((seen[0], quote['weight_kg'], quote['real_kg']), (3.293, '3.293', '0.375'))
# Past the box's cubed weight, the film's real weight is what counts.
freight.quote('jadlog', '01310100', '60', 1490)
self.assertEqual(seen[1], 4.8)
def test_a_box_needs_its_factor(self):
for env in ({'JADLOG_CAIXA_CM': '56x14x14'}, {'JADLOG_CUBAGEM_KG_M3': '300'},
{'JADLOG_CAIXA_CM': '56x14', 'JADLOG_CUBAGEM_KG_M3': '300'},
{'JADLOG_CAIXA_CM': '56x14x14', 'JADLOG_CUBAGEM_KG_M3': '0'}):
with self.subTest(env=env), self.assertRaises(RuntimeError):
self.freight(lambda request: httpx.Response(200), **env)
# Neither: the real weight only, as before the box was known.
self.assertIsNone(self.freight(lambda request: httpx.Response(200)).cubed_kg())
def test_the_weight_has_no_default(self):
with mock.patch.dict(os.environ, {}, clear=True), self.assertRaises(KeyError):
JadlogFreight(client(lambda request: httpx.Response(200)))