def solve(self, latitude, longitude, colors): if latitude is None or longitude is None or colors is None: raise ServiceException(ServiceCodes.EMPTY_PARAMS, msg='Empty parameters') try: colors = [color for color in xrange(1, int(colors) + 1)] except (TypeError, ValueError) as f: raise ServiceException(ServiceCodes.BAD_PARAMS, msg=f.message) try: input_data = self._geolocator.resolve(latitude, longitude) except (GeocoderTimedOut, GeocoderServiceError, GeocoderQueryError) as e: raise ServiceException(ServiceCodes.FAIL, msg=e.message) if not input_data: raise ServiceException( ServiceCodes.FAIL, msg='No country found with these coordinates ({},{})'.format( latitude, longitude)) map = Map(self._geo_data, input_data, colors, 'mrv', 'lcv') if map.backtracking_search(): return self._build_json_response(map) return None
def solve(self, sudoku): if sudoku is None: raise ServiceException(ServiceCodes.EMPTY_PARAMS, msg='Empty parameters') if len(sudoku) != 81: raise ServiceException(ServiceCodes.BAD_PARAMS, msg='Sudoku grid must have 81 cells, not {}'.format(len(sudoku))) sdk = Sudoku(sudoku, 'mrv', 'lcv') if sdk.backtracking_search(): return "".join([str(sdk.variables[key]) for key in sorted(sdk.variables)]) return None
def testSudokuSyncDataFail(self, mock_class): mock_class.side_effect = ServiceException(ServiceCodes.FAIL, msg='') recv = self._flask_app.post('/sudoku_sync', data=json.dumps(dict(foo='1')), content_type='application/json') assert_that(recv.status_code, equal_to(409))
def testMapSyncDatawithEmptyParams(self, mock_class): mock_class.side_effect = ServiceException(ServiceCodes.EMPTY_PARAMS, msg='') recv = self._flask_app.post('/map_sync', data=json.dumps(dict(foo='1')), content_type='application/json') assert_that(recv.status_code, equal_to(422))
def testRunException(self, mock_class): with mock.patch( 'csp_solver_cloud.src.server.maptask.MapTask.update_state'): mock_class.side_effect = ServiceException(ServiceCodes.FAIL, msg='') assert_that( calling(self._celery_task.run).with_args(1, 1, 4, 1, None), raises(Reject))