feat: a Tiny order can be the client's own SKUs per length, 1 metro plus the rest
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 2m31s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images (push) Successful in 59s

Dropstar's catalogue sells film as SKUs per length, and an order is a sum of
them: 1,30 m is one "1 metro" (DTFIMP.TX.100CM) and one "30 cm"
(DTFIMP.TX.0030CM). The adapter sent one product with the metres as the
quantity. TINY_SKU_<MODE> names a mode's SKU prefix; the order then carries
N times the metre SKU and one 10 cm SKU for the rest, at the price the
customer paid, the last line taking what is left of the item so the order
adds up to the cent. Each code is looked up once by its exact SKU among the
active products. "Testar conexão" and tiny_probe conferir check all ten, and
tiny_probe pedido takes --metros to show a split test order.

Modes without the setting keep their product id: UV and artes avulsas stay
that way until their SKUs are confirmed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-05 10:46:07 -03:00
parent a5c1ca53c0
commit e81b2b1681
7 changed files with 197 additions and 14 deletions

View File

@@ -240,6 +240,70 @@ class TinyStatusTests(FakeTinyCase):
self.assertNotIn('transportador', order_payload(PICKUP, 1))
TEXTIL = {**PAID, 'order': {**PAID['order'], 'items': [
{'mode': 'file', 'grade': 100, 'billed_metres': '2.8', 'unit_cents': 1490, 'total_cents': 4172}]}}
@mock.patch.dict(os.environ, {**PRODUCTS, 'TINY_SKU_TEXTIL_FOLHA': 'DTFIMP.TX'})
class TinySkuTests(FakeTinyCase):
"""Têxtil folha sold as the client's SKUs: 1 metro plus 10 to 90 cm."""
def setUp(self):
super().setUp()
skus = {'DTFIMP.TX.100CM': 200, **{f'DTFIMP.TX.{cm:04d}CM': 200 + cm // 10 for cm in range(10, 100, 10)},
# Same family, different product: must never be taken for 10 cm.
'DTFIMP.TX.010CM': 299}
for code, pid in skus.items():
self.products[str(pid)] = {'id': pid, 'sku': code, 'descricao': code, 'situacao': 'A'}
def test_metres_become_metre_skus_plus_one_centimetre_sku_at_the_price_paid(self):
self.tiny.deliver('k1', TEXTIL)
lines = [(i['produto']['id'], i['quantidade'], i['valorUnitario']) for i in self.orders[0]['itens']]
# 2,80 m at R$ 14,90: 2 × "1 metro" + 1 × "80 cm", adding up to R$ 41,72.
self.assertEqual(lines, [(200, 2, 14.9), (208, 1, 11.92)])
self.assertAlmostEqual(sum(q * v for _, q, v in lines), 41.72)
self.assertIn('2.8 m', self.orders[0]['itens'][0]['infoAdicional'])
# Each code is looked up once, then remembered.
self.tiny.deliver('k2', {**TEXTIL, 'number': 43})
self.assertEqual(self.calls.count(('GET', '/produtos')), 2)
def test_whole_metres_and_small_rests(self):
from app.tiny import sku_lines
item = lambda metres: {'billed_metres': metres, 'unit_cents': 1990}
self.assertEqual(sku_lines(item('1'), 'P'), [('P.100CM', 1, 1990)])
self.assertEqual(sku_lines(item('1.3'), 'P'), [('P.100CM', 1, 1990), ('P.0030CM', 1, 597)])
self.assertEqual(sku_lines(item('12.4'), 'P'), [('P.100CM', 12, 1990), ('P.0040CM', 1, 796)])
with self.assertRaises(TinyError):
sku_lines(item('1.25'), 'P')
def test_a_missing_sku_fails_before_the_order_is_created(self):
del self.products['208']
with self.assertRaisesRegex(TinyError, 'DTFIMP.TX.0080CM'):
self.tiny.deliver('k3', TEXTIL)
self.assertNotIn(('POST', '/pedidos'), self.calls)
def test_other_modes_keep_their_product_id(self):
self.tiny.deliver('k4', PAID)
self.assertEqual(self.orders[0]['itens'][0]['produto'], {'id': 102})
def test_settings_check_and_probe_report_the_skus(self):
from app.tiny import check, required_settings
self.assertNotIn('TINY_PRODUCT_TEXTIL_FOLHA', required_settings())
self.assertIn('TINY_PRODUCT_TEXTIL_AVULSA', required_settings())
self.assertEqual(check(orders=self.tiny)['DTF Têxtil 57 cm · folha montada'], 'ok')
self.products['205']['situacao'] = 'I'
self.tiny.skus.clear()
self.assertIn('DTFIMP.TX.0050CM', check(orders=self.tiny)['DTF Têxtil 57 cm · folha montada'])
from app import tiny_probe
lines = []
tiny_probe.main(['pedido', '--cnpj', '11222333000181', '--email', 'a@example.test',
'--celular', '16999999999', '--metros', '1,3'], orders=self.tiny, out=lines.append)
text = '\n'.join(lines)
self.assertIn('"quantidade": 1', text)
self.assertIn('"valorUnitario": 4.47', text)
self.assertEqual(self.orders, [])
@mock.patch.dict(os.environ, PRODUCTS)
class TinyProbeTests(FakeTinyCase):
"""The supervised console tool run against the fake Tiny."""