"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-4.0.0/monasca_api/tests/test_rest.py" (13 May 2020, 2696 Bytes) of package /linux/misc/openstack/monasca-api-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_rest.py": 3.1.0_vs_4.0.0.

    1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 # not use this file except in compliance with the License. You may obtain
    3 # a copy of the License at
    4 #
    5 #      http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 # License for the specific language governing permissions and limitations
   11 # under the License.
   12 
   13 from unittest import mock
   14 
   15 from oslotest import base
   16 
   17 from monasca_api.common.rest import exceptions
   18 from monasca_api.common.rest import utils
   19 
   20 
   21 class TestRestUtils(base.BaseTestCase):
   22 
   23     def setUp(self):
   24         super(TestRestUtils, self).setUp()
   25         self.mock_json_patcher = mock.patch('monasca_api.common.rest.utils.json')
   26         self.mock_json = self.mock_json_patcher.start()
   27 
   28     def tearDown(self):
   29         super(TestRestUtils, self).tearDown()
   30         self.mock_json_patcher.stop()
   31 
   32     def test_read_body_with_success(self):
   33         self.mock_json.loads.return_value = ""
   34         payload = mock.Mock()
   35 
   36         utils.read_body(payload)
   37 
   38         self.mock_json.loads.assert_called_once_with(payload.read.return_value)
   39 
   40     def test_read_body_empty_content_in_payload(self):
   41         self.mock_json.loads.return_value = ""
   42         payload = mock.Mock()
   43         payload.read.return_value = None
   44 
   45         self.assertIsNone(utils.read_body(payload))
   46 
   47     def test_read_body_json_loads_exception(self):
   48         self.mock_json.loads.side_effect = Exception
   49         payload = mock.Mock()
   50 
   51         self.assertRaises(exceptions.DataConversionException,
   52                           utils.read_body, payload)
   53 
   54     def test_read_body_unsupported_content_type(self):
   55         unsupported_content_type = mock.Mock()
   56 
   57         self.assertRaises(
   58             exceptions.UnsupportedContentTypeException, utils.read_body, None,
   59             unsupported_content_type)
   60 
   61     def test_read_body_unreadable_content_error(self):
   62         unreadable_content = mock.Mock()
   63         unreadable_content.read.side_effect = Exception
   64 
   65         self.assertRaises(
   66             exceptions.UnreadableContentError,
   67             utils.read_body, unreadable_content)
   68 
   69     def test_as_json_success(self):
   70         data = mock.Mock()
   71 
   72         dumped_json = utils.as_json(data)
   73 
   74         self.assertEqual(dumped_json, self.mock_json.dumps.return_value)
   75 
   76     def test_as_json_with_exception(self):
   77         data = mock.Mock()
   78         self.mock_json.dumps.side_effect = Exception
   79 
   80         self.assertRaises(exceptions.DataConversionException,
   81                           utils.as_json, data)