def setUp(self): self.r1 = [ 1, 'Toilet Paper', 'this is NOT a description', 'home', 'Requesting', 1 ] self.r2 = [ 2, 'Hand Sanitizer', 'this MAY be a decription', 'Maya', 'Provided', 3 ] self.r3 = [ 3, 'Food', 'this is POSSIBLY a description', 'laCalle', 'Provided', 1 ] self.r4 = [ 4, 'send help pls', 'this is ME DOING a description', 'home', 'Requesting', 3 ] self.r5 = [ 5, 'beer', 'this is JUST a description', 'bar', 'Requesting', 4 ] self.requests: list = [self.r1, self.r2, self.r3, self.r4, self.r5] self.user1 = [1, 'Morsa', 'faces4444', '*****@*****.**', '7878598899'] self.keys = [ 'rid', 'rtitle', 'rdescription', 'rlocation', 'rstatus', 'ruser' ] self.rh = RequestHandler() self.dao = RequestDAO() self.daoUSER = UserDAO() self.app = Flask(__name__)
def delete_request_by_id(self, rid: int): try: dao = RequestDAO() row = dao.get_request_by_id(rid) if not row: return jsonify(Error="Request" + str(rid) + " not found."), 404 else: if dao.delete_request_by_id(rid) > 0: return jsonify(DeletedRequest=self.create_request_dict(row)), 200 else: return jsonify(Error="Delete failed"), 404 except Exception as e: print(e) return jsonify(ERROR=e), 500
def setUp(self): self.r1 = [ 1, 'Toilet Paper', 'this is NOT a description', 'home', 'Requesting', 1 ] self.r2 = [ 2, 'Hand Sanitizer', 'this MAY be a decription', 'Maya', 'Provided', 3 ] self.r3 = [ 3, 'Food', 'this is POSSIBLY a description', 'laCalle', 'Provided', 1 ] self.r4 = [ 4, 'send help pls', 'this is ME DOING a description', 'home', 'Requesting', 3 ] self.r5 = [ 5, 'beer', 'this is JUST a description', 'bar', 'Requesting', 4 ] self.keys = [ 'rid', 'rtitle', 'rdescription', 'rlocation', 'rstatus', 'ruser' ] self.rh = RequestHandler() self.dao = RequestDAO() self.app = Flask(__name__)
def insert_request(self, json_input): if len(json_input ) != 5: # check if there are sufficient elements in input return jsonify(Error="Malformed insert request"), 400 try: # check parameters are valid rtitle: str = json_input['rtitle'] rdescription: str = json_input['rdescription'] rlocation: str = json_input['rlocation'] rstatus: str = json_input['rstatus'] ruser: int = json_input['ruser'] # must be an int except Exception as e: print(e) return jsonify( Error="Unexpected attributes in insert request"), 400 try: if rtitle and rdescription and rlocation and rstatus and ruser: rid: int = RequestDAO().insert_request(rtitle, rdescription, rlocation, rstatus, ruser) else: return jsonify(Error="One or more attribute is empty"), 400 except Exception as e: print(e) return jsonify(Error="Request insertion failed horribly."), 400 # Finally returns an request dict of the inserted request. return jsonify(Request=self.create_request_dict( [rid, rtitle, rdescription, rlocation, rstatus, ruser])), 201
def insert_provider(self, json_input): if len(json_input ) != 2: # check if there are sufficient elements in input return jsonify(Error="Malformed insert provider request"), 400 try: # check parameters are valid puser = json_input['puser'] prequest = json_input['prequest'] except: return jsonify( Error="Unexpected attributes in insert provider request"), 400 try: if puser and prequest: # Validate that puser != ruser: if puser == RequestHandler.create_request_dict( RequestDAO().get_request_by_id(prequest))['ruser']: return jsonify( Error="User cannot provide his own request"), 400 # Insert provider dao = ProviderDAO() pid = dao.insert_provider(puser, prequest) # Update status of request form = {} form['prequest'] = prequest form[ 'rstatus'] = 'InProgress' # YELLOW, its updated to delivery on it's way. self.update_request_status_by_provider(form) else: return jsonify(Error="One or more attribute is empty"), 400 except: return jsonify(Error="Provider insertion failed horribly."), 400 # Finally returns an user dict of the inserted provider. return jsonify( Provider=self.createProviderDict([pid, puser, prequest])), 201
def get_request_by_status(self, status: str): try: requests = RequestDAO().get_request_by_status(status) results = list() for row in requests: results.append(self.create_request_dict(row)) return jsonify(Requests=results), 200 except Exception as e: print(e) return jsonify(ERROR=e), 500
def get_request_by_uid(self, uid: int): try: requests = RequestDAO().get_requests_by_user_id(uid) results = list() for row in requests: results.append(self.create_request_dict(row)) return jsonify(Requests=results), 200 except Exception as e: print(e) return jsonify(ERROR=e), 500
def get_requests_by_user_status(self, rid: int, status: str): try: requests = RequestDAO().get_requests_by_user_status(rid, status) results = list() for row in requests: results.append(self.create_request_dict(row)) return jsonify(Requests=results), 200 except Exception as e: print(e) return jsonify(ERROR="Unexpected error"), 500
def get_request_by_location(self, location: str): try: requests = RequestDAO().get_request_by_location(location) results = list() for row in requests: results.append(self.create_request_dict(row)) return jsonify(Requests=results) except Exception as e: print(e) return jsonify(ERROR=e), 500
class UserHandlerTestCase(unittest.TestCase): # unit tests for validating user operations def setUp(self): self.r1 = [ 1, 'Toilet Paper', 'this is NOT a description', 'home', 'Requesting', 1 ] self.r2 = [ 2, 'Hand Sanitizer', 'this MAY be a decription', 'Maya', 'Provided', 3 ] self.r3 = [ 3, 'Food', 'this is POSSIBLY a description', 'laCalle', 'Provided', 1 ] self.r4 = [ 4, 'send help pls', 'this is ME DOING a description', 'home', 'Requesting', 3 ] self.r5 = [ 5, 'beer', 'this is JUST a description', 'bar', 'Requesting', 4 ] self.requests: list = [self.r1, self.r2, self.r3, self.r4, self.r5] self.user1 = [1, 'Morsa', 'faces4444', '*****@*****.**', '7878598899'] self.keys = [ 'rid', 'rtitle', 'rdescription', 'rlocation', 'rstatus', 'ruser' ] self.rh = RequestHandler() self.dao = RequestDAO() self.daoUSER = UserDAO() self.app = Flask(__name__) def test_create_request_dict(self): self.assertDictEqual(dict(zip(self.keys, self.r1)), self.rh.create_request_dict(self.r1)) self.assertDictEqual(dict(zip(self.keys, self.r2)), self.rh.create_request_dict(self.r2)) self.assertDictEqual(dict(zip(self.keys, self.r3)), self.rh.create_request_dict(self.r3)) self.assertDictEqual(dict(zip(self.keys, self.r4)), self.rh.create_request_dict(self.r4)) self.assertDictEqual(dict(zip(self.keys, self.r5)), self.rh.create_request_dict(self.r5)) def test_get_all_requests(self): with self.app.app_context(): self.assertTrue( len(self.rh.get_all_requests()[0].json["Requests"]) > 1) def test_get_request_by_uid(self): with self.app.app_context(): uid = self.daoUSER.insert_user(self.user1[1], self.user1[2], self.user1[3], self.user1[4]) for r in self.requests: r[5] = uid rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5]) request = self.rh.get_request_by_uid(uid)[0].json["Requests"] r[0] = rid self.assertIn(dict(zip(self.keys, r)), request) print("DELETED", self.daoUSER.delete_user_by_id(uid)) def test_get_requests_by_user_status(self): with self.app.app_context(): uid = self.daoUSER.insert_user(self.user1[1], self.user1[2], self.user1[3], self.user1[4]) for r in self.requests: r[5] = uid rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5]) request = self.rh.get_requests_by_user_status( rid=r[5], status=r[4])[0].json["Requests"] r[0] = rid self.assertIn(dict(zip(self.keys, r)), request) print("DELETED", self.daoUSER.delete_user_by_id(uid)) def test_insert_request(self): with self.app.app_context(): uid = self.daoUSER.insert_user(self.user1[1], self.user1[2], self.user1[3], self.user1[4]) for r in self.requests: r[5] = uid rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5]) request = self.rh.get_all_requests()[0].json["Requests"] r[0] = rid self.assertIn(dict(zip(self.keys, r)), request) print("DELETED", self.daoUSER.delete_user_by_id(uid)) def test_delete_request_by_id(self): with self.app.app_context(): uid = self.daoUSER.insert_user(self.user1[1], self.user1[2], self.user1[3], self.user1[4]) for r in self.requests: r[5] = uid rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5]) r[0] = rid self.rh.delete_request_by_id(r[0]) request = self.rh.get_all_requests()[0].json["Requests"] self.assertNotIn(dict(zip(self.keys, r)), request) print("DELETED", self.daoUSER.delete_user_by_id(uid)) def test_get_request_by_location(self): with self.app.app_context(): uid = self.daoUSER.insert_user(self.user1[1], self.user1[2], self.user1[3], self.user1[4]) for r in self.requests: r[5] = uid rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5]) request = self.rh.get_request_by_location( r[3])[0].json["Requests"] r[0] = rid self.assertIn(dict(zip(self.keys, r)), request) print("DELETED", self.daoUSER.delete_user_by_id(uid)) def test_get_request_by_status(self): with self.app.app_context(): uid = self.daoUSER.insert_user(self.user1[1], self.user1[2], self.user1[3], self.user1[4]) for r in self.requests: r[5] = uid rid = self.dao.insert_request(r[1], r[2], r[3], r[4], r[5]) request = self.rh.get_request_by_status( r[4])[0].json["Requests"] r[0] = rid self.assertIn(dict(zip(self.keys, r)), request) print("DELETED", self.daoUSER.delete_user_by_id(uid))