Ejemplo n.º 1
0
    def test_serialize_result_expands_key_given_results_array(self):
        """
        When calling serialize_results with an array of results, should
        serialize the result[key] with the given serializer and queryset from
        the constructor and return the resulting array.
        Here {key} is the first field given in the fields tuple in the
        constructor
        """

        class DummySerializer(DynamicFieldsSerializer):
            id = serializers.CharField()
            iati_identifier = serializers.CharField()

        group_by = GroupBy(
            query_param="test",
            fields="key",
            queryset=self.queryset,
            serializer=DummySerializer,
            serializer_main_field='iati_identifier',
            serializer_fk='iati_identifier',
            serializer_fields=['iati_identifier']
        )

        results = [
            {
                "key": "test",
                "count": 12345,
            },
            {
                "key": "test2",
                "count": 67890,
            }
        ]

        rf = RequestFactory()
        request = rf.get('/api/activities/')

        new_results = group_by.serialize_results(results, request)

        self.assertEqual(list(new_results), [
            {
                "key": OrderedDict([
                        ('iati_identifier', 'test'),
                ]),
                "count": 12345,
            },
            {
                "key": OrderedDict([
                    ('iati_identifier', 'test2'),
                ]),
                "count": 67890,
            }
        ])
Ejemplo n.º 2
0
    def test_serializer_fields_limits_serializer_fields(self):
        """
        When given a dynamic serializer, serializer_fields should determine
        the fields that the serialized field should expose
        """
        class DummySerializer(DynamicFieldsSerializer):
            id = serializers.CharField()
            iati_identifier = serializers.CharField()

        group_by = GroupBy(
            query_param="test",
            fields="key",
            queryset=self.queryset,
            serializer=DummySerializer,
            serializer_main_field='iati_identifier',
            serializer_fk='iati_identifier',
            serializer_fields=['iati_identifier']
        )

        results = [
            {
                "key": "test",
                "count": 12345,
            },
            {
                "key": "test2",
                "count": 67890,
            }
        ]

        rf = RequestFactory()
        request = rf.get('/api/activities/')

        new_results = group_by.serialize_results(results, request)

        self.assertCountEqual(new_results, [
            {
                "key": {
                    "iati_identifier": "test",
                },
                "count": 12345,
            },
            {
                "key": {
                    "iati_identifier": "test2",
                },
                "count": 67890,
            }
        ])