Skip to content

Commit

Permalink
Server View class tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Apr 14, 2020
1 parent c65aa71 commit 37b4703
Showing 1 changed file with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions tests/test_server_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
from labthings.server import view
from werkzeug.http import parse_set_header
from werkzeug.wrappers import Response as ResponseBase
from flask import make_response

import pytest


def common_test(app):
c = app.test_client()

assert c.get("/").data == b"GET"
assert c.post("/").data == b"POST"
assert c.put("/").status_code == 405
assert c.delete("/").status_code == 405
meths = parse_set_header(c.open("/", method="OPTIONS").headers["Allow"])
assert sorted(meths) == ["GET", "HEAD", "OPTIONS", "POST"]


def test_method_based_view(app):
class Index(view.View):
def get(self):
return "GET"

def post(self):
return "POST"

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


def test_view_patching(app):
class Index(view.View):
def get(self):
1 // 0

def post(self):
1 // 0

class Other(Index):
def get(self):
return "GET"

def post(self):
return "POST"

view_obj = Index.as_view("index")
view_obj.view_class = Other
app.add_url_rule("/", view_func=view_obj)
common_test(app)


def test_accept_default_application_json(app, client):
class Index(view.View):
def get(self):
return "GET"

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

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


def test_return_response(app, client):
class Index(view.View):
def get(self):
return make_response("GET", 200)

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

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


def test_missing_method(app, client):
class Index(view.View):
def get(self):
return "GET"

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

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


def test_missing_head_method(app, client):
class Index(view.View):
def get(self):
return "GET"

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

with client:
res = client.head("/")
assert res.status_code == 200


def test_get_value_text():
class Index(view.View):
def get(self):
return "GET"

assert Index().get_value() == "GET"


def test_get_value_missing():
class Index(view.View):
def post(self):
return "POST"

assert Index().get_value() is None


def test_get_value_raise_if_not_callable():
class Index(view.View):
def post(self):
return "POST"

Index.get = "GET"

with pytest.raises(TypeError):
Index().get_value()


def test_get_value_response_text(app_ctx):
class Index(view.View):
def get(self):
return make_response("GET", 200)

with app_ctx.test_request_context():
assert isinstance(Index().get(), ResponseBase)
assert Index().get().json is None
assert Index().get_value() == "GET"


def test_get_value_response_json(app_ctx):
class Index(view.View):
def get(self):
return make_response({"json": "body"}, 200)

with app_ctx.test_request_context():
assert isinstance(Index().get(), ResponseBase)
assert Index().get().json is not None
assert Index().get_value() == {"json": "body"}

0 comments on commit 37b4703

Please sign in to comment.