Skip to content

Commit 37b4703

Browse files
author
Joel Collins
committed
Server View class tests
1 parent c65aa71 commit 37b4703

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

tests/test_server_view.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
from labthings.server import view
2+
from werkzeug.http import parse_set_header
3+
from werkzeug.wrappers import Response as ResponseBase
4+
from flask import make_response
5+
6+
import pytest
7+
8+
9+
def common_test(app):
10+
c = app.test_client()
11+
12+
assert c.get("/").data == b"GET"
13+
assert c.post("/").data == b"POST"
14+
assert c.put("/").status_code == 405
15+
assert c.delete("/").status_code == 405
16+
meths = parse_set_header(c.open("/", method="OPTIONS").headers["Allow"])
17+
assert sorted(meths) == ["GET", "HEAD", "OPTIONS", "POST"]
18+
19+
20+
def test_method_based_view(app):
21+
class Index(view.View):
22+
def get(self):
23+
return "GET"
24+
25+
def post(self):
26+
return "POST"
27+
28+
app.add_url_rule("/", view_func=Index.as_view("index"))
29+
common_test(app)
30+
31+
32+
def test_view_patching(app):
33+
class Index(view.View):
34+
def get(self):
35+
1 // 0
36+
37+
def post(self):
38+
1 // 0
39+
40+
class Other(Index):
41+
def get(self):
42+
return "GET"
43+
44+
def post(self):
45+
return "POST"
46+
47+
view_obj = Index.as_view("index")
48+
view_obj.view_class = Other
49+
app.add_url_rule("/", view_func=view_obj)
50+
common_test(app)
51+
52+
53+
def test_accept_default_application_json(app, client):
54+
class Index(view.View):
55+
def get(self):
56+
return "GET"
57+
58+
app.add_url_rule("/", view_func=Index.as_view("index"))
59+
60+
with client:
61+
res = client.get("/", headers=[("Accept", "application/json")])
62+
assert res.status_code == 200
63+
assert res.content_type == "application/json"
64+
65+
66+
def test_return_response(app, client):
67+
class Index(view.View):
68+
def get(self):
69+
return make_response("GET", 200)
70+
71+
app.add_url_rule("/", view_func=Index.as_view("index"))
72+
73+
with client:
74+
res = client.get("/", headers=[("Accept", "application/json")])
75+
assert res.status_code == 200
76+
assert res.data == b"GET"
77+
78+
79+
def test_missing_method(app, client):
80+
class Index(view.View):
81+
def get(self):
82+
return "GET"
83+
84+
app.add_url_rule("/", view_func=Index.as_view("index"))
85+
86+
with client:
87+
res = client.post("/", headers=[("Accept", "application/json")])
88+
assert res.status_code == 405
89+
90+
91+
def test_missing_head_method(app, client):
92+
class Index(view.View):
93+
def get(self):
94+
return "GET"
95+
96+
app.add_url_rule("/", view_func=Index.as_view("index"))
97+
98+
with client:
99+
res = client.head("/")
100+
assert res.status_code == 200
101+
102+
103+
def test_get_value_text():
104+
class Index(view.View):
105+
def get(self):
106+
return "GET"
107+
108+
assert Index().get_value() == "GET"
109+
110+
111+
def test_get_value_missing():
112+
class Index(view.View):
113+
def post(self):
114+
return "POST"
115+
116+
assert Index().get_value() is None
117+
118+
119+
def test_get_value_raise_if_not_callable():
120+
class Index(view.View):
121+
def post(self):
122+
return "POST"
123+
124+
Index.get = "GET"
125+
126+
with pytest.raises(TypeError):
127+
Index().get_value()
128+
129+
130+
def test_get_value_response_text(app_ctx):
131+
class Index(view.View):
132+
def get(self):
133+
return make_response("GET", 200)
134+
135+
with app_ctx.test_request_context():
136+
assert isinstance(Index().get(), ResponseBase)
137+
assert Index().get().json is None
138+
assert Index().get_value() == "GET"
139+
140+
141+
def test_get_value_response_json(app_ctx):
142+
class Index(view.View):
143+
def get(self):
144+
return make_response({"json": "body"}, 200)
145+
146+
with app_ctx.test_request_context():
147+
assert isinstance(Index().get(), ResponseBase)
148+
assert Index().get().json is not None
149+
assert Index().get_value() == {"json": "body"}

0 commit comments

Comments
 (0)