Пример #1
0
    def test_iterable_nested_dict(self):
        student = {
            'name':
            'Danish',
            'age':
            24,
            'courses': [{
                'code': 'CS201',
                'name': 'Data Structures'
            }, {
                'code': 'CS205',
                'name': 'Computer Networks'
            }]
        }

        query = ["name", "age", {
            "courses": [[
                "code",
                "name",
            ]]
        }]
        self.assertEqual(
            dictfier.filter(student, query), {
                'name':
                'Danish',
                'age':
                24,
                'courses': [{
                    'code': 'CS201',
                    'name': 'Data Structures'
                }, {
                    'code': 'CS205',
                    'name': 'Computer Networks'
                }]
            })
Пример #2
0
    def test_nested_dict(self):
        student = {
            "name": "Danish",
            "age": 24,
            "course": {
                "code": "CS201",
                "name": "Data Structures"
            }
        }

        query = ["name", "age", {
            "course": [
                "code",
                "name",
            ]
        }]
        self.assertEqual(
            dictfier.filter(student, query), {
                "name": "Danish",
                "age": 24,
                "course": {
                    "code": "CS201",
                    "name": "Data Structures"
                }
            })
Пример #3
0
    def test_custom_nested_dict(self):
        # Customize how dictfier obtains nested flat obj
        # per nested field
        student = {
            'name': 'Danish',
            'age': 24,
            'course': {
                'name': 'Data Structures',
                'code': 'CS201'
            }
        }

        query = [
            "name",
            "age",
            {
                "course":
                dictfier.useobj(
                    lambda obj: obj["course"],
                    ["name", "code"]  # This is a query
                )
            }
        ]
        self.assertEqual(
            dictfier.filter(student, query), {
                'name': 'Danish',
                'age': 24,
                'course': {
                    'name': 'Data Structures',
                    'code': 'CS201'
                }
            })
Пример #4
0
    def retrieve(self, request, pk=None):
        queryset = self.get_queryset()
        object = get_object_or_404(queryset, pk=pk)
        serializer = self.get_serializer(object, context={'request': request})

        if self.tofilter(request):
            query_str = self.get_query_str(request)

            schema = self.get_serializer().get_fields()
            query = Query(query_str, schema)

            try:
                parsed_query = query.parse()
            except (FormatError, InvalidField) as e:
                return Response({"error": str(e)}, 400)
            except FieldNotFound as e:
                return Response({"error": str(e)}, 404)

            # No extra [] cuz only one resource is expected
            filter_query = parsed_query

            filtered_data = dictfier.filter(serializer.data, filter_query)
            return Response(filtered_data)

        return Response(serializer.data)
Пример #5
0
 def test_newfield_api(self):
     student = {'name': 'Danish', 'age': 24}
     query = ["name", "age", {"school": dictfier.newfield("St Patrick")}]
     self.assertEqual(dictfier.filter(student, query), {
         'name': 'Danish',
         'age': 24,
         'school': 'St Patrick'
     })
Пример #6
0
 def test_global_filter_config_with_parent_and_field_name_params(self):
     # Customize how dictfier obtains flat obj,
     # nested flat obj and nested iterable obj
     # per filter call (global)
     student = {
         'name': 'Danish',
         'age': 24,
         'course': {
             'name':
             'Data Structures',
             'code':
             'CS201',
             'books': [{
                 'title': 'Advanced Data Structures',
                 'publish_date': '2018'
             }, {
                 'title': 'Basic Data Structures',
                 'publish_date': '2010'
             }]
         }
     }
     query = [
         "name", "age", {
             "course":
             ["name", "code", {
                 "books": [["title", "publish_date"]]
             }]
         }
     ]
     self.assertEqual(
         dictfier.filter(
             student,
             query,
             flat_obj=lambda obj, parent, field_name: parent[field_name],
             nested_iter_obj=lambda obj, parent, field_name: parent[
                 field_name],
             nested_flat_obj=lambda obj, parent, field_name: parent[
                 field_name]), {
                     'name': 'Danish',
                     'age': 24,
                     'course': {
                         'name':
                         'Data Structures',
                         'code':
                         'CS201',
                         'books': [{
                             'title': 'Advanced Data Structures',
                             'publish_date': '2018'
                         }, {
                             'title': 'Basic Data Structures',
                             'publish_date': '2010'
                         }]
                     }
                 })
Пример #7
0
    def test_flat_dict(self):
        student = {"name": "Danish", "age": 24}

        query = [
            "name",
            "age",
        ]
        self.assertEqual(dictfier.filter(student, query), {
            "name": "Danish",
            "age": 24
        })
Пример #8
0
    def retrieve(self, request, pk=None):
        queryset = self.get_queryset()
        object = get_object_or_404(queryset, pk=pk)
        serializer = self.get_serializer(object, context={'request': request})

        if self.query_param_name in request.query_params:
            query = json.loads(request.query_params[self.query_param_name])

            data = dictfier.filter(serializer.data, query)
            return Response(data)
        return Response(serializer.data)
Пример #9
0
 def test_dictfield_api(self):
     student = {'name': 'Danish', 'age': 24}
     query = [
         "name",
         {
             "age_in_years": dictfier.dictfield("age")
         },
     ]
     self.assertEqual(dictfier.filter(student, query), {
         'name': 'Danish',
         'age_in_years': 24
     })
