Skip to content

Commit

Permalink
Tidied up client fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Apr 16, 2020
1 parent 65d3b0e commit 7d7d679
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
16 changes: 8 additions & 8 deletions tests/test_server_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def put(self, args):

app.add_url_rule("/", view_func=WrappedCls.as_view("index"))

with app.test_client() as c:
with client as c:
assert c.get("/").data == b'{"integer":1}\n'
assert c.post("/", json={"integer": 5}).data == b'{"integer":5}\n'
assert c.put("/", json={"integer": 5}).data == b'{"integer":5}\n'
Expand All @@ -204,7 +204,7 @@ def post(self, data):

app.add_url_rule("/", view_func=Index.as_view("index"))

with app.test_client() as c:
with client as c:
assert c.post("/", data=b"5\n").data == b"5"


Expand All @@ -220,7 +220,7 @@ def post(self, data):

app.add_url_rule("/", view_func=Index.as_view("index"))

with app.test_client() as c:
with client as c:
assert c.post("/").status_code == 400


Expand All @@ -237,7 +237,7 @@ def post(self, data):

app.add_url_rule("/", view_func=Index.as_view("index"))

with app.test_client() as c:
with client as c:
assert c.post("/").status_code == 200


Expand All @@ -253,7 +253,7 @@ def post(self, data):

app.add_url_rule("/", view_func=Index.as_view("index"))

with app.test_client() as c:
with client as c:
assert c.post("/").data == b"5"


Expand All @@ -269,7 +269,7 @@ def post(self, data):

app.add_url_rule("/", view_func=Index.as_view("index"))

with app.test_client() as c:
with client as c:
assert c.post("/", data=b"{}").status_code == 400


Expand All @@ -285,7 +285,7 @@ def post(self, data):

app.add_url_rule("/", view_func=Index.as_view("index"))

with app.test_client() as c:
with client as c:
assert c.post("/", json={"integer": 5}).data == b"{'integer': 5}"


Expand All @@ -301,7 +301,7 @@ def post(self, data):

app.add_url_rule("/", view_func=Index.as_view("index"))

with app.test_client() as c:
with client as c:
assert c.post("/").data == b"5"


Expand Down
20 changes: 10 additions & 10 deletions tests/test_server_view_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
from labthings.server.view import builder


def test_property_of(app):
def test_property_of(app, client):
obj = type("obj", (object,), {"property_name": "propertyValue"})

GeneratedClass = builder.property_of(obj, "property_name")
app.add_url_rule("/", view_func=GeneratedClass.as_view("index"))

with app.test_client() as c:
with client as c:
assert c.get("/").data == b"propertyValue"
assert c.post("/", data=b"newPropertyValue").data == b"newPropertyValue"
assert c.get("/").data == b"newPropertyValue"


def test_property_of_dict(app):
def test_property_of_dict(app, client):
obj = type(
"obj",
(object,),
Expand All @@ -30,7 +30,7 @@ def test_property_of_dict(app):
GeneratedClass = builder.property_of(obj, "properties")
app.add_url_rule("/", view_func=GeneratedClass.as_view("index"))

with app.test_client() as c:
with client as c:
assert (
c.get("/").data
== b'{"property_name":"propertyValue","property_name_2":"propertyValue2"}\n'
Expand Down Expand Up @@ -64,25 +64,25 @@ def test_property_of_name_description():
assert GeneratedClass.__apispec__.get("summary") == "property description"


def test_action_from(app):
def test_action_from(app, client):
def f(arg: int, kwarg: str = "default"):
return {"arg": arg, "kwarg": kwarg}

GeneratedClass = builder.action_from(f)
app.add_url_rule("/", view_func=GeneratedClass.as_view("index"))

with app.test_client() as c:
with client as c:
assert c.post("/", json={"arg": 5}).data == b'{"arg":5,"kwarg":"default"}\n'


def test_action_from_task(app):
def test_action_from_task(app, client):
def f(arg: int, kwarg: str = "default"):
return {"arg": arg, "kwarg": kwarg}

GeneratedClass = builder.action_from(f, task=True,)
app.add_url_rule("/", view_func=GeneratedClass.as_view("index"))

with app.test_client() as c:
with client as c:
response = c.post("/", json={"arg": 5}).json
# Check we get back a Task representation
assert isinstance(response, dict)
Expand All @@ -103,15 +103,15 @@ def f(arg: int, kwarg: str = "default"):
)


def test_static_from(app, app_ctx, static_path):
def test_static_from(app, client, app_ctx, static_path):

GeneratedClass = builder.static_from(static_path,)
app.add_url_rule("/static", view_func=GeneratedClass.as_view("index"))

with app_ctx.test_request_context():
assert GeneratedClass().get("text").status_code == 200

with app.test_client() as c:
with client as c:
assert c.get("/static/text").data == b"text"


Expand Down

0 comments on commit 7d7d679

Please sign in to comment.