class TestPresentablePrimaryKeyRelatedField(APISimpleTestCase):
    def setUp(self):
        self.queryset = MockQueryset([
            MockObject(pk=1, name="foo"),
            MockObject(pk=2, name="bar"),
            MockObject(pk=3, name="baz"),
        ])
        self.instance = self.queryset.items[2]
        self.field = PresentablePrimaryKeyRelatedField(
            queryset=self.queryset,
            presentation_serializer=PresentationSerializer)

    def test_representation(self):
        representation = self.field.to_representation(self.instance)
        expected_representation = PresentationSerializer(self.instance).data
        assert representation == expected_representation

    def test_read_source_with_context(self):
        representation = SerializerWithPresentable(self.instance)
        expected_representation = [
            PresentationSerializer(x).data for x in MockObject().foo_property
        ]
        assert representation.data[
            'test_many_field'] == expected_representation
        assert representation.data[
            'test_function_field'] == expected_representation

        expected_representation = PresentationSerializer(
            MockObject().bar_property).data
        assert representation.data['test_field'] == expected_representation
Пример #2
0
class TestPresentablePrimaryKeyRelatedField(APISimpleTestCase):
    def setUp(self):
        self.queryset = MockQueryset([
            MockObject(pk=1, name="foo"),
            MockObject(pk=2, name="bar"),
            MockObject(pk=3, name="baz"),
        ])
        self.instance = self.queryset.items[2]
        self.field = PresentablePrimaryKeyRelatedField(
            queryset=self.queryset,
            presentation_serializer=PresentationSerializer)

    def test_representation(self):
        representation = self.field.to_representation(self.instance)
        expected_representation = PresentationSerializer(self.instance).data
        assert representation == expected_representation
Пример #3
0
class TestPresentablePrimaryKeyRelatedField(APISimpleTestCase):
    class PresentationSerializer(serializers.Serializer):
        def to_representation(self, instance):
            return {"pk": instance.pk, "name": instance.name}

    def setUp(self):

        self.queryset = MockQueryset([
            MockObject(pk=1, name='foo'),
            MockObject(pk=2, name='bar'),
            MockObject(pk=3, name='baz')
        ])
        self.instance = self.queryset.items[2]
        self.field = PresentablePrimaryKeyRelatedField(
            queryset=self.queryset,
            presentation_serializer=self.PresentationSerializer)

    def test_representation(self):
        representation = self.field.to_representation(self.instance)
        expected_representation = self.PresentationSerializer(
            self.instance).data
        assert representation == expected_representation