Skip to content
4 changes: 1 addition & 3 deletions project/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Request(db.Model):
enddate = db.Column(db.Date, nullable=False)
description = db.Column(db.Text, nullable=False)
requester = db.Column(db.Text, nullable=False)
finalized = db.Column(db.Boolean, nullable=False)
finalized = db.Column(db.Boolean, nullable=False, default=False)
lender = db.Column(db.Text, nullable=True)
productcategoryid = db.Column(
db.SmallInteger, db.ForeignKey("product_category.productcategoryid")
Expand All @@ -49,14 +49,12 @@ def __init__(
requester,
lender,
productcategoryid,
finalized,
):
self.productname = productname
self.startdate = startdate
self.enddate = enddate
self.description = description
self.requester = requester
self.finalized = finalized
self.lender = lender
self.productcategoryid = productcategoryid

Expand Down
2 changes: 1 addition & 1 deletion project/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def edit_request(requestid):
def delete_request(requestid):
request = Request.query.filter_by(requestid=requestid).first()

error_response = {"status": "fail", "message": "Could not delete request."}
error_response = {"status": "fail", "message": "Could not found request to delete."}

if not request:
return jsonify(error_response), 404
Expand Down
32 changes: 31 additions & 1 deletion project/tests/test_product_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,45 @@
from project.api.models import db
from project.tests.utils import add_category

PRODUCT_CATEGORY_BASE_URL = "/product_category"


class TestProductCategory(BaseTestCase):
def test_add_categories(self):
with self.client:
response = self.client.post(
PRODUCT_CATEGORY_BASE_URL,
data=json.dumps({"name": "Jogos"}),
content_type="application/json",
)

data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 201)
self.assertIn("Category Jogos was created!", data["data"]["message"])
self.assertIn("success", data["status"])

def test_add_categories_invalid_json(self):
with self.client:
response = self.client.post(
PRODUCT_CATEGORY_BASE_URL,
data=json.dumps({}),
content_type="application/json",
)

data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 400)
self.assertIn("Invalid payload.", data["message"])
self.assertIn("fail", data["status"])

def test_get_all_category(self):
add_category("Eletrodomésticos")
add_category("Livros e revistas")
add_category("Eletrônicos")

with self.client:
response = self.client.get("/product_category")
response = self.client.get(PRODUCT_CATEGORY_BASE_URL)
data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 200)
Expand Down
163 changes: 143 additions & 20 deletions project/tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,83 @@
from project.tests.utils import add_request, add_category


FAKE_EMAIL = "tah_tu@email.com"
FAKE_DESCRIPTION = (
"Queria um Uno emprestado para jogar com meus amigos neste fim de semana!"
)
FAKE_ENDDATE = "2020-09-30 00:00:00.000"
FAKE_STARTDATE = "2020-09-12 00:00:00.000"
REQUEST_BASE_URL = "/requests"


class TestRequest(BaseTestCase):
def test_create_request(self):
add_category("Eletrodomésticos")
with self.client:
response = self.client.post(
REQUEST_BASE_URL,
data=json.dumps(
{
"productname": "Batedeira",
"startdate": FAKE_STARTDATE,
"enddate": FAKE_ENDDATE,
"description": "Preciso de uma batedeira para fazer meu bolo de aniversario.",
"requester": "tah_tu@gmail.com",
"productcategoryid": 1,
"lender": None,
}
),
content_type="application/json",
)

data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 201)
self.assertIn("success", data["status"])

def test_create_request_invalid_json(self):
with self.client:
response = self.client.post(
REQUEST_BASE_URL,
data=json.dumps({}),
content_type="application/json",
)

data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 400)
self.assertIn("Invalid payload.", data["message"])
self.assertIn("fail", data["status"])

def test_get_all_requests(self):
add_category("Eletrodomésticos")
add_request(
"Banco Imobiliario",
"2020-09-12 00:00:00.000",
"2020-09-30 00:00:00.000",
FAKE_STARTDATE,
FAKE_ENDDATE,
"Queria um banco imobiliário emprestado para jogar com meus amigos neste fim de semana!",
"matheus@email.com",
1,
)
add_request(
"Jogo da vida",
"2020-09-12 00:00:00.000",
"2020-09-30 00:00:00.000",
FAKE_STARTDATE,
FAKE_ENDDATE,
"Queria um jogo da vida emprestado para jogar com meus amigos neste fim de semana!",
"matheus@email.com",
1,
)
add_request(
"War",
"2020-09-12 00:00:00.000",
"2020-09-30 00:00:00.000",
FAKE_STARTDATE,
FAKE_ENDDATE,
"Queria um war emprestado para jogar com meus amigos neste fim de semana!",
"matheus@email.com",
1,
)

