Skip to content

Commit

Permalink
Added CBOR representation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jtc42 committed May 2, 2020
1 parent c6cccdd commit efcc038
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
27 changes: 27 additions & 0 deletions tests/test_server_representations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from labthings.server import representations
from flask import Response
import cbor2
import pytest


Expand Down Expand Up @@ -60,3 +61,29 @@ def test_pretty_output_json(app_ctx_debug):
response.data
== b'{\n "a": "String",\n "b": 5,\n "c": [\n 10,\n 20,\n 30,\n 40,\n 50\n ],\n "d": {\n "a": "String",\n "b": 5,\n "c": [\n 10,\n 20,\n 30,\n 40,\n 50\n ]\n }\n}\n'
)


def test_encode_cbor():
data = {
"a": "String",
"b": 5,
"c": [10, 20, 30, 40, 50],
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
}
assert cbor2.loads(representations.encode_cbor(data)) == data


def test_output_cbor(app_ctx):
data = {
"a": "String",
"b": 5,
"c": [10, 20, 30, 40, 50],
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
}

with app_ctx.test_request_context():
response = representations.output_cbor(data, 200)
assert isinstance(response, Response)
assert response.status_code == 200
assert response.headers.get("Content-Type") == "application/cbor"
assert cbor2.loads(response.data) == data
35 changes: 29 additions & 6 deletions tests/test_server_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from werkzeug.wrappers import Response as ResponseBase
from flask import make_response

import json
import cbor2

import pytest


Expand Down Expand Up @@ -53,14 +56,29 @@ def post(self):
def test_accept_default_application_json(app, client):
class Index(view.View):
def get(self):
return "GET"
return {"key": "value"}

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

with client:
res = client.get("/", headers=[("Accept", "application/json")])
res = client.get("/")
assert res.status_code == 200
assert res.content_type == "application/json"
assert json.loads(res.data) == {"key": "value"}


def test_accept_default_application_cbor(app, cbor_client):
class Index(view.View):
def get(self):
return {"key": "value"}

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

with cbor_client:
res = cbor_client.get("/")
assert res.status_code == 200
assert res.content_type == "application/cbor"
assert cbor2.loads(res.data) == {"key": "value"}


def test_return_response(app, client):
Expand All @@ -71,7 +89,7 @@ def get(self):
app.add_url_rule("/", view_func=Index.as_view("index"))

with client:
res = client.get("/", headers=[("Accept", "application/json")])
res = client.get("/")
assert res.status_code == 200
assert res.data == b"GET"

Expand All @@ -84,7 +102,7 @@ def get(self):
app.add_url_rule("/", view_func=Index.as_view("index"))

with client:
res = client.post("/", headers=[("Accept", "application/json")])
res = client.post("/")
assert res.status_code == 405


Expand All @@ -105,6 +123,7 @@ class Index(view.View):
def get(self):
return "GET"

# Main test
assert Index().get_value() == "GET"


Expand All @@ -113,6 +132,7 @@ class Index(view.View):
def post(self):
return "POST"

# Main test
assert Index().get_value() is None


Expand All @@ -124,6 +144,7 @@ def post(self):
Index.get = "GET"

with pytest.raises(TypeError):
# Main test
Index().get_value()


Expand All @@ -134,7 +155,8 @@ def get(self):

with app_ctx.test_request_context():
assert isinstance(Index().get(), ResponseBase)
assert Index().get().json is None
assert Index().get().headers.get("Content-Type") == "text/html; charset=utf-8"
# Main test
assert Index().get_value() == "GET"


Expand All @@ -145,5 +167,6 @@ def get(self):

with app_ctx.test_request_context():
assert isinstance(Index().get(), ResponseBase)
assert Index().get().json is not None
assert Index().get().headers.get("Content-Type") == "application/json"
# Main test
assert Index().get_value() == {"json": "body"}

0 comments on commit efcc038

Please sign in to comment.