当前位置: 首页>>代码示例>>Python>>正文


Python test.APIClient方法代码示例

本文整理汇总了Python中rest_framework.test.APIClient方法的典型用法代码示例。如果您正苦于以下问题:Python test.APIClient方法的具体用法?Python test.APIClient怎么用?Python test.APIClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rest_framework.test的用法示例。


在下文中一共展示了test.APIClient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_object_viewset

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_object_viewset(self):
        """
        Tests the ObjectMutlipleModelAPIViewSet with the default settings
        """
        client = APIClient()
        response = client.get('/object/', format='api')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertEqual(len(response.data), 2)
        self.assertEqual(response.data, {
            'Play': [
                {'title': 'Romeo And Juliet', 'genre': 'Tragedy', 'year': 1597},
                {'title': "A Midsummer Night's Dream", 'genre': 'Comedy', 'year': 1600},
                {'title': 'Julius Caesar', 'genre': 'Tragedy', 'year': 1623},
                {'title': 'As You Like It', 'genre': 'Comedy', 'year': 1623},
            ],
            'Poem': [
                {'title': "Shall I compare thee to a summer's day?", 'style': 'Sonnet'},
                {'title': "As a decrepit father takes delight", 'style': 'Sonnet'}
            ]
        }) 
开发者ID:MattBroach,项目名称:DjangoRestMultipleModels,代码行数:23,代码来源:test_viewsets.py

示例2: test_flat_viewset

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_flat_viewset(self):
        """
        Tests the ObjectMutlipleModelAPIViewSet with the default settings
        """
        client = APIClient()
        response = client.get('/flat/', format='api')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertEqual(len(response.data), 6)
        self.assertEqual(response.data, [
            {'genre': 'Tragedy', 'title': 'Romeo And Juliet', 'year': 1597, 'type': 'Play'},
            {'genre': 'Comedy', 'title': 'A Midsummer Night\'s Dream', 'year': 1600, 'type': 'Play'},
            {'genre': 'Tragedy', 'title': 'Julius Caesar', 'year': 1623, 'type': 'Play'},
            {'genre': 'Comedy', 'title': 'As You Like It', 'year': 1623, 'type': 'Play'},
            {'title': "Shall I compare thee to a summer's day?", 'style': 'Sonnet', 'type': 'Poem'},
            {'title': "As a decrepit father takes delight", 'style': 'Sonnet', 'type': 'Poem'},
        ]) 
开发者ID:MattBroach,项目名称:DjangoRestMultipleModels,代码行数:19,代码来源:test_viewsets.py

示例3: test_html_renderer

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_html_renderer(self):
        """
        Testing bug in which results dict failed to be passed into template context
        """
        client = APIClient()
        response = client.get('/template', format='html')

        # test the data is formatted properly and shows up in the template
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('data', response.data)
        self.assertContains(response, "Tragedy")
        self.assertContains(response, "<html>")
        self.assertContains(response, "decrepit")

        # test that the JSONRenderer does NOT add the dictionary wrapper to the data
        response = client.get('/template?format=json')

        # test the data is formatted properly and shows up in the template
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertNotIn('data', response.data)
        self.assertNotIn('<html>', response) 
开发者ID:MattBroach,项目名称:DjangoRestMultipleModels,代码行数:23,代码来源:test_html_renderer.py

示例4: test_metric_map_values

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_metric_map_values(self):
        """
        Test contents of the metrics.

        Test that the COST_MODEL_METRIC_MAP constant is properly formatted and contains the required data.
        """
        url = reverse("metrics")
        client = APIClient()

        params = {"source_type": Provider.PROVIDER_OCP}
        url = url + "?" + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers).data["data"]
        self.assertEquals(len(COST_MODEL_METRIC_MAP), len(response))
        for metric in COST_MODEL_METRIC_MAP:
            self.assertIsNotNone(metric.get("source_type"))
            self.assertIsNotNone(metric.get("metric"))
            self.assertIsNotNone(metric.get("label_metric"))
            self.assertIsNotNone(metric.get("label_measurement_unit"))
            self.assertIsNotNone(metric.get("default_cost_type")) 
开发者ID:project-koku,项目名称:koku,代码行数:21,代码来源:tests_views.py

示例5: test_execute_query_ocp_aws_storage

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_execute_query_ocp_aws_storage(self):
        """Test that OCP on AWS Storage endpoint works."""
        url = reverse("reports-openshift-aws-storage")
        client = APIClient()
        response = client.get(url, **self.headers)

        expected_end_date = self.dh.today.date().strftime("%Y-%m-%d")
        expected_start_date = self.ten_days_ago.strftime("%Y-%m-%d")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        dates = sorted([item.get("date") for item in data.get("data")])
        self.assertEqual(dates[0], expected_start_date)
        self.assertEqual(dates[-1], expected_end_date)

        for item in data.get("data"):
            if item.get("values"):
                values = item.get("values")[0]
                self.assertTrue("usage" in values)
                self.assertTrue("cost" in values) 
