<requirement name="python-fastapi-sqlite-stack" stories="app_server,cli">
Implement in Python 3.11 or newer with FastAPI, served by uvicorn, over SQLite.
</requirement>
<requirement name="fxrates-executable" stories="cli">
The product is an executable named `fxrates` at the application root.
</requirement>
<requirement name="test-suite-and-launcher" stories="tests">
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.
</requirement>
<requirement name="test-coverage" stories="tests">
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.
</requirement>
<requirement name="rates-schema" stories="database">
| `rates` | `date` text `YYYY-MM-DD`, `currency` text, `rate` text | unique (`date`, `currency`) |
</requirement>
<requirement name="rate-storage-and-arithmetic" stories="database,convert,history,api_rates,api_latest">
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.
</requirement>
<requirement name="database-location" stories="database">
`DATABASE_URL` names the SQLite database file, optionally prefixed `sqlite://`. When it is unset,
`DATABASE_PATH` is used, then `data/app.db`.
</requirement>
<requirement name="shared-persistence-access" stories="architecture,database">
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.
</requirement>
<requirement name="initialize-empty-store" stories="init,database">
`fxrates init` | Drops the `rates` table if present and creates it empty. | Nothing.
</requirement>
<requirement name="load-ecb-history" stories="load,database">
`fxrates load <csv>` | Loads an ECB history file (rules below). | `dates,rates`, then the number of dates and of rates stored from the file.
</requirement>
<requirement name="fetch-ecb-history" stories="fetch,load">
`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`.
</requirement>
<requirement name="serve-http-api" stories="cli,app_server">
`fxrates serve [--port <n>]` | Serves the HTTP API on `127.0.0.1:<n>` with uvicorn until stopped. Default port `8000`. | Nothing required.
</requirement>
<requirement name="parse-ecb-csv-header" stories="load">
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.
</requirement>
<requirement name="validate-ecb-csv-rows" stories="load">
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.
</requirement>
<requirement name="reject-invalid-file-atomically" stories="load">
**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.
</requirement>
<requirement name="replace-existing-rates" stories="load">
A rate for a (`date`, `currency`) that is already stored replaces it, so loading the same file
twice leaves the database unchanged.
</requirement>
<requirement name="report-load-counts" stories="load">
The reported `dates` counts the dates with at least one stored rate, and `rates` counts the
rates stored.
</requirement>
<requirement name="report-cli-errors" stories="cli_errors">
A failing command writes nothing to standard output and one diagnostic line to standard error.
</requirement>
<requirement name="stub-fetch-download" stories="fetch,tests">
`fetch` is never run by the harness. Its tests replace the download with a stub and never use
the network.
</requirement>
<requirement name="cli-exit-codes" stories="cli_errors">
**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.
</requirement>
<requirement name="json-http-responses" stories="app_server">
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`.
</requirement>
<requirement name="effective-publication-date" stories="api_rates,api_latest,convert">
**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`.
</requirement>
<requirement name="health-endpoint" stories="api_health">
| `GET /health` | `{"status": "ok"}` |
</requirement>
<requirement name="stats-endpoint" stories="api_stats">
| `GET /stats` | `{"dates": <n>, "rates": <n>, "first": <date>, "last": <date>}`. `first` and `last` are `null` when the table is empty. |
</requirement>
<requirement name="currencies-endpoint" stories="api_currencies">
| `GET /currencies` | `{"base": "EUR", "currencies": [<code>, ...]}`: every currency with at least one stored rate, ascending. |
</requirement>
<requirement name="rates-by-date-endpoint" stories="api_rates">
| `GET /rates/{date}` | `{"date": <effective date>, "requested": <date>, "base": "EUR", "rates": {<code>: <rate>, ...}}`: every currency with a rate on the effective date. |
</requirement>
<requirement name="latest-rates-endpoint" stories="api_latest">
| `GET /rates/latest` | As `/rates/{date}` for the latest publication date, with `"requested": "latest"`.
</requirement>
<requirement name="convert-endpoint" stories="convert">
| `GET /convert?from=<code>&to=<code>&amount=<amount>[&date=<date>]` | `{"date": <effective date>, "from": <code>, "to": <code>, "amount": <amount>, "rate": <rate>, "result": <result>}`. Without `date`, the latest publication date is used. |
</requirement>
<requirement name="history-endpoint" stories="history">
| `GET /history/{code}[?start=<date>][&end=<date>]` | `{"currency": <code>, "base": "EUR", "count": <n>, "min": <rate>, "max": <rate>, "mean": <mean>, "points": [{"date": <date>, "rate": <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 `[]`. |
</requirement>
<requirement name="rate-response-values" stories="api_rates,api_latest">
- `<rate>` in `/rates`, `min`, `max`, and `points` is the stored text, as a JSON string.
</requirement>
<requirement name="convert-amount-validation-and-echo" stories="convert">
- `/convert` `amount` matches `[0-9]+(\.[0-9]+)?` and is echoed as given, as a JSON string.
</requirement>
<requirement name="convert-rate-rounding" stories="convert">
- `/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"`.
</requirement>
<requirement name="convert-result-rounding" stories="convert">
- `/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"`.
</requirement>
<requirement name="history-summary-values" stories="history">
- `/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.
</requirement>
<requirement name="not-found-api-errors" stories="api_rates,api_latest,convert,history">
| `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`). |
</requirement>
<requirement name="validation-api-errors" stories="convert,api_rates,api_latest,history">
| `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. |
</requirement>
<requirement name="live-load-visibility" stories="load,app_server">
A load that completes while the server is running is visible to the next request.
</requirement>
<requirement name="supplied-grader-conformance" stories="grader">
`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.
</requirement>
<requirement name="grader-http-and-command-checks" stories="grader">
The harness sets `DATABASE_URL` to a fresh temporary SQLite file, runs `./fxrates init` and
`./fxrates load`, starts `./fxrates serve --port <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)
```
</requirement>
<requirement name="read-only-scoring-assets" stories="grader,load">
- `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
</requirement>
<requirement name="health-scoring-and-application-isolation" stories="grader,api_health,load">
Application code never reads `sources/run.sh` and never embeds an answer. Every value comes from
the database.
</requirement>
<unattached story="architecture"/>Run artifact