STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_api_nonfinite_serialization.py
Go to the documentation of this file.
1"""Non-finite floats must not take down an API response.
2
3A NaN anywhere in the results dict made json.dumps raise
4"Out of range float values are not JSON compliant", which the evaluate router
5turned into a blanket HTTP 500 on forward evaluation. NaN is a legal model output
6(e.g. a lag model that does not define K_v), so it must serialise as null.
7"""
8import math
9import numpy as np
10import pytest
11
12from backend.routers.evaluate import convert_numpy as ev_convert
13from backend.routers.flight import convert_numpy as fl_convert
14from backend.routers.timeseries import convert_numpy as ts_convert
15from backend.routers.optimizer import convert_numpy as op_convert
16
17CONVERTERS = [
18 pytest.param(ev_convert, id="evaluate"),
19 pytest.param(fl_convert, id="flight"),
20 pytest.param(ts_convert, id="timeseries"),
21 pytest.param(op_convert, id="optimizer"),
22]
23
24
25@pytest.mark.parametrize("convert", CONVERTERS)
26@pytest.mark.parametrize("bad", [float("nan"), float("inf"), float("-inf"),
27 np.float64("nan"), np.float64("inf")])
29 assert convert(bad) is None
30
31
32@pytest.mark.parametrize("convert", CONVERTERS)
34 """The shape that actually broke: NaN buried in the stability payload."""
35 import json
36 payload = {
37 "F": np.float64(8000.0),
38 "stability_rich": {"chug": {"lag_breakdown": {
39 "O": {"K_v": float("nan"), "tau_vap_s": 0.0037},
40 "F": {"K_v": np.float64("nan"), "tau_vap_s": 0.0141},
41 }}},
42 "series": [1.0, float("inf"), np.float64("nan"), 4.0],
43 }
44 out = convert(payload)
45 json.dumps(out) # must not raise
46 assert out["stability_rich"]["chug"]["lag_breakdown"]["O"]["K_v"] is None
47 assert out["stability_rich"]["chug"]["lag_breakdown"]["F"]["K_v"] is None
48 assert out["series"][1] is None and out["series"][2] is None
49
50
51@pytest.mark.parametrize("convert", CONVERTERS)
53 """The guard must not eat real numbers."""
54 assert convert(np.float64(8000.0)) == pytest.approx(8000.0)
55 assert convert(0.0) == 0.0
56 assert convert(-1.5) == pytest.approx(-1.5)
57 assert convert(np.int64(26)) == 26
json_pointer< string_t > convert() const &
Definition json.hpp:14640