开发者ID:project-koku,项目名称:koku,代码行数:21,代码来源:tests_views.py

示例6: test_execute_query_ocp_aws_storage_last_thirty_days

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_execute_query_ocp_aws_storage_last_thirty_days(self):
        """Test that OCP CPU endpoint works."""
        url = reverse("reports-openshift-aws-storage")
        client = APIClient()
        params = {"filter[time_scope_value]": "-30", "filter[time_scope_units]": "day", "filter[resolution]": "daily"}
        url = url + "?" + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers)

        expected_end_date = self.dh.today
        expected_start_date = self.dh.n_days_ago(expected_end_date, 29)
        expected_end_date = str(expected_end_date.date())
        expected_start_date = str(expected_start_date.date())
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        dates = sorted([item.get("date") for item in data.get("data")])
        self.assertEqual(dates[0], expected_start_date)
        self.assertEqual(dates[-1], expected_end_date)

        for item in data.get("data"):
            if item.get("values"):
                values = item.get("values")[0]
                self.assertTrue("usage" in values)
                self.assertTrue("cost" in values) 
开发者ID:project-koku,项目名称:koku,代码行数:25,代码来源:tests_views.py

示例7: test_execute_query_ocp_aws_storage_this_month

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_execute_query_ocp_aws_storage_this_month(self):
        """Test that data is returned for the full month."""
        url = reverse("reports-openshift-aws-storage")
        client = APIClient()
        params = {
            "filter[resolution]": "monthly",
            "filter[time_scope_value]": "-1",
            "filter[time_scope_units]": "month",
        }
        url = url + "?" + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers)

        expected_date = self.dh.today.strftime("%Y-%m")

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        dates = sorted([item.get("date") for item in data.get("data")])
        self.assertEqual(dates[0], expected_date)
        self.assertNotEqual(data.get("data")[0].get("values", []), [])
        values = data.get("data")[0].get("values")[0]
        self.assertTrue("usage" in values)
        self.assertTrue("cost" in values) 
开发者ID:project-koku,项目名称:koku,代码行数:24,代码来源:tests_views.py

示例8: test_execute_query_ocp_aws_storage_this_month_daily

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_execute_query_ocp_aws_storage_this_month_daily(self):
        """Test that data is returned for the full month."""
        url = reverse("reports-openshift-aws-storage")
        client = APIClient()
        params = {"filter[resolution]": "daily", "filter[time_scope_value]": "-1", "filter[time_scope_units]": "month"}
        url = url + "?" + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers)

        expected_start_date = self.dh.this_month_start.strftime("%Y-%m-%d")
        expected_end_date = self.dh.today.strftime("%Y-%m-%d")

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        dates = sorted([item.get("date") for item in data.get("data")])
        self.assertEqual(dates[0], expected_start_date)
        self.assertEqual(dates[-1], expected_end_date)

        for item in data.get("data"):
            if item.get("values"):
                values = item.get("values")[0]
                self.assertTrue("usage" in values)
                self.assertTrue("cost" in values) 
开发者ID:project-koku,项目名称:koku,代码行数:24,代码来源:tests_views.py

示例9: test_execute_query_ocp_aws_storage_last_month_daily

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_execute_query_ocp_aws_storage_last_month_daily(self):
        """Test that data is returned for the full month."""
        url = reverse("reports-openshift-aws-storage")
        client = APIClient()
        params = {"filter[resolution]": "daily", "filter[time_scope_value]": "-2", "filter[time_scope_units]": "month"}
        url = url + "?" + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers)

        expected_start_date = self.dh.last_month_start.strftime("%Y-%m-%d")
        expected_end_date = self.dh.last_month_end.strftime("%Y-%m-%d")

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        dates = sorted([item.get("date") for item in data.get("data")])
        self.assertEqual(dates[0], expected_start_date)
        self.assertEqual(dates[-1], expected_end_date)

        for item in data.get("data"):
            if item.get("values"):
                values = item.get("values")[0]
                self.assertTrue("usage" in values)
                self.assertTrue("cost" in values) 
开发者ID:project-koku,项目名称:koku,代码行数:24,代码来源:tests_views.py

示例10: test_execute_query_ocp_aws_storage_with_group_by_and_limit

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_execute_query_ocp_aws_storage_with_group_by_and_limit(self):
        """Test that data is grouped by and limited."""
        url = reverse("reports-openshift-aws-storage")
        client = APIClient()
        params = {
            "group_by[node]": "*",
            "filter[limit]": 1,
            "filter[resolution]": "monthly",
            "filter[time_scope_value]": "-2",
            "filter[time_scope_units]": "month",
        }
        url = url + "?" + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()
        data = data.get("data", [])
        for entry in data:
            other = entry.get("nodes", [])[-1:]
            self.assertNotEqual(other, [])
            self.assertIn("Other", other[0].get("node")) 
