Skip to content

Commit

Permalink
API complete
Browse files Browse the repository at this point in the history
  • Loading branch information
eoa21004 committed Oct 31, 2024
1 parent 7add32c commit b14c594
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 122 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/ms5.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions Backend/Docker
Original file line number Diff line number Diff line change
@@ -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" ]


30 changes: 30 additions & 0 deletions Backend/dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
8 changes: 8 additions & 0 deletions Backend/main.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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():
Expand Down
11 changes: 11 additions & 0 deletions Backend/pet_func.py
Original file line number Diff line number Diff line change
@@ -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
156 changes: 34 additions & 122 deletions Backend/test_main.py
Original file line number Diff line number Diff line change
@@ -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"}

26 changes: 26 additions & 0 deletions Backend/user_func.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions Frontend/hi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm just testing the ci/cd :)

0 comments on commit b14c594

Please sign in to comment.