-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
|
||
flask>=2.3.0,>=3.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from flask import Flask | ||
|
||
from tunsberg.api import get_api_version, get_api_version_from_flask_url | ||
|
||
|
||
app = Flask(__name__) | ||
|
||
|
||
def test_get_api_version_from_flask_url_default(): | ||
""" Test that the default pattern matches """ | ||
with app.test_request_context('/api/v1/resource'): | ||
assert get_api_version_from_flask_url() == 'v1' | ||
|
||
|
||
def test_get_api_version_from_flask_url_custom_pattern(): | ||
""" Test that the custom pattern matches """ | ||
with app.test_request_context('/api/v2_1/resource'): | ||
assert get_api_version_from_flask_url(pattern=r'v\d+_\d+') == 'v2_1' | ||
|
||
|
||
def test_get_api_version_from_flask_url_no_version(): | ||
""" Test that the default return value is empty string """ | ||
with app.test_request_context('/api/resource'): | ||
assert get_api_version_from_flask_url() == '' | ||
|
||
|
||
def test_get_api_version_from_flask_url_default_return(): | ||
""" Test that the default return value is 'v0' """ | ||
with app.test_request_context('/api/resource'): | ||
assert get_api_version_from_flask_url(default='v0') == 'v0' | ||
|
||
|
||
def test_get_api_version_from_flask_url_no_default_return(): | ||
""" Test that the default return value is empty string """ | ||
with app.test_request_context('/api/resource'): | ||
assert get_api_version_from_flask_url(default='') == '' | ||
|
||
|
||
def test_get_api_version_standard(): | ||
""" Test that the standard pattern matches """ | ||
assert get_api_version('app.api.v1') == 'v1' | ||
assert get_api_version('module.v2.submodule') == 'v2' | ||
assert get_api_version('service.v3_1.beta') == 'v3_1' | ||
|
||
|
||
def test_get_api_version_no_version(): | ||
""" Test that the default return value is empty string """ | ||
assert get_api_version('app.api') == '' | ||
assert get_api_version('module.submodule') == '' | ||
|
||
|
||
def test_get_api_version_custom_pattern(): | ||
""" Test that the custom pattern matches """ | ||
assert get_api_version('app.api.v2021_04', pattern=r'v\d{4}_\d{2}') == 'v2021_04' | ||
assert get_api_version('service.v2021.beta', pattern=r'v\d{4}') == 'v2021' | ||
|
||
|
||
def test_get_api_version_custom_default(): | ||
""" Test that the custom default value is returned """ | ||
assert get_api_version('app.api', default='no_version') == 'no_version' | ||
assert get_api_version('module.submodule', default='undefined') == 'undefined' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import re | ||
|
||
from flask import request | ||
|
||
|
||
def get_api_version(folder_name: str, pattern: str = r'v\d+(_\d+)?', default: str = '') -> str: | ||
r""" | ||
Get API version from folder name. | ||
:param folder_name: Folder name | ||
:type folder_name: str | ||
:param pattern: Regular expression pattern to match version. Default is 'v\\d+(_\\d+)?', | ||
which matches 'v<number>' or 'v<number>_<number>'. | ||
:type pattern: str | ||
:param default: Default return value if no version is found. Defaults to ''. | ||
:type default: str | ||
:return: API version as string, or the default value if no version is found | ||
:rtype: str | ||
""" | ||
if folder_name == 'app.api': | ||
return default | ||
|
||
version_parts = folder_name.split('.') | ||
version_regex = re.compile(pattern) | ||
|
||
for part in version_parts: | ||
if version_regex.match(part): | ||
return part | ||
|
||
return default | ||
|
||
|
||
def get_api_version_from_flask_url(pattern: str = r'v\d+(_\d+)?', default: str = '') -> str: | ||
r""" | ||
Get the API version from a Flask URL. | ||
:param pattern: Regular expression pattern to match version. Default is 'v\\d+(_\\d+)?', | ||
which matches 'v<number>' or 'v<number>_<number>'. | ||
:type pattern: str | ||
:param default: Default return value if no version is found. Defaults to ''. | ||
:type default: str | ||
:return: API version as string, or the default value if no version is found | ||
:rtype: str | ||
""" | ||
url_list = request.url.split('/') | ||
version_regex = re.compile(pattern) | ||
|
||
for item in url_list: | ||
if version_regex.match(item): | ||
return item | ||
|
||
return default |