|
| 1 | +from datetime import datetime, timedelta, timezone |
| 2 | + |
| 3 | +import pytest |
| 4 | +from fastapi import HTTPException |
| 5 | + |
| 6 | +from app.v1.endpoints.read.query_parameters import CommonQueryParams |
| 7 | + |
| 8 | + |
| 9 | +def _future_datetime(): |
| 10 | + return (datetime.now(timezone.utc) + timedelta(days=1)).isoformat().replace( |
| 11 | + "+00:00", "Z" |
| 12 | + ) |
| 13 | + |
| 14 | + |
| 15 | +def test_common_query_params_accepts_valid_as_of(): |
| 16 | + params = CommonQueryParams(as_of="2024-01-01T00:00:00Z") |
| 17 | + |
| 18 | + assert params.as_of == "2024-01-01T00:00:00Z" |
| 19 | + |
| 20 | + |
| 21 | +def test_common_query_params_rejects_invalid_as_of(): |
| 22 | + with pytest.raises(HTTPException) as exc_info: |
| 23 | + CommonQueryParams(as_of="not-a-date") |
| 24 | + |
| 25 | + assert exc_info.value.status_code == 422 |
| 26 | + assert "$as_of" in exc_info.value.detail["message"] |
| 27 | + |
| 28 | + |
| 29 | +def test_common_query_params_rejects_future_as_of(): |
| 30 | + with pytest.raises(HTTPException) as exc_info: |
| 31 | + CommonQueryParams(as_of=_future_datetime()) |
| 32 | + |
| 33 | + assert exc_info.value.status_code == 422 |
| 34 | + assert "future" in exc_info.value.detail["message"] |
| 35 | + |
| 36 | + |
| 37 | +def test_common_query_params_accepts_valid_from_to(): |
| 38 | + params = CommonQueryParams( |
| 39 | + from_to="2024-01-01T00:00:00Z/2024-01-02T00:00:00Z" |
| 40 | + ) |
| 41 | + |
| 42 | + assert params.from_to == "2024-01-01T00:00:00Z/2024-01-02T00:00:00Z" |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize( |
| 46 | + "from_to", |
| 47 | + [ |
| 48 | + "2024-01-01T00:00:00Z", |
| 49 | + "/2024-01-02T00:00:00Z", |
| 50 | + "2024-01-01T00:00:00Z/", |
| 51 | + "not-a-date/2024-01-02T00:00:00Z", |
| 52 | + ], |
| 53 | +) |
| 54 | +def test_common_query_params_rejects_invalid_from_to(from_to): |
| 55 | + with pytest.raises(HTTPException) as exc_info: |
| 56 | + CommonQueryParams(from_to=from_to) |
| 57 | + |
| 58 | + assert exc_info.value.status_code == 422 |
| 59 | + |
| 60 | + |
| 61 | +def test_common_query_params_rejects_reversed_from_to(): |
| 62 | + with pytest.raises(HTTPException) as exc_info: |
| 63 | + CommonQueryParams( |
| 64 | + from_to="2024-01-02T00:00:00Z/2024-01-01T00:00:00Z" |
| 65 | + ) |
| 66 | + |
| 67 | + assert exc_info.value.status_code == 422 |
| 68 | + assert "greater" in exc_info.value.detail["message"] |
| 69 | + |
| 70 | + |
| 71 | +def test_common_query_params_rejects_as_of_with_from_to(): |
| 72 | + with pytest.raises(HTTPException) as exc_info: |
| 73 | + CommonQueryParams( |
| 74 | + as_of="2024-01-01T00:00:00Z", |
| 75 | + from_to="2024-01-01T00:00:00Z/2024-01-02T00:00:00Z", |
| 76 | + ) |
| 77 | + |
| 78 | + assert exc_info.value.status_code == 422 |
| 79 | + assert "cannot be used together" in exc_info.value.detail["message"] |
0 commit comments