def test_as_json_recursive_response(self): mock = self.MockLazyResult('hello world') mock2 = self.MockLazyResult(self.MockModel(data=mock)) response2 = Response('mock', mock2) self.assertEqual({'data': 'hello world'}, response2.as_json())
def test_as_json_recursive_response_via_list(self): mock = self.MockLazyResult('hello world') mock2 = self.MockLazyResult([mock]) response2 = Response('mock', mock2) self.assertEqual(['hello world'], response2.as_json())
def test_as_json_nested_nan(self): mock = self.MockLazyResult([self.MockModel(data=float('nan'))]) mock2 = self.MockLazyResult(self.MockModel(data=mock)) response2 = Response('mock', mock2) self.assertEqual({'data': [{'data': None}]}, response2.as_json())
def post(self): import requests job_ids = self.params.get('job_ids', None) if job_ids is None: return Response( 'Bad Request', LazyResult( lambda: 'Expected a json object with key \'job_ids\''), status=400) tb_locations = self._transform_request(job_ids) response = requests.post( f'{self._tensorboard_api_host()}/create_sym_links', json=tb_locations) if response.status_code == 400: return Response('Bad Request', LazyResult(lambda: response.text), status=400) elif response.status_code == 500: return Response('Tensorboard Rest API Error', LazyResult(lambda: 'Internal Server Error'), status=500) return Response( 'Jobs', LazyResult(lambda: {'url': f'{self._tensorboard_host()}'}))
def delete(self): from foundations_core_rest_api_components.response import Response from foundations_core_rest_api_components.lazy_result import LazyResult from foundations_contrib.global_state import config_manager job_deployment_class = config_manager['deployment_implementation'][ 'deployment_type'] job_deployment = job_deployment_class(self.job_id, None, None) job_status = job_deployment.get_job_status() if job_status is None: return Response( 'Jobs', LazyResult( lambda: f'Job {self.job_id} is in an unknown state')) if not (job_status == 'completed' or job_status == 'failed'): return Response( 'Jobs', LazyResult( lambda: f'Job {self.job_id} is not completed or failed')) if all(job_deployment.cancel_jobs([self.job_id]).values()): return Response( 'Jobs', LazyResult(lambda: f'Job {self.job_id} successfully deleted')) return Response( 'Jobs', LazyResult(lambda: f'Job {self.job_id} could not be deleted'))
def test_status_with_no_result_and_no_fallback(self): mock = self.MockLazyResult(None) response = Response('mock', mock) with self.assertRaises(ValueError) as error_context: response.status() self.assertTrue('No response data and no fallback provided!' in error_context.exception.args)
def test_as_json_with_parent(self): mock_parent = self.MockLazyResult('hello world') response_parent = Response('mock', mock_parent) mock = self.MockLazyResult('hello world') response = Response('mock', mock, parent=response_parent) response.evaluate() self.assertTrue(mock_parent.called)
def test_as_json_recursive_response_via_response_in_property_containing_model( self): mock = self.MockLazyResult([self.MockModel(data='hello world')]) mock2 = self.MockLazyResult(self.MockModel(data=mock)) response2 = Response('mock', mock2) self.assertEqual({'data': [{ 'data': 'hello world' }]}, response2.as_json())
def index(self): from foundations_contrib.jobs.logs import job_logs try: logs = job_logs(self.params['job_id']) return Response('Jobs', LazyResult(lambda: {'log': logs})) except Exception as exc: return Response( 'Error', LazyResult(lambda: {'message': 'Internal Server Error'}), status=500)
def index(self): from foundations_rest_api.v2beta.models.project import Project from foundations_core_rest_api_components.response import Response from foundations_core_rest_api_components.lazy_result import LazyResult project_name = self.params.pop("project_name") jobs_data_future = Project.find_by(name=project_name).only( ["name", "jobs", "output_metric_names", "parameters"]) jobs_data_future = jobs_data_future.apply_filters(self.params, fields=["jobs"]) fallback = Response("Jobs", LazyResult(lambda: "This project was not found"), status=404) return Response("Jobs", jobs_data_future, fallback=fallback)
def index(self): from foundations_rest_api.v2beta.models.project import Project from foundations_core_rest_api_components.response import Response from foundations_core_rest_api_components.lazy_result import LazyResult project_name = self.params.pop('project_name') jobs_data_future = Project.find_by(name=project_name).only( ['name', 'jobs', 'output_metric_names']) jobs_data_future = jobs_data_future.apply_filters({}, fields=['jobs']) fallback = Response( 'Jobs', LazyResult(lambda: 'This project or sort detail was not found'), status=404) jobs_data_future = self.sort_jobs(jobs_data_future) return Response('Jobs', jobs_data_future, fallback=fallback)
def _mock_resource(self, method, callback, status=200, cookie=None): from foundations_core_rest_api_components.lazy_result import LazyResult mock_klass = Mock() mock_instance = Mock() mock_klass.return_value = mock_instance result = LazyResult(lambda: callback(mock_instance)) getattr(mock_instance, method).side_effect = lambda: Response('Mock', result, status=status, cookie=cookie) return mock_klass
def delete(self): from foundations_rest_api.global_state import redis_connection job_annotations_key = f"jobs:{self._job_id()}:annotations" redis_connection.hdel(job_annotations_key, self._key()) return Response( "Jobs", LazyResult( lambda: f"Tag key: {self._key()} deleted from job {self._job_id()}"), )
def test_status_with_fallback_different_fallback(self): mock_fallback = self.MockLazyResult('') response_fallback = Response('mock', mock_fallback, status=404) mock = self.MockLazyResult(None) response = Response('mock', mock, fallback=response_fallback) response.evaluate() self.assertEqual(404, response.status())
def test_as_json_with_fallback_different_fallback(self): mock_fallback = self.MockLazyResult('hello and goodbye to the world') response_fallback = Response('mock', mock_fallback) mock = self.MockLazyResult(None) response = Response('mock', mock, fallback=response_fallback) response.evaluate() self.assertEqual('hello and goodbye to the world', response.as_json())
def post(self): from foundations_rest_api.global_state import redis_connection from foundations_internal.fast_serializer import serialize from datetime import datetime redis_connection.rpush( f"project:{self._project_name()}:note_listing", serialize( { "date": datetime.now(), "message": self._message(), "author": self._author(), } ), ) return Response( "Note Listing", LazyResult( lambda: f"Note with author: {self._author()} created with message: {self._message()}" ), )
def post(self): from foundations_core_rest_api_components.lazy_result import LazyResult from foundations_core_rest_api_components.response import Response def _index(): return self.params return Response('Mock', LazyResult(_index))
def post(self): from foundations_core_rest_api_components.lazy_result import LazyResult from foundations_core_rest_api_components.response import Response def _post(): return 'some post data' return Response('Mock', LazyResult(_post))
def index(self): from foundations_core_rest_api_components.lazy_result import LazyResult from foundations_core_rest_api_components.response import Response def _index(): return 'some index data' return Response('Mock', LazyResult(_index))
def delete(self): from foundations_core_rest_api_components.lazy_result import LazyResult from foundations_core_rest_api_components.response import Response def _delete(): return 'some data' return Response('Mock', LazyResult(_delete), status=403)
def put(self): from foundations_core_rest_api_components.lazy_result import LazyResult from foundations_core_rest_api_components.response import Response def _put(): return self.params return Response('Mock', LazyResult(_put), status=self.status_code)
def index(self): return Response("Note Listing", LazyResult(self._get_note_listing))
def show(self): from foundations_core_rest_api_components.lazy_result import LazyResult from foundations_core_rest_api_components.response import Response def _show(): return 'some specific data' return Response('Mock', LazyResult(_show))
def index(self): return Response("Jobs", LazyResult(self._project_metrics_or_single_metric))
def test_constant_returns_constant_with_default_cookie_value(self): response = Response.constant(self.dummy_value) self.assertEqual(None, response.cookie())
def test_evaluate(self): mock = self.MockLazyResult('hello') response = Response('mock', mock) self.assertEqual('hello', response.evaluate())
def test_evaluate_different_action(self): mock = self.MockLazyResult('hello world') response = Response('mock', mock) self.assertEqual('hello world', response.evaluate())
def delete(self): from foundations_core_rest_api_components.lazy_result import LazyResult from foundations_core_rest_api_components.response import Response def _delete(): return self.params return Response('Mock', LazyResult(_delete))
def update(self): from foundations_core_rest_api_components.lazy_result import LazyResult from foundations_core_rest_api_components.response import Response def _update(): return 'some updated data' return Response('Mock', LazyResult(_update))
def test_as_json(self): mock = self.MockLazyResult(self.MockModel(data='hello')) response = Response('mock', mock) self.assertEqual({'data': 'hello'}, response.as_json())