Skip to content

Commit

Permalink
Merge pull request #48 from CSCI-GA-2820-SP24-003/alexqzh23/issue36
Browse files Browse the repository at this point in the history
Alex Qi/issue36/Read customers based on multiple fields
  • Loading branch information
Carzgil authored Apr 3, 2024
2 parents 21ce911 + a24f95b commit 308c882
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 5 deletions.
12 changes: 12 additions & 0 deletions service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,15 @@ def query_by_address(cls, address):
"""It should return a list of all customers with a certain address"""
logger.info("Processing lookup for %s ...", address)
return cls.query.filter(cls.address == address)

@classmethod
def query_by_username(cls, username):
"""It should return a list of all customers with a certain username"""
logger.info("Processing lookup for %s ...", username)
return cls.query.filter(cls.username == username)

@classmethod
def query_by_email(cls, email):
"""It should return a list of all customers with a certain email"""
logger.info("Processing lookup for %s ...", email)
return cls.query.filter(cls.email == email)
44 changes: 39 additions & 5 deletions service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import hashlib
from flask import jsonify, request, url_for, abort
from flask import current_app as app # Import Flask application
from service.models import Customer
from service.models import Customer, Gender
from service.common import status # HTTP Status Codes


Expand Down Expand Up @@ -131,15 +131,49 @@ def delete_customers(customer_id):


######################################################################
# LIST ALL CUSTOMERS
# LIST ALL CUSTOMERS BY ATTRIBUTES
######################################################################
@app.route("/customers", methods=["GET"])
def list_customers():
"""Returns all of the Customers"""
"""Returns all of the Customers by some Attributes"""
app.logger.info("Request for customer list")

customers = Customer.all()

query = Customer.query

# if 'username' in request.args:
# query = Customer.query_by_username(request.args.get('username'))
# if 'email' in request.args:
# query = Customer.query_by_email(request.args.get('email'))
# if 'first_name' in request.args:
# query = Customer.find_by_name(request.args.get('first_name'))
# if 'last_name' in request.args:
# query = Customer.query_by_last_name(request.args.get('last_name'))
# if 'address' in request.args:
# query = Customer.query_by_address(request.args.get('address'))

# dynamic querying
for param in ["username", "email", "first_name", "last_name", "address"]:
if param in request.args:
value = request.args.get(param)
query = query.filter(getattr(Customer, param) == value)

if 'gender' in request.args:
gender_value = request.args.get('gender').upper()
if gender_value in Gender.__members__:
query = Customer.query_by_gender(Gender[gender_value])
else:
return jsonify({"error": "Invalid gender value"}), 400

if "active" in request.args:
active_value = request.args.get("active").lower()
if active_value in ["true", "1"]:
query = query.filter(Customer.active)
elif active_value in ["false", "0"]:
query = query.filter(~Customer.active)
else:
return jsonify({"error": "Invalid active value"}), 400

customers = query.all()
results = [customer.serialize() for customer in customers]
app.logger.info("Returning %d customers", len(results))
return jsonify(results), status.HTTP_200_OK
Expand Down
28 changes: 28 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,31 @@ def test_query_by_address(self):
self.assertEqual(query.count(), count)
for customer in query:
self.assertEqual(customer.address, address)

def test_query_by_username(self):
"""It should Find a Customer by Username"""
customers = CustomerFactory.create_batch(10)
for customer in customers:
customer.create()
username = customers[0].username
count = len(
[customer for customer in customers if customer.username == username]
)
found = Customer.query_by_username(username)
self.assertEqual(found.count(), count)
for customer in found:
self.assertEqual(customer.username, username)

def test_query_by_email(self):
"""It should return a list of all customers with the specified email."""
customers = CustomerFactory.create_batch(10)
for customer in customers:
customer.create()
# logging.debug(customers)
email = customers[0].email
count = len([customer for customer in customers if customer.email == email])
query = Customer.query_by_email(email)

self.assertEqual(query.count(), count)
for customer in query:
self.assertEqual(customer.email, email)
143 changes: 143 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,149 @@ def test_get_customer_list(self):
data = response.get_json()
self.assertEqual(len(data), 5)

def test_get_customer_list_with_username(self):
"""It should filter customers by username"""
CustomerFactory(username="user123").create()
CustomerFactory(username="user456").create()
CustomerFactory(username="user789").create()