Пример #10
0
    def test_empty_query_against_flat_nested_and_iterable_dict(self):
        book1 = {'title': 'Advanced Data Structures', 'publish_date': '2018'},
        book2 = {'title': 'Basic Data Structures', 'publish_date': '2010'}

        course1 = {'code': 'CS201', 'name': 'Data Structures', 'book': book1}
        course2 = {'code': 'CS205', 'name': 'Computer Networks', 'book': book2}
        courses = [course1, course2]

        student = {
            'name':
            'Danish',
            'age':
            24,
            'courses': [{
                'code': 'CS201',
                'name': 'Data Structures'
            }, {
                'code': 'CS205',
                'name': 'Computer Networks'
            }]
        }

        query1 = []  # Empty outer flat query

        query2 = [[]]  # Empty outer iterable query

        # Empty nested flat query
        query3 = [{"book": []}]

        # Empty nested iterable query
        query4 = [{"courses": [[]]}]

        self.assertEqual(dictfier.filter(student, query1), {})

        self.assertEqual(dictfier.filter(courses, query2), [{}, {}])

        self.assertEqual(dictfier.filter(course1, query3), {"book": {}})

        self.assertEqual(dictfier.filter(student, query4),
                         {"courses": [{}, {}]})
Пример #11
0
    def test_dictfield_api_with_call_kwarg(self):
        def age_in_months():
            return 24 * 12

        student = {'name': 'Danish', 'age': 24, 'age_in_months': age_in_months}
        query = [
            "name",
            {
                "months": dictfier.dictfield("age_in_months", call=True)
            },
        ]
        self.assertEqual(dictfier.filter(student, query), {
            'name': 'Danish',
            'months': 288
        })
Пример #12
0
    def test_useobj_api(self):
        student = {'name': 'Danish', 'age': 24}

        def age_in_months(obj):
            # Do the computation here then return the result
            return obj["age"] * 12

        query = [
            "name",
            {
                "age_in_months": dictfier.useobj(age_in_months)
            },
        ]
        self.assertEqual(dictfier.filter(student, query), {
            'name': 'Danish',
            'age_in_months': 288
        })
Пример #13
0
    def list(self, request):
        queryset = self.get_queryset()
        if self.filter_backends is not None:
            queryset = self.filter_queryset(queryset)
        if self.pagination_class is not None:
            queryset = self.paginate_queryset(queryset)

        serializer = self.get_serializer(queryset,
                                         many=True,
                                         context={'request': request})

        response = Response
        if self.paginator is not None:
            response = self.get_paginated_response

        if self.query_param_name in request.query_params:
            query = json.loads(request.query_params[self.query_param_name])
            data = dictfier.filter(serializer.data, query)

            return response(data)
        return response(serializer.data)
Пример #14
0
 def test_custom_iterable_nested_dict(self):
     # Customize how dictfier obtains nested iterable obj
     # per nested field
     student = {
         'name':
         'Danish',
         'age':
         24,
         'courses': [{
             'code': 'CS201',
             'name': 'Data Structures'
         }, {
             'code': 'CS205',
             'name': 'Computer Networks'
         }]
     }
     query = [
         "name", "age", {
             "courses":
             dictfier.useobj(lambda obj: obj["courses"], [[
                 "code",
                 "name",
             ]])
         }
     ]
     self.assertEqual(
         dictfier.filter(student, query), {
             'name':
             'Danish',
             'age':
             24,
             'courses': [{
                 'code': 'CS201',
                 'name': 'Data Structures'
             }, {
                 'code': 'CS205',
                 'name': 'Computer Networks'
             }]
         })
Пример #15
0
    def list(self, request):
        queryset = self.get_queryset()
        if self.filter_backends is not None:
            queryset = self.filter_queryset(queryset)

        if self.pagination_class is not None:
            queryset = self.paginate_queryset(queryset)

        if self.paginator is not None:
            response = self.get_paginated_response
        else:
            response = Response

        serializer = self.get_serializer(queryset,
                                         many=True,
                                         context={'request': request})

        if self.tofilter(request):
            query_str = self.get_query_str(request)
            schema = self.get_serializer().get_fields()
            query = Query(query_str, schema)

            try:
                parsed_query = query.parse()
            except (FormatError, InvalidField) as e:
                return Response({"error": str(e)}, 400)
            except FieldNotFound as e:
                return Response({"error": str(e)}, 404)

            # extra [] cuz a list of resources is expected
            filter_query = [parsed_query]

            filtered_data = dictfier.filter(serializer.data, filter_query)
            return response(filtered_data)

        return response(serializer.data)
Пример #16
0
    def test_query_format_violation(self):
        book1 = {'title': 'Advanced Data Structures', 'publish_date': '2018'},
        book2 = {'title': 'Basic Data Structures', 'publish_date': '2010'}

        course1 = {'code': 'CS201', 'name': 'Data Structures', 'book': book1}
        course2 = {'code': 'CS205', 'name': 'Computer Networks', 'book': book2}
        courses = [course1, course2]

        student = {'name': 'Danish', 'age': 24, 'courses': [course1, course2]}

        query1 = [
            "name",
            "age",
            566  # FormatError
        ]
        query2 = [
            "name",
            "age",
            ["name"]  # FormatError
        ]

        query3 = [
            "name",
            "age",
            {
                "courses": [
                    ["code", "name"],
                    "name",  # FormatError
                ]
            }
        ]

        query4 = [
            "name",
            "age",
            {
                "courses": [
                    "name",  # FormatError
                    ["code", "name"],
                ]
            }
        ]

        query5 = [
            "name",
            "age",
            {
                "class": "HBO"  # TypeError
            }
        ]

        with self.assertRaises(dictfier.exceptions.FormatError):
            dictfier.filter(student, query1)

        with self.assertRaises(dictfier.exceptions.FormatError):
            dictfier.filter(student, query2)

        with self.assertRaises(dictfier.exceptions.FormatError):
            dictfier.filter(student, query3)

        with self.assertRaises(dictfier.exceptions.FormatError):
            dictfier.filter(student, query4)

        with self.assertRaises(TypeError):
            dictfier.filter(student, query5)