Пример #1
0
    def test_given_resource_id_is_none_then_does_not_add_link(self):
        serializer = ResourceSerializer({'users': 'user_id'})

        mapping = {'user_id': None}

        result = serializer.serialize(mapping)

        decoded_result = json.loads(result)

        assert_that(decoded_result, has_entries(mapping))
        assert_that(decoded_result['links'], contains())
Пример #2
0
    def test_given_resource_id_is_none_then_does_not_add_link(self):
        generator = Mock(LinkGenerator)
        serializer = ResourceSerializer([generator])

        generator.can_generate.return_value = False
        mapping = {'user_id': None}

        result = serializer.serialize(mapping)

        decoded_result = json.loads(result)

        assert_that(decoded_result, has_entries(mapping))
        assert_that(decoded_result['links'], contains())
Пример #3
0
    def test_given_mapping_then_generates_all_links(self, url_for):
        url_for.side_effect = lambda r, resource_id, _external: 'http://localhost/{}/{}'.format(r.split('.')[0], resource_id)
        serializer = ResourceSerializer({'users': 'user_id', 'lines': 'line_id'})

        mapping = {'user_id': 1, 'line_id': 2}
        user_link = {'rel': 'users', 'href': 'http://localhost/users/1'}
        line_link = {'rel': 'lines', 'href': 'http://localhost/lines/2'}

        result = serializer.serialize(mapping)

        decoded_result = json.loads(result)

        assert_that(decoded_result, has_entries(mapping))
        assert_that(decoded_result['links'], has_items(user_link, line_link))
Пример #4
0
    def test_given_mapping_then_generates_links(self):
        generator = Mock(LinkGenerator)
        serializer = ResourceSerializer([generator])

        user_link = {'rel': 'users', 'href': 'http://localhost/users/1'}
        generator.generate.return_value = user_link

        mapping = {'user_id': 1, 'line_id': 2}

        result = serializer.serialize(mapping)

        decoded_result = json.loads(result)

        assert_that(decoded_result, has_entries(mapping))
        assert_that(decoded_result['links'], has_item(user_link))
Пример #5
0
    def test_given_list_of_items_then_adds_total(self):
        generator = Mock(LinkGenerator)
        serializer = ResourceSerializer(generator)
        mapping = Mock()

        item = {
            'id': 1,
            'links': [{
                'rel': 'resources',
                'href': 'http://localhost/resources/1'
            }]
        }

        with patch.object(serializer, '_map_item') as mock_serialize:
            mock_serialize.return_value = item
            result = serializer.serialize_list([mapping])

        expected_entries = {'total': 1, 'items': [item]}

        decoded_result = json.loads(result)
        assert_that(decoded_result, has_entries(expected_entries))
Пример #6
0
 def setUp(self):
     self.serializer = ResourceSerializer({'resources': 'id'})
Пример #7
0
class TestResourceSerializer(unittest.TestCase):

    def setUp(self):
        self.serializer = ResourceSerializer({'resources': 'id'})

    @patch('flask.helpers.url_for')
    def test_given_mapping_then_generates_all_links(self, url_for):
        url_for.side_effect = lambda r, resource_id, _external: 'http://localhost/{}/{}'.format(r.split('.')[0], resource_id)
        serializer = ResourceSerializer({'users': 'user_id', 'lines': 'line_id'})

        mapping = {'user_id': 1, 'line_id': 2}
        user_link = {'rel': 'users', 'href': 'http://localhost/users/1'}
        line_link = {'rel': 'lines', 'href': 'http://localhost/lines/2'}

        result = serializer.serialize(mapping)

        decoded_result = json.loads(result)

        assert_that(decoded_result, has_entries(mapping))
        assert_that(decoded_result['links'], has_items(user_link, line_link))

    def test_given_resource_id_is_none_then_does_not_add_link(self):
        serializer = ResourceSerializer({'users': 'user_id'})

        mapping = {'user_id': None}

        result = serializer.serialize(mapping)

        decoded_result = json.loads(result)

        assert_that(decoded_result, has_entries(mapping))
        assert_that(decoded_result['links'], contains())

    def test_given_list_of_items_then_adds_total(self):
        mapping = Mock()

        item = {'id': 1,
                'links': [{'rel': 'resources',
                           'href': 'http://localhost/resources/1'}]}

        with patch.object(self.serializer, '_map_item') as mock_serialize:
            mock_serialize.return_value = item
            result = self.serializer.serialize_list([mapping])

        expected_entries = {'total': 1,
                            'items': [item]}

        decoded_result = json.loads(result)
        assert_that(decoded_result, has_entries(expected_entries))

    def test_given_list_of_items_with_additional_total_then_replaces_total(self):
        mapping = Mock()

        item = {'id': 1,
                'links': [{'rel': 'resources',
                           'href': 'http://localhost/resources/1'}]}

        with patch.object(self.serializer, '_map_item') as mock_serialize:
            mock_serialize.return_value = item
            result = self.serializer.serialize_list([mapping], total=5)

        expected_entries = {'total': 5,
                            'items': [item]}

        decoded_result = json.loads(result)
        assert_that(decoded_result, has_entries(expected_entries))