Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Commit

Permalink
Refactor routing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
c-bata committed Jan 1, 2017
1 parent 747e841 commit 121a128
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
from unittest import TestCase
from kobin.routes import Route, Router
from kobin.environs import HTTPError
from kobin.environs import HTTPError, Response


class RouteTests(TestCase):
def test_call(self):
def dummy_func(num: int):
return num
def dummy_func(num: int) -> Response:
return Response(f'hello {num}')
route = Route('/hoge/{num}', 'GET', 'hoge', dummy_func)
self.assertEqual(route.callback(**{'num': 1}), 1)
actual_response = route.callback(num=1)
self.assertEqual(actual_response.body, [b'hello 1'])

def test_match_method(self):
def dummy_func(num: int):
return num
def dummy_func(num: int) -> Response:
return Response(f'hello {num}')
route = Route('/hoge/{num}', 'GET', 'hoge', dummy_func)
self.assertTrue(route._match_method('GET'))

def test_match_path(self):
def dummy_func(num: int):
return num
def dummy_func(num: int) -> Response:
return Response(f'hello {num}')
route = Route('/hoge/{num}', 'GET', 'hoge', dummy_func)
self.assertTrue(route._match_path('/hoge/1'))

def test_match_path_with_slash(self):
def dummy_func(num: int):
return num
def dummy_func(num: int) -> Response:
return Response(f'hello {num}')
route = Route('/hoge/{num}', 'GET', 'hoge', dummy_func)
self.assertTrue(route._match_path('hoge/1/'))

def test_match(self):
def dummy_func(num: int):
return num
def dummy_func(num: int) -> Response:
return Response(f'hello {num}')
route = Route('/hoge/{num}', 'GET', 'hoge', dummy_func)
self.assertTrue(route.match('GET', 'hoge/1/'))

Expand All @@ -40,8 +41,8 @@ def setUp(self):
self.router = Router()

def test_match_dynamic_routes_with_casted_number(self):
def dummy_func(year: int) -> int:
return year
def dummy_func(year: int) -> Response:
return Response(f'hello {year}')

self.router.add('GET', '/tests/{year}', 'hoge', dummy_func)
test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/tests/2015/'}
Expand All @@ -50,7 +51,7 @@ def dummy_func(year: int) -> int:

def test_match_dynamic_routes_with_string(self):
def dummy_func(name):
return name
return Response(f'hello {name}')

self.router.add('GET', '/tests/{name}', 'hoge', dummy_func)
test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/tests/kobin/'}
Expand All @@ -59,7 +60,7 @@ def dummy_func(name):

def test_404_not_found(self):
def dummy_func(name):
return name
return Response(f'hello {name}')

self.router.add('GET', '/tests/{name}', 'hoge', dummy_func)
test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/this_is_not_found'}
Expand Down

0 comments on commit 121a128

Please sign in to comment.