import random import warnings import pytest from app import app, SERVICES, URL_PREFIX TOLERANCE = 0.02 @pytest.fixture def client(): app.config['TESTING'] = True with app.test_client() as c: yield c def _call(client, endpoint, service, amount): resp = client.post(f'{URL_PREFIX}/{endpoint}', json={'service': service, 'amount': amount}) assert resp.status_code == 200, resp.get_json() return resp.get_json() def _near(a, b): return abs(float(a) - float(b)) <= TOLERANCE def _check(label, a, b): diff = abs(float(a) - float(b)) if diff > TOLERANCE: warnings.warn(f"{label}: {float(a):.4f} vs {float(b):.4f} (diff={diff:.4f})", stacklevel=2) def test_net_retail_roundtrip(client): net = random.uniform(58, 258) for service_key in SERVICES: r1 = _call(client, 'net_to_retail', service_key, net) retail = float(r1['retail']) r2 = _call(client, 'retail_to_net', service_key, retail) _check(f'[{service_key}] list', r1['list'], r2['list']) _check(f'[{service_key}] net', net, r2['net']) shared_keys = set(r1.get('details', {})) & set(r2.get('details', {})) for k in shared_keys: _check(f'[{service_key}] details.{k}', r1['details'][k], r2['details'][k])