Skip to content

Commit ad654b4

Browse files
author
Joel Collins
committed
Default task view coverage
1 parent 25bb4f1 commit ad654b4

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

tests/test_server_default_views.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,67 @@
1-
def test_docs(thing, thing_client, app_ctx):
1+
from labthings.core.tasks import taskify, dictionary
2+
3+
import gevent
4+
5+
6+
def test_docs(thing, thing_client):
27
with thing_client as c:
38
assert c.get("/docs/swagger").json == thing.spec.to_dict()
49
assert c.get("/docs/swagger-ui").status_code == 200
510

611

7-
def test_extensions(thing, thing_client, app_ctx):
12+
def test_extensions(thing_client):
813
with thing_client as c:
914
assert c.get("/extensions").json == []
15+
16+
17+
def test_tasks_list(thing_client):
18+
def task_func():
19+
pass
20+
21+
task_obj = taskify(task_func)()
22+
23+
with thing_client as c:
24+
response = c.get("/tasks").json
25+
assert len(response) == 1
26+
assert response[0].get("id") == str(task_obj.id)
27+
28+
29+
def test_task_representation(thing_client):
30+
def task_func():
31+
pass
32+
33+
task_obj = taskify(task_func)()
34+
task_id = str(task_obj.id)
35+
36+
with thing_client as c:
37+
response = c.get(f"/tasks/{task_id}").json
38+
assert response
39+
40+
41+
def test_task_representation_missing(thing_client):
42+
with thing_client as c:
43+
assert c.get(f"/tasks/missing_id").status_code == 404
44+
45+
46+
def test_task_kill(thing_client):
47+
def task_func():
48+
while True:
49+
gevent.sleep(0)
50+
51+
task_obj = taskify(task_func)()
52+
task_id = str(task_obj.id)
53+
54+
# Wait for task to start
55+
task_obj.started_event.wait()
56+
assert task_id in dictionary()
57+
58+
# Send a DELETE request to terminate the task
59+
with thing_client as c:
60+
assert c.delete(f"/tasks/{task_id}").status_code == 200
61+
# Test task was terminated
62+
assert task_obj.state.get("status") == "terminated"
63+
64+
65+
def test_task_kill_missing(thing_client):
66+
with thing_client as c:
67+
assert c.delete(f"/tasks/missing_id").status_code == 404

0 commit comments

Comments
 (0)