STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_error_sentences.py
Go to the documentation of this file.
1
"""Every error response is {"detail": "<one plain sentence>"} (docs/investor contract C3).
2
3
FastAPI's own request-validation failures used to send `detail` as a list of {loc, msg, type}
4
objects, which a page rendered as "[object Object]".
5
"""
6
7
from
fastapi.testclient
import
TestClient
8
9
from
backend.main
import
app, validation_sentence
10
11
12
def
test_validation_list_becomes_one_sentence
():
13
errs = [{
"loc"
: (
"body"
,
"lox_pressure_psi"
),
"msg"
:
"Input should be a valid number"
,
"type"
:
"x"
},
14
{
"loc"
: (
"body"
,
"fuel_pressure_psi"
),
"msg"
:
"Field required"
,
"type"
:
"y"
}]
15
# Every problem, by its field -- not the first and a count (a rail with two bad fields used
16
# to say only the first).
17
assert
validation_sentence(errs) == (
18
"Lox pressure psi: Input should be a valid number; fuel pressure psi: Field required."
19
)
20
assert
validation_sentence([]) ==
"Some of the values were not accepted."
21
22
23
def
test_a_malformed_request_answers_with_a_sentence
():
24
client = TestClient(app)
25
r = client.post(
"/api/evaluate"
, json={
"lox_pressure_psi"
:
"not a number"
})
26
assert
r.status_code == 422
27
detail = r.json()[
"detail"
]
28
assert
isinstance(detail, str)
and
detail.endswith(
"."
)
29
30
31
def
test_the_field_paths_ride_along_for_pages_with_their_own_names
():
32
client = TestClient(app)
33
r = client.post(
"/api/evaluate"
, json={
"lox_pressure_psi"
:
"not a number"
})
34
errors = r.json()[
"errors"
]
35
assert
errors
and
all(isinstance(e[
"loc"
], list)
and
"msg"
in
e
for
e
in
errors)
backend.main
Definition
main.py:1
test_error_sentences.test_a_malformed_request_answers_with_a_sentence
test_a_malformed_request_answers_with_a_sentence()
Definition
test_error_sentences.py:23
test_error_sentences.test_the_field_paths_ride_along_for_pages_with_their_own_names
test_the_field_paths_ride_along_for_pages_with_their_own_names()
Definition
test_error_sentences.py:31
test_error_sentences.test_validation_list_becomes_one_sentence
test_validation_list_becomes_one_sentence()
Definition
test_error_sentences.py:12
EngineDesign
tests
test_error_sentences.py
Generated by
1.15.0