def test_improperly_configured_related_field(self):
        resources = [
            Resource(
                'host',
                'hosts',
                WebsiteHostViewSet,
                [
                    NestedResource(
                        'website',
                        'websites',
                        WebsiteViewSet,
                        parent_field_lookup=RequestParentLookupHelper(
                            'hosts',
                            'host',
                        ),
                    ),
                ],
            ),
            Resource('developer', 'developers', DeveloperViewSet)
        ]
        urlpatterns = make_urlpatterns_from_resources(resources)

        assert_raises(
            ImproperlyConfigured,
            self._make_url_via_field,
            'website-detail',
            self.programming_language1,
            urlpatterns=urlpatterns,
        )
    def test_indirect_model_relation_resource(self):
        resources = [
            Resource(
                'developer',
                'developers',
                DeveloperViewSet2,
                [
                    NestedResource(
                        'version',
                        'versions',
                        ProgrammingLanguageVersionViewSet,
                        parent_field_lookup='language__author',
                    ),
                ],
            ),
        ]
        urlpatterns = make_urlpatterns_from_resources(resources)

        version = self.programming_language_version
        view_name = 'version-detail'
        url_generated = self._make_url_via_field(
            view_name,
            version,
            urlpatterns=urlpatterns,
        )
        url_expected = self._make_url_with_kwargs(
            view_name,
            {
                'developer': self.developer1.pk,
                'version': version.pk,
            },
            urlpatterns=urlpatterns,
        )
        eq_(url_expected, url_generated)
    def test_illegal_parent_field_lookup_type(self):
        resources = [
            Resource(
                'developer',
                'developers',
                DeveloperViewSet,
                [
                    NestedResource(
                        'language',
                        'languages',
                        ProgrammingLanguageViewSet,
                        parent_field_lookup=_FakeParentLookupHelper('author'),
                    ),
                ],
            ),
        ]

        urlpatterns = make_urlpatterns_from_resources(resources)

        assert_raises(
            AssertionError,
            self._make_url_via_field,
            'language-detail',
            self.programming_language1,
            urlpatterns=urlpatterns,
        )
    def test_non_existing_child_detail(self):
        resources = [
            Resource(
                'developer',
                'developers',
                DeveloperViewSet,
                [
                    NestedResource(
                        'language',
                        'languages',
                        ProgrammingLanguageViewSet,
                        parent_field_lookup='author',
                        ),
                ],
            ),
        ]
        urlpatterns = make_urlpatterns_from_resources(resources)

        client = _TestClient(urlpatterns)

        url_path = reverse(
            'language-detail',
            kwargs={
                'author': self.developer1.pk,
                'pk': self.programming_language2.pk + 1,
                },
            urlconf=urlpatterns,
            )
        response = client.get(url_path)
        eq_(404, response.status_code)
    def test_indirect_child_detail_via_one_to_one(self):
        visit = WebsiteVisit.objects.create(website=self.website)
        resources = [
            Resource(
                "developer",
                "developers",
                DeveloperViewSet,
                [
                    NestedResource(
                        "language",
                        "languages",
                        ProgrammingLanguageViewSet,
                        [
                            NestedResource(
                                "visit", "visits", WebsiteVisitViewSet, parent_field_lookup="website__language"
                            )
                        ],
                        parent_field_lookup="author",
                    )
                ],
            )
        ]
        urlpatterns = make_urlpatterns_from_resources(resources)

        client = TestClient(urlpatterns)

        url_path = reverse(
            "visit-detail",
            kwargs={"developer": self.developer1.pk, "language": self.programming_language1.pk, "visit": visit.pk},
            urlconf=urlpatterns,
        )
        response = client.get(url_path)
        eq_(200, response.status_code)
    def test_child_list_with_non_existing_parent(self):
        urlpatterns = make_urlpatterns_from_resources(self._RESOURCES)

        client = TestClient(urlpatterns)

        url_path = reverse("language-list", kwargs={"developer": self.non_existing_developer_pk}, urlconf=urlpatterns)
        response = client.get(url_path)
        eq_(404, response.status_code)
    def test_parent_list(self):
        urlpatterns = make_urlpatterns_from_resources(self._RESOURCES)

        client = TestClient(urlpatterns)

        url_path = reverse("developer-list", urlconf=urlpatterns)
        response = client.get(url_path)
        eq_(200, response.status_code)
    def test_parent_list_mounted_on_different_urlpath(self):
        api_urls = list(make_urlpatterns_from_resources(self._RESOURCES))
        urlpatterns = (url(r"^api/", include(api_urls)),)

        client = TestClient(urlpatterns)

        url_path = reverse("developer-list", urlconf=urlpatterns)
        response = client.get(url_path)
        eq_(200, response.status_code)
    def test_resources_resolution_with_hyphenated_resource_name():
        resources = [Resource("software-developer", "developers", DeveloperViewSet)]
        urlpatterns = make_urlpatterns_from_resources(resources)

        url_path1 = reverse("software_developer-list", urlconf=urlpatterns)
        eq_("/developers/", url_path1)

        url_path2 = reverse("software_developer-detail", kwargs={"software_developer": 1}, urlconf=urlpatterns)
        eq_("/developers/1/", url_path2)
    def test_resources_resolution_with_custom_router():
        resources = [Resource("developer", "developers", DeveloperViewSet)]
        urlpatterns = make_urlpatterns_from_resources(resources, SimpleRouter)
        eq_(2, len(urlpatterns))

        url_path1 = reverse("developer-list", urlconf=urlpatterns)
        eq_("/developers/", url_path1)

        url_path2 = reverse("developer-detail", kwargs={"developer": 1}, urlconf=urlpatterns)
        eq_("/developers/1/", url_path2)
    def test_default_router():
        resources = []
        urlpatterns = make_urlpatterns_from_resources(resources)
        eq_(2, len(urlpatterns))

        url_path1 = reverse("api-root", urlconf=urlpatterns)
        eq_("/", url_path1)

        url_path2 = reverse("api-root", kwargs={"format": "json"}, urlconf=urlpatterns)
        eq_("/.json", url_path2)
    def test_default_router():
        resources = []
        urlpatterns = make_urlpatterns_from_resources(resources)
        eq_(2, len(urlpatterns))

        url_path1 = reverse('api-root', urlconf=urlpatterns)
        eq_('/', url_path1)

        url_path2 = \
            reverse('api-root', kwargs={'format': 'json'}, urlconf=urlpatterns)
        eq_('/.json', url_path2)
    def test_resources_resolution_with_default_router():
        resources = [Resource('developer', 'developers', DeveloperViewSet)]
        urlpatterns = make_urlpatterns_from_resources(resources)

        url_path = reverse('developer-list', urlconf=urlpatterns)
        eq_('/developers/', url_path)

        view_callable, view_args, view_kwargs = resolve(url_path, urlpatterns)
        ok_(getattr(view_callable, 'is_fixture', False))
        eq_((), view_args)
        eq_({}, view_kwargs)
    def test_resources_resolution_with_custom_router():
        resources = [Resource('developer', 'developers', DeveloperViewSet)]
        urlpatterns = make_urlpatterns_from_resources(resources, SimpleRouter())
        eq_(2, len(urlpatterns))

        url_path1 = reverse('developer-list', urlconf=urlpatterns)
        eq_('/developers/', url_path1)

        url_path2 = \
            reverse('developer-detail', kwargs={'pk': 1}, urlconf=urlpatterns)
        eq_('/developers/1/', url_path2)
    def test_parent_detail(self):
        urlpatterns = make_urlpatterns_from_resources(self._RESOURCES)

        client = TestClient(urlpatterns)

        url_path = reverse("developer-detail", kwargs={"developer": self.developer1.pk}, urlconf=urlpatterns)
        response = client.get(url_path)
        response_data = response.data
        expected_languages_url = reverse("language-list", kwargs={"developer": self.developer1.pk}, urlconf=urlpatterns)
        languages_url = response_data["programming_languages"]
        ok_(languages_url.endswith(expected_languages_url))
        eq_(200, response.status_code)
    def test_child_detail_with_wrong_parent(self):
        urlpatterns = make_urlpatterns_from_resources(self._RESOURCES)

        client = TestClient(urlpatterns)

        url_path = reverse(
            "language-detail",
            kwargs={"developer": self.developer1.pk, "language": self.programming_language2.pk},
            urlconf=urlpatterns,
        )
        response = client.get(url_path)
        eq_(404, response.status_code)
    def test_non_existing_parent_detail(self):
        resources = [Resource('developer', 'developers', DeveloperViewSet)]
        urlpatterns = make_urlpatterns_from_resources(resources)

        client = _TestClient(urlpatterns)

        url_path = reverse(
            'developer-detail',
            kwargs={'pk': self.developer2.pk + 1},
            urlconf=urlpatterns,
            )
        response = client.get(url_path)
        eq_(404, response.status_code)
    def test_nested_resources_resolution():
        resources = [
            Resource(
                "developer",
                "developers",
                DeveloperViewSet,
                [NestedResource("language", "languages", ProgrammingLanguageViewSet, parent_field_lookup="author")],
            )
        ]
        urlpatterns = make_urlpatterns_from_resources(resources)

        url_path = reverse("language-list", kwargs={"developer": 1}, urlconf=urlpatterns)
        eq_("/developers/1/languages/", url_path)
    def test_resources_resolution_with_hyphenated_resource_name():
        resources = \
            [Resource('software-developer', 'developers', DeveloperViewSet)]
        urlpatterns = make_urlpatterns_from_resources(resources)

        url_path1 = reverse('software_developer-list', urlconf=urlpatterns)
        eq_('/developers/', url_path1)

        url_path2 = reverse(
            'software_developer-detail',
            kwargs={'software_developer': 1},
            urlconf=urlpatterns,
        )
        eq_('/developers/1/', url_path2)
    def test_child_detail_inside_namespace(self):
        namespace = 'v1'

        api_urls = make_urlpatterns_from_resources(self._RESOURCES)
        urlpatterns = _mount_urls_on_namespace(api_urls, namespace)

        response = _make_request_to_namespaced_url(
            namespace,
            'language-detail',
            {
                'developer': self.developer1.pk,
                'language': self.programming_language1.pk,
            },
            urlpatterns,
        )
        eq_(200, response.status_code)
    def test_detail_with_non_existing_grandparent(self):
        urlpatterns = make_urlpatterns_from_resources(self._RESOURCES)

        client = TestClient(urlpatterns)

        url_path = reverse(
            "version-detail",
            kwargs={
                "developer": self.non_existing_developer_pk,
                "language": self.programming_language1.pk,
                "version": self.programming_language_version.pk,
            },
            urlconf=urlpatterns,
        )
        response = client.get(url_path)
        eq_(404, response.status_code)
    def test_grand_child_detail(self):
        urlpatterns = make_urlpatterns_from_resources(self._RESOURCES)

        client = TestClient(urlpatterns)

        url_path = reverse(
            "version-detail",
            kwargs={
                "developer": self.developer1.pk,
                "language": self.programming_language1.pk,
                "version": self.programming_language_version.pk,
            },
            urlconf=urlpatterns,
        )
        response = client.get(url_path)
        eq_(200, response.status_code)
def make_response_for_request(
    view_name,
    view_kwargs,
    resources=None,
    method_name='GET',
    environ_items=None,
    **kwargs
):
    if resources:
        urlpatterns = make_urlpatterns_from_resources(resources)
    else:
        urlpatterns = None
    client = TestClient(urlpatterns, environ_items)
    url_path = reverse(view_name, kwargs=view_kwargs, urlconf=urlpatterns)
    method = getattr(client, method_name.lower())
    response = method(url_path, **kwargs)
    return response
    def test_child_list_with_non_viewable_parent(self):
        resources = [
            Resource(
                "website",
                "websites",
                _WebsiteViewSetWithCustomGetQueryset,
                [NestedResource("host", "hosts", WebsiteHostViewSet, parent_field_lookup="websites")],
            )
        ]
        urlpatterns = make_urlpatterns_from_resources(resources)

        client = TestClient(urlpatterns)

        url_path = reverse("host-list", kwargs={"website": self.website.pk}, urlconf=urlpatterns)

        response = client.get(url_path)
        eq_(404, response.status_code)
    def test_reverse_many_to_many_relationships(self):
        resources = [
            Resource(
                "host",
                "hosts",
                WebsiteHostViewSet,
                [NestedResource("website", "websites", WebsiteViewSet, parent_field_lookup="hosts")],
            )
        ]
        urlpatterns = make_urlpatterns_from_resources(resources)

        client = TestClient(urlpatterns)

        url_path = reverse(
            "website-detail", kwargs={"website": self.website.pk, "host": self.website_host.pk}, urlconf=urlpatterns
        )
        response = client.get(url_path)
        eq_(200, response.status_code)
    def _get_response_from_child_resource_list(
        self,
        parent_view_set,
        child_view_set,
        url_name=None,
        urlvars=None,
        user=None,
        ):
        resources = self._build_resources(parent_view_set, child_view_set)
        urlpatterns = make_urlpatterns_from_resources(resources)

        client = TestClient(urlpatterns, user)

        url_name = url_name or 'language-list'
        urlvars = urlvars or {'developer': self.developer1.pk}
        url_path = reverse(url_name, kwargs=urlvars, urlconf=urlpatterns)
        response = client.get(url_path)
        return response
    def test_parent_detail(self):
        response = self._make_response_for_request(
            'developer-detail',
            {'developer': self.developer1.pk},
        )

        response_data = response.data

        urlpatterns = make_urlpatterns_from_resources(self._RESOURCES)
        expected_languages_url = reverse(
            'language-list',
            kwargs={'developer': self.developer1.pk},
            urlconf=urlpatterns,
        )
        languages_url = response_data['programming_languages']
        ok_(languages_url.endswith(expected_languages_url))

        eq_(200, response.status_code)
    def setUp(self):
        super(_BaseTestCase, self).setUp()

        website_resource = Resource('website', 'websites', WebsiteViewSet)
        self.resources = [
            Resource(
                'developer',
                'developers',
                DeveloperViewSet,
                [
                    NestedResource(
                        'language',
                        'languages',
                        ProgrammingLanguageViewSet,
                        [
                            NestedResource(
                                'version',
                                'versions',
                                ProgrammingLanguageVersionViewSet,
                                parent_field_lookup='language',
                            ),
                            NestedResource(
                                'implementation',
                                'implementations',
                                ProgrammingLanguageImplementationViewSet,
                                parent_field_lookup='language',
                            ),
                        ],
                        parent_field_lookup='author',
                        cross_linked_resources={'website': website_resource},
                    ),
                ],

            ),
            Resource(
                'website-visit',
                'website-visits',
                WebsiteVisitViewSet,
                cross_linked_resources={'website': website_resource},
            ),
            website_resource,
        ]

        self.urlpatterns = make_urlpatterns_from_resources(self.resources)
    def test_nested_resources_resolution():
        resources = [
            Resource(
                'developer',
                'developers',
                DeveloperViewSet,
                [
                    NestedResource(
                        'language',
                        'languages',
                        ProgrammingLanguageViewSet,
                        parent_field_lookup='author',
                    ),
                ],
            ),
        ]
        urlpatterns = make_urlpatterns_from_resources(resources)

        url_path = \
            reverse('language-list', kwargs={'author': 1}, urlconf=urlpatterns)
        eq_('/developers/1/languages/', url_path)
    def setUp(self):
        super(TestRelatedLinkedField, self).setUp()

        self.resources = [
            Resource(
                "developer",
                "developers",
                DeveloperViewSet,
                [
                    NestedResource(
                        "language",
                        "languages",
                        ProgrammingLanguageViewSet,
                        [
                            NestedResource(
                                "version", "versions", ProgrammingLanguageVersionViewSet, parent_field_lookup="language"
                            )
                        ],
                        parent_field_lookup="author",
                    )
                ],
            )
        ]
        self.urlpatterns = make_urlpatterns_from_resources(self.resources)