示例#1
0
    def test_one_to_one(self):
        """
        Test a related ToOne resource with a nested full ToOne resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Company.objects.count(), 0)
        self.assertEqual(Address.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'company': {
                'name': 'Yum Yum Pie Factory!',
                'address': {
                    'line': 'Somewhere, Utah'
                }
            }
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.raw_post_data = json.dumps(data)
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail',
                               kwargs={
                                   'pk': pk,
                                   'resource_name': pr._meta.resource_name,
                                   'api_name': pr._meta.api_name
                               })
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content)
        self.assertEqual(person['name'], 'Joan Rivers')

        company = person['company']
        self.assertEqual(company['name'], 'Yum Yum Pie Factory!')

        address = company['address']
        self.assertEqual(address['line'], 'Somewhere, Utah')

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.path = reverse('api_dispatch_detail',
                               kwargs={
                                   'pk': pk,
                                   'resource_name': pr._meta.resource_name,
                                   'api_name': pr._meta.api_name
                               })
        request.raw_post_data = resp.content
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#2
0
    def test_many_to_many(self):
        """
        Test a related ToMany resource with a nested full ToMany resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(Bone.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'dogs': [
                {
                    'name': 'Snoopy',
                    'bones': [
                        {
                            'color': 'white'
                        }
                    ]
                }
            ]
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.path = reverse('api_dispatch_list', kwargs={'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        request.set_body(json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Dog.objects.count(), 1)
        self.assertEqual(Bone.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(person['name'], 'Joan Rivers')
        self.assertEqual(len(person['dogs']), 1)

        dog = person['dogs'][0]
        self.assertEqual(dog['name'], 'Snoopy')
        self.assertEqual(len(dog['bones']), 1)

        bone = dog['bones'][0]
        self.assertEqual(bone['color'], 'white')

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.set_body(json.dumps(person))
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#3
0
    def test_many_to_many(self):
        """
        Test a related ToMany resource with a nested full ToMany resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(Bone.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'dogs': [
                {
                    'name': 'Snoopy',
                    'bones': [
                        {
                            'color': 'white'
                        }
                    ]
                }
            ]
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.path = reverse('api_dispatch_list', kwargs={'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        request.set_body(json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Dog.objects.count(), 1)
        self.assertEqual(Bone.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(person['name'], 'Joan Rivers')
        self.assertEqual(len(person['dogs']), 1)

        dog = person['dogs'][0]
        self.assertEqual(dog['name'], 'Snoopy')
        self.assertEqual(len(dog['bones']), 1)

        bone = dog['bones'][0]
        self.assertEqual(bone['color'], 'white')

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.set_body(json.dumps(person))
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#4
0
    def test_many_to_one_extra_data_ignored(self):
        """
        Test a related ToMany resource with a nested full ToOne resource
        
        FieldError would result when extra data is included on an embedded
        resource for an already saved object.
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(DogHouse.objects.count(), 0)

        pr = PersonResource()

        data = {"name": "Joan Rivers", "dogs": [{"name": "Snoopy", "house": {"color": "Red"}}]}

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "POST"
        request.set_body(json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Dog.objects.count(), 1)
        self.assertEqual(DogHouse.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = "GET"
        request.path = reverse(
            "api_dispatch_detail",
            kwargs={"pk": pk, "resource_name": pr._meta.resource_name, "api_name": pr._meta.api_name},
        )
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content.decode("utf-8"))
        self.assertEqual(person["name"], "Joan Rivers")
        self.assertEqual(len(person["dogs"]), 1)

        dog = person["dogs"][0]
        self.assertEqual(dog["name"], "Snoopy")

        house = dog["house"]
        self.assertEqual(house["color"], "Red")

        # clients may include extra data, which should be ignored. Make extra data is ignored on the resource and sub resources.
        person["thisfieldshouldbeignored"] = "foobar"
        person["dogs"][0]["thisfieldshouldbeignored"] = "foobar"

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "PUT"
        request.set_body(json.dumps(person))
        request.path = reverse(
            "api_dispatch_detail",
            kwargs={"pk": pk, "resource_name": pr._meta.resource_name, "api_name": pr._meta.api_name},
        )
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#5
0
    def test_one_to_one(self):
        """
        Test a related ToOne resource with a nested full ToOne resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Company.objects.count(), 0)
        self.assertEqual(Address.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'company': {
                'name': 'Yum Yum Pie Factory!',
                'address': {
                    'line': 'Somewhere, Utah'
                }
            }
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.set_body(json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail', kwargs={
            'pk': pk,
            'resource_name': pr._meta.resource_name,
            'api_name': pr._meta.api_name
        })
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(person['name'], 'Joan Rivers')

        company = person['company']
        self.assertEqual(company['name'], 'Yum Yum Pie Factory!')

        address = company['address']
        self.assertEqual(address['line'], 'Somewhere, Utah')

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.path = reverse('api_dispatch_detail', kwargs={
            'pk': pk,
            'resource_name': pr._meta.resource_name,
            'api_name': pr._meta.api_name
        })
        request.set_body(resp.content.decode('utf-8'))
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#6
0
    def test_one_to_many(self):
        """
        Test a related ToOne resource with a nested full ToMany resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Company.objects.count(), 0)
        self.assertEqual(Product.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'company': {
                'name': 'Yum Yum Pie Factory!',
                'products': [
                    {
                        'name': 'Tasty Pie'
                    }
                ]
            }
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        setattr(request, self.body_attr, json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Company.objects.count(), 1)
        self.assertEqual(Product.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content)
        self.assertEqual(person['name'], 'Joan Rivers')

        company = person['company']
        self.assertEqual(company['name'], 'Yum Yum Pie Factory!')
        self.assertEqual(len(company['products']), 1)

        product = company['products'][0]
        self.assertEqual(product['name'], 'Tasty Pie')

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        setattr(request, self.body_attr, json.dumps(person))
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#7
0
    def test_many_to_one(self):
        """
        Test a related ToMany resource with a nested full ToOne resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(DogHouse.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'dogs': [
                {
                    'name': 'Snoopy',
                    'house': {
                        'color': 'Red'
                    }
                }
            ]
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.raw_post_data = json.dumps(data)
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Dog.objects.count(), 1)
        self.assertEqual(DogHouse.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content)
        self.assertEqual(person['name'], 'Joan Rivers')
        self.assertEqual(len(person['dogs']), 1)

        dog = person['dogs'][0]
        self.assertEqual(dog['name'], 'Snoopy')

        house = dog['house']
        self.assertEqual(house['color'], 'Red')

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.raw_post_data = json.dumps(person)
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#8
0
    def test_one_to_many(self):
        """
        Test a related ToOne resource with a nested full ToMany resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Company.objects.count(), 0)
        self.assertEqual(Product.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'company': {
                'name': 'Yum Yum Pie Factory!',
                'products': [
                    {
                        'name': 'Tasty Pie'
                    }
                ]
            }
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        setattr(request, self.body_attr, json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Company.objects.count(), 1)
        self.assertEqual(Product.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content)
        self.assertEqual(person['name'], 'Joan Rivers')

        company = person['company']
        self.assertEqual(company['name'], 'Yum Yum Pie Factory!')
        self.assertEqual(len(company['products']), 1)

        product = company['products'][0]
        self.assertEqual(product['name'], 'Tasty Pie')

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        setattr(request, self.body_attr, json.dumps(person))
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#9
0
    def test_many_to_many(self):
        """
        Test a related ToMany resource with a nested full ToMany resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(Bone.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'dogs': [
                {
                    'name': 'Snoopy',
                    'bones': [
                        {
                            'color': 'white'
                        }
                    ]
                }
            ]
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.raw_post_data = json.dumps(data)
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content)
        self.assertEqual(person['name'], 'Joan Rivers')
        self.assertEqual(len(person['dogs']), 1)

        dog = person['dogs'][0]
        self.assertEqual(dog['name'], 'Snoopy')
        self.assertEqual(len(dog['bones']), 1)

        bone = dog['bones'][0]
        self.assertEqual(bone['color'], 'white')

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.raw_post_data = resp.content
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#10
0
    def test_many_to_one(self):
        """
        Test a related ToMany resource with a nested full ToOne resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(DogHouse.objects.count(), 0)

        pr = PersonResource()

        data = {"name": "Joan Rivers", "dogs": [{"name": "Snoopy", "house": {"color": "Red"}}]}

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "POST"
        request.set_body(json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Dog.objects.count(), 1)
        self.assertEqual(DogHouse.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = "GET"
        request.path = reverse(
            "api_dispatch_detail",
            kwargs={"pk": pk, "resource_name": pr._meta.resource_name, "api_name": pr._meta.api_name},
        )
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content.decode("utf-8"))
        self.assertEqual(person["name"], "Joan Rivers")
        self.assertEqual(len(person["dogs"]), 1)

        dog = person["dogs"][0]
        self.assertEqual(dog["name"], "Snoopy")

        house = dog["house"]
        self.assertEqual(house["color"], "Red")

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "PUT"
        request.set_body(json.dumps(person))
        request.path = reverse(
            "api_dispatch_detail",
            kwargs={"pk": pk, "resource_name": pr._meta.resource_name, "api_name": pr._meta.api_name},
        )
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#11
0
    def test_one_to_one(self):
        """
        Test a related ToOne resource with a nested full ToOne resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Company.objects.count(), 0)
        self.assertEqual(Address.objects.count(), 0)

        pr = PersonResource()

        data = {
            "name": "Joan Rivers",
            "company": {"name": "Yum Yum Pie Factory!", "address": {"line": "Somewhere, Utah"}},
        }

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "POST"
        request.set_body(json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = "GET"
        request.path = reverse(
            "api_dispatch_detail",
            kwargs={"pk": pk, "resource_name": pr._meta.resource_name, "api_name": pr._meta.api_name},
        )
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content.decode("utf-8"))
        self.assertEqual(person["name"], "Joan Rivers")

        company = person["company"]
        self.assertEqual(company["name"], "Yum Yum Pie Factory!")

        address = company["address"]
        self.assertEqual(address["line"], "Somewhere, Utah")

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "PUT"
        request.path = reverse(
            "api_dispatch_detail",
            kwargs={"pk": pk, "resource_name": pr._meta.resource_name, "api_name": pr._meta.api_name},
        )
        request.set_body(resp.content.decode("utf-8"))
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#12
0
    def test_many_to_many_change_nested(self):
        """
        Test a related ToMany resource with a nested full ToMany resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(Bone.objects.count(), 0)

        pr = PersonResource()

        person = Person.objects.create(name='Joan Rivers')
        dog = person.dogs.create(name='Snoopy')
        bone = dog.bones.create(color='white')

        pk = person.pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail',
                               kwargs={
                                   'pk': pk,
                                   'resource_name': pr._meta.resource_name,
                                   'api_name': pr._meta.api_name
                               })
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        data = json.loads(resp.content.decode('utf-8'))

        self.assertEqual(data['dogs'][0]['bones'][0]['color'], 'white')

        # Change just a nested resource via PUT
        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        data['dogs'][0]['bones'][0]['color'] = 'gray'
        body = json.dumps(data)
        request.set_body(body)
        request.path = reverse('api_dispatch_detail',
                               kwargs={
                                   'pk': pk,
                                   'resource_name': pr._meta.resource_name,
                                   'api_name': pr._meta.api_name
                               })
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)

        self.assertEqual(Bone.objects.count(), 1)
        bone = Bone.objects.all()[0]
        self.assertEqual(bone.color, 'gray')
示例#13
0
    def test_many_to_many(self):
        """
        Test a related ToMany resource with a nested full ToMany resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(Bone.objects.count(), 0)

        pr = PersonResource()

        data = {"name": "Joan Rivers", "dogs": [{"name": "Snoopy", "bones": [{"color": "white"}]}]}

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "POST"
        request.raw_post_data = json.dumps(data)
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Dog.objects.count(), 1)
        self.assertEqual(Bone.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = "GET"
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content)
        self.assertEqual(person["name"], "Joan Rivers")
        self.assertEqual(len(person["dogs"]), 1)

        dog = person["dogs"][0]
        self.assertEqual(dog["name"], "Snoopy")
        self.assertEqual(len(dog["bones"]), 1)

        bone = dog["bones"][0]
        self.assertEqual(bone["color"], "white")

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "PUT"
        request.raw_post_data = json.dumps(person)
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#14
0
    def test_one_to_many(self):
        """
        Test a related ToOne resource with a nested full ToMany resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Company.objects.count(), 0)
        self.assertEqual(Product.objects.count(), 0)

        pr = PersonResource()

        data = {"name": "Joan Rivers", "company": {"name": "Yum Yum Pie Factory!", "products": [{"name": "Tasty Pie"}]}}

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "POST"
        request.set_body(json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Company.objects.count(), 1)
        self.assertEqual(Product.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = "GET"
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content.decode("utf-8"))
        self.assertEqual(person["name"], "Joan Rivers")

        company = person["company"]
        self.assertEqual(company["name"], "Yum Yum Pie Factory!")
        self.assertEqual(len(company["products"]), 1)

        product = company["products"][0]
        self.assertEqual(product["name"], "Tasty Pie")

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "PUT"
        request.set_body(json.dumps(person))
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#15
0
    def test_many_to_many_change_nested(self):
        """
        Test a related ToMany resource with a nested full ToMany resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(Bone.objects.count(), 0)

        pr = PersonResource()

        person = Person.objects.create(name='Joan Rivers')
        dog = person.dogs.create(name='Snoopy')
        bone = dog.bones.create(color='white')

        pk = person.pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        data = json.loads(resp.content.decode('utf-8'))

        self.assertEqual(data['dogs'][0]['bones'][0]['color'], 'white')

        # Change just a nested resource via PUT
        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        data['dogs'][0]['bones'][0]['color'] = 'gray'
        body = json.dumps(data)
        request.set_body(body)
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)

        self.assertEqual(Bone.objects.count(), 1)
        bone = Bone.objects.all()[0]
        self.assertEqual(bone.color, 'gray')
    NoteWithUpdatableUserResource, ContactResource, ContactGroupResource


api = Api(api_name='v1')
api.register(NoteResource(), canonical=True)
api.register(NoteWithUpdatableUserResource(), canonical=True)
api.register(UserResource(), canonical=True)
api.register(CategoryResource(), canonical=True)
api.register(TagResource(), canonical=True)
api.register(TaggableResource(), canonical=True)
api.register(TaggableTagResource(), canonical=True)
api.register(ExtraDataResource(), canonical=True)
api.register(FreshNoteResource(), canonical=True)
api.register(FreshMediaBitResource(), canonical=True)
api.register(ForumResource(), canonical=True)
api.register(CompanyResource(), canonical=True)
api.register(ProductResource(), canonical=True)
api.register(AddressResource(), canonical=True)
api.register(PersonResource(), canonical=True)
api.register(DogResource(), canonical=True)
api.register(DogHouseResource(), canonical=True)
api.register(BoneResource(), canonical=True)
api.register(PostResource(), canonical=True)
api.register(LabelResource(), canonical=True)
api.register(OrderResource(), canonical=True)
api.register(OrderItemResource(), canonical=True)
api.register(ContactResource(), canonical=True)
api.register(ContactGroupResource(), canonical=True)

urlpatterns = api.urls
示例#17
0
    def test_many_to_one(self):
        """
        Test a related ToMany resource with a nested full ToOne resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(DogHouse.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'dogs': [{
                'name': 'Snoopy',
                'house': {
                    'color': 'Red'
                }
            }]
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.raw_post_data = json.dumps(data)
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Dog.objects.count(), 1)
        self.assertEqual(DogHouse.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail',
                               kwargs={
                                   'pk': pk,
                                   'resource_name': pr._meta.resource_name,
                                   'api_name': pr._meta.api_name
                               })
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content)
        self.assertEqual(person['name'], 'Joan Rivers')
        self.assertEqual(len(person['dogs']), 1)

        dog = person['dogs'][0]
        self.assertEqual(dog['name'], 'Snoopy')

        house = dog['house']
        self.assertEqual(house['color'], 'Red')

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.raw_post_data = json.dumps(person)
        request.path = reverse('api_dispatch_detail',
                               kwargs={
                                   'pk': pk,
                                   'resource_name': pr._meta.resource_name,
                                   'api_name': pr._meta.api_name
                               })
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#18
0
    def test_many_to_one_extra_data_ignored(self):
        """
        Test a related ToMany resource with a nested full ToOne resource

        FieldError would result when extra data is included on an embedded
        resource for an already saved object.
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(DogHouse.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'dogs': [{
                'name': 'Snoopy',
                'house': {
                    'color': 'Red'
                }
            }]
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.set_body(json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Dog.objects.count(), 1)
        self.assertEqual(DogHouse.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail',
                               kwargs={
                                   'pk': pk,
                                   'resource_name': pr._meta.resource_name,
                                   'api_name': pr._meta.api_name
                               })
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(person['name'], 'Joan Rivers')
        self.assertEqual(len(person['dogs']), 1)

        dog = person['dogs'][0]
        self.assertEqual(dog['name'], 'Snoopy')

        house = dog['house']
        self.assertEqual(house['color'], 'Red')

        # clients may include extra data, which should be ignored. Make extra data is ignored on the resource and sub resources.
        person['thisfieldshouldbeignored'] = 'foobar'
        person['dogs'][0]['thisfieldshouldbeignored'] = 'foobar'

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.set_body(json.dumps(person))
        request.path = reverse('api_dispatch_detail',
                               kwargs={
                                   'pk': pk,
                                   'resource_name': pr._meta.resource_name,
                                   'api_name': pr._meta.api_name
                               })
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#19
0
    def test_many_to_one_extra_data_ignored(self):
        """
        Test a related ToMany resource with a nested full ToOne resource

        FieldError would result when extra data is included on an embedded
        resource for an already saved object.
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(DogHouse.objects.count(), 0)

        pr = PersonResource()

        data = {
            'name': 'Joan Rivers',
            'dogs': [
                {
                    'name': 'Snoopy',
                    'house': {
                        'color': 'Red'
                    }
                }
            ]
        }

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'POST'
        request.set_body(json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Dog.objects.count(), 1)
        self.assertEqual(DogHouse.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = 'GET'
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(person['name'], 'Joan Rivers')
        self.assertEqual(len(person['dogs']), 1)

        dog = person['dogs'][0]
        self.assertEqual(dog['name'], 'Snoopy')

        house = dog['house']
        self.assertEqual(house['color'], 'Red')

        # clients may include extra data, which should be ignored. Make extra data is ignored on the resource and sub resources.
        person['thisfieldshouldbeignored'] = 'foobar'
        person['dogs'][0]['thisfieldshouldbeignored'] = 'foobar'

        request = MockRequest()
        request.GET = {'format': 'json'}
        request.method = 'PUT'
        request.set_body(json.dumps(person))
        request.path = reverse('api_dispatch_detail', kwargs={'pk': pk, 'resource_name': pr._meta.resource_name, 'api_name': pr._meta.api_name})
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)
示例#20
0
    def test_many_to_many(self):
        """
        Test a related ToMany resource with a nested full ToMany resource
        """
        self.assertEqual(Person.objects.count(), 0)
        self.assertEqual(Dog.objects.count(), 0)
        self.assertEqual(Bone.objects.count(), 0)

        pr = PersonResource()

        data = {"name": "Joan Rivers", "dogs": [{"name": "Snoopy", "bones": [{"color": "white"}]}]}

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "POST"
        request.path = reverse(
            "api_dispatch_list", kwargs={"resource_name": pr._meta.resource_name, "api_name": pr._meta.api_name}
        )
        request.set_body(json.dumps(data))
        resp = pr.post_list(request)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual(Person.objects.count(), 1)
        self.assertEqual(Dog.objects.count(), 1)
        self.assertEqual(Bone.objects.count(), 1)

        pk = Person.objects.all()[0].pk
        request = MockRequest()
        request.method = "GET"
        request.path = reverse(
            "api_dispatch_detail",
            kwargs={"pk": pk, "resource_name": pr._meta.resource_name, "api_name": pr._meta.api_name},
        )
        resp = pr.get_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 200)

        person = json.loads(resp.content.decode("utf-8"))
        self.assertEqual(person["name"], "Joan Rivers")
        self.assertEqual(len(person["dogs"]), 1)

        dog = person["dogs"][0]
        self.assertEqual(dog["name"], "Snoopy")
        self.assertEqual(len(dog["bones"]), 1)

        bone = dog["bones"][0]
        self.assertEqual(bone["color"], "white")

        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "PUT"
        request.set_body(json.dumps(person))
        request.path = reverse(
            "api_dispatch_detail",
            kwargs={"pk": pk, "resource_name": pr._meta.resource_name, "api_name": pr._meta.api_name},
        )
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)

        # Change just a nested resource via PUT
        request = MockRequest()
        request.GET = {"format": "json"}
        request.method = "PUT"
        person["dogs"][0]["bones"][0]["color"] = "gray"
        body = json.dumps(person)
        request.set_body(body)
        request.path = reverse(
            "api_dispatch_detail",
            kwargs={"pk": pk, "resource_name": pr._meta.resource_name, "api_name": pr._meta.api_name},
        )
        resp = pr.put_detail(request, pk=pk)
        self.assertEqual(resp.status_code, 204)

        self.assertEqual(Bone.objects.count(), 1)
        bone = Bone.objects.all()[0]
        self.assertEqual(bone.color, "gray")