From 5a431f84227cb9651427a21ebca6f75c2a01935c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Wed, 14 Oct 2026 13:38:46 -0300 Subject: [PATCH] fix: Tiny's test order names a new customer as the real orders do The supervised test order would have created Dropstar as a Tiny contact named after its e-mail: the console tool did not look the CNPJ up in the company registry as the worker does. It now uses the same lookup, and the preview says which name the new contact will get. Co-Authored-By: Claude Opus 5.5 --- app/tiny_probe.py | 13 ++++++++++--- tests/test_tiny.py | 7 +++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/tiny_probe.py b/app/tiny_probe.py index 2558b74..70bd93f 100644 --- a/app/tiny_probe.py +++ b/app/tiny_probe.py @@ -101,8 +101,13 @@ def pedido(orders, payload, confirm, out, wait=time.sleep): cnpj = payload['order']['customer']['cnpj'] contacts = orders.request('GET', '/contatos', params={'cpfCnpj': cnpj, 'limit': 5}).get('itens') or [] known = [c for c in contacts if ''.join(ch for ch in str(c.get('cpfCnpj') or '') if ch.isdigit()) == cnpj] - out(f"Contato {cnpj}: " + (f"já existe no Tiny (id {known[0]['id']})" if known - else 'não existe; será criado com o e-mail como nome')) + if known: + out(f"Contato {cnpj}: já existe no Tiny (id {known[0]['id']})") + else: + # Named as the worker names a new customer: the company in the CNPJ registry. + company = orders.names(cnpj) if orders.names else None + name = tiny.contact_payload(payload['order'], company)['nome'] + out(f'Contato {cnpj}: não existe; será criado como "{name}"') out('Pedido que será enviado:') out(json.dumps(tiny.order_payload(payload, known[0]['id'] if known else 0, product_id=orders.product_id, pickup_id=orders.pickup_id()), @@ -148,7 +153,9 @@ def main(argv=None, orders=None, out=print): args = parser.parse_args(argv) if orders is None: load_secret_files() - orders = tiny.TinyOrders(auth=tiny.TinyAuth()) + from .cnpj import company + # The same names for new contacts as the worker uses (app/worker.py). + orders = tiny.TinyOrders(auth=tiny.TinyAuth(), names=company) try: if args.command == 'produtos': produtos(orders, args.termo, out) diff --git a/tests/test_tiny.py b/tests/test_tiny.py index 6c283d9..fc8ce4c 100644 --- a/tests/test_tiny.py +++ b/tests/test_tiny.py @@ -336,6 +336,13 @@ class TinySkuTests(FakeTinyCase): text = '\n'.join(lines) self.assertIn('"quantidade": 1', text) self.assertIn('"valorUnitario": 4.47', text) + # A new contact is previewed with the name it will get. + self.assertIn('será criado como "a@example.test"', text) + self.tiny.names = lambda cnpj: {'legal': 'EMPRESA TESTE LTDA', 'trade': 'Teste'} + lines.clear() + tiny_probe.main(['pedido', '--cnpj', '11222333000182', '--email', 'a@example.test', + '--celular', '16999999999'], orders=self.tiny, out=lines.append) + self.assertIn('será criado como "EMPRESA TESTE LTDA"', '\n'.join(lines)) self.assertEqual(self.orders, [])