From a7a0355dfeb3a2d7e6b7a17888ccac5276ef4f7e Mon Sep 17 00:00:00 2001 From: Jahnavi Maddhuri Date: Fri, 13 Dec 2024 13:35:01 -0800 Subject: [PATCH] add tests --- .github/workflows/cicd.yml | 19 ++++++++++++++- Makefile | 50 ++++++++++++++++++++++++++++++++++++++ app.py | 36 +++++++++++++++++---------- requirements.txt | 3 ++- test_app.py | 39 +++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 Makefile create mode 100644 test_app.py diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index af1e83b..a81a27b 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -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 @@ -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 \ No newline at end of file + 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 \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a347e1f --- /dev/null +++ b/Makefile @@ -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 diff --git a/app.py b/app.py index 7a40e38..b879d0c 100644 --- a/app.py +++ b/app.py @@ -4,6 +4,7 @@ render_template_string, ) from dotenv import load_dotenv +import random load_dotenv() @@ -12,19 +13,10 @@ HTML_TEMPLATE = """ - - Movie Request Form - -

Enter Your Details

- -

- -

- -

- + +
@@ -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"

Hello World!

" + return f"

{random.choice(cheer_up_phrases)}

" return render_template_string(HTML_TEMPLATE) if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000, debug=True) \ No newline at end of file + app.run(host='0.0.0.0', port=5001, debug=True) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index bd82ef5..ef6d525 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ pylint ruff Flask==2.0.1 Werkzeug==2.0.1 -python-dotenv \ No newline at end of file +python-dotenv +random \ No newline at end of file diff --git a/test_app.py b/test_app.py new file mode 100644 index 0000000..a3acdc8 --- /dev/null +++ b/test_app.py @@ -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() \ No newline at end of file