def test_delete_grid(self): ''' Test delete_grid method''' handler_obj = GridHandler() err, res = handler_obj.store_grid( get_dummy_valid_grid('delete grid by id')) err, res = handler_obj.delete_grid(res.get('id')) self.assertEqual(err, False)
def test_no_optimal_route(self): ''' Test update_grid method''' handler_obj = GridHandler() err, res = handler_obj.store_grid( get_dummy_valid_grid('no optimal grid by id')) err, res = handler_obj.find_optimal_route(res.pop('id'), 'a', 'x') self.assertEqual(err, True)
def test_update_grid(self): ''' Test update_grid method''' handler_obj = GridHandler() err, res = handler_obj.store_grid( get_dummy_valid_grid('update grid by id')) res['name'] = 'update grid' err, res = handler_obj.update_grid(res.pop('id'), res) self.assertEqual(err, False)
def test_optimal_route_view_set(self): '''Tets delte method of each grid view set''' handler_obj = GridHandler() err, res = handler_obj.store_grid( get_dummy_valid_grid(name='test_optimal_each_grid')) factory = APIRequestFactory() view = OptimalRouteViewSet.as_view() request = factory.get('/grids' + res.get('id') + '?start=a&end=b') response = view(request, res.get('id')) self.assertEqual(response.status_code, 200)
def test_delete_each_grid_view_set(self): '''Tets delte method of each grid view set''' handler_obj = GridHandler() err, res = handler_obj.store_grid( get_dummy_valid_grid(name='test_delete_each_grid')) factory = APIRequestFactory() view = EachGridViewSet.as_view() request = factory.delete('/grids' + res.get('id')) response = view(request, res.get('id')) self.assertEqual(response.status_code, 200)
def test_update_each_grid_view_set(self): '''Tets put method of each grid view set''' handler_obj = GridHandler() err, res = handler_obj.store_grid( get_dummy_valid_grid(name='test_update_each_grid')) factory = APIRequestFactory() view = EachGridViewSet.as_view() request = factory.put( '/grids/' + res.get('id'), json.dumps(get_dummy_valid_grid('test_update_each_grid1')), content_type='application/json') response = view(request, res.get('id')) self.assertEqual(response.status_code, 200)
class OptimalRouteViewSet(APIView): '''view Class for finding optimal route''' def __init__(self): '''Initialize method for EachGridViewSet''' self.handler_obj = GridHandler() def get(self, request, grid_id): '''get method to find the optimal route between two points Args: grid_id: string Return: dictionary Raise: BadRequest ''' start = request.query_params.get('start', None) end = request.query_params.get('end', None) if not start and not end: raise BadRequest(400, 'Please provide start and end nodes !') err, res = self.handler_obj.find_optimal_route(grid_id, start, end) if err: raise BadRequest(400, res) return JSONResponse({'data': res, 'message': 'success', 'statusCode': 200})
class GridViewSet(APIView): '''View class for Gride will Process GET (get all grid) and POST(add a grid)request for gride ''' def __init__(self): '''Initializer method for GridViewSet''' self.handler_obj = GridHandler() def get(self, request): '''get function process GET request Args: request: Request Object Return: JSONResponse ''' offset = int(request.query_params.get('offset', 0)) size = int(request.query_params.get('size', 10)) res, count, offset = self.handler_obj.get_all_grid(offset, size) return JSONResponse({'data': res, 'message': 'success', 'statusCode': 200, 'count': count, 'offset': offset}) def post(self, request): '''get function process POST request Args: request: Request Object Return: JSONResponse Raise: BadRequest ''' data = request.data err, res = self.handler_obj.store_grid(data) if err: raise BadRequest(400, res) return Created({'data': res, 'message': 'success', 'statusCode': 201})
def __init__(self): '''Initialize method for EachGridViewSet''' self.handler_obj = GridHandler()
class EachGridViewSet(APIView): '''View class for Gride will Process GET, POST, DELETE, UPDATE request for each gride ''' def __init__(self): '''Initialize method for EachGridViewSet''' self.handler_obj = GridHandler() def get(self, request, grid_id): '''get function to get each Gride Args: request: Request object grid_id: String(UUID) Return: JSONResponse Raise: BadRequest ''' #get each grid err, res = self.handler_obj.get_grid(grid_id) if err: raise BadRequest(400, res) return JSONResponse({'data': res, 'message': 'success', 'statusCode': 201}) def put(self, request, grid_id): '''Update method for each Gride Args: request: Request object grid_id: String(UUID) Return: JSONResponse Raise: BadRequest ''' data = request.data err, res = self.handler_obj.update_grid(grid_id, data) if err: raise BadRequest(400, res) return JSONResponse({'data': res, 'message': 'success', 'statusCode': 201}) def delete(self, request, grid_id): '''DELETE method for each Gride Args: request: Request object grid_id: String(UUID) Return: JSONResponse Raise: BadRequest ''' #delete each grid err, res = self.handler_obj.delete_grid(grid_id) if err: raise BadRequest(400, res) return OK({'message': res, 'statusCode': 200})
def test_get_all_grid(self): ''' Test get_all_grid method''' handler_obj = GridHandler() res, count, offset = handler_obj.get_all_grid(0, 10) self.assertEqual(count, 0)
def test_store_grid_with_wrong_grid(self): '''Tets for a wrong grid''' handler_obj = GridHandler() err, res = handler_obj.store_grid(get_dummy_invalid_grid()) self.assertEqual(err, True)
def test_store_grid_with_duplicate_name(self): '''Test for duplicate name''' self.assertRaises(PymodmValidationError, GridHandler().store_grid, get_dummy_valid_grid())
def test_store_grid(self): '''Test store_grid success''' handler_obj = GridHandler() err, res = handler_obj.store_grid(get_dummy_valid_grid()) self.assertEqual(err, False)