|
1 | | -import pytest |
2 | | -import time |
3 | 1 | import json |
| 2 | +import time |
| 3 | + |
| 4 | +import pytest |
4 | 5 |
|
5 | 6 | from labthings import LabThing |
6 | 7 | from labthings.views import ActionView |
7 | 8 |
|
| 9 | + |
8 | 10 | @pytest.mark.filterwarnings("ignore:Exception in thread") |
9 | 11 | def test_action_exception_handling(thing_with_some_views, client): |
10 | 12 | """Check errors in an Action are handled correctly |
11 | 13 |
|
12 | | - |
| 14 | +
|
13 | 15 |
|
14 | 16 | `/FieldProperty` has a validation constraint - it |
15 | 17 | should return a "bad response" error if invoked with |
16 | | - anything other than |
| 18 | + anything other than |
17 | 19 | """ |
18 | | - # `/FailAction` raises an `Exception`. |
19 | | - # This ought to return a 201 code representing the |
20 | | - # action that was successfully started - but should |
| 20 | + # `/FailAction` raises an `Exception`. |
| 21 | + # This ought to return a 201 code representing the |
| 22 | + # action that was successfully started - but should |
21 | 23 | # show that it failed through the "status" field. |
22 | 24 |
|
23 | 25 | # This is correct for the current (24/7/2021) behaviour |
24 | | - # but may want to change for the next version, e.g. |
| 26 | + # but may want to change for the next version, e.g. |
25 | 27 | # returning a 500 code. For further discussion... |
26 | 28 | r = client.post("/FailAction") |
27 | 29 | assert r.status_code == 201 |
28 | 30 | action = r.get_json() |
29 | 31 | assert action["status"] == "error" |
30 | 32 |
|
| 33 | + |
31 | 34 | def test_action_abort_and_validation(thing_with_some_views, client): |
32 | 35 | """Check HTTPExceptions result in error codes. |
33 | | - |
| 36 | +
|
34 | 37 | Subclasses of HTTPError should result in a non-200 return code, not |
35 | 38 | just failures. This covers Marshmallow validation (400) and |
36 | 39 | use of `abort()`. |
37 | 40 | """ |
38 | 41 | # `/AbortAction` should return a 418 error code |
39 | 42 | r = client.post("/AbortAction") |
40 | 43 | assert r.status_code == 418 |
41 | | - |
| 44 | + |
| 45 | + |
42 | 46 | def test_action_validate(thing_with_some_views, client): |
43 | 47 | # `/ActionWithValidation` should fail with a 400 error |
44 | 48 | # if `test_arg` is not either `one` or `two` |
45 | | - r = client.post("/ActionWithValidation", data=json.dumps({"test_arg":"one"})) |
| 49 | + r = client.post("/ActionWithValidation", data=json.dumps({"test_arg": "one"})) |
46 | 50 | assert r.status_code in [200, 201] |
47 | | - r = client.post("/ActionWithValidation", data=json.dumps({"test_arg":"three"})) |
| 51 | + r = client.post("/ActionWithValidation", data=json.dumps({"test_arg": "three"})) |
48 | 52 | assert r.status_code in [422] |
49 | | - |
50 | | - |
|
0 commit comments