Skip to content

Commit

Permalink
Merge pull request #46 from hadpro24/develop
Browse files Browse the repository at this point in the history
Develop : Remove path name `path_reverse` to `reverse`
  • Loading branch information
hadpro24 authored Jul 25, 2021
2 parents 419b53c + 81af845 commit 143781d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions nimba/http/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .utils import router, render, path_reverse
from .utils import router, render, reverse
from .utils import ROUTES

all = [
'router',
'render'
'path_reverse',
'reverse',
'ROUTES',
]
4 changes: 2 additions & 2 deletions nimba/http/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
REVERSE_ROUTE_INFO = {}
PROJECT_MASK = 'PROJECT_MASK_PATH'

def path_reverse(name_path:str, args=None, kwargs=None) -> str:
def reverse(name_path:str, args=None, kwargs=None) -> str:
if not isinstance(name_path, str) or not re.match(r"^[^\d\W][\w-]*\Z", name_path):
raise ValueError("Name path must but a valid identifier name.")
args = args or {}
Expand Down Expand Up @@ -93,7 +93,7 @@ def render(template, contexts=None, status=200, charset='utf-8', content_type='t
)
#load env jinja2
contexts['load_static'] = load_static
contexts['path_reverse'] = path_reverse
contexts['reverse'] = reverse
with open(path, 'r') as content_file:
content = content_file.read()
html_render = env.from_string(content)
Expand Down
26 changes: 13 additions & 13 deletions tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import shutil
from unittest.mock import patch

from nimba.http import router, render, path_reverse
from nimba.http import router, render, reverse
from nimba.core.server import Application
from nimba.core.exceptions import NoReverseFound

Expand Down Expand Up @@ -85,49 +85,49 @@ def test_route_with_template(self):
self.assertEqual(200, response['status_code'])
self.assertIn("hello, world", response['text'])

def test_path_reverse_with_function_name(self):
url = path_reverse('about')
def test_reverse_with_function_name(self):
url = reverse('about')
response = self.get(url)
self.assertEqual(200, response['status_code'])
self.assertEqual(TEST, response['text'])

def test_path_reverse_with_name(self):
url = path_reverse('articles')
def test_reverse_with_name(self):
url = reverse('articles')
response = self.get(url)
self.assertEqual(200, response['status_code'])
self.assertEqual(TEST, response['text'])

def test_path_reverse_with_name_and_kwargs(self):
def test_reverse_with_name_and_kwargs(self):
#error type name path
with self.assertRaises(ValueError) as error:
path_reverse(57885)
reverse(57885)
self.assertEqual(str(error.exception), "Name path must but a valid identifier name.")
#bad name give
invalid_path = 'invalid-article'
with self.assertRaises(NoReverseFound) as error:
path_reverse(invalid_path)
reverse(invalid_path)
self.assertEqual(str(error.exception), f"Reverse for {invalid_path} not found.")
#give kwargs and args
with self.assertRaises(ValueError) as error:
path_reverse('article', kwargs={'id': 5}, args={'name': 'test'})
reverse('article', kwargs={'id': 5}, args={'name': 'test'})
self.assertEqual(str(error.exception), ("Don't use *args and **kwargs."
"*args is for get and **kwargs for post method."))
#invalid parmas name
invalid_params = 'id_wrong'
with self.assertRaises(NoReverseFound) as error:
path_reverse('article', kwargs={invalid_params: 5})
reverse('article', kwargs={invalid_params: 5})
self.assertEqual(str(error.exception), ("Reverse for article not found. "
"Keyword arguments 'id' not found."))
#valid
_id = 5
url = path_reverse('article', kwargs={'id': _id})
url = reverse('article', kwargs={'id': _id})
response = self.get(url)
self.assertEqual(200, response['status_code'])
self.assertEqual(str(_id), response['text'])

def test_path_reverse_with_args(self):
def test_reverse_with_args(self):
name = 'Harouna Diallo'
url = path_reverse('info', args={'name': name})
url = reverse('info', args={'name': name})
response = self.get(url)
self.assertEqual(200, response['status_code'])
self.assertEqual(name, response['text'])
Expand Down

0 comments on commit 143781d

Please sign in to comment.