Skip to content

Commit

Permalink
test: added unit tests for app
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemcguire committed Jan 17, 2025
1 parent a499081 commit d9a0cfa
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.aider*
12 changes: 9 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ repos:
args: [--exit-non-zero-on-fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.3"
# - repo: https://github.com/pre-commit/mirrors-prettier
# rev: "v3.0.3"
# hooks:
# - id: prettier

- repo: https://github.com/biomejs/pre-commit
rev: "v0.6.1"
hooks:
- id: prettier
- id: biome-check
additional_dependencies: ["@biomejs/[email protected]"]
Empty file added CONVENTIONS.md
Empty file.
24 changes: 19 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import os

from dotenv import load_dotenv
from flask import Flask

app = Flask(__name__)

@app.route('/add/<int:num1>/<int:num2>')

@app.route("/")
def hello():
"""Returns a simple greeting."""
return "hello world"


@app.route("/add/<num1>/<num2>")
def add(num1: int, num2: int) -> str:
"""Adds two numbers together.
Expand All @@ -13,7 +23,8 @@ def add(num1: int, num2: int) -> str:
Returns:
The sum of the two numbers as a string.
"""
return str(num1 + num2)
return str(int(num1) + int(num2))


def fibonacci(n: int) -> int | str:
"""Calculates the nth Fibonacci number.
Expand All @@ -36,7 +47,8 @@ def fibonacci(n: int) -> int | str:
a, b = b, a + b
return b

@app.route('/fibonacci/<int:n>')

@app.route("/fibonacci/<int:n>")
def get_fibonacci(n: int) -> str:
"""Gets the nth Fibonacci number via the fibonacci function.
Expand All @@ -48,5 +60,7 @@ def get_fibonacci(n: int) -> str:
"""
return str(fibonacci(n))

if __name__ == '__main__':
app.run(debug=True)

if __name__ == "__main__":
load_dotenv()
app.run(debug=os.getenv("ENV") == "dev")
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ classifiers = [
"Programming Language :: Python :: 3.13",
"Topic :: Software Development :: Libraries :: Python Modules",
]
dependencies = [
"flask>=3.1.0",
"python-dotenv>=1.0.1",
]

[project.urls]
Homepage = "https://lukemcguire.github.io/flask-test/"
Expand Down
74 changes: 54 additions & 20 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,75 @@
import pytest
from app import app

from app import app, fibonacci


@pytest.fixture
def client():
"""Creates a test client for the Flask application."""
app.config["TESTING"] = True
with app.test_client() as client:
yield client

def test_add(client):
response = client.get('/add/2/3')
assert response.status_code == 200
assert response.data == b'5'

response = client.get('/add/-1/1')
def test_hello(client):
"""Tests the hello endpoint."""
response = client.get("/")
assert response.status_code == 200
assert response.data == b'0'
assert response.data.decode("utf-8") == "hello world"


def test_fibonacci(client):
response = client.get('/fibonacci/1')
def test_add(client):
"""Tests the add endpoint with various inputs."""
# Test positive numbers
response = client.get("/add/1/2")
assert response.status_code == 200
assert response.data == b'0'
assert response.data.decode("utf-8") == "3"

response = client.get('/fibonacci/2')
# Test zero
response = client.get("/add/0/0")
assert response.status_code == 200
assert response.data == b'1'
assert response.data.decode("utf-8") == "0"

response = client.get('/fibonacci/5')
# Test negative numbers
response = client.get("/add/%2D1/1") # Using URL-encoded minus sign
assert response.status_code == 200
assert response.data == b'3'
assert response.data.decode("utf-8") == "0"

response = client.get('/fibonacci/10')
# Additional negative number test cases
response = client.get("/add/%2D5/3")
assert response.status_code == 200
assert response.data == b'55'
assert response.data.decode("utf-8") == "-2"

response = client.get('/fibonacci/0')

def test_fibonacci_function():
"""Tests the fibonacci function directly."""
# Test invalid input
assert fibonacci(0) == "Invalid input"
assert fibonacci(-1) == "Invalid input"

# Test base cases
assert fibonacci(1) == 0
assert fibonacci(2) == 1

# Test other valid inputs
assert fibonacci(3) == 1
assert fibonacci(4) == 2
assert fibonacci(5) == 3
assert fibonacci(6) == 5
assert fibonacci(7) == 8


def test_fibonacci_endpoint(client):
"""Tests the fibonacci endpoint with various inputs."""
# Test valid inputs
response = client.get("/fibonacci/1")
assert response.status_code == 200
assert response.data == b'Invalid input'
assert response.data.decode("utf-8") == "0"

response = client.get('/fibonacci/-1')
response = client.get("/fibonacci/7")
assert response.status_code == 200
assert response.data == b'Invalid input'
assert response.data.decode("utf-8") == "8"

# Test invalid input type (will be caught by Flask's routing)
response = client.get("/fibonacci/invalid")
assert response.status_code == 404
64 changes: 64 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d9a0cfa

Please sign in to comment.