From 8eefdd1b2aa08953c6ee667faa468cb52c7e4071 Mon Sep 17 00:00:00 2001 From: Mikaeel Date: Sun, 12 Feb 2023 02:24:03 +0330 Subject: [PATCH 1/8] feat: Add view controllers --- requirements.txt | 1 + robyn/__init__.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/requirements.txt b/requirements.txt index 00cda3741..493b161c1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ uvloop; platform_system!="Windows" watchdog==2.2.1 multiprocess==0.70.14 jinja2==3.1.2 +nestd===0.3.0 \ No newline at end of file diff --git a/robyn/__init__.py b/robyn/__init__.py index bfc24987c..013340fa1 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -4,6 +4,7 @@ import os import signal from typing import Callable, List, Optional +from nestd import get_all_nested from watchdog.observers import Observer @@ -165,6 +166,40 @@ def terminating_signal_handler(_sig, _frame): observer.stop() observer.join() + + def add_view(self, endpoint: str, view, 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] + """ + 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']: + 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 + + 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) + + return inner + def get(self, endpoint: str, const: bool = False): """ The @app.get decorator to add a route with the GET method From 53c93bbf99ec30687d2f15ef8f3e3a57c03c585e Mon Sep 17 00:00:00 2001 From: Mikaeel Date: Sun, 12 Feb 2023 02:45:49 +0330 Subject: [PATCH 2/8] fix: remove = character --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 493b161c1..978e9bff1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,4 @@ uvloop; platform_system!="Windows" watchdog==2.2.1 multiprocess==0.70.14 jinja2==3.1.2 -nestd===0.3.0 \ No newline at end of file +nestd==0.3.0 \ No newline at end of file From aa7a272608d3c7b07a2eee6449b108f02de4be57 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 Feb 2023 23:17:46 +0000 Subject: [PATCH 3/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- robyn/__init__.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/robyn/__init__.py b/robyn/__init__.py index 013340fa1..838754f7d 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -21,6 +21,7 @@ from robyn.ws import WS + class Robyn: """This is the python wrapper for the Robyn binaries.""" @@ -166,7 +167,6 @@ def terminating_signal_handler(_sig, _frame): observer.stop() observer.join() - def add_view(self, endpoint: str, view, const: bool = False): """ [This is base handler for the view decorators] @@ -174,18 +174,27 @@ def add_view(self, endpoint: str, view, const: bool = False): :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] """ + def get_functions(view): functions = get_all_nested(view) output = [] - for (name, handler) in functions: + for name, handler in functions: route_type = name.upper() - if route_type in ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']: + if route_type in [ + "GET", + "POST", + "PUT", + "DELETE", + "PATCH", + "HEAD", + "OPTIONS", + ]: output.append((route_type.upper(), handler)) return output handlers = get_functions(view) routes = [] - for (route_type, handler) in handlers: + for route_type, handler in handlers: routes.append(self._add_route(route_type, endpoint, handler, const)) return routes @@ -195,6 +204,7 @@ def view(self, endpoint: str, const: bool = False): :param endpoint str: endpoint to server the route """ + def inner(handler): return self.add_view(endpoint, handler, const) From c347963e8251ee5fb30611d2938714807952ab0f Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Sat, 18 Feb 2023 13:26:30 +0000 Subject: [PATCH 4/8] test: add integration tests for views --- integration_tests/__init__.py | 0 integration_tests/base_routes.py | 25 +++++++++++++++++++++++ integration_tests/conftest.py | 2 +- integration_tests/test_async_views.py | 25 +++++++++++++++++++++++ integration_tests/test_base_url.py | 2 +- integration_tests/test_basic_routes.py | 2 +- integration_tests/test_delete_requests.py | 2 +- integration_tests/test_file_download.py | 2 +- integration_tests/test_get_requests.py | 2 +- integration_tests/test_middlewares.py | 2 +- integration_tests/test_patch_requests.py | 2 +- integration_tests/test_post_requests.py | 2 +- integration_tests/test_put_requests.py | 2 +- integration_tests/test_status_code.py | 2 +- integration_tests/test_sync_views.py | 25 +++++++++++++++++++++++ integration_tests/views/__init__.py | 4 ++++ integration_tests/views/async_view.py | 11 ++++++++++ integration_tests/views/test_view.py | 11 ++++++++++ pyproject.toml | 1 + requirements.txt | 2 +- robyn/__init__.py | 19 ++++------------- 21 files changed, 118 insertions(+), 27 deletions(-) create mode 100644 integration_tests/__init__.py create mode 100644 integration_tests/test_async_views.py create mode 100644 integration_tests/test_sync_views.py create mode 100644 integration_tests/views/__init__.py create mode 100644 integration_tests/views/async_view.py create mode 100644 integration_tests/views/test_view.py diff --git a/integration_tests/__init__.py b/integration_tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/integration_tests/base_routes.py b/integration_tests/base_routes.py index e761337de..5aa5d11e9 100644 --- a/integration_tests/base_routes.py +++ b/integration_tests/base_routes.py @@ -5,6 +5,8 @@ from robyn.robyn import Response from robyn.templating import JinjaTemplate +from integration_tests.views import SyncView, AsyncView + app = Robyn(__file__) websocket = WS(app, "/web_socket") @@ -482,6 +484,27 @@ async def async_raise(): raise Exception() +# ===== Views ===== + +@app.view("/sync_decorator_view") +def sync_decorator_view(): + def get(): + return "Hello, world!" + + def post(request): + body = bytearray(request["body"]).decode("utf-8") + return {"status_code": 200, "body": body} + + +@app.view("/async_decorator_view") +def async_decorator_view(): + async def get(): + return "Hello, world!" + + async def post(request): + body = bytearray(request["body"]).decode("utf-8") + return {"status_code": 200, "body": body} + # ===== Main ===== @@ -493,4 +516,6 @@ async def async_raise(): index_file="index.html", ) app.startup_handler(startup_handler) + app.add_view("/sync_view", SyncView) + app.add_view("/async_view", AsyncView) app.start(port=8080) diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py index 4b174f4ac..ed571e46f 100644 --- a/integration_tests/conftest.py +++ b/integration_tests/conftest.py @@ -8,7 +8,7 @@ import platform import pytest -from helpers.network_helpers import get_network_host +from integration_tests.helpers.network_helpers import get_network_host def spawn_process(command: List[str]) -> subprocess.Popen: diff --git a/integration_tests/test_async_views.py b/integration_tests/test_async_views.py new file mode 100644 index 000000000..e1757b6f4 --- /dev/null +++ b/integration_tests/test_async_views.py @@ -0,0 +1,25 @@ +from integration_tests.helpers.http_methods_helpers import get, post + + +def test_get_async_view(session): + r = get("/async_view") + assert r.status_code == 200 + assert r.text == "Hello, world!" + + +def test_post_async_view(session): + r = post("/async_view", data={"name": "John"}) + assert r.status_code == 200 + assert "John" in r.text + + +def test_get_async_decorator_view(session): + r = get("/async_decorator_view") + assert r.status_code == 200 + assert r.text == "Hello, world!" + + +def test_post_async_decorator_view(session): + r = post("/async_decorator_view", data={"name": "John"}) + assert r.status_code == 200 + assert "John" in r.text diff --git a/integration_tests/test_base_url.py b/integration_tests/test_base_url.py index 146230c9e..137ada22d 100644 --- a/integration_tests/test_base_url.py +++ b/integration_tests/test_base_url.py @@ -2,7 +2,7 @@ import requests -from helpers.network_helpers import get_network_host +from integration_tests.helpers.network_helpers import get_network_host def test_default_url_index_request(default_session): diff --git a/integration_tests/test_basic_routes.py b/integration_tests/test_basic_routes.py index d07c723cf..57c29c278 100644 --- a/integration_tests/test_basic_routes.py +++ b/integration_tests/test_basic_routes.py @@ -7,7 +7,7 @@ import pytest -from helpers.http_methods_helpers import get +from integration_tests.helpers.http_methods_helpers import get @pytest.mark.parametrize( diff --git a/integration_tests/test_delete_requests.py b/integration_tests/test_delete_requests.py index 818adc7eb..8df977310 100644 --- a/integration_tests/test_delete_requests.py +++ b/integration_tests/test_delete_requests.py @@ -1,5 +1,5 @@ import pytest -from helpers.http_methods_helpers import delete +from integration_tests.helpers.http_methods_helpers import delete @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_file_download.py b/integration_tests/test_file_download.py index a6ec8a4e9..c34733dd3 100644 --- a/integration_tests/test_file_download.py +++ b/integration_tests/test_file_download.py @@ -1,5 +1,5 @@ import pytest -from helpers.http_methods_helpers import get +from integration_tests.helpers.http_methods_helpers import get @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_get_requests.py b/integration_tests/test_get_requests.py index 6792b5004..965f547cd 100644 --- a/integration_tests/test_get_requests.py +++ b/integration_tests/test_get_requests.py @@ -1,7 +1,7 @@ import pytest from requests import Response -from helpers.http_methods_helpers import get +from integration_tests.helpers.http_methods_helpers import get @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_middlewares.py b/integration_tests/test_middlewares.py index 608c4da85..c9a62baa5 100644 --- a/integration_tests/test_middlewares.py +++ b/integration_tests/test_middlewares.py @@ -1,6 +1,6 @@ import pytest -from helpers.http_methods_helpers import get +from integration_tests.helpers.http_methods_helpers import get @pytest.mark.skip(reason="Fix middleware request headers modification") diff --git a/integration_tests/test_patch_requests.py b/integration_tests/test_patch_requests.py index 6aa90b6c1..6b0b761f5 100644 --- a/integration_tests/test_patch_requests.py +++ b/integration_tests/test_patch_requests.py @@ -1,5 +1,5 @@ import pytest -from helpers.http_methods_helpers import patch +from integration_tests.helpers.http_methods_helpers import patch @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_post_requests.py b/integration_tests/test_post_requests.py index 5e538e5c8..7315a939e 100644 --- a/integration_tests/test_post_requests.py +++ b/integration_tests/test_post_requests.py @@ -1,5 +1,5 @@ import pytest -from helpers.http_methods_helpers import post +from integration_tests.helpers.http_methods_helpers import post @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_put_requests.py b/integration_tests/test_put_requests.py index b4e7b77d9..a233d8f22 100644 --- a/integration_tests/test_put_requests.py +++ b/integration_tests/test_put_requests.py @@ -1,5 +1,5 @@ import pytest -from helpers.http_methods_helpers import put +from integration_tests.helpers.http_methods_helpers import put @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_status_code.py b/integration_tests/test_status_code.py index 17061ce13..1b6b90b44 100644 --- a/integration_tests/test_status_code.py +++ b/integration_tests/test_status_code.py @@ -1,5 +1,5 @@ import pytest -from helpers.http_methods_helpers import get +from integration_tests.helpers.http_methods_helpers import get def test_404_status_code(session): diff --git a/integration_tests/test_sync_views.py b/integration_tests/test_sync_views.py new file mode 100644 index 000000000..689dad3dd --- /dev/null +++ b/integration_tests/test_sync_views.py @@ -0,0 +1,25 @@ +from integration_tests.helpers.http_methods_helpers import get, post + + +def test_get_async_view(session): + r = get("/sync_view") + assert r.status_code == 200 + assert r.text == "Hello, world!" + + +def test_post_async_view(session): + r = post("/sync_view", data={"name": "John"}) + assert r.status_code == 200 + assert "John" in r.text + + +def test_get_async_decorator_view(session): + r = get("/sync_decorator_view") + assert r.status_code == 200 + assert r.text == "Hello, world!" + + +def test_post_async_decorator_view(session): + r = post("/sync_decorator_view", data={"name": "John"}) + assert r.status_code == 200 + assert "John" in r.text diff --git a/integration_tests/views/__init__.py b/integration_tests/views/__init__.py new file mode 100644 index 000000000..b5bd9e407 --- /dev/null +++ b/integration_tests/views/__init__.py @@ -0,0 +1,4 @@ +from .test_view import SyncView +from .async_view import AsyncView + +__all__ = ["SyncView", "AsyncView"] diff --git a/integration_tests/views/async_view.py b/integration_tests/views/async_view.py new file mode 100644 index 000000000..644674303 --- /dev/null +++ b/integration_tests/views/async_view.py @@ -0,0 +1,11 @@ +def AsyncView(): + async def get(): + return "Hello, world!" + + async def post(request): + body = bytes(request["body"]).decode("utf-8") + return { + "status": 200, + "body": body, + "headers": {"Content-Type": "text/json"}, + } diff --git a/integration_tests/views/test_view.py b/integration_tests/views/test_view.py new file mode 100644 index 000000000..2aec64f43 --- /dev/null +++ b/integration_tests/views/test_view.py @@ -0,0 +1,11 @@ +def SyncView(): + def get(): + return "Hello, world!" + + def post(request): + body = bytes(request["body"]).decode("utf-8") + return { + "status": 200, + "body": body, + "headers": {"Content-Type": "text/json"}, + } diff --git a/pyproject.toml b/pyproject.toml index 529223046..450509e36 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ name = "robyn" dependencies = [ 'watchdog == 2.2.1', 'multiprocess == 0.70.14', + 'nestd==0.3.1', # conditional 'uvloop == 0.17.0; sys_platform == "darwin"', 'uvloop == 0.17.0; platform_machine == "x86_64"', diff --git a/requirements.txt b/requirements.txt index 978e9bff1..69ab33175 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,4 @@ uvloop; platform_system!="Windows" watchdog==2.2.1 multiprocess==0.70.14 jinja2==3.1.2 -nestd==0.3.0 \ No newline at end of file +nestd==0.3.1 diff --git a/robyn/__init__.py b/robyn/__init__.py index 838754f7d..84a54125a 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -21,7 +21,6 @@ from robyn.ws import WS - class Robyn: """This is the python wrapper for the Robyn binaries.""" @@ -167,36 +166,27 @@ 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): """ @@ -204,7 +194,6 @@ def view(self, endpoint: str, const: bool = False): :param endpoint str: endpoint to server the route """ - def inner(handler): return self.add_view(endpoint, handler, const) From f1f68d3b721774f0982d846512cc34c763fca890 Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Sat, 18 Feb 2023 18:16:38 +0000 Subject: [PATCH 5/8] fix: pytest namespacing options --- .github/workflows/python-CI.yml | 2 +- integration_tests/__init__.py | 0 pyproject.toml | 3 +++ 3 files changed, 4 insertions(+), 1 deletion(-) delete mode 100644 integration_tests/__init__.py diff --git a/.github/workflows/python-CI.yml b/.github/workflows/python-CI.yml index 0888985e5..449f5bc16 100644 --- a/.github/workflows/python-CI.yml +++ b/.github/workflows/python-CI.yml @@ -32,4 +32,4 @@ jobs: pip install --no-index --find-links=dist/ robyn - name: Test with pytest run: | - pytest ./integration_tests + pytest diff --git a/integration_tests/__init__.py b/integration_tests/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/pyproject.toml b/pyproject.toml index 450509e36..509734291 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,9 @@ Changelog = "https://github.com/sansyrox/robyn/blob/main/CHANGELOG.md" [project.optional-dependencies] "templating" = ["jinja2 == 3.0.1"] +[tool.pytest.ini_options] +pythonpaths = ["."] + [tool.ruff] line-length = 160 From 165d6934ea64a15ca3ea2954ea15b270f302c58f Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Sat, 18 Feb 2023 23:25:31 +0000 Subject: [PATCH 6/8] fix: tests --- integration_tests/base_routes.py | 2 +- integration_tests/conftest.py | 2 +- integration_tests/test_async_views.py | 2 +- integration_tests/test_base_url.py | 2 +- integration_tests/test_basic_routes.py | 2 +- integration_tests/test_delete_requests.py | 2 +- integration_tests/test_file_download.py | 2 +- integration_tests/test_get_requests.py | 2 +- integration_tests/test_middlewares.py | 2 +- integration_tests/test_patch_requests.py | 2 +- integration_tests/test_post_requests.py | 2 +- integration_tests/test_put_requests.py | 2 +- integration_tests/test_status_code.py | 2 +- integration_tests/test_sync_views.py | 2 +- pyproject.toml | 4 ---- 15 files changed, 14 insertions(+), 18 deletions(-) diff --git a/integration_tests/base_routes.py b/integration_tests/base_routes.py index 5aa5d11e9..89df67895 100644 --- a/integration_tests/base_routes.py +++ b/integration_tests/base_routes.py @@ -5,7 +5,7 @@ from robyn.robyn import Response from robyn.templating import JinjaTemplate -from integration_tests.views import SyncView, AsyncView +from views import SyncView, AsyncView app = Robyn(__file__) websocket = WS(app, "/web_socket") diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py index ed571e46f..4b174f4ac 100644 --- a/integration_tests/conftest.py +++ b/integration_tests/conftest.py @@ -8,7 +8,7 @@ import platform import pytest -from integration_tests.helpers.network_helpers import get_network_host +from helpers.network_helpers import get_network_host def spawn_process(command: List[str]) -> subprocess.Popen: diff --git a/integration_tests/test_async_views.py b/integration_tests/test_async_views.py index e1757b6f4..0c4969543 100644 --- a/integration_tests/test_async_views.py +++ b/integration_tests/test_async_views.py @@ -1,4 +1,4 @@ -from integration_tests.helpers.http_methods_helpers import get, post +from helpers.http_methods_helpers import get, post def test_get_async_view(session): diff --git a/integration_tests/test_base_url.py b/integration_tests/test_base_url.py index 137ada22d..146230c9e 100644 --- a/integration_tests/test_base_url.py +++ b/integration_tests/test_base_url.py @@ -2,7 +2,7 @@ import requests -from integration_tests.helpers.network_helpers import get_network_host +from helpers.network_helpers import get_network_host def test_default_url_index_request(default_session): diff --git a/integration_tests/test_basic_routes.py b/integration_tests/test_basic_routes.py index 57c29c278..d07c723cf 100644 --- a/integration_tests/test_basic_routes.py +++ b/integration_tests/test_basic_routes.py @@ -7,7 +7,7 @@ import pytest -from integration_tests.helpers.http_methods_helpers import get +from helpers.http_methods_helpers import get @pytest.mark.parametrize( diff --git a/integration_tests/test_delete_requests.py b/integration_tests/test_delete_requests.py index 8df977310..818adc7eb 100644 --- a/integration_tests/test_delete_requests.py +++ b/integration_tests/test_delete_requests.py @@ -1,5 +1,5 @@ import pytest -from integration_tests.helpers.http_methods_helpers import delete +from helpers.http_methods_helpers import delete @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_file_download.py b/integration_tests/test_file_download.py index c34733dd3..a6ec8a4e9 100644 --- a/integration_tests/test_file_download.py +++ b/integration_tests/test_file_download.py @@ -1,5 +1,5 @@ import pytest -from integration_tests.helpers.http_methods_helpers import get +from helpers.http_methods_helpers import get @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_get_requests.py b/integration_tests/test_get_requests.py index 965f547cd..6792b5004 100644 --- a/integration_tests/test_get_requests.py +++ b/integration_tests/test_get_requests.py @@ -1,7 +1,7 @@ import pytest from requests import Response -from integration_tests.helpers.http_methods_helpers import get +from helpers.http_methods_helpers import get @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_middlewares.py b/integration_tests/test_middlewares.py index c9a62baa5..608c4da85 100644 --- a/integration_tests/test_middlewares.py +++ b/integration_tests/test_middlewares.py @@ -1,6 +1,6 @@ import pytest -from integration_tests.helpers.http_methods_helpers import get +from helpers.http_methods_helpers import get @pytest.mark.skip(reason="Fix middleware request headers modification") diff --git a/integration_tests/test_patch_requests.py b/integration_tests/test_patch_requests.py index 6b0b761f5..6aa90b6c1 100644 --- a/integration_tests/test_patch_requests.py +++ b/integration_tests/test_patch_requests.py @@ -1,5 +1,5 @@ import pytest -from integration_tests.helpers.http_methods_helpers import patch +from helpers.http_methods_helpers import patch @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_post_requests.py b/integration_tests/test_post_requests.py index 7315a939e..5e538e5c8 100644 --- a/integration_tests/test_post_requests.py +++ b/integration_tests/test_post_requests.py @@ -1,5 +1,5 @@ import pytest -from integration_tests.helpers.http_methods_helpers import post +from helpers.http_methods_helpers import post @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_put_requests.py b/integration_tests/test_put_requests.py index a233d8f22..b4e7b77d9 100644 --- a/integration_tests/test_put_requests.py +++ b/integration_tests/test_put_requests.py @@ -1,5 +1,5 @@ import pytest -from integration_tests.helpers.http_methods_helpers import put +from helpers.http_methods_helpers import put @pytest.mark.parametrize("function_type", ["sync", "async"]) diff --git a/integration_tests/test_status_code.py b/integration_tests/test_status_code.py index 1b6b90b44..17061ce13 100644 --- a/integration_tests/test_status_code.py +++ b/integration_tests/test_status_code.py @@ -1,5 +1,5 @@ import pytest -from integration_tests.helpers.http_methods_helpers import get +from helpers.http_methods_helpers import get def test_404_status_code(session): diff --git a/integration_tests/test_sync_views.py b/integration_tests/test_sync_views.py index 689dad3dd..ea2748e1c 100644 --- a/integration_tests/test_sync_views.py +++ b/integration_tests/test_sync_views.py @@ -1,4 +1,4 @@ -from integration_tests.helpers.http_methods_helpers import get, post +from helpers.http_methods_helpers import get, post def test_get_async_view(session): diff --git a/pyproject.toml b/pyproject.toml index 509734291..5ae950d95 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,10 +44,6 @@ Changelog = "https://github.com/sansyrox/robyn/blob/main/CHANGELOG.md" [project.optional-dependencies] "templating" = ["jinja2 == 3.0.1"] -[tool.pytest.ini_options] -pythonpaths = ["."] - - [tool.ruff] line-length = 160 exclude = ["src/*" , ".git" , "docs"] From 6a2e76f4b1582d9ce2b0e6cc5cb6479e4bbc47ca Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 18 Feb 2023 23:26:02 +0000 Subject: [PATCH 7/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- integration_tests/base_routes.py | 2 ++ robyn/__init__.py | 1 + 2 files changed, 3 insertions(+) diff --git a/integration_tests/base_routes.py b/integration_tests/base_routes.py index 89df67895..f49ea6aab 100644 --- a/integration_tests/base_routes.py +++ b/integration_tests/base_routes.py @@ -486,6 +486,7 @@ async def async_raise(): # ===== Views ===== + @app.view("/sync_decorator_view") def sync_decorator_view(): def get(): @@ -505,6 +506,7 @@ async def post(request): body = bytearray(request["body"]).decode("utf-8") return {"status_code": 200, "body": body} + # ===== Main ===== diff --git a/robyn/__init__.py b/robyn/__init__.py index 84a54125a..c915e12a8 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -194,6 +194,7 @@ def view(self, endpoint: str, const: bool = False): :param endpoint str: endpoint to server the route """ + def inner(handler): return self.add_view(endpoint, handler, const) From 4b5dd5be1451706935aedc94cfe8d89bdad12d3c Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Sun, 19 Feb 2023 19:22:38 +0000 Subject: [PATCH 8/8] style: Refactor code to make things cleaner --- integration_tests/base_routes.py | 8 ++-- integration_tests/test_async_views.py | 25 ----------- integration_tests/test_sync_views.py | 25 ----------- integration_tests/test_views.py | 41 +++++++++++++++++++ integration_tests/views/__init__.py | 2 +- .../views/{test_view.py => sync_view.py} | 0 robyn/__init__.py | 16 ++++---- 7 files changed, 54 insertions(+), 63 deletions(-) delete mode 100644 integration_tests/test_async_views.py delete mode 100644 integration_tests/test_sync_views.py create mode 100644 integration_tests/test_views.py rename integration_tests/views/{test_view.py => sync_view.py} (100%) diff --git a/integration_tests/base_routes.py b/integration_tests/base_routes.py index f49ea6aab..6bd656812 100644 --- a/integration_tests/base_routes.py +++ b/integration_tests/base_routes.py @@ -487,7 +487,7 @@ async def async_raise(): # ===== Views ===== -@app.view("/sync_decorator_view") +@app.view("/sync/view/decorator") def sync_decorator_view(): def get(): return "Hello, world!" @@ -497,7 +497,7 @@ def post(request): return {"status_code": 200, "body": body} -@app.view("/async_decorator_view") +@app.view("/async/view/decorator") def async_decorator_view(): async def get(): return "Hello, world!" @@ -518,6 +518,6 @@ async def post(request): index_file="index.html", ) app.startup_handler(startup_handler) - app.add_view("/sync_view", SyncView) - app.add_view("/async_view", AsyncView) + app.add_view("/sync/view", SyncView) + app.add_view("/async/view", AsyncView) app.start(port=8080) diff --git a/integration_tests/test_async_views.py b/integration_tests/test_async_views.py deleted file mode 100644 index 0c4969543..000000000 --- a/integration_tests/test_async_views.py +++ /dev/null @@ -1,25 +0,0 @@ -from helpers.http_methods_helpers import get, post - - -def test_get_async_view(session): - r = get("/async_view") - assert r.status_code == 200 - assert r.text == "Hello, world!" - - -def test_post_async_view(session): - r = post("/async_view", data={"name": "John"}) - assert r.status_code == 200 - assert "John" in r.text - - -def test_get_async_decorator_view(session): - r = get("/async_decorator_view") - assert r.status_code == 200 - assert r.text == "Hello, world!" - - -def test_post_async_decorator_view(session): - r = post("/async_decorator_view", data={"name": "John"}) - assert r.status_code == 200 - assert "John" in r.text diff --git a/integration_tests/test_sync_views.py b/integration_tests/test_sync_views.py deleted file mode 100644 index ea2748e1c..000000000 --- a/integration_tests/test_sync_views.py +++ /dev/null @@ -1,25 +0,0 @@ -from helpers.http_methods_helpers import get, post - - -def test_get_async_view(session): - r = get("/sync_view") - assert r.status_code == 200 - assert r.text == "Hello, world!" - - -def test_post_async_view(session): - r = post("/sync_view", data={"name": "John"}) - assert r.status_code == 200 - assert "John" in r.text - - -def test_get_async_decorator_view(session): - r = get("/sync_decorator_view") - assert r.status_code == 200 - assert r.text == "Hello, world!" - - -def test_post_async_decorator_view(session): - r = post("/sync_decorator_view", data={"name": "John"}) - assert r.status_code == 200 - assert "John" in r.text diff --git a/integration_tests/test_views.py b/integration_tests/test_views.py new file mode 100644 index 000000000..60aa48fbf --- /dev/null +++ b/integration_tests/test_views.py @@ -0,0 +1,41 @@ +from helpers.http_methods_helpers import get, post + + +def test_get_sync_view(session): + r = get("/sync/view") + assert r.text == "Hello, world!" + + +def test_post_sync_view(session): + r = post("/sync/view", data={"name": "John"}) + assert "John" in r.text + + +def test_get_sync_decorator_view(session): + r = get("/sync/view/decorator") + assert r.text == "Hello, world!" + + +def test_post_sync_decorator_view(session): + r = post("/sync/view/decorator", data={"name": "John"}) + assert "John" in r.text + + +def test_get_async_view(session): + r = get("/async/view") + assert r.text == "Hello, world!" + + +def test_post_async_view(session): + r = post("/async/view", data={"name": "John"}) + assert "John" in r.text + + +def test_get_async_decorator_view(session): + r = get("/async/view/decorator") + assert r.text == "Hello, world!" + + +def test_post_async_decorator_view(session): + r = post("/async/view/decorator", data={"name": "John"}) + assert "John" in r.text diff --git a/integration_tests/views/__init__.py b/integration_tests/views/__init__.py index b5bd9e407..669f94bae 100644 --- a/integration_tests/views/__init__.py +++ b/integration_tests/views/__init__.py @@ -1,4 +1,4 @@ -from .test_view import SyncView +from .sync_view import SyncView from .async_view import AsyncView __all__ = ["SyncView", "AsyncView"] diff --git a/integration_tests/views/test_view.py b/integration_tests/views/sync_view.py similarity index 100% rename from integration_tests/views/test_view.py rename to integration_tests/views/sync_view.py diff --git a/robyn/__init__.py b/robyn/__init__.py index c915e12a8..2b086a2ed 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -40,11 +40,11 @@ def __init__(self, file_object: str) -> None: def _add_route(self, route_type, endpoint, handler, is_const=False): """ - [This is base handler for all the decorators] + This is base handler for all the decorators - :param route_type [str]: [route type between GET/POST/PUT/DELETE/PATCH] - :param endpoint [str]: [endpoint for the route added] - :param handler [function]: [represents the sync or async function passed as a handler for the route] + :param route_type str: route type between GET/POST/PUT/DELETE/PATCH + :param endpoint str: endpoint for the route added + :param handler function: represents the sync or async function passed as a handler for the route """ """ We will add the status code here only @@ -168,10 +168,10 @@ def terminating_signal_handler(_sig, _frame): def add_view(self, endpoint: str, view: Callable, const: bool = False): """ - [This is base handler for the view decorators] + 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] + :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"} @@ -181,7 +181,7 @@ def get_functions(view): for name, handler in functions: route_type = name.upper() if route_type in http_methods: - output.append((route_type.upper(), handler)) + output.append((route_type, handler)) return output handlers = get_functions(view)