-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pylint debugging for .py files in Backend
- Loading branch information
IshayuR
committed
Oct 31, 2024
1 parent
70379a1
commit 97eeba9
Showing
6 changed files
with
86 additions
and
190 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,36 @@ | ||
from flask import Flask, json, request, jsonify | ||
# Module main.py | ||
# This module is the main entry point for the pet adoption web application. | ||
from flask import Flask, request, jsonify | ||
import random | ||
from PetFuc import Get_Random_Pet | ||
from UserFuc import Add_User_Faviorte, Remove_User_Faviorte, Replace_User_Location | ||
|
||
|
||
from pet_func import get_random_pet | ||
from user_func import add_user_favorite, remove_user_favorite, replace_user_location | ||
|
||
app = Flask(__name__) | ||
|
||
user_acc_info = {"Name": "Eric", "Location": "Connecticut", "Occupation": "Student"} # Example user information | ||
|
||
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(): | ||
json_data = request.get_json() | ||
|
||
# Api accesses database and append post json in database | ||
#Retrun database with succses code | ||
return jsonify(Add_User_Faviorte(json_data)), 200 | ||
|
||
@app.route("/get_random_pet", methods=["GET"]) | ||
def get_random_pet_endpoint(): | ||
"""Fetch and return random pet information for the homepage.""" | ||
return get_random_pet(), 200 | ||
|
||
# Un Faviote Route. remove user faviorte pet from there database | ||
@app.route("/Remove_User_Faviorte", methods=["DELETE"]) | ||
def remove_user_faviorte(): | ||
json_data = request.get_json() | ||
@app.route("/add_user_favorite", methods=["POST"]) | ||
def add_user_favorite_endpoint(): | ||
"""Add a user's favorite pet to the database.""" | ||
json_data = request.get_json() | ||
return jsonify(add_user_favorite(json_data)), 200 | ||
|
||
# Api accesses database and append post json in database | ||
#Retrun database with succses code | ||
return jsonify(Remove_User_Faviorte(json_data)), 200 # return new user database | ||
@app.route("/remove_user_favorite", methods=["DELETE"]) | ||
def remove_user_favorite_endpoint(): | ||
"""Remove a user's favorite pet from the database.""" | ||
json_data = request.get_json() | ||
return jsonify(remove_user_favorite(json_data)), 200 | ||
|
||
# Change a Users account location | ||
@app.route("/Change_User_Location", methods=["PUT"]) | ||
@app.route("/change_user_location", methods=["PUT"]) | ||
def replace_user_information(): | ||
NewLocation = request.get_json() # Get new location from post request | ||
return jsonify(Replace_User_Location(NewLocation)) # retrun user account info | ||
"""Change a user's account location.""" | ||
new_location = request.get_json() | ||
return jsonify(replace_user_location(new_location)), 200 | ||
|
||
if __name__ == "__main__": | ||
app.run(host="0.0.0.0", port=5000, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Module pet_func.py | ||
# This module handles pet-related functionalities. | ||
from flask import jsonify | ||
|
||
def get_random_pet(): | ||
"""Return a random pet's information.""" | ||
randomint = "9" # API generates random integer that represents a PET ID | ||
# Api accesses database and returns information in database | ||
# Return information such as profile picture associated with the random ID | ||
return jsonify({"id": randomint, "name": "Sam", "sex": "Male", "Age": "8", "location": "Connecticut", "Breed": "Pug"}) # Return dog information |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,42 @@ | ||
# Module test_main.py | ||
# This module contains unit tests for the pet adoption web application. | ||
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") | ||
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) | ||
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) | ||
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) | ||
assert response.json() == [] # Assuming the database is modified accordingly | ||
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) | ||
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(f"{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":"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() | ||
test_get_random_pet() | ||
test_add_user_favorite() | ||
test_remove_user_favorite() | ||
test_change_user_location() | ||
print("Test check Complete: Passed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Module user_func.py | ||
# This module handles user-related functionalities such as managing favorite pets and user account information. | ||
from flask import jsonify | ||
|
||
user_favorite_database = [] # Simulate a database for user favorites | ||
user_acc_info = {"Name": "Eric", "Password": "2020", "Location": "Connecticut", "Occupation": "Student"} # Example user account information | ||
|
||
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 |