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 setUp(self): self.ph = ProviderHandler() self.dao = ProviderDAO() self.new_provider = { "puser": 6, "prequest": 3, } self.new_provider2 = { "puser": 7, "prequest": 3, } self.new_provider3 = { "puser": 9, "prequest": 5, } self.app = Flask(__name__)
def delete_provider(self, puser: int): try: provider = ProviderDAO().delete_provider_by_id(puser) if provider: return jsonify(Provider="Provider was successfully deleted.") else: return jsonify(ERROR="Provider Not Found"), 404 except: return jsonify(ERROR="Handler Error"), 500
def get_provider_by_user_id(self, puser: int): try: provider = ProviderDAO().get_provider_by_user_id(puser) if provider: return jsonify(Provider=self.createProviderDict(provider)) else: return jsonify(ERROR="Provider Not Found"), 404 except: return jsonify(ERROR="Handler Error"), 500
def get_all_providers(self): try: providers = ProviderDAO().get_all_providers() results = list() for row in providers: results.append(self.createProviderDict(row)) return jsonify(Providers=results) except: return jsonify(ERROR="Server error!"), 500
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: dao = ProviderDAO() pid = dao.insert_provider(puser, prequest) 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 user. return jsonify( Provider=self.createProviderDict([pid, puser, prequest])), 201
def update_request_status_by_provider(self, json_input): if len(json_input ) != 2: # check if there are sufficient elements in input return jsonify(Error="Malformed update status request"), 400 try: # check parameters are valid prequest = json_input['prequest'] rstatus = json_input['rstatus'] except: return jsonify( Error="Unexpected attributes in status update request"), 400 try: if prequest and rstatus: dao = ProviderDAO() pid = dao.update_status_by_prequest(prequest, rstatus) json_input[ 'pid'] = pid # Add pid to the result dictionary (prequest, rstatus, pid) else: return jsonify(Error="One or more attribute is empty"), 400 except: return jsonify(Error="Status update failed horribly."), 400 # Finally returns the request & status that were updated. return jsonify(RequestStatus=json_input), 201
class ProviderHandlerTestCase(unittest.TestCase): # unit tests for validating user operations def setUp(self): self.ph = ProviderHandler() self.dao = ProviderDAO() self.new_provider = { "puser": 6, "prequest": 3, } self.new_provider2 = { "puser": 7, "prequest": 3, } self.new_provider3 = { "puser": 9, "prequest": 5, } self.app = Flask(__name__) # def test_get_all_providers(self): # will get a list of providers with self.app.app_context(): result = json.loads( self.ph.get_all_providers().get_data())['Providers'] self.assertTrue(len(result) >= 1) def test_get_provider_by_id(self): with self.app.app_context(): result = json.loads( self.ph.get_all_providers().get_data())['Providers'] first_provider = result[0] provider_result = json.loads( self.ph.get_provider_by_id( first_provider['pid']).get_data())['Provider'] self.assertEqual(provider_result, first_provider) self.assertEqual(self.ph.get_provider_by_id(-1)[1], 404) def test_get_provider_by_request_id(self): with self.app.app_context(): result = json.loads( self.ph.get_all_providers().get_data())['Providers'] first_provider = result[0] provider_result = json.loads( self.ph.get_provider_by_request_id( first_provider['prequest']).get_data())['Provider'] self.assertEqual(provider_result, first_provider) self.assertEqual(self.ph.get_provider_by_request_id(-1)[1], 404) def test_get_provider_by_user_id(self): with self.app.app_context(): result = json.loads( self.ph.get_all_providers().get_data())['Providers'] first_provider = result[0] provider_result = json.loads( self.ph.get_provider_by_user_id( first_provider['puser']).get_data())['Provider'] self.assertEqual(provider_result, first_provider) self.assertEqual(self.ph.get_provider_by_user_id(-1)[1], 404) def test_insert_provider(self): with self.app.app_context(): # Test for providing its own request result = self.ph.insert_provider(self.new_provider) self.assertEqual(result[1], 400) # Test for providing a request successfully result = self.ph.insert_provider(self.new_provider2) pid = json.loads(result[0].get_data())['Provider']['pid'] self.assertEqual(result[1], 201) self.dao.delete_provider_by_id( pid) # so test provider is not persisted # Test for unsuccessful parameters self.new_provider2.pop('puser') result2 = self.ph.insert_provider(self.new_provider2) self.assertEqual(result2[1], 400) # provider should never enter db