with self.client:
response = self.client.get("/requests")
response = self.client.get(REQUEST_BASE_URL)
data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 200)
Expand All @@ -47,23 +94,23 @@ def test_get_filtered_requests(self):
add_category("Eletrodomésticos")
add_request(
"Jogo da vida",
"2020-09-12 00:00:00.000",
"2020-09-30 00:00:00.000",
FAKE_STARTDATE,
FAKE_ENDDATE,
"Queria um jogo da vida emprestado para jogar com meus amigos neste fim de semana!",
"matheus@email.com",
1,
)
add_request(
"War",
"2020-09-12 00:00:00.000",
"2020-09-30 00:00:00.000",
FAKE_STARTDATE,
FAKE_ENDDATE,
"Queria um war emprestado para jogar com meus amigos neste fim de semana!",
"matheus@email.com",
1,
)

with self.client:
response = self.client.get("/requests/1")
response = self.client.get(f"{REQUEST_BASE_URL}/1")
data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 200)
Expand All @@ -76,16 +123,16 @@ def test_update_lender_request(self):
add_category("Eletrodomésticos")
product = add_request(
"Banco Imobiliario",
"2020-09-12 00:00:00.000",
"2020-09-30 00:00:00.000",
FAKE_STARTDATE,
FAKE_ENDDATE,
"Queria um banco imobiliário emprestado para jogar com meus amigos neste fim de semana!",
"matheus@email.com",
1,
)

with self.client:
response = self.client.patch(
f"/requests/{product.requestid}",
f"{REQUEST_BASE_URL}/{product.requestid}",
data=json.dumps({"lender": "maia@email.com"}),
content_type="application/json",
)
Expand All @@ -96,7 +143,7 @@ def test_update_lender_request(self):
def test_cannot_update_non_existing_request_lender(self):
with self.client:
response = self.client.patch(
"/requests/8d27b6c1-ac8a-4f29-97b0-96cef6938267",
f"{REQUEST_BASE_URL}/8d27b6c1-ac8a-4f29-97b0-96cef6938267",
data=json.dumps({"lender": "maia@email.com"}),
content_type="application/json",
)
Expand All @@ -108,16 +155,16 @@ def test_finalize_request(self):
add_category("Eletrodomésticos")
product = add_request(
"Banco Imobiliario",
"2020-09-12 00:00:00.000",
"2020-09-30 00:00:00.000",
FAKE_STARTDATE,
FAKE_ENDDATE,
"Queria um banco imobiliário emprestado para jogar com meus amigos neste fim de semana!",
"matheus@email.com",
1,
)

with self.client:
response = self.client.patch(
f"/requests/{product.requestid}/finalize",
f"{REQUEST_BASE_URL}/{product.requestid}/finalize",
content_type="application/json",
)

Expand All @@ -127,9 +174,85 @@ def test_finalize_request(self):
def test_cannot_finalize_non_existing_request(self):
with self.client:
response = self.client.patch(
"/requests/8d27b6c1-ac8a-4f29-97b0-96cef6938267/finalize",
f"{REQUEST_BASE_URL}/8d27b6c1-ac8a-4f29-97b0-96cef6938267/finalize",
content_type="application/json",
)

data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 404)
self.assertEqual(data["status"], "fail")
self.assertEqual(data["message"], "Request not found")

def test_edit_request(self):
add_category("Eletrodomésticos")
request = add_request(
"Uno",
FAKE_STARTDATE,
FAKE_ENDDATE,
FAKE_DESCRIPTION,
FAKE_EMAIL,
1,
)

with self.client:
response = self.client.put(
f"{REQUEST_BASE_URL}/{request.requestid}",
data=json.dumps(
{
"productname": "Uno",
"startdate": FAKE_STARTDATE,
"enddate": FAKE_ENDDATE,
"description": FAKE_DESCRIPTION,
"requester": FAKE_EMAIL,
"productcategoryid": 1,
"lender": None,
}
),
content_type="application/json",
)

data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 201)
self.assertIn("Update completed!", data["data"]["update_status"])
self.assertIn("success", data["status"])

def test_edit_request_inexistent_id(self):
with self.client:
response = self.client.put(f"{REQUEST_BASE_URL}/8783472")
data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 404)
self.assertIn("fail", data["status"])
self.assertIn("Invalid payload.", data["message"])

def test_delete_request(self):
add_category("Eletrodomésticos")
request = add_request(
"Uno",
FAKE_STARTDATE,
FAKE_ENDDATE,
FAKE_DESCRIPTION,
FAKE_EMAIL,
1,
)

with self.client:
response = self.client.delete(f"{REQUEST_BASE_URL}/{request.requestid}")

data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 200)
self.assertIn("Request deleted!", data["data"]["message"])
self.assertIn("success", data["status"])

def test_delete_request_inexistent_id(self):
with self.client:
response = self.client.delete(
f"{REQUEST_BASE_URL}/8d27b6c1-ac8a-4f29-97b0-96cef6938267"
)
data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 404)
self.assertIn("fail", data["status"])
self.assertIn("Could not found request to delete.", data["message"])
2 changes: 0 additions & 2 deletions project/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ def add_request(
productname, startdate, enddate, description, requester, productcategoryid
):
lender = None
finalized = False

request = Request(
productname,
Expand All @@ -17,7 +16,6 @@ def add_request(
requester,
lender,
productcategoryid,
finalized,
)

db.session.add(request)
Expand Down