-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utils: Added is_it_true utility function
Also added documentation and tests to go along with it
- Loading branch information
Showing
8 changed files
with
125 additions
and
7 deletions.
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
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,4 +1,4 @@ | ||
flask>=2.0.0 | ||
flask>=2.2.0 | ||
sphinx==7.1.2 | ||
sphinx-notfound-page | ||
sphinx-rtd-theme==1.3.0rc1 |
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
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
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,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") |
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.0.0 | ||
flask>=2.2.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,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 |
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