Python script that tests POST /api/pedidos/simular against rule H4 (discount and shipping). The expected result is calculated from the written rule, not from the system's current behavior: if the system is wrong, the test case fails.
| File | Description |
|---|---|
h4.py |
Test script |
resultado_h4.csv |
Generated on every run, with PASS/FAIL per case |
- Discount by subtotal: below R$ 500.00, none. From R$ 500.00 to R$ 1,999.99, 5%. From R$ 2,000.00 on, 10%.
- Rounding: to the nearest cent, with half a cent rounding up.
- Shipping by customer address: Imperatriz/MA is free. Other cities in Maranhão (MA) pay R$ 25.00. Other states pay R$ 60.00.
- Free shipping to any destination when the value after discount reaches R$ 1,500.00.
- Total = subtotal − discount + shipping.
- Python 3.8 or higher (https://www.python.org/downloads/). On Windows, check "Add Python to PATH" during installation.
- Internet access to the system under test.
- Seller login code and password (provided with the assessment).
To check your installation, run python3 --version. If that fails, try python --version (Windows: py --version). Use whichever name works in the commands below.
python3 -m pip install requests
Windows: py -m pip install requests
Use the command for your terminal, replacing the placeholders with the real values.
Linux / macOS / WSL / Git Bash:
export CODIGO="your_code"
export SENHA="your_password"
Windows PowerShell:
$env:CODIGO="your_code"
$env:SENHA="your_password"
Windows CMD:
set CODIGO=your_code
set SENHA=your_password
The variables only last for the open terminal window. If you close it, set them again. Never write the code and password into the file or commit them to the repository.
In the folder containing h4.py:
python3 h4.py
Windows: py h4.py
- Terminal: one line per case, with
PASSorFAIL, the expected and the obtained values (discount, shipping, total). resultado_h4.csv: in the same folder, opens in Excel or Google Sheets. Columns:res, destino, prod, qtd, subtotal, desc_esp, frete_esp, total_esp, desc_obt, frete_obt, total_obt(result, destination, product, quantity, subtotal, expected/obtained discount, shipping and total).FAILmeans the system diverged from rule H4 (a bug).
- The script calls
POST /api/resetat startup: it restores the sample data of the environment and erases previous changes. - On every run it creates 3 test customers (Imperatriz/MA, Caxias/MA, São Paulo/SP) with generated valid CPF numbers.
- The expected values use
Decimal(exact cents) with half a cent rounding up. - Only subtotals that a product × quantity can form are tested. The exact values 1,499.99, 1,578.95 and 1,999.99 are not generated and should be tested manually with combined items.
- The script uses 1 item per order and does not compare the
subtotalfield returned by the system.
| Error | Cause and fix |
|---|---|
KeyError: 'CODIGO' |
Credentials not set in this window (step 2). |
KeyError: 'token' |
Wrong code or password. |
ModuleNotFoundError: requests |
Run step 1 with the same Python used in step 3. |
python is not recognized |
Use py (Windows) or python3, or reinstall with "Add to PATH" checked. |
KeyError: 'id', 'preco' or 'estoque' |
JSON field names differ. Check GET /api/produtos and adjust the script. |
| Connection error or timeout | Check your internet or VPN and that the base URL is reachable. |
132 cases (44 subtotals × 3 destinations): 67 PASS and 65 FAIL.
| Cause | Fails | Example |
|---|---|---|
| Discount truncated instead of rounded to the nearest cent | 51 | 503.10 × 5% = 25.155. Expected 25.16, obtained 25.15. |
| Limits of 500.00 and 2,000.00 treated as "greater than" instead of "greater than or equal to" | 6 | 500.00 gets no discount (expected 25.00). 2,000.00 gets 5% = 100.00 (expected 10% = 200.00). |
| Free shipping calculated on the subtotal instead of the value after discount | 14 | Caxias/MA, subtotal 1,500.00: shipping 0 and total 1,425.00. Expected shipping 25.00 and total 1,450.00. |
Six cases have two defects at the same time, which is why the sum exceeds 65. Imperatriz does not show the shipping defect because shipping there is already zero. The remaining cases (below 500.00 and with exact-cent discounts) passed.
The script builds fictional orders, calculates from the rule what should be charged, asks the system and compares.
1. Tools (lines 1 to 3)
csvsaves the spreadsheet,randomdraws digits for the CPF numbers andrequeststalks to the API.DecimalandROUND_HALF_UPgive exact cents and the "half a cent rounds up" rule.ceilfinds the smallest quantity that reaches a boundary value.
2. Configuration (lines 4 to 6)
CODIGOandSENHAcome from environment variables.Bis the base URL.
3. Logging in (lines 7 to 9)
- Opens a session, logs in with
POST /api/loginand attaches the token (Authorization: Bearer) to all following calls. POST /api/resetrestores the sample data so every run starts from the same point.
4. CPF generator (function cpf)
- Draws 9 digits and calculates the 2 check digits. Rule H1 requires a valid and unique CPF, so each run generates new ones.
5. Test customers
DESTdefines 3 destinations, one per shipping tier: Imperatriz (free), another city in MA (R$ 25.00) and another state (R$ 60.00).- Creates one customer per destination (
POST /api/clientes) and stores theidinC, because shipping depends on the customer's address.
6. Rule H4 in code (function esp, the oracle)
- Discount percentage: 10% if subtotal ≥ 2000, 5% if ≥ 500, otherwise 0.
- Discount rounded to the cent with
ROUND_HALF_UP. - Shipping: 0 for Imperatriz, 25 for destinations in MA, 60 for the rest. It becomes 0 if
subtotal − discount ≥ 1500. - Returns
(discount, shipping, total). The expected value comes from the rule, never from the system.
7. Choosing the cases
- Lists the products (
GET /api/produtos) and drops those with stock 0 (H2). - For each product, calculates the quantity that reaches the boundaries 500, 1500, 1578.95, 1999.99 and 2000, and tests that quantity and the one before it (just below and just above the boundary).
- Respects the quantity range from 1 up to the stock (H3) and records each subtotal only once. The value 1578.95 is the smallest subtotal where, after 5%, the value reaches 1,500.
8. Execution and output
- For each destination and subtotal: calculates the expected values, calls
POST /api/pedidos/simular, extracts discount, shipping and total from the response and compares. - A missing field in the response becomes
-1and the case fails. - Prints each case and writes
resultado_h4.csv.