예제 #1
0
class TestParser(unittest.TestCase):

    def setUp(self):
        self.registry = Mock(ParserRegistry)
        self.parser = Parser(self.registry)

    def test_given_a_request_then_extracts_and_returns_parsed_content(self):
        content_type = 'application/json'
        request = Mock(headers={'Content-Type': content_type})
        content = request.data = Mock()
        document = Mock(Document)
        parser = self.registry.parser_for_content_type.return_value = Mock()
        parsed_content = parser.return_value

        result = self.parser.parse(request, document)

        self.registry.parser_for_content_type.assert_called_once_with(content_type)
        parser.assert_called_once_with(content, document)

        assert_that(result, equal_to(parsed_content))

    def test_given_no_content_type_then_raises_error(self):
        request = Mock(headers={}, data=None)

        self.assertRaises(ContentTypeError, self.parser.parse, request, Mock())

    def test_given_a_document_and_action_then_validates_document(self):
        request = Mock()
        document = Mock(Document)
        parser = self.registry.parser_for_content_type.return_value = Mock()
        parsed_content = parser.return_value
        action = 'action'

        self.parser.parse(request, document, action)

        document.validate.assert_called_once_with(parsed_content, action)

    def test_given_a_list_of_fields_then_returns_a_document_proxy(self):
        field1 = Mock(Field)
        field2 = Mock(Field)

        result = self.parser.document(field1, field2)
        assert_that(result, instance_of(DocumentProxy))
        assert_that(result.document.fields, contains(field1, field2))

    def test_given_a_custom_content_parser_when_registered_then_added_to_the_registry(self):
        content_parser = Mock()

        self.parser.content_parser('custom/type')(content_parser)

        self.registry.register.assert_called_once_with('custom/type', content_parser)
예제 #2
0
 def setUp(self):
     self.registry = Mock(ParserRegistry)
     self.parser = Parser(self.registry)
예제 #3
0
 def setUp(self):
     self.registry = Mock(ParserRegistry)
     self.parser = Parser(self.registry)
예제 #4
0
class TestParser(unittest.TestCase):
    def setUp(self):
        self.registry = Mock(ParserRegistry)
        self.parser = Parser(self.registry)

    def test_given_a_request_then_extracts_and_returns_parsed_content(self):
        content_type = 'application/json'
        request = Mock(mimetype=content_type)
        content = request.data = Mock()
        document = Mock(Document)
        parser = self.registry.parser_for_content_type.return_value = Mock()
        parsed_content = parser.return_value

        result = self.parser.parse(request, document)

        self.registry.parser_for_content_type.assert_called_once_with(
            content_type)
        parser.assert_called_once_with(content, document)

        assert_that(result, equal_to(parsed_content))

    def test_given_a_request_with_option_then_extracts_only_mimetype(self):
        content_type = 'application/json'
        content_type_with_option = '{content_type}; charset=UTF-8'.format(
            content_type=content_type)
        request = Mock(mimetype=content_type,
                       headers={'Content-Type': content_type_with_option})
        content = request.data = Mock()
        document = Mock(Document)
        parser = self.registry.parser_for_content_type.return_value = Mock()
        parsed_content = parser.return_value

        result = self.parser.parse(request, document)

        self.registry.parser_for_content_type.assert_called_once_with(
            content_type)
        parser.assert_called_once_with(content, document)

        assert_that(result, equal_to(parsed_content))

    def test_given_no_content_type_then_raises_error(self):
        request = Mock(mimetype='', data=None)

        self.assertRaises(ContentTypeError, self.parser.parse, request, Mock())

    def test_given_a_document_and_action_then_validates_document(self):
        request = Mock()
        document = Mock(Document)
        parser = self.registry.parser_for_content_type.return_value = Mock()
        parsed_content = parser.return_value
        action = 'action'

        self.parser.parse(request, document, action)

        document.validate.assert_called_once_with(parsed_content, action)

    def test_given_a_list_of_fields_then_returns_a_document_proxy(self):
        field1 = Mock(Field)
        field2 = Mock(Field)

        result = self.parser.document(field1, field2)
        assert_that(result, instance_of(DocumentProxy))
        assert_that(result.document.fields, contains(field1, field2))

    def test_given_a_custom_content_parser_when_registered_then_added_to_the_registry(
            self):
        content_parser = Mock()

        self.parser.content_parser('custom/type')(content_parser)

        self.registry.register.assert_called_once_with('custom/type',
                                                       content_parser)