From b14c59411f0959acc7d92bff520d6f6c7030bee2 Mon Sep 17 00:00:00 2001 From: Eric Asante Date: Thu, 31 Oct 2024 17:25:05 -0400 Subject: [PATCH] API complete --- .github/workflows/ms5.yaml | 64 +++++++++++++++ Backend/Docker | 11 +++ Backend/dockerfile | 30 +++++++ Backend/main.py | 8 ++ Backend/pet_func.py | 11 +++ Backend/test_main.py | 156 ++++++++----------------------------- Backend/user_func.py | 26 +++++++ Frontend/hi.txt | 1 + 8 files changed, 185 insertions(+), 122 deletions(-) create mode 100644 .github/workflows/ms5.yaml create mode 100644 Backend/Docker create mode 100644 Backend/dockerfile create mode 100644 Backend/pet_func.py create mode 100644 Backend/user_func.py create mode 100644 Frontend/hi.txt diff --git a/.github/workflows/ms5.yaml b/.github/workflows/ms5.yaml new file mode 100644 index 0000000..326c0ec --- /dev/null +++ b/.github/workflows/ms5.yaml @@ -0,0 +1,64 @@ +name: Pylint and API Testing + +on: + push: + branches: [Milestone-5_feature_branch] + +jobs: + test-and-lint: + runs-on: self-hosted # or ubuntu-latest + strategy: + matrix: + python-version: ["3.10"] + + steps: + #Checkout code + - uses: actions/checkout@v2 # Updated to correct version + + #Set up Python + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + + #Install everything + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint flask flask_cors flasgger pytest + + #Run pylint on all Python files + - name: Analyze code with pylint, excluding main.py + run: | + pylint $(git ls-files '*.py') + + #Build Docker Image for API + - name: Build API Docker Image + run: | + docker build -t api-image -f Backend/dockerfile . + + + # Stop and Remove Existing Container (if any). this was the error + - name: Stop and Remove Existing API Container + run: | + docker stop api-container || true # Stop if running, ignore if not + sleep 10 + docker rm api-container || true # Remove if exists, ignore if not + sleep 1 + + #Start API Container + - name: Start API Container + run: | + docker run -d -p 5000:5000 --name api-container --memory=7g api-image # Start container on port 5000 + + #Run all Python tests in the directory + - name: Run the Python tests in directory + run: | + docker exec api-container python3 main.py # Execute main.py + #docker exec api-container pytest test_main.py # Execute test_main.py + + #Stop and Remove API Container after tests + - name: Stop and Remove API Container + run: | + docker stop api-container # Stop container after tests + docker rm api-container # Remove container after tests diff --git a/Backend/Docker b/Backend/Docker new file mode 100644 index 0000000..1021b0d --- /dev/null +++ b/Backend/Docker @@ -0,0 +1,11 @@ +FROM python:3.9-slim +WORKDIR /Docker1 +COPY main.py main.py +COPY PetFuc.py PetFuc.py +COPY UserFuc.py UserFuc.py +COPY test_main.py test_main.py +COPY requirements.txt requirements.txt +RUN pip3 install -r requirements.txt +CMD [ "python3", "main.py" ] + + diff --git a/Backend/dockerfile b/Backend/dockerfile new file mode 100644 index 0000000..fd397fc --- /dev/null +++ b/Backend/dockerfile @@ -0,0 +1,30 @@ +# Use Python as the base image +FROM python:3.8-slim-buster + +# Install pip and Python dependencies +RUN apt-get update && apt-get install -y python3 python3-pip + +# Keeps Python from generating .pyc files in the container +ENV PYTHONDONTWRITEBYTECODE=1 + +# Turns off buffering for easier container logging +ENV PYTHONUNBUFFERED=1 + + +# Set the working directory in the container +WORKDIR /app + + +# Install pip requirements +COPY Backend/requirements.txt . +RUN python3 -m pip install -r requirements.txt + +# Copy main application file +COPY Backend/main.py . + +# Set the port for the application +ENV PORT=5000 +EXPOSE 5000 + +# Command to run the application +CMD ["python3", "main.py"] diff --git a/Backend/main.py b/Backend/main.py index 9df3c8f..bcb9f9f 100644 --- a/Backend/main.py +++ b/Backend/main.py @@ -1,14 +1,21 @@ from flask import Flask, json, request, jsonify +import random from PetFuc import Get_Random_Pet from UserFuc import Add_User_Faviorte, Remove_User_Faviorte, Replace_User_Location + + app = Flask(__name__) + +UserAccInfo = {"Name":"eric","Location":"connecticut","Occupation":"Student"} # example user information + #Get Random Pet information - For homepage @app.route("/Get_Random_pet", methods=["GET"]) def get_random_pet(): return Get_Random_Pet(), 200 # Return dog information + # Faviortes route. add user faviorte pet to database @app.route("/Add_User_Faviorte", methods=["POST"]) def add_user_faviorte(): @@ -18,6 +25,7 @@ def add_user_faviorte(): #Retrun database with succses code return jsonify(Add_User_Faviorte(json_data)), 200 + # Un Faviote Route. remove user faviorte pet from there database @app.route("/Remove_User_Faviorte", methods=["DELETE"]) def remove_user_faviorte(): diff --git a/Backend/pet_func.py b/Backend/pet_func.py new file mode 100644 index 0000000..6638903 --- /dev/null +++ b/Backend/pet_func.py @@ -0,0 +1,11 @@ +""" This module handles pet-related functionalities, providing details about random pets. +It uses the Flask framework to return pet information in a JSON format. """ +from flask import jsonify + +def get_random_pet(): + """Return a random pet's information.""" + randomint = "9" # API generates a random integer representing a PET ID + # API accesses database and returns information associated with the random ID + # Returns information such as profile picture of the pet + return jsonify({"id": randomint, "name": "Sam", "sex": "Male", "Age": "8", + "location": "Connecticut", "Breed": "Pug"}) # Return pet details diff --git a/Backend/test_main.py b/Backend/test_main.py index 57becdf..ad27f16 100644 --- a/Backend/test_main.py +++ b/Backend/test_main.py @@ -1,132 +1,44 @@ +""" This module contains unit tests for the pet adoption web application, testing the functionality +of various endpoints. It uses the requests library to simulate HTTP requests. """ import requests -from flask import Flask, json, request, jsonify - -def test_Get_Random_pet(): - """ - Test for http://localhost:5000/Get_Random_pet - """ +def test_get_random_pet(): + """Test for http://localhost:5000/get_random_pet.""" url = "http://localhost:5000" - - response = requests.get(url + "/Get_Random_pet") - - expected_response = { - "id": "9", - "name": "sam", - "sex": "male", - "Age": "8", - "location": "Connecticwut", - "Breed": "Pug" - } - + response = requests.get(f"{url}/get_random_pet", timeout=10) + expected_response = {"id": "9", "name": "Sam", "sex": "Male", "Age": "8", + "location": "Connecticut", "Breed": "Pug"} assert response.json() == expected_response -# ----------------------------------------------------------------------------- - -def test_Add_User_Faviorte(): - """ - Test for http://localhost:5000/Add_User_Faviorte - """ - +def test_add_user_favorite(): + """Test for http://localhost:5000/add_user_favorite.""" url = "http://localhost:5000" - - FaviortePet1 = { - "id": "9", - "name": "sam", - "sex": "male", - "Age": "8", - "location": "Connecticut", - "Breed": "Pug" - } - - - response = requests.post((url + "/Add_User_Faviorte"), json=(FaviortePet1)) - - assert response.json() == [{'Age': '8', 'Breed': 'Pug', 'id': '9', 'location': 'Connecticut', 'name': 'sam', 'sex': 'male'}] - - FaviortePet2 = { - "id": "90", - "name": "ben", - "sex": "male", - "Age": "2", - "location": "New York", - "Breed": "Husky" - } - - response = requests.post((url + "/Add_User_Faviorte"), json=(FaviortePet2)) - - assert response.json() == [{'Age': '8', 'Breed': 'Pug', 'id': '9', 'location': 'Connecticut', 'name': 'sam', 'sex': 'male'}, {'Age': '2', 'Breed': 'Husky', 'id': '90' -, 'location': 'New York', 'name': 'ben', 'sex': 'male'}] - -# ---------------------------------------------------------------------------------- - -def test_Remove_User_Faviorte(): - """ - Test for http://localhost:5000/Remove_User_Faviorte - """ - + favorite_pet1 = {"id": "9", "name": "Sam", "sex": "Male", "Age": "8", + "location": "Connecticut", "Breed": "Pug"} + response = requests.post(f"{url}/add_user_favorite", json=favorite_pet1, timeout=10) + assert response.json() == [favorite_pet1] + favorite_pet2 = {"id": "90", "name": "Ben", "sex": "Male", "Age": "2", + "location": "New York", "Breed": "Husky"} + response = requests.post(f"{url}/add_user_favorite", json=favorite_pet2, timeout=10) + assert response.json() == [favorite_pet1, favorite_pet2] + +def test_remove_user_favorite(): + """Test for http://localhost:5000/remove_user_favorite.""" url = "http://localhost:5000" - - FaviortePet1 = { - "id": "9", - "name": "sam", - "sex": "male", - "Age": "8", - "location": "Connecticut", - "Breed": "Pug" - } - - - response = requests.delete((url + "/Remove_User_Faviorte"), json=(FaviortePet1)) - - assert response.json() == [{'Age': '2', 'Breed': 'Husky', 'id': '90', 'location': 'New York', 'name': 'ben', 'sex': 'male'}] - - FaviortePet2 = { - "id": "90", - "name": "ben", - "sex": "male", - "Age": "2", - "location": "New York", - "Breed": "Husky" - } - - response = requests.delete((url + "/Remove_User_Faviorte"), json=(FaviortePet2)) - + favorite_pet1 = {"id": "9", "name": "Sam", "sex": "Male", "Age": "8", + "location": "Connecticut", "Breed": "Pug"} + response = requests.delete(f"{url}/remove_user_favorite", json=favorite_pet1, timeout=10) + assert response.json() == [] + favorite_pet2 = {"id": "90", "name": "Ben", "sex": "Male", "Age": "2", + "location": "New York", "Breed": "Husky"} + response = requests.delete(f"{url}/remove_user_favorite", json=favorite_pet2, timeout=10) assert response.json() == [] -# ---------------------------------------------------------------------------------- - - - -def test_Change_User_Location(): - """ - Test for http://localhost:5000/Change_User_Location - """ +def test_change_user_location(): + """Test for http://localhost:5000/change_user_location.""" url = "http://localhost:5000" - - response = requests.put((url + "/Change_User_Location"), json={"Location":"New York"}) - - assert response.json() == {"Location": "New York","Name": "eric","Occupation": "Student","Password": "2020"} - - - response = requests.put((url + "/Change_User_Location"), json={"Location":"Chicago"}) - - assert response.json() == {"Location": "Chicago","Name": "eric","Occupation": "Student","Password": "2020"} - - response = requests.put((url + "/Change_User_Location"), json={"Location":"Massachusetts"}) - - assert response.json() == {"Location": "Massachusetts","Name": "eric","Occupation": "Student","Password": "2020"} - - - - - - - - - -test_Get_Random_pet() -test_Add_User_Faviorte() -test_Remove_User_Faviorte() -test_Change_User_Location() -print("Test check Complete: Passed") + response = requests.put(f"{url}/change_user_location", json={"Location": "New York"}, + timeout=10) + assert response.json() == {"Location": "New York", "Name": "Eric", "Occupation": + "Student", "Password": "2020"} + \ No newline at end of file diff --git a/Backend/user_func.py b/Backend/user_func.py new file mode 100644 index 0000000..51ff4ad --- /dev/null +++ b/Backend/user_func.py @@ -0,0 +1,26 @@ +""" This module handles user-related functionalities such as +managing favorites and updating user information. """ + +user_favorite_database = [] # Simulate a database for user favorites +user_acc_info = { + "Name": "Eric", + "Password": "2020", + "Location": "Connecticut", + "Occupation": "Student" +} + +def add_user_favorite(json_data): + """Add user favorite pet to the database.""" + user_favorite_database.append(json_data) + return user_favorite_database # Return updated user favorites database + +def remove_user_favorite(json_data): + """Remove user favorite pet from the database.""" + user_favorite_database.remove(json_data) + return user_favorite_database # Return updated user favorites database + +def replace_user_location(new_location): + """Change the user account location.""" + new_location = new_location.get("Location") + user_acc_info["Location"] = new_location # Set new location associated with the user + return user_acc_info # Return updated user account information diff --git a/Frontend/hi.txt b/Frontend/hi.txt new file mode 100644 index 0000000..53a199a --- /dev/null +++ b/Frontend/hi.txt @@ -0,0 +1 @@ +I'm just testing the ci/cd :) \ No newline at end of file