Skip to content

Commit

Permalink
Utils: Added is_it_true utility function
Browse files Browse the repository at this point in the history
Also added documentation and tests to go along with it
  • Loading branch information
Seluj78 committed Jun 10, 2024
1 parent 2cb807e commit 130cc6b
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ jobs:
- name: Run tests
run: |
PYTHON_VERSION=${{ matrix.python-version }}
TOX_ENV="py${PYTHON_VERSION//./}"
TOX_ENV=`tox --listenvs | grep "py${PYTHON_VERSION//./}-" | tr '\n' ','`
tox -e $TOX_ENV
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
flask>=2.0.0
flask>=2.2.0
sphinx==7.1.2
sphinx-notfound-page
sphinx-rtd-theme==1.3.0rc1
6 changes: 6 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ Decorators
.. automodule:: flask_utils.decorators
:members:

Utilities
---------

.. automodule:: flask_utils.utils
:members:

Private API
----------------------

Expand Down
4 changes: 3 additions & 1 deletion flask_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Increment versions here according to SemVer
__version__ = "0.3.0"
__version__ = "0.4.0"

from flask_utils.errors import ConflictError
from flask_utils.errors import ForbiddenError
Expand All @@ -15,6 +15,7 @@

from flask_utils.decorators import validate_params

from flask_utils.utils import is_it_true

from flask_utils.errors import register_error_handlers

Expand All @@ -32,4 +33,5 @@
"UnprocessableEntityError",
"ServiceUnavailableError",
"validate_params",
"is_it_true",
]
36 changes: 36 additions & 0 deletions flask_utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
def is_it_true(value: str) -> bool:
"""
This function checks if a string value is true.
Useful for flask's request.form.get() method and request.args.get() method
:param value: String value to check if it is true
:return: True if value is true, False otherwise
:Example:
.. code-block:: python
from flask_utils import is_it_true
@app.route('/example', methods=['GET'])
def example():
is_ordered = request.args.get('is_ordered', type=is_it_true, default=False)
This allows your API to accept these kind of requests:
.. code-block:: python
import requests
response = requests.get('http://localhost:5000/example?is_ordered=true')
print(response.json()) # True
response = requests.get('http://localhost:5000/example?is_ordered=1')
print(response.json()) # True
response = requests.get('http://localhost:5000/example?is_ordered=yes')
print(response.json()) # True
.. versionadded:: 0.4.0
"""
return value.lower() in ("true", "1", "yes")
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
flask>=2.0.0
flask>=2.2.0
76 changes: 76 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import pytest
from flask import jsonify
from flask import request

from flask_utils import is_it_true


class TestIsItTrueFunction:
def test_string_true(self):
assert is_it_true("true") is True

def test_string_true_uppercase(self):
assert is_it_true("TRUE") is True

def test_string_true_yes(self):
assert is_it_true("yes") is True

def test_string_true_yes_uppercase(self):
assert is_it_true("YES") is True

def test_string_true_one(self):
assert is_it_true("1") is True

def test_string_false(self):
assert is_it_true("false") is False

def test_string_false_no(self):
assert is_it_true("no") is False

def test_string_false_zero(self):
assert is_it_true("0") is False


class TestIsItTrueInFlask:
@pytest.fixture(autouse=True)
def setup(self, flask_client):
@flask_client.route("/example", methods=["GET"])
def example():
is_ordered = request.args.get("is_ordered", type=is_it_true, default=False)
return jsonify(is_ordered)

def test_is_ordered_true(self, client):
response = client.get("example?is_ordered=true")
assert response.json is True

def test_is_ordered_true_uppercase(self, client):
response = client.get("example?is_ordered=TRUE")
assert response.json is True

def test_is_ordered_true_yes(self, client):
response = client.get("example?is_ordered=yes")
assert response.json is True

def test_is_ordered_true_yes_uppercase(self, client):
response = client.get("example?is_ordered=YES")
assert response.json is True

def test_is_ordered_true_one(self, client):
response = client.get("example?is_ordered=1")
assert response.json is True

def test_is_ordered_false(self, client):
response = client.get("example?is_ordered=false")
assert response.json is False

def test_is_ordered_false_no(self, client):
response = client.get("example?is_ordered=no")
assert response.json is False

def test_is_ordered_false_zero(self, client):
response = client.get("example?is_ordered=0")
assert response.json is False

def test_is_ordered_default(self, client):
response = client.get("example")
assert response.json is False
4 changes: 1 addition & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
[tox]
envlist = py{38,39,310,311,312}-flask{20,21,22,23,30,latest}
envlist = py{38,39,310,311,312}-flask{22,23,30,latest}

[testenv]
deps =
pytest
flask20: Flask==2.*
flask21: Flask==2.1.*
flask22: Flask==2.2.*
flask23: Flask==2.3.*
flask30: Flask==3.*
Expand Down

0 comments on commit 130cc6b

Please sign in to comment.