This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
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.
Add unittests for API endpoints (#4)
* Add unittests for API endpoints Signed-off-by: starry69 <[email protected]> * Add black/flake8 GH action and add pytest in python-app action Signed-off-by: starry69 <[email protected]> * Fix deepsrc confing Signed-off-by: starry69 <[email protected]>
- Loading branch information
1 parent
0361c67
commit d113d45
Showing
15 changed files
with
196 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
version = 1 | ||
|
||
test_patterns = [ | ||
"tests/**", | ||
"test_*.py" | ||
] | ||
|
||
[[analyzers]] | ||
name = "python" | ||
enabled = true | ||
|
||
[analyzers.meta] | ||
runtime_version = "3.x.x" | ||
runtime_version = "3.x.x" |
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,5 @@ | ||
[flake8] | ||
ignore = E203, E266, E501, W503, F403, F401 | ||
max-line-length = 79 | ||
max-complexity = 18 | ||
select = B,C,E,F,W,T4,B9 |
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,11 @@ | ||
name: Lint | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-python@v2 | ||
- uses: psf/black@stable |
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,15 @@ | ||
name: pre-commit | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: [master] | ||
|
||
jobs: | ||
pre-commit: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-python@v2 | ||
- uses: pre-commit/[email protected] | ||
|
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,31 @@ | ||
exclude: '.github/' | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v3.3.0 | ||
hooks: | ||
- id: check-added-large-files | ||
- id: check-ast | ||
- id: check-builtin-literals | ||
- id: check-case-conflict | ||
- id: check-docstring-first | ||
- id: check-json | ||
- id: check-merge-conflict | ||
- id: check-yaml | ||
- id: debug-statements | ||
- id: detect-private-key | ||
- id: end-of-file-fixer | ||
- id: fix-byte-order-marker | ||
- id: fix-encoding-pragma | ||
- id: pretty-format-json | ||
- id: requirements-txt-fixer | ||
- id: trailing-whitespace | ||
|
||
- repo: https://github.com/ambv/black | ||
rev: stable | ||
hooks: | ||
- id: black | ||
language_version: python3 | ||
- repo: https://gitlab.com/pycqa/flake8 | ||
rev: 3.8.4 | ||
hooks: | ||
- id: flake8 |
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,16 @@ | ||
[tool.black] | ||
line-length = 79 | ||
include = '\.pyi?$' | ||
exclude = ''' | ||
/( | ||
\.git | ||
| \.hg | ||
| \.mypy_cache | ||
| \.tox | ||
| \.venv | ||
| _build | ||
| buck-out | ||
| build | ||
| dist | ||
)/ | ||
''' |
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,5 @@ | ||
black | ||
flake8 | ||
pre-commit | ||
pytest | ||
requests |
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,6 +1,6 @@ | ||
Flask | ||
Flask-RESTful | ||
sqlalchemy | ||
gunicorn | ||
psycopg2 | ||
shortuuid | ||
gunicorn | ||
sqlalchemy |
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
Empty file.
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,94 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# MIT License | ||
# Copyright (C) 2020-2021 Stɑrry Shivɑm. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
"""Includes unittests for public API endpoints.""" | ||
|
||
|
||
import pytest | ||
import requests as r | ||
|
||
|
||
BASE_URL = "https://superhero-quotes.herokuapp.com" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["banner", "expected_banner", "total"], | ||
[ | ||
("mcu", "Marvel Cinematic Universe (MCU)", 5), | ||
("dcu", "DC Universe (DCU)", 10), | ||
], | ||
) | ||
def test_grab_endpoint(banner, expected_banner, total): | ||
# make request to the API | ||
res = r.get(f"{BASE_URL}/grab?banner={banner}&size={total}") | ||
# check status code | ||
assert res.status_code == 200 | ||
# check total quotes | ||
json_data = res.json() | ||
assert json_data["TotalQuotes"] == total | ||
assert len(json_data["Items"]) == total | ||
# finally check banner | ||
assert json_data["Banner"] == expected_banner | ||
|
||
|
||
def test_default_grab_endpoint_size(): | ||
# make request to the API without passing size parameter | ||
banners = ["dcu", "mcu"] | ||
for b in banners: | ||
res = r.get(f"{BASE_URL}/grab?banner={b}") | ||
# by default it should return 10 quotes | ||
assert res.json()["TotalQuotes"] == 10 | ||
|
||
|
||
def test_invalid_grab_banner(): | ||
# make request to API by passing 'pizza' in banner parameter | ||
res = r.get(f"{BASE_URL}/grab?banner=pizza") | ||
# status 404: not found | ||
assert res.status_code == 404 | ||
assert res.json()["message"].startswith("Not found") | ||
|
||
|
||
def test_random_endpoint(): | ||
# make request to /random endpoint. | ||
res = r.get(f"{BASE_URL}/random") | ||
# check status code | ||
assert res.status_code == 200 | ||
json_data = res.json() | ||
assert json_data["Banner"] in { | ||
"DC Universe (DCU)", | ||
"Marvel Cinematic Universe (MCU)", | ||
} | ||
|
||
|
||
def test_quoteid_endpoint(): | ||
# make request with specific quoteId | ||
quote_id = "k3fhzAKsvCeuFhXPHPQcnT" | ||
res = r.get(f"{BASE_URL}/quoteId/{quote_id}") | ||
# check status code | ||
assert res.status_code == 200 | ||
json_data = res.json() | ||
# check if id & quote matches | ||
quote_text = "The pen, is truly mightier than the sword! " | ||
assert json_data["Stuff"]["id"] == quote_id | ||
assert json_data["Stuff"]["data"]["quote"] == quote_text | ||
|
||
|
||
def test_invalid_quoteid(): | ||
# make request with invalid quoteid | ||
invalid_id = "1234abcd" | ||
res = r.get(f"{BASE_URL}/quoteId/{invalid_id}") | ||
# status 404: not found | ||
assert res.status_code == 404 | ||
# check error message | ||
err_msg = "Can't find any quote for this id." | ||
assert res.json()["message"] == err_msg |
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,3 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
# Hack for herokowowo | ||
from QuotesAPI.__main__ import app | ||
|
||
|