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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_django-rest-framework-nested-resource
------------
Tests for `django-rest-framework-nested-resource` models module.
"""
from django.test import TestCase
from django.core.urlresolvers import reverse
from rest_framework import status
from tests.models import (
ManyToManyTargetModel,
ManyToManySourceModel,
)
class NestedManyToManyRelationshipTest(TestCase):
"""
Test nested resources when the nested model has the ManyToMany relationship
declared on it.
"""
def test_list_view_filtered_correctly(self):
"""
Test that the list view /m2m-targets/<target_pk>/m2m-sources/ is
filtered correctly to only show objects related to the
`ManyToManyTargetModel` designated by the url.
"""
# Generate the primary model for the url
target_a = ManyToManyTargetModel.objects.create()
url = reverse('nested-m2m-sources-list', kwargs={'target_pk': target_a.pk})
# Generate another `ManyToManyTargetModel` instance to ensure instances
# that are only associated with it are not shown.
target_b = ManyToManyTargetModel.objects.create()
source_a = ManyToManySourceModel.objects.create()
source_b = ManyToManySourceModel.objects.create()
source_c = ManyToManySourceModel.objects.create()
# `target_a` is linked to only `source_a` and `source_b`
target_a.sources.add(source_a)
target_a.sources.add(source_b)
# `target_b` is linked to only `source_b` and `source_c`
target_b.sources.add(source_b)
target_b.sources.add(source_c)
response = self.client.get(url)
self.assertEqual(
response.status_code,
status.HTTP_200_OK,
msg=response.data,
)
self.assertEqual(len(response.data), 2)
returned_pks = set([obj['id'] for obj in response.data])
self.assertIn(source_a.pk, returned_pks)
self.assertIn(source_b.pk, returned_pks)
def test_detail_view_filtered_correctly(self):
"""
Test that the nested detail view allows accessing related
`ManyToManySourceModel` instances.
"""
# Generate the a target and source, and link them through the m2m
# relationship.
target = ManyToManyTargetModel.objects.create()
source = ManyToManySourceModel.objects.create()
target.sources.add(source)
url = reverse(
'nested-m2m-sources-detail',
kwargs={'target_pk': target.pk, 'pk': source.pk})
response = self.client.get(url)
self.assertEqual(
response.status_code,
status.HTTP_200_OK,
msg=response.data,
)
self.assertEqual(response.data.get('id'), source.pk)
class NestedReverseManyToManyRelationshipTest(TestCase):
"""
Test nested resources when the parent model has the ManyToMany relationship
declared on it.
"""
def test_list_view_filtered_correctly(self):
"""
Test that the list view /m2m-sources/<source_pk>/m2m-targets/ is
filtered correctly to only show objects related to the
`ManyToManyTargetModel` designated by the url.
"""
# Generate the primary model for the url
source_a = ManyToManySourceModel.objects.create()
url = reverse('nested-m2m-targets-list', kwargs={'source_pk': source_a.pk})
# Generate another `ManyToManySourceModel` instance to ensure instances
# that are only associated with it are not shown.
source_b = ManyToManySourceModel.objects.create()
target_a = ManyToManyTargetModel.objects.create()
target_b = ManyToManyTargetModel.objects.create()
target_c = ManyToManyTargetModel.objects.create()
# `target_a` is linked to only `source_a` and `source_b`
source_a.targets.add(target_a)
source_a.targets.add(target_b)
# `target_b` is linked to only `source_b` and `source_c`
source_b.targets.add(target_b)
source_b.targets.add(target_c)
response = self.client.get(url)
self.assertEqual(
response.status_code,
status.HTTP_200_OK,
msg=response.data,
)
self.assertEqual(len(response.data), 2)
returned_pks = set([obj['id'] for obj in response.data])
self.assertIn(target_a.pk, returned_pks)
self.assertIn(target_b.pk, returned_pks)
def test_detail_view_filtered_correctly(self):
"""
Test that the nested detail view allows accessing related
`ManyToManyTargetModel` instances.
"""
# Generate the a source and target, and link them through the m2m
# relationship.
source = ManyToManySourceModel.objects.create()
target = ManyToManyTargetModel.objects.create()
source.targets.add(target)
url = reverse(
'nested-m2m-targets-detail',
kwargs={'source_pk': target.pk, 'pk': source.pk})
response = self.client.get(url)
self.assertEqual(
response.status_code,
status.HTTP_200_OK,
msg=response.data,
)
self.assertEqual(response.data.get('id'), target.pk)
|