def test_saves_correct_data_to_database(self, mock_insert: MagicMock, mock_get_algo: MagicMock): mock_get_algo.return_value = create_algorithm() productlines.create_productline( algorithm_id='test-algo-id', bbox=(0, 0, 30, 30), category='test-category', max_cloud_cover=42, name='test-name', spatial_filter_id='test-spatial-filter-id', start_on=DATE_START, stop_on=DATE_STOP, user_id='test-user-id', ) _, args = mock_insert.call_args self.assertRegex(args['productline_id'], '^[a-z]+$') self.assertEqual('test-algo-id', args['algorithm_id']) self.assertEqual('test-algo-name', args['algorithm_name']) self.assertEqual((0, 0, 30, 30), args['bbox']) self.assertEqual('test-category', args['category']) self.assertEqual(42, args['max_cloud_cover']) self.assertEqual('test-name', args['name']) self.assertEqual('test-spatial-filter-id', args['spatial_filter_id']) self.assertEqual(DATE_START, args['start_on']) self.assertEqual(DATE_STOP, args['stop_on']) self.assertEqual('test-user-id', args['user_id'])
def test_gracefully_handles_database_error(self, _): self._mockdb.raise_on_execute() with self.assertRaises(DatabaseError): productlines.create_productline( algorithm_id='test-algo-id', bbox=(0, 0, 30, 30), category='test-category', max_cloud_cover=42, name='test-name', spatial_filter_id='test-spatial-filter-id', start_on=DATE_START, stop_on=DATE_STOP, user_id='test-user-id', )
def test_gracefully_handles_algorithm_validation_error(self, mock: MagicMock): mock.side_effect = ValidationError('test-error-message') with self.assertRaises(ValidationError): productlines.create_productline( algorithm_id='test-algo-id', bbox=(0, 0, 30, 30), category='test-category', max_cloud_cover=42, name='test-name', spatial_filter_id='test-spatial-filter-id', start_on=DATE_START, stop_on=DATE_STOP, user_id='test-user-id', )
def test_gracefully_handles_algorithm_not_found(self, mock: MagicMock): mock.side_effect = NotFound('test-algo-id') with self.assertRaisesRegex(NotFound, 'algorithm `test-algo-id` does not exist'): productlines.create_productline( algorithm_id='test-algo-id', bbox=(0, 0, 30, 30), category='test-category', max_cloud_cover=42, name='test-name', spatial_filter_id='test-spatial-filter-id', start_on=DATE_START, stop_on=DATE_STOP, user_id='test-user-id', )
def test_requests_correct_algorithm(self, mock: MagicMock): mock.return_value = create_algorithm() productlines.create_productline( algorithm_id='test-algo-id', bbox=(0, 0, 30, 30), category='test-category', max_cloud_cover=42, name='test-name', spatial_filter_id='test-spatial-filter-id', start_on=DATE_START, stop_on=DATE_STOP, user_id='test-user-id', ) mock.assert_called_with('test-algo-id')
def test_assigns_correct_category(self, _): record = productlines.create_productline( algorithm_id='test-algo-id', bbox=(0, 0, 30, 30), category='test-category', max_cloud_cover=42, name='test-name', spatial_filter_id='test-spatial-filter-id', start_on=DATE_START, stop_on=DATE_STOP, user_id='test-user-id', ) self.assertEqual('test-category', record.category)
def test_assigns_auto_generated_id(self, _): record = productlines.create_productline( algorithm_id='test-algo-id', bbox=(0, 0, 30, 30), category='test-category', max_cloud_cover=42, name='test-name', spatial_filter_id='test-spatial-filter-id', start_on=DATE_START, stop_on=DATE_STOP, user_id='test-user-id', ) self.assertRegex(record.productline_id, r'^[a-z]+$')
def test_returns_productline(self, _): record = productlines.create_productline( algorithm_id='test-algo-id', bbox=(0, 0, 30, 30), category='test-category', max_cloud_cover=42, name='test-name', spatial_filter_id='test-spatial-filter-id', start_on=DATE_START, stop_on=DATE_STOP, user_id='test-user-id', ) self.assertIsInstance(record, productlines.ProductLine)
def test_assigns_correct_algorithm_name(self, mock: MagicMock): mock.return_value = create_algorithm() record = productlines.create_productline( algorithm_id='test-algo-id', bbox=(0, 0, 30, 30), category='test-category', max_cloud_cover=42, name='test-name', spatial_filter_id='test-spatial-filter-id', start_on=DATE_START, stop_on=DATE_STOP, user_id='test-user-id', ) self.assertEqual('test-algo-name', record.algorithm_name)
def create_productline(): try: payload = flask.request.get_json() algorithm_id = _get_string(payload, 'algorithm_id', max_length=100) category = _get_string(payload, 'category', nullable=True, max_length=64) min_x = _get_number(payload, 'min_x', min_value=-180, max_value=180) min_y = _get_number(payload, 'min_y', min_value=-90, max_value=90) max_x = _get_number(payload, 'max_x', min_value=-180, max_value=180) max_y = _get_number(payload, 'max_y', min_value=-90, max_value=90) max_cloud_cover = int(_get_number(payload, 'max_cloud_cover', min_value=0, max_value=100)) name = _get_string(payload, 'name', max_length=100) spatial_filter_id = _get_string(payload, 'spatial_filter_id', nullable=True, max_length=64) start_on = _get_datetime(payload, 'start_on') stop_on = _get_datetime(payload, 'stop_on', nullable=True) except JSONDecodeError: return 'Invalid input: request body must be a JSON object', 400 except ValidationError as err: return 'Invalid input: {}'.format(err), 400 try: productline = _productlines.create_productline( algorithm_id=algorithm_id, bbox=(min_x, min_y, max_x, max_y), category=category, max_cloud_cover=max_cloud_cover, name=name, spatial_filter_id=spatial_filter_id, start_on=start_on.date(), stop_on=stop_on.date() if stop_on else None, user_id=flask.request.user.user_id, ) except _algorithms.NotFound as err: return 'Algorithm {} does not exist'.format(err.service_id), 500 except DatabaseError: return 'A database error prevents product line creation', 500 return flask.jsonify({ 'productline': productline.serialize(), }), 201