Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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()
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
 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()
Exemplo n.º 5
0
 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()
Exemplo n.º 6
0
 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)
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 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()
Exemplo n.º 9
0
 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')
Exemplo n.º 10
0
 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))
Exemplo n.º 11
0
 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()
Exemplo n.º 12
0
 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')
Exemplo n.º 13
0
 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))
Exemplo n.º 14
0
 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()