Esempio n. 1
0
    def test_create_response_body_converter(self, mocker,
                                            schema_mock_and_argument):
        # Setup
        schema_mock, argument = schema_mock_and_argument
        expected_result = "data"
        load_result = mocker.Mock()
        load_result.data = expected_result
        schema_mock.load.return_value = load_result
        converter = converters.MarshmallowConverter()
        response = mocker.Mock(spec=["json"])
        c = converter.create_response_body_converter(argument)

        # Run & Verify: with response
        result = c.convert(response)
        response.json.assert_called_with()
        schema_mock.load.assert_called_with(response.json())
        assert expected_result == result

        # Run & Verify: with json
        data = {"hello": "world"}
        result = c.convert(data)
        schema_mock.load.assert_called_with(data)
        assert expected_result == result

        # Run & Verify: raise validation errors for user to handle.
        schema_mock.load.side_effect = marshmallow.exceptions.MarshmallowError

        with pytest.raises(marshmallow.exceptions.MarshmallowError):
            c.convert(data)
Esempio n. 2
0
    def test_create_response_body_converter_without_schema(self):
        # Setup
        converter = converters.MarshmallowConverter()

        # Run
        c = converter.create_response_body_converter("not a schema")

        # Verify
        assert c is None
Esempio n. 3
0
    def test_make_request_body_converter_without_schema(self):
        # Setup
        converter = converters.MarshmallowConverter()

        # Run
        c = converter.make_request_body_converter("not a schema")

        # Verify
        assert c is None
Esempio n. 4
0
    def test_create_string_converter(self, schema_mock_and_argument):
        # Setup
        _, argument = schema_mock_and_argument
        converter = converters.MarshmallowConverter()

        # Run
        c = converter.create_string_converter(argument, None)

        # Verify
        assert c is None
Esempio n. 5
0
    def test_create_response_body_converter_with_unsupported_response(
            self, schema_mock_and_argument):
        # Setup
        schema_mock, argument = schema_mock_and_argument
        converter = converters.MarshmallowConverter()

        # Run
        c = converter.create_response_body_converter(argument)
        result = c.convert("unsupported response")

        # Verify
        return result is None
Esempio n. 6
0
    def test_make_request_body_converter(self, mocker, schema_mock_and_argument):
        # Setup
        schema_mock, argument = schema_mock_and_argument
        expected_result = "data"
        dump_result = mocker.Mock()
        dump_result.data = expected_result
        schema_mock.dump.return_value = dump_result
        converter = converters.MarshmallowConverter()
        request_body = {"id": 0}

        # Run
        c = converter.make_request_body_converter(argument)
        result = c.convert(request_body)

        # Verify
        schema_mock.dump.assert_called_with(request_body)
        assert expected_result == result
Esempio n. 7
0
    def test_make_response_body_converter(self, mocker, schema_mock_and_argument):
        # Setup
        schema_mock, argument = schema_mock_and_argument
        expected_result = "data"
        load_result = mocker.Mock()
        load_result.data = expected_result
        schema_mock.load.return_value = load_result
        converter = converters.MarshmallowConverter()
        response = mocker.Mock()

        # Run
        c = converter.make_response_body_converter(argument)
        result = c.convert(response)

        # Verify
        response.json.assert_called_with()
        schema_mock.load.assert_called_with(response.json())
        assert expected_result == result
Esempio n. 8
0
    def test_create_request_body_converter(self, mocker,
                                           schema_mock_and_argument,
                                           is_marshmallow_3):
        # Setup
        schema_mock, argument = schema_mock_and_argument
        expected_result = "data"
        schema_mock.dump.return_value = self._mock_data(
            mocker, expected_result, is_marshmallow_3)
        converter = converters.MarshmallowConverter()
        converter.is_marshmallow_3 = is_marshmallow_3
        request_body = {"id": 0}

        # Run
        c = converter.create_request_body_converter(argument)
        result = c.convert(request_body)

        # Verify
        schema_mock.dump.assert_called_with(request_body)
        assert expected_result == result
Esempio n. 9
0
 def test_init_without_marshmallow(self):
     old_marshmallow = converters.MarshmallowConverter.marshmallow
     converters.MarshmallowConverter.marshmallow = None
     with pytest.raises(ImportError):
         converters.MarshmallowConverter()
     converters.MarshmallowConverter.marshmallow = old_marshmallow