Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jahnavi-maddhuri committed Dec 13, 2024
1 parent 17ab80b commit a7a0355
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 15 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install
run: make install

- name: Log in to Docker Hub
uses: docker/login-action@v2
Expand All @@ -32,4 +40,13 @@ jobs:

# Run the Docker container (if applicable)
- name: Run Docker container
run: docker run -p 5001:5001 ${{ secrets.DOCKER_USERNAME }}/jahnavi-docker-app:latest
run: docker run -p 5001:5001 ${{ secrets.DOCKER_USERNAME }}/jahnavi-docker-app:latest

- name: Format
run: make format

- name: Lint
run: make lint

- name: Test
run: make test
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Define the image name
IMAGE_NAME = CheerUpApp
DOCKER_USERNAME = jahnavimaddhuri

# Build the Docker image
build:
docker build -t $(IMAGE_NAME) .

# Run Docker container
run:
docker run -p 5000:5000 $(IMAGE_NAME)

# Remove Docker image
clean:
docker rmi $(IMAGE_NAME)

image_show:
docker images

container_show:
docker ps

push:
docker login
docker tag $(IMAGE_NAME) $(DOCKER_USERNAME)/$(IMAGE_NAME)
docker push $(DOCKER_USERNAME)/$(IMAGE_NAME):latest

login:
docker login -u ${DOCKER_USERNAME}

install:
pip install --upgrade pip &&\
pip install -r requirements.txt

test:
python -m pytest -vv --cov=app --cov=mylib test_*.py

format:
black *.py

lint:
#disable comment to test speed
#pylint --disable=R,C --ignore-patterns=test_.*?py *.py mylib/*.py
#ruff linting is 10-100X faster than pylint
ruff check *.py mylib/*.py

container-lint:
docker run --rm -i hadolint/hadolint < Dockerfile

refactor: format lint
36 changes: 23 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
render_template_string,
)
from dotenv import load_dotenv
import random

load_dotenv()

Expand All @@ -12,19 +13,10 @@
HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>Movie Request Form</title>
</head>
<body>
<h1>Enter Your Details</h1>
<form method="POST" action="/">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="genre">Genre:</label>
<input type="text" id="genre" name="genre" required><br><br>
<label for="mood">Mood:</label>
<input type="text" id="mood" name="mood" required><br><br>
<button type="submit">Submit</button>
<label>Having a bum day? Let's make it better! Hit this button:</label>
<button>Cheer Me Up!</button>
</form>
</body>
</html>
Expand All @@ -33,9 +25,27 @@
@app.route('/', methods=["GET", "POST"])

def hello_world():
cheer_up_phrases = [
"Data never sleeps, but neither do breakthroughs—you're crushing it!",
"You’re like a neural net—always learning and adapting!",
"Remember, even NaN values are part of the dataset!",
"Life’s a gradient descent—keep moving toward your optimum!",
"Your hard work is the feature everyone notices!",
"Keep calm and let the algorithm do the heavy lifting!",
"Debugging life one line of code at a time—keep it up!",
"Your data game is *outlier-level* impressive!",
"You're a clustering champ—always finding your center!",
"Master’s degree: Loading... 90% complete. You’ve got this!",
"You’re the key to cracking the ultimate dataset: life!",
"Machine learning? More like *mastered* learning!",
"A few more semesters, and you’re the top variable in the model!",
"Data wrangling = life wrangling. You’re doing both like a pro!",
"Remember, correlation doesn’t imply exhaustion—rest up and conquer!"
]

if request.method == "POST":
return f"<h1>Hello World!</h1>"
return f"<h1>{random.choice(cheer_up_phrases)}</h1>"
return render_template_string(HTML_TEMPLATE)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
app.run(host='0.0.0.0', port=5001, debug=True)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pylint
ruff
Flask==2.0.1
Werkzeug==2.0.1
python-dotenv
python-dotenv
random
39 changes: 39 additions & 0 deletions test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest
from app import app

@pytest.fixture
def client():
app.config['TESTING'] = True
with app.test_client() as client:
yield client

def test_get_request(client):
"""Test the GET request to the root endpoint."""
response = client.get('/')
assert response.status_code == 200
assert b"Having a bum day? Let's make it better! Hit this button:" in response.data

def test_post_request(client):
"""Test the POST request to the root endpoint."""
response = client.post('/')
assert response.status_code == 200
assert any(phrase.encode() in response.data for phrase in [
"Data never sleeps, but neither do breakthroughs—you're crushing it!",
"You’re like a neural net—always learning and adapting!",
"Remember, even NaN values are part of the dataset!",
"Life’s a gradient descent—keep moving toward your optimum!",
"Your hard work is the feature everyone notices!",
"Keep calm and let the algorithm do the heavy lifting!",
"Debugging life one line of code at a time—keep it up!",
"Your data game is *outlier-level* impressive!",
"You're a clustering champ—always finding your center!",
"Master’s degree: Loading... 90% complete. You’ve got this!",
"You’re the key to cracking the ultimate dataset: life!",
"Machine learning? More like *mastered* learning!",
"A few more semesters, and you’re the top variable in the model!",
"Data wrangling = life wrangling. You’re doing both like a pro!",
"Remember, correlation doesn’t imply exhaustion—rest up and conquer!"
])

if __name__ == '__main__':
pytest.main()

0 comments on commit a7a0355

Please sign in to comment.