Implement in Python 3.11 or newer with FastAPI, served by uvicorn, over SQLite.
The product is an executable named `fxrates` at the application root.
The completed application provides a POSIX-compatible `bin/test.sh` that runs its own pytest suite
from the application root and exits zero only when every test passes.
The suite covers each command, each load rule, each endpoint, the effective-date rule, rounding, and each error status,
against a real temporary database, with `fetch` stubbed.
| `rates` | `date` text `YYYY-MM-DD`, `currency` text, `rate` text | unique (`date`, `currency`) |
A rate is stored exactly as the ECB publishes it, as text. `0.943` stays `0.943`, never `0.9430
` and never a float. Arithmetic on rates uses Python's `decimal` module.
`DATABASE_URL` names the SQLite database file, optionally prefixed `sqlite://`. When it is unset,
`DATABASE_PATH` is used, then `data/app.db`.
All database access goes through the shipped persistence library and the relational layer generated
from `DATABASE.md`. Application code never opens a database driver directly.
`fxrates init` | Drops the `rates` table if present and creates it empty. | Nothing.
`fxrates load ` | Loads an ECB history file (rules below). | `dates,rates`, then the number of dates and of rates stored from the file.
`fxrates fetch` | Downloads `https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip`, extracts `eurofxref-hist.csv`, and loads it as `load` does. | As `load`.
`fxrates serve [--port ]` | Serves the HTTP API on `127.0.0.1:` with uvicorn until stopped. Default port `8000`. | Nothing required.
The file is CSV. The header's first field is `Date`, and every other field is a three-letter
uppercase currency code. The ECB writes a trailing comma on every line, and the resulting
empty last field is ignored.
Each row is a date (`YYYY-MM-DD`, a real calendar date, not repeated within the file) followed
by one value per currency. The value `N/A` means no rate, and nothing is stored for it. Any
other value must match `[0-9]+(\.[0-9]+)?` and be nonzero.
**A rejected file changes nothing.** The whole file is checked before the first write. A row
with the wrong field count, an invalid or repeated date, or an invalid value rejects the file.
A rate for a (`date`, `currency`) that is already stored replaces it, so loading the same file
twice leaves the database unchanged.
The reported `dates` counts the dates with at least one stored rate, and `rates` counts the
rates stored.
A failing command writes nothing to standard output and one diagnostic line to standard error.
`fetch` is never run by the harness. Its tests replace the download with a stub and never use
the network.
**Exit codes.** `0` success. `1` operational failure: the file cannot be read, or a `fetch`
download fails. `2` usage error: unknown command, wrong argument count, `--port` not an integer.
`3` rejected file.
Every response is JSON. EUR is the base. A rate is the number of units of a currency per euro, and
EUR itself has the implicit rate `1`.
**Effective date.** The ECB publishes on working days only. A request for date `D` uses the latest
publication date on or before `D`. If none exists, the response is `404`.
| `GET /health` | `{"status": "ok"}` |
| `GET /stats` | `{"dates": , "rates": , "first": , "last": }`. `first` and `last` are `null` when the table is empty. |
| `GET /currencies` | `{"base": "EUR", "currencies": [, ...]}`: every currency with at least one stored rate, ascending. |
| `GET /rates/{date}` | `{"date": , "requested": , "base": "EUR", "rates": {: , ...}}`: every currency with a rate on the effective date. |
| `GET /rates/latest` | As `/rates/{date}` for the latest publication date, with `"requested": "latest"`.
| `GET /convert?from=&to=&amount=[&date=]` | `{"date": , "from": , "to": , "amount": , "rate": , "result": }`. Without `date`, the latest publication date is used. |
| `GET /history/{code}[?start=][&end=]` | `{"currency": , "base": "EUR", "count": , "min": , "max": , "mean": , "points": [{"date": , "rate": }, ...]}` for the publication dates from `start` through `end` inclusive, ascending. Both bounds are optional. With no points, `count` is `0`, `min`, `max`, and `mean` are `null`, and `points` is `[]`. |
- `` in `/rates`, `min`, `max`, and `points` is the stored text, as a JSON string.
- `/convert` `amount` matches `[0-9]+(\.[0-9]+)?` and is echoed as given, as a JSON string.
- `/convert` `rate` is `rate(to) / rate(from)`, computed exactly and rounded half to even to six
decimal places, written with exactly six decimals, as a JSON string: `"161.770000"`.
- `/convert` `result` is `amount × rate(to) / rate(from)`, computed exactly from the stored rates
(not from the rounded `rate`) and rounded half to even to two decimal places, written with exactly two decimals, as a JSON string: `"82.01"`.
- `/history` `min` and `max` are the stored text of the smallest and largest rate by value.
`mean` is the exact mean rounded half to even to six decimal places, written with exactly six
decimals, as a JSON string.
| `404` | No publication on or before the requested date; a `/convert` currency other than EUR with no rate on the effective date; a `/history` currency with no stored rate at all (including `EUR`). |
| `422` | A malformed date in the path or query, a malformed `amount`, or a missing required query parameter. FastAPI's request validation produces this status. |
A load that completes while the server is running is visible to the next request.
`sources/run.sh` is the scoring entry point. It is supplied, not authored, and is staged verbatim
into the application root with the data files. `drydock uat` runs `sh sources/run.sh` from the
completed application root and takes its exit code and output as the score.
The harness sets `DATABASE_URL` to a fresh temporary SQLite file, runs `./fxrates init` and
`./fxrates load`, starts `./fxrates serve --port ` in the background on a free local port,
waits up to 30 seconds for `GET /health`, and then runs HTTP requests against `127.0.0.1`. It
makes 35 checks. A command check compares exit code and standard output. An HTTP check compares
the status code and, for status 200, the JSON body as a value (key order and whitespace do not
matter). Standard error and error-response bodies are never compared. The harness never runs
`fetch` and uses no network beyond `127.0.0.1`. It prints `PASS` or `FAIL` per check and ends with:
```text
FxRates: NN passed, NN failed (of 35)
```
- `sources/run.sh` — the scoring entry point and its known answers
- `sources/eurofxref-hist_2025-01.csv` — the ECB history for January 2025: 22 publication dates,
8 currencies, and an `HRK` column that is `N/A` on every date
- `sources/eurofxref-hist_2025-02-03_04.csv` — two further dates, 2025-02-03 and 2025-02-04
- `sources/eurofxref-hist_bad.csv` — a valid first row and an invalid rate on its second row
Application code never reads `sources/run.sh` and never embeds an answer. Every value comes from
the database.