def test_default(self, mock_request): """Test default is returned (and schema is applied to it).""" mock_request.json = None mock_handler = mock.Mock() route = rest.body(default='100', schema=int)(mock_handler) route() mock_handler.assert_called_once_with(100)
def test_schema_fail(self, mock_request): """Test schema is enforced.""" mock_request.json = 'ALPHA' mock_handler = mock.Mock() route = rest.body(schema=int)(mock_handler) with self.assertRaises(bottle.HTTPError): route()
def test_decoration(self): """Test decorated function is called.""" mock_handler = mock.Mock(return_value='X') decorated = rest.body()(mock_handler) self.assertTrue(callable(decorated)) self.assertEqual(decorated('arg', kwarg=2), 'X') mock_handler.assert_called_once_with(None, 'arg', kwarg=2)
def test_invalid_data(self, mock_request): """Test invalid data is handled gracefully.""" type(mock_request).json = mock.PropertyMock(side_effect=ValueError) mock_handler = mock.Mock() route = rest.body(schema=int)(mock_handler) with self.assertRaises(bottle.HTTPError): route()
def test_required(self, mock_request): """Test required is enforced.""" mock_request.json = None mock_handler = mock.Mock() route = rest.body(required=True)(mock_handler) with self.assertRaises(bottle.HTTPError) as context: route() self.assertEqual(context.exception.body, 'Call body cannot be empty')
def test_schema(self, mock_request): """Test schema callable is called.""" data = "100" mock_request.json = data mock_handler = mock.Mock() route = rest.body(schema=int)(mock_handler) route() mock_handler.assert_called_once_with(int(data))
def test_invalid_encoding(self, mock_request): """Test invalid encoding is handled gracefully.""" type(mock_request).json = mock.PropertyMock( side_effect=UnicodeDecodeError('ascii', b'', 0, 1, 'bad')) mock_handler = mock.Mock() route = rest.body(schema=int)(mock_handler) with self.assertRaises(bottle.HTTPError): route()