| 1 | #!/usr/bin/env bash |
| 2 | set -e |
| 3 | export PYTHONPYCACHEPREFIX="$(mktemp -d)" |
| 4 | rm -f summary.json |
| 5 | python3 sales_report.py sales.csv summary.json |
| 6 | python3 - <<'PY' |
| 7 | import json |
| 8 | |
| 9 | with open("summary.json") as f: |
| 10 | summary = json.load(f) |
| 11 | assert summary == { |
| 12 | "east": {"total": 970.0, "top": "lanterns"}, |
| 13 | "north": {"total": 160.5, "top": "sails"}, |
| 14 | "west": {"total": 355.0, "top": "anchors"}, |
| 15 | }, summary |
| 16 | assert list(summary) == sorted(summary), "keys must be sorted" |
| 17 | PY |
| 18 | # 泛化检查:换一份数据(含平手)再跑一次 |
| 19 | cat > alt.csv <<'CSV' |
| 20 | region,product,units,unit_price |
| 21 | south,kegs,2,10.0 |
| 22 | south,anchors,1,20.0 |
| 23 | CSV |
| 24 | python3 sales_report.py alt.csv alt.json |
| 25 | python3 - <<'PY' |
| 26 | import json |
| 27 | |
| 28 | with open("alt.json") as f: |
| 29 | alt = json.load(f) |
| 30 | assert alt == {"south": {"total": 40.0, "top": "anchors"}}, alt |
| 31 | PY |
| 32 |