response = self.client.get(f"{BASE_URL}?username=user123")
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.get_json()
self.assertEqual(len(data), 1)
self.assertEqual(data[0]['username'], 'user123')

def test_get_customer_list_with_email(self):
"""It should filter customers by email"""
CustomerFactory(email="[email protected]").create()
CustomerFactory(email="[email protected]").create()
CustomerFactory(email="[email protected]").create()

response = self.client.get(f"{BASE_URL}[email protected]")
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.get_json()
self.assertEqual(len(data), 1)
self.assertEqual(data[0]['email'], "[email protected]")

def test_get_customer_list_with_first_name(self):
"""It should filter customers by first name"""
customers = CustomerFactory.create_batch(2)
for customer in customers:
customer.first_name = "name123"
customer.create()
customers = CustomerFactory.create_batch(3)
for customer in customers:
customer.first_name = "name456"
customer.create()
customers = CustomerFactory.create_batch(4)
for customer in customers:
customer.first_name = "name789"
customer.create()

response = self.client.get(f"{BASE_URL}?first_name=name123")
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.get_json()
self.assertEqual(len(data), 2)
self.assertEqual(data[0]['first_name'], "name123")

def test_get_customer_list_with_last_name(self):
"""It should filter customers by last name"""
customers = CustomerFactory.create_batch(2)
for customer in customers:
customer.last_name = "name123"
customer.create()
customers = CustomerFactory.create_batch(3)
for customer in customers:
customer.last_name = "name456"
customer.create()
customers = CustomerFactory.create_batch(4)
for customer in customers:
customer.last_name = "name789"
customer.create()

response = self.client.get(f"{BASE_URL}?last_name=name123")
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.get_json()
self.assertEqual(len(data), 2)
self.assertEqual(data[0]['last_name'], "name123")

def test_get_customer_list_with_address(self):
"""It should filter customers by address"""
customers = CustomerFactory.create_batch(2)
for customer in customers:
customer.address = "123 Main St"
customer.create()
customers = CustomerFactory.create_batch(3)
for customer in customers:
customer.address = "456 Main St"
customer.create()
customers = CustomerFactory.create_batch(4)
for customer in customers:
customer.address = "789 Main St"
customer.create()

response = self.client.get(f"{BASE_URL}?address=123 Main St")
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.get_json()
self.assertEqual(len(data), 2)
self.assertEqual(data[0]['address'], "123 Main St")

def test_get_customer_list_with_gender(self):
"""It should filter customers by gender"""
customers = CustomerFactory.create_batch(2)
for customer in customers:
customer.gender = "MALE"
customer.create()
customers = CustomerFactory.create_batch(3)
for customer in customers:
customer.gender = "FEMALE"
customer.create()
customers = CustomerFactory.create_batch(4)
for customer in customers:
customer.gender = "UNKNOWN"
customer.create()

response = self.client.get(f"{BASE_URL}?gender=MALE")
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.get_json()
self.assertEqual(len(data), 2)
self.assertEqual(data[0]['gender'], "MALE")

def test_get_customer_list_with_active_status(self):
"""It should filter customers by active status"""
customers = CustomerFactory.create_batch(2)
for customer in customers:
customer.active = True
customer.create()
customers = CustomerFactory.create_batch(3)
for customer in customers:
customer.active = False
customer.create()

response = self.client.get(f"{BASE_URL}?active=true")
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.get_json()
self.assertEqual(len(data), 2)

response = self.client.get(f"{BASE_URL}?active=false")
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.get_json()
self.assertEqual(len(data), 3)

def test_get_customer_list_with_invalid_gender(self):
"""It should return an error for invalid gender value"""
response = self.client.get(f"{BASE_URL}?gender=NOT_A_GENDER")
self.assertEqual(response.status_code, 400)
data = response.get_json()
self.assertIn("Invalid gender value", data["error"])

def test_get_customer_list_with_invalid_active(self):
"""It should return an error for invalid active value"""
response = self.client.get(f"{BASE_URL}?active=not_a_boolean")
self.assertEqual(response.status_code, 400)
data = response.get_json()
self.assertIn("Invalid active value", data["error"])

def test_update_customer(self):
"""It should Update an existing Customer"""
# create a customer to update
Expand Down

0 comments on commit 308c882

Please sign in to comment.