File: test_utils.py

package info (click to toggle)
python-djangorestframework-flex-fields 1.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: python: 1,410; sh: 6; makefile: 4
file content (49 lines) | stat: -rw-r--r-- 1,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from django.test import TestCase

from rest_flex_fields import is_included, is_expanded, WILDCARD_ALL, WILDCARD_ASTERISK


class MockRequest(object):
    def __init__(self, query_params=None, method="GET"):
        if query_params is None:
            query_params = {}
        self.query_params = query_params
        self.method = method


class TestUtils(TestCase):
    def test_should_be_included(self):
        request = MockRequest(query_params={})
        self.assertTrue(is_included(request, "name"))

    def test_should_not_be_included(self):
        request = MockRequest(query_params={"omit": "name,address"})
        self.assertFalse(is_included(request, "name"))

    def test_should_not_be_included_and_due_to_omit_and_has_dot_notation(self):
        request = MockRequest(query_params={"omit": "friend.name,address"})
        self.assertFalse(is_included(request, "name"))

    def test_should_not_be_included_and_due_to_fields_and_has_dot_notation(self):
        request = MockRequest(query_params={"fields": "hobby,address"})
        self.assertFalse(is_included(request, "name"))

    def test_should_be_expanded(self):
        request = MockRequest(query_params={"expand": "name,address"})
        self.assertTrue(is_expanded(request, "name"))

    def test_should_not_be_expanded(self):
        request = MockRequest(query_params={"expand": "name,address"})
        self.assertFalse(is_expanded(request, "hobby"))

    def test_should_be_expanded_and_has_dot_notation(self):
        request = MockRequest(query_params={"expand": "person.name,address"})
        self.assertTrue(is_expanded(request, "name"))

    def test_all_should_be_expanded(self):
        request = MockRequest(query_params={"expand": WILDCARD_ALL})
        self.assertTrue(is_expanded(request, "name"))

    def test_asterisk_should_be_expanded(self):
        request = MockRequest(query_params={"expand": WILDCARD_ASTERISK})
        self.assertTrue(is_expanded(request, "name"))