Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit c0a4864

Browse files
committed
Utils: Added is_it_true utility function
Also added documentation and tests to go along with it
1 parent 6e05954 commit c0a4864

File tree

8 files changed

+125
-7
lines changed

8 files changed

+125
-7
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ jobs:
3333
- name: Run tests
3434
run: |
3535
PYTHON_VERSION=${{ matrix.python-version }}
36-
TOX_ENV="py${PYTHON_VERSION//./}"
36+
TOX_ENV=`tox --listenvs | grep "py${PYTHON_VERSION//./}-" | tr '\n' ','`
3737
tox -e $TOX_ENV

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
flask>=2.0.0
1+
flask>=2.2.0
22
sphinx==7.1.2
33
sphinx-notfound-page
44
sphinx-rtd-theme==1.3.0rc1

docs/source/api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ Decorators
2727
.. automodule:: flask_utils.decorators
2828
:members:
2929

30+
Utilities
31+
---------
32+
33+
.. automodule:: flask_utils.utils
34+
:members:
35+
3036
Private API
3137
----------------------
3238

flask_utils/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Increment versions here according to SemVer
2-
__version__ = "0.3.0"
2+
__version__ = "0.4.0"
33

44
from flask_utils.errors import ConflictError
55
from flask_utils.errors import ForbiddenError
@@ -15,6 +15,7 @@
1515

1616
from flask_utils.decorators import validate_params
1717

18+
from flask_utils.utils import is_it_true
1819

1920
from flask_utils.errors import register_error_handlers
2021

@@ -32,4 +33,5 @@
3233
"UnprocessableEntityError",
3334
"ServiceUnavailableError",
3435
"validate_params",
36+
"is_it_true",
3537
]

flask_utils/utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def is_it_true(value: str) -> bool:
2+
"""
3+
This function checks if a string value is true.
4+
Useful for flask's request.form.get() method and request.args.get() method
5+
6+
:param value: String value to check if it is true
7+
:return: True if value is true, False otherwise
8+
9+
:Example:
10+
11+
.. code-block:: python
12+
13+
from flask_utils import is_it_true
14+
15+
@app.route('/example', methods=['GET'])
16+
def example():
17+
is_ordered = request.args.get('is_ordered', type=is_it_true, default=False)
18+
19+
This allows your API to accept these kind of requests:
20+
21+
.. code-block:: python
22+
23+
import requests
24+
25+
response = requests.get('http://localhost:5000/example?is_ordered=true')
26+
print(response.json()) # True
27+
28+
response = requests.get('http://localhost:5000/example?is_ordered=1')
29+
print(response.json()) # True
30+
31+
response = requests.get('http://localhost:5000/example?is_ordered=yes')
32+
print(response.json()) # True
33+
34+
.. versionadded:: 0.4.0
35+
"""
36+
return value.lower() in ("true", "1", "yes")

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
flask>=2.0.0
1+
flask>=2.2.0

tests/test_utils.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import pytest
2+
from flask import jsonify
3+
from flask import request
4+
5+
from flask_utils import is_it_true
6+
7+
8+
class TestIsItTrueFunction:
9+
def test_string_true(self):
10+
assert is_it_true("true") is True
11+
12+
def test_string_true_uppercase(self):
13+
assert is_it_true("TRUE") is True
14+
15+
def test_string_true_yes(self):
16+
assert is_it_true("yes") is True
17+
18+
def test_string_true_yes_uppercase(self):
19+
assert is_it_true("YES") is True
20+
21+
def test_string_true_one(self):
22+
assert is_it_true("1") is True
23+
24+
def test_string_false(self):
25+
assert is_it_true("false") is False
26+
27+
def test_string_false_no(self):
28+
assert is_it_true("no") is False
29+
30+
def test_string_false_zero(self):
31+
assert is_it_true("0") is False
32+
33+
34+
class TestIsItTrueInFlask:
35+
@pytest.fixture(autouse=True)
36+
def setup(self, flask_client):
37+
@flask_client.route("/example", methods=["GET"])
38+
def example():
39+
is_ordered = request.args.get("is_ordered", type=is_it_true, default=False)
40+
return jsonify(is_ordered)
41+
42+
def test_is_ordered_true(self, client):
43+
response = client.get("example?is_ordered=true")
44+
assert response.json is True
45+
46+
def test_is_ordered_true_uppercase(self, client):
47+
response = client.get("example?is_ordered=TRUE")
48+
assert response.json is True
49+
50+
def test_is_ordered_true_yes(self, client):
51+
response = client.get("example?is_ordered=yes")
52+
assert response.json is True
53+
54+
def test_is_ordered_true_yes_uppercase(self, client):
55+
response = client.get("example?is_ordered=YES")
56+
assert response.json is True
57+
58+
def test_is_ordered_true_one(self, client):
59+
response = client.get("example?is_ordered=1")
60+
assert response.json is True
61+
62+
def test_is_ordered_false(self, client):
63+
response = client.get("example?is_ordered=false")
64+
assert response.json is False
65+
66+
def test_is_ordered_false_no(self, client):
67+
response = client.get("example?is_ordered=no")
68+
assert response.json is False
69+
70+
def test_is_ordered_false_zero(self, client):
71+
response = client.get("example?is_ordered=0")
72+
assert response.json is False
73+
74+
def test_is_ordered_default(self, client):
75+
response = client.get("example")
76+
assert response.json is False

tox.ini

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
[tox]
2-
envlist = py{38,39,310,311,312}-flask{20,21,22,23,30,latest}
2+
envlist = py{38,39,310,311,312}-flask{22,23,30,latest}
33

44
[testenv]
55
deps =
66
pytest
7-
flask20: Flask==2.*
8-
flask21: Flask==2.1.*
97
flask22: Flask==2.2.*
108
flask23: Flask==2.3.*
119
flask30: Flask==3.*

0 commit comments

Comments
 (0)