=== BEGIN ARTIFACT FEATURE-Rates.md === # FEATURE: Effective-Date Rates | Field | Value | |-------------|-------| | Version | 20261026 V1 | | Description | Retrieves stored rates for the latest publication on or before a requested date. | | Depends On | FEATURE-AppServer.md, DATABASE.md | | Provides | GET /rates/{date} | | Consumes | create_app(), rates repository operations | ## Questions - None. ## Purpose Return the ECB rates for a requested date using the latest available publication on or before that date. ## Trigger and workflow A caller requests `GET /rates/{date}`. Validate that the path value is a real calendar date in `YYYY-MM-DD` form. Find the latest publication on or before the request, then return its stored rates as published text. Return `404` when no publication exists on or before the requested date. Malformed dates return `422`. ## Database Interface - `FEATURE-RATE-003` — read: find publications on or before a date - supplies: `requested_date` - returns: `date`, `currency`, `rate` - `FEATURE-RATE-002` — read: list rates for a publication date - supplies: `date` - returns: `date`, `currency`, `rate` ## Programmatic Acceptance === AC rates-effective-date === Intent: The route resolves a requested date to the latest available publication and returns its stored rates. Requires: python-package=fastapi; scope=test Requires: python-package=httpx2; scope=test from app_server.testing import web_client from persistence.testing import db_session requested = "2099-06-03" effective = "2099-06-02" currency = "QAF" rate = "6.125" with db_session() as db: db.rates.upsert(date=effective, currency=currency, rate=rate) client = web_client() response = client.get(f"/rates/{requested}") body = response.json() assert response.status_code == 200 assert body["date"] == effective assert body["requested"] == requested assert body["base"] == "EUR" assert body["rates"][currency] == rate === END AC rates-effective-date === === AC rates-errors === Intent: The route returns the declared errors for a missing effective publication and a malformed date. Requires: python-package=fastapi; scope=test Requires: python-package=httpx2; scope=test from app_server.testing import web_client client = web_client() assert client.get("/rates/1900-01-01").status_code == 404 assert client.get("/rates/2025-13-01").status_code == 422 === END AC rates-errors === ## User Acceptance - None. ## Guardrails - Resolve rates using the latest publication date on or before the request. - Return stored rate text unchanged; do not convert it to floating point. - Do not hardcode dates, counts, or rates in application code. === END ARTIFACT === === BEGIN ARTIFACT FEATURE-LatestRates.md === # FEATURE: Latest Publication Rates | Field | Value | |-------------|-------| | Version | 20261026 V1 | | Description | Retrieves rates from the latest stored publication. | | Depends On | FEATURE-AppServer.md, DATABASE.md | | Provides | GET /rates/latest | | Consumes | create_app(), rates repository operations | ## Questions - None. ## Purpose Return all stored rates for the latest publication date, identifying the request as `latest`. ## Trigger and workflow A caller requests `GET /rates/latest`. Find the most recent publication date and retrieve its rates. Return `404` if no publication exists. ## Database Interface - `FEATURE-RATE-003` — read: find publications on or before a date - supplies: `requested_date` - returns: `date`, `currency`, `rate` - `FEATURE-RATE-002` — read: list rates for a publication date - supplies: `date` - returns: `date`, `currency`, `rate` ## Programmatic Acceptance === AC latest-rates-response === Intent: The latest-rates route returns the stored rates for the latest publication. Requires: python-package=fastapi; scope=test Requires: python-package=httpx2; scope=test from app_server.testing import web_client from persistence.testing import db_session date = "2099-07-02" currency = "QAG" rate = "7.25" with db_session() as db: db.rates.upsert(date=date, currency=currency, rate=rate) response = web_client().get("/rates/latest") body = response.json() assert response.status_code == 200 assert body["date"] == date assert body["requested"] == "latest" assert body["base"] == "EUR" assert body["rates"][currency] == rate === END AC latest-rates-response === === AC latest-rates-empty === Intent: The latest-rates route returns 404 when there is no publication. Requires: python-package=fastapi; scope=test Requires: python-package=httpx2; scope=test from app_server.testing import web_client assert web_client().get("/rates/latest").status_code == 404 === END AC latest-rates-empty === ## User Acceptance - None. ## Guardrails - Derive the latest date and rates from repository queries. - Preserve stored rate text exactly. === END ARTIFACT === === BEGIN ARTIFACT FEATURE-Convert.md === # FEATURE: Currency Conversion | Field | Value | |-------------|-------| | Version | 20261026 V1 | | Description | Converts an amount between two currencies using exact decimal arithmetic. | | Depends On | FEATURE-AppServer.md, DATABASE.md | | Provides | GET /convert | | Consumes | create_app(), rates repository operations | ## Questions - None. ## Purpose Convert an amount from one currency to another using ECB rates, with EUR as the base currency. ## Trigger and workflow A caller supplies `from`, `to`, and `amount`, with an optional `date`. Missing required parameters, malformed amounts, or malformed dates return `422`. Without a date, use the latest publication. With a date, resolve the latest publication on or before it. Return `404` when no effective publication exists or a requested non-EUR currency lacks a rate on that publication. EUR has the implicit rate `1`. The conversion rate is `rate(to) / rate(from)`, rounded half-even to six decimal places. The result is calculated directly from the unrounded stored rates and amount, rounded half-even to two decimal places. Both are strings with fixed decimal places; echo the amount text unchanged. ## Database Interface - `FEATURE-RATE-003` — read: find publications on or before a date - supplies: `requested_date` - returns: `date`, `currency`, `rate` - `FEATURE-RATE-002` — read: list rates for a publication date - supplies: `date` - returns: `date`, `currency`, `rate` ## Programmatic Acceptance === AC convert-decimal-and-rounding === Intent: The conversion endpoint echoes its input amount and calculates its rate and result from the fixture rates with the specified rounding. Requires: python-package=fastapi; scope=test Requires: python-package=httpx2; scope=test from decimal import Decimal, ROUND_HALF_EVEN from app_server.testing import web_client from persistence.testing import db_session date = "2099-08-01" source_currency = "QAH" target_currency = "QAI" amount = "2.00" source_rate = "2" target_rate = "5" with db_session() as db: db.rates.upsert(date=date, currency=source_currency, rate=source_rate) db.rates.upsert(date=date, currency=target_currency, rate=target_rate) response = web_client().get( "/convert", params={"from": source_currency, "to": target_currency, "amount": amount, "date": date}, ) body = response.json() expected_rate = (Decimal(target_rate) / Decimal(source_rate)).quantize( Decimal("0.000001"), rounding=ROUND_HALF_EVEN ) expected_result = (Decimal(amount) * Decimal(target_rate) / Decimal(source_rate)).quantize( Decimal("0.01"), rounding=ROUND_HALF_EVEN ) assert response.status_code == 200 assert body["date"] == date assert body["from"] == source_currency assert body["to"] == target_currency assert body["amount"] == amount assert body["rate"] == f"{expected_rate:.6f}" assert body["result"] == f"{expected_result:.2f}" === END AC convert-decimal-and-rounding === === AC convert-validation-and-unknown-currency === Intent: The conversion endpoint rejects missing or malformed inputs and returns 404 for a currency without a rate. Requires: python-package=fastapi; scope=test Requires: python-package=httpx2; scope=test from app_server.testing import web_client client = web_client() assert client.get("/convert", params={"from": "USD", "to": "EUR"}).status_code == 422 assert client.get( "/convert", params={"from": "USD", "to": "EUR", "amount": "abc"} ).status_code == 422 assert client.get( "/convert", params={"from": "USD", "to": "EUR", "amount": "1", "date": "2025-13-01"} ).status_code == 422 assert client.get( "/convert", params={"from": "USD", "to": "QAJ", "amount": "1", "date": "2099-08-02"} ).status_code == 404 === END AC convert-validation-and-unknown-currency === ## User Acceptance - None. ## Guardrails - Use `decimal.Decimal` only; never use floating-point arithmetic. - Compute the result from unrounded source rates, not from the rounded response rate. - Preserve the caller's amount text in the response. - Do not embed rates or computed answers in application code. === END ARTIFACT === === BEGIN ARTIFACT FEATURE-History.md === # FEATURE: Currency History | Field | Value | |-------------|-------| | Version | 20261026 V1 | | Description | Returns a currency's stored rate history and summary statistics over optional bounds. | | Depends On | FEATURE-AppServer.md, DATABASE.md | | Provides | GET /history/{code} | | Consumes | create_app(), rates repository operations | ## Questions - None. ## Purpose Return a currency's publication points and min, max, and mean statistics over an optional inclusive date range. ## Trigger and workflow A caller requests a currency history with optional `start` and `end` dates. Malformed bounds return `422`. Return points in ascending date order and preserve each published rate string. Return `404` if the currency has no stored history at all, including EUR. If it has history but no points in the selected range, return count zero, null min/max/mean, and an empty points list. Compute minimum and maximum by numeric value. Compute the exact mean using `Decimal` and round half-even to six decimal places. ## Database Interface - `FEATURE-RATE-004` — read: list a currency's rates within optional inclusive bounds - supplies: `currency`, `start_date`, `end_date` - returns: `date`, `currency`, `rate` - `FEATURE-RATE-005` — read: determine whether the currency has any stored rate - supplies: none - returns: `date`, `currency`, `rate` ## Programmatic Acceptance === AC history-range-and-statistics === Intent: The history endpoint returns bounded points and statistics derived from the seeded rates. Requires: python-package=fastapi; scope=test Requires: python-package=httpx2; scope=test from decimal import Decimal, ROUND_HALF_EVEN from app_server.testing import web_client from persistence.testing import db_session currency = "QAK" dates = ["2099-09-01", "2099-09-02", "2099-09-03"] rates = ["1.1", "1.3", "1.2"] start = dates[0] end = dates[2] with db_session() as db: for date, rate in zip(dates, rates): db.rates.upsert(date=date, currency=currency, rate=rate) response = web_client().get(f"/history/{currency}", params={"start": start, "end": end}) body = response.json() expected_mean = (sum((Decimal(rate) for rate in rates), Decimal(0)) / len(rates)).quantize( Decimal("0.000001"), rounding=ROUND_HALF_EVEN ) assert response.status_code == 200 assert body["currency"] == currency assert body["base"] == "EUR" assert [point["date"] for point in body["points"]] == dates assert [point["rate"] for point in body["points"]] == rates assert body["count"] == len(rates) assert body["min"] == min(rates, key=Decimal) assert body["max"] == max(rates, key=Decimal) assert body["mean"] == f"{expected_mean:.6f}" === END AC history-range-and-statistics === === AC history-empty-range-and-errors === Intent: The history endpoint distinguishes an empty range from a currency with no history and rejects malformed bounds. Requires: python-package=fastapi; scope=test Requires: python-package=httpx2; scope=test from app_server.testing import web_client from persistence.testing import db_session currency = "QAL" date = "2099-10-01" rate = "1.4" with db_session() as db: db.rates.upsert(date=date, currency=currency, rate=rate) client = web_client() empty = client.get(f"/history/{currency}", params={"start": "2099-10-02"}) assert empty.status_code == 200 assert empty.json()["count"] == 0 assert empty.json()["min"] is None assert empty.json()["max"] is None assert empty.json()["mean"] is None assert empty.json()["points"] == [] assert client.get("/history/QAM").status_code == 404 assert client.get(f"/history/{currency}", params={"start": "2099-10-01-x"}).status_code == 422 === END AC history-empty-range-and-errors === ## User Acceptance - None. ## Guardrails - Preserve stored rate text for history points and min/max values. - Use exact decimal arithmetic for mean and round half-even to six decimal places. - Treat a currency with no history as 404, distinct from an empty bounded result. === END ARTIFACT === === BEGIN ARTIFACT FEATURE-CLI-Errors.md === # FEATURE: CLI Usage and Error Handling | Field | Value | |-------------|-------| | Version | 20261026 V1 | | Description | Defines exit statuses and output behavior for invalid and failed CLI commands. | | Depends On | FEATURE-CLI.md, FEATURE-Load.md | | Provides | CLI usage and error exit behavior | | Consumes | ./fxrates | ## Questions - None. ## Purpose Give callers consistent exit statuses and output channels for CLI usage and operational failures. ## Behavior - Unknown commands, wrong argument counts, and a non-integer `--port` return exit status `2`. - Operational file read failures return exit status `1`. - Rejected CSV files return exit status `3`. - Failed commands write nothing to standard output and one diagnostic line to standard error. - Successful commands retain the output contract of their owning feature. ## Database Interface - None — persistence is owned by DATABASE.md. ## Programmatic Acceptance === AC cli-error-contract === Intent: The CLI returns the documented usage and file error exit statuses. from cli.testing import cli_runner unknown_code, unknown_out, unknown_err = cli_runner("frobnicate") wrong_count_code, wrong_count_out, wrong_count_err = cli_runner("load") port_code, port_out, port_err = cli_runner("serve", "--port", "notaport") missing_file_code, missing_file_out, missing_file_err = cli_runner( "load", "missing-fxrates-input.csv" ) rejected_code, rejected_out, rejected_err = cli_runner("load", "sources/eurofxref-hist_bad.csv") assert unknown_code == 2 assert wrong_count_code == 2 assert port_code == 2 assert missing_file_code == 1 assert rejected_code == 3 assert unknown_out == "" assert wrong_count_out == "" assert port_out == "" assert missing_file_out == "" assert rejected_out == "" assert unknown_err.endswith("\n") assert wrong_count_err.endswith("\n") assert port_err.endswith("\n") assert missing_file_err.endswith("\n") assert rejected_err.endswith("\n") === END AC cli-error-contract === ## User Acceptance - None. ## Guardrails - Diagnostics go to standard error. - Do not compare or prescribe diagnostic wording. - Preserve the specified exit codes and keep standard output empty for failures.