开发者ID:project-koku,项目名称:koku,代码行数:23,代码来源:tests_views.py

示例11: test_execute_query_ocp_aws_costs_group_by_project

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_execute_query_ocp_aws_costs_group_by_project(self):
        """Test that grouping by project filters data."""
        with tenant_context(self.tenant):
            # Force Django to do GROUP BY to get nodes
            projects = (
                OCPAWSCostLineItemDailySummary.objects.filter(usage_start__gte=self.ten_days_ago)
                .values(*["namespace"])
                .annotate(project_count=Count("namespace"))
                .all()
            )
            project_of_interest = projects[0].get("namespace")

        url = reverse("reports-openshift-aws-costs")
        client = APIClient()
        params = {"group_by[project]": project_of_interest}

        url = url + "?" + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()
        for entry in data.get("data", []):
            for project in entry.get("projects", []):
                self.assertEqual(project.get("project"), project_of_interest) 
开发者ID:project-koku,项目名称:koku,代码行数:26,代码来源:tests_views.py

示例12: test_execute_query_ocp_aws_instance_type

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_execute_query_ocp_aws_instance_type(self):
        """Test that the instance type API runs."""
        url = reverse("reports-openshift-aws-instance-type")
        client = APIClient()
        response = client.get(url, **self.headers)

        expected_end_date = self.dh.today.date().strftime("%Y-%m-%d")
        expected_start_date = self.ten_days_ago.strftime("%Y-%m-%d")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.json()
        dates = sorted([item.get("date") for item in data.get("data")])
        self.assertEqual(dates[0], expected_start_date)
        self.assertEqual(dates[-1], expected_end_date)

        for item in data.get("data"):
            if item.get("values"):
                values = item.get("values")[0]
                self.assertTrue("usage" in values)
                self.assertTrue("cost" in values)
                self.assertTrue("count" in values) 
开发者ID:project-koku,项目名称:koku,代码行数:22,代码来源:tests_views.py

示例13: test_execute_query_default_pagination

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_execute_query_default_pagination(self):
        """Test that the default pagination works."""
        url = reverse("reports-openshift-aws-instance-type")
        client = APIClient()
        params = {
            "filter[resolution]": "monthly",
            "filter[time_scope_value]": "-1",
            "filter[time_scope_units]": "month",
        }
        url = url + "?" + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        response_data = response.json()
        data = response_data.get("data", [])
        meta = response_data.get("meta", {})
        count = meta.get("count", 0)

        self.assertIn("total", meta)
        self.assertIn("filter", meta)
        self.assertIn("count", meta)

        self.assertEqual(len(data), count) 
开发者ID:project-koku,项目名称:koku,代码行数:25,代码来源:tests_views.py

示例14: test_order_by_tag_wo_group

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_order_by_tag_wo_group(self):
        """Test that order by tags without a group-by fails."""
        baseurl = reverse("reports-openshift-aws-instance-type")
        client = APIClient()

        tag_url = reverse("openshift-aws-tags")
        tag_url = tag_url + "?filter[time_scope_value]=-1&key_only=True"
        response = client.get(tag_url, **self.headers)
        tag_keys = response.data.get("data", [])

        for key in tag_keys:
            order_by_dict_key = f"order_by[tag:{key}]"
            params = {
                "filter[resolution]": "monthly",
                "filter[time_scope_value]": "-1",
                "filter[time_scope_units]": "month",
                order_by_dict_key: random.choice(["asc", "desc"]),
            }

            url = baseurl + "?" + urlencode(params, quote_via=quote_plus)
            response = client.get(url, **self.headers)
            self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) 
开发者ID:project-koku,项目名称:koku,代码行数:24,代码来源:tests_views.py

示例15: test_order_by_tag_w_wrong_group

# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIClient [as 别名]
def test_order_by_tag_w_wrong_group(self):
        """Test that order by tags with a non-matching group-by fails."""
        baseurl = reverse("reports-openshift-aws-instance-type")
        client = APIClient()

        tag_url = reverse("openshift-aws-tags")
        tag_url = tag_url + "?filter[time_scope_value]=-1&key_only=True"
        response = client.get(tag_url, **self.headers)
        tag_keys = response.data.get("data", [])

        for key in tag_keys:
            order_by_dict_key = f"order_by[tag:{key}]"
            params = {
                "filter[resolution]": "monthly",
                "filter[time_scope_value]": "-1",
                "filter[time_scope_units]": "month",
                order_by_dict_key: random.choice(["asc", "desc"]),
                "group_by[usage]": random.choice(["asc", "desc"]),
            }

            url = baseurl + "?" + urlencode(params, quote_via=quote_plus)
            response = client.get(url, **self.headers)
            self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) 
开发者ID:project-koku,项目名称:koku,代码行数:25,代码来源:tests_views.py


注:本文中的rest_framework.test.APIClient方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。