Skip to content

Commit 0b2cbbb

Browse files
author
Joel Collins
committed
Added basic marshalling tests
1 parent 4ddc120 commit 0b2cbbb

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

tests/test_marshalling.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from labthings.marshalling import marshalling as ms
2+
from labthings.schema import Schema
3+
from labthings import fields
4+
5+
6+
def test_schema_to_converter_schema():
7+
class TestSchema(Schema):
8+
foo = fields.String()
9+
10+
test_schema = TestSchema()
11+
converter = ms.schema_to_converter(test_schema)
12+
assert converter == test_schema.dump
13+
assert converter({"foo": 5}) == {"foo": "5"}
14+
15+
16+
def test_schema_to_converter_map():
17+
test_schema = {"foo": fields.String()}
18+
converter = ms.schema_to_converter(test_schema)
19+
assert converter({"foo": 5}) == {"foo": "5"}
20+
21+
22+
def test_schema_to_converter_field():
23+
field = fields.String()
24+
converter = ms.schema_to_converter(field)
25+
assert converter(5) == "5"
26+
27+
28+
def test_schema_to_converter_none():
29+
assert ms.schema_to_converter(object()) is None

tests/test_marshalling_args.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from labthings import views, fields
2+
from labthings.schema import Schema
3+
from labthings.marshalling.args import use_args, use_body
4+
5+
6+
def test_use_body_string(app, client):
7+
class Index(views.MethodView):
8+
@use_body(fields.String(required=True))
9+
def post(self, args):
10+
return args
11+
12+
app.add_url_rule("/", view_func=Index.as_view("index"))
13+
14+
with client:
15+
res = client.post("/", data="string", content_type="application/json")
16+
assert res.data.decode() == "string"
17+
18+
19+
def test_use_body_no_data_error(app, client):
20+
class Index(views.MethodView):
21+
@use_body(fields.String(required=True))
22+
def post(self, args):
23+
return args
24+
25+
app.add_url_rule("/", view_func=Index.as_view("index"))
26+
27+
with client:
28+
res = client.post("/", content_type="application/json")
29+
assert res.status_code == 400
30+
31+
32+
def test_use_body_no_data_missing(app, client):
33+
class Index(views.MethodView):
34+
@use_body(fields.String(missing="default"))
35+
def post(self, args):
36+
return args
37+
38+
app.add_url_rule("/", view_func=Index.as_view("index"))
39+
40+
with client:
41+
res = client.post("/", content_type="application/json")
42+
assert res.data.decode() == "default"
43+
44+
45+
def test_use_body_validation_error(app, client):
46+
class Index(views.MethodView):
47+
@use_body(fields.String(required=True))
48+
def post(self, args):
49+
return args
50+
51+
app.add_url_rule("/", view_func=Index.as_view("index"))
52+
53+
with client:
54+
res = client.post("/", json={"foo": "bar"}, content_type="application/json")
55+
assert res.status_code == 400
56+
57+
58+
def test_use_args_field(app, client):
59+
class Index(views.MethodView):
60+
@use_args(fields.String(required=True))
61+
def post(self, args):
62+
return args
63+
64+
app.add_url_rule("/", view_func=Index.as_view("index"))
65+
66+
with client:
67+
res = client.post("/", data="string", content_type="application/json")
68+
assert res.data.decode() == "string"
69+
70+
71+
def test_use_args_map(app, client):
72+
class Index(views.MethodView):
73+
@use_args({"foo": fields.String(required=True)})
74+
def post(self, args):
75+
return args
76+
77+
app.add_url_rule("/", view_func=Index.as_view("index"))
78+
79+
with client:
80+
res = client.post("/", json={"foo": "bar"}, content_type="application/json")
81+
assert res.json == {"foo": "bar"}
82+
83+
84+
def test_use_args_schema(app, client):
85+
class TestSchema(Schema):
86+
foo = fields.String(required=True)
87+
88+
class Index(views.MethodView):
89+
@use_args(TestSchema())
90+
def post(self, args):
91+
return args
92+
93+
app.add_url_rule("/", view_func=Index.as_view("index"))
94+
95+
with client:
96+
res = client.post("/", json={"foo": "bar"}, content_type="application/json")
97+
assert res.json == {"foo": "bar"}

0 commit comments

Comments
 (0)