Skip to content

Commit

Permalink
test: add integration tests for views
Browse files Browse the repository at this point in the history
  • Loading branch information
sansyrox committed Feb 18, 2023
1 parent df78a06 commit a17611f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
13 changes: 13 additions & 0 deletions integration_tests/base_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from robyn.robyn import Response
from robyn.templating import JinjaTemplate

from integration_tests.views.test_view import TestView

app = Robyn(__file__)
websocket = WS(app, "/web_socket")

Expand Down Expand Up @@ -484,6 +486,16 @@ async def async_raise():

# ===== Main =====

@app.view("/test_decorator_view")
def decorator_view():
def get():
return "Hello, world!"

def post(request):
body = bytearray(request["body"]).decode("utf-8")
return {"status_code": 200, "body": body}



if __name__ == "__main__":
app.add_request_header("server", "robyn")
Expand All @@ -493,4 +505,5 @@ async def async_raise():
index_file="index.html",
)
app.startup_handler(startup_handler)
app.add_view("/test_view", TestView)
app.start(port=8080)
21 changes: 21 additions & 0 deletions integration_tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from http_methods_helpers import get, post


def test_view(session):
r = get("/test_view")
assert r.status_code == 200
assert r.text == "Get Request!"

r = post("/test_view", data={"name": "John"})
assert r.status_code == 200
assert r.text == "Post Request!"


def test_decorator_view(session):
r = get("/test_decorator_view")
assert r.status_code == 200
assert r.text == "Hello, world!"

r = post("/test_decorator_view")
assert r.status_code == 200
assert r.text == "Hello, world!"
12 changes: 12 additions & 0 deletions integration_tests/views/test_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def TestView():
def get():
return "Hello, world!"

def post(request):
body = bytes(request["body"]).decode("utf-8")
print(body)
return {
"status": 200,
"body": body,
"headers": {"Content-Type": "text/json"},
}
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ name = "robyn"
dependencies = [
'watchdog == 2.2.1',
'multiprocess == 0.70.14',
'nestd==0.3.0',
# conditional
'uvloop == 0.17.0; sys_platform == "darwin"',
'uvloop == 0.17.0; platform_machine == "x86_64"',
Expand Down
20 changes: 4 additions & 16 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
from robyn.types import Directory, Header
from robyn.ws import WS

logger = logging.getLogger(__name__)


class Robyn:
"""This is the python wrapper for the Robyn binaries."""
Expand Down Expand Up @@ -168,44 +166,34 @@ def terminating_signal_handler(_sig, _frame):
observer.stop()
observer.join()

def add_view(self, endpoint: str, view, const: bool = False):
def add_view(self, endpoint: str, view: Callable, const: bool = False):
"""
[This is base handler for the view decorators]
:param endpoint [str]: [endpoint for the route added]
:param handler [function]: [represents the function passed as a parent handler for single route with different route types]
"""
http_methods = {"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"}

def get_functions(view):
functions = get_all_nested(view)
output = []
for name, handler in functions:
route_type = name.upper()
if route_type in [
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"HEAD",
"OPTIONS",
]:
if route_type in http_methods:
output.append((route_type.upper(), handler))
return output

handlers = get_functions(view)
routes = []
for route_type, handler in handlers:
routes.append(self._add_route(route_type, endpoint, handler, const))
return routes
self._add_route(route_type, endpoint, handler, const)

def view(self, endpoint: str, const: bool = False):
"""
The @app.view decorator to add a view with the GET/POST/PUT/DELETE/PATCH/HEAD/OPTIONS method
:param endpoint str: endpoint to server the route
"""

def inner(handler):
return self.add_view(endpoint, handler, const)

Expand Down

0 comments on commit a17611f

Please sign in to comment.