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_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_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 _make_request_to_namespaced_url(namespace, url_name, url_kwargs, urlconf):
    request_factory = APIRequestFactory(SERVER_NAME='example.org')
    request = request_factory.get('/')
    request.versioning_scheme = NamespaceVersioning()
    request.version = namespace
    url_path = reverse(
        url_name,
        kwargs=url_kwargs,
        urlconf=urlconf,
        request=request,
    )

    client = TestClient(urlconf)
    response = client.get(url_path)
    return response
    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 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 _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_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 test_indirect_relation_detail(self):
        resources = [
            Resource(
                "developer",
                "developers",
                DeveloperViewSet2,
                [
                    NestedResource(
                        "version", "versions", ProgrammingLanguageVersionViewSet, parent_field_lookup="language__author"
                    )
                ],
            )
        ]
        urlpatterns = make_urlpatterns_from_resources(resources)

        client = TestClient(urlpatterns)

        url_path = reverse(
            "version-detail",
            kwargs={"developer": self.developer1.pk, "version": self.programming_language_version.pk},
            urlconf=urlpatterns,
        )
        response = client.get(url_path)
        eq_(200, response.status_code)