本文整理汇总了Python中rest_framework.test.APIRequestFactory方法的典型用法代码示例。如果您正苦于以下问题:Python test.APIRequestFactory方法的具体用法?Python test.APIRequestFactory怎么用?Python test.APIRequestFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.test
的用法示例。
在下文中一共展示了test.APIRequestFactory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_get(self, query_params, filter_args):
qp_msg = 'query_params={query_params}'
expected_data = Site.objects.filter(**filter_args)
request = APIRequestFactory().get(self.request_path + query_params)
request.user = self.staff_user
view = self.view_class.as_view({'get': 'list'})
response = view(request)
assert response.status_code == 200, qp_msg.format(query_params=query_params)
assert set(response.data.keys()) == set(
['count', 'next', 'previous', 'results'])
assert len(response.data['results']) == len(expected_data), qp_msg.format(
query_params=query_params)
results = response.data['results']
# Validate just the first object's structure
for field_name in self.expected_result_keys:
assert field_name in results[0]
# Validate the ids match up
expected_ids = expected_data.values_list('id', flat=True)
actual_ids = [o['id'] for o in results]
assert set(actual_ids) == set(expected_ids)
示例2: test_list_method
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_list_method(self, monkeypatch, course_test_data):
"""
We need to add pagination to the list method and add that to the test
"""
site = course_test_data['site']
users = course_test_data['users']
course_overview = course_test_data['course_overview']
if organizations_support_sites():
caller = UserFactory(is_staff=True)
map_users_to_org_site(caller=caller, site=site, users=users)
else:
caller = UserFactory(is_staff=True)
request = APIRequestFactory().get(self.base_request_path)
request.META['HTTP_HOST'] = site.domain
monkeypatch.setattr(django.contrib.sites.shortcuts,
'get_current_site',
lambda req: site)
force_authenticate(request, user=caller)
view = self.view_class.as_view({'get': 'list'})
response = view(request)
assert response.status_code == status.HTTP_200_OK
示例3: test_retrieve_method
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_retrieve_method(self, monkeypatch, course_test_data):
site = course_test_data['site']
users = course_test_data['users']
course_overview = course_test_data['course_overview']
if organizations_support_sites():
caller = UserFactory(is_staff=True)
map_users_to_org_site(caller=caller, site=site, users=users)
else:
caller = UserFactory(is_staff=True)
request_path = self.base_request_path
request = APIRequestFactory().get(request_path)
request.META['HTTP_HOST'] = site.domain
monkeypatch.setattr(django.contrib.sites.shortcuts,
'get_current_site',
lambda req: site)
force_authenticate(request, user=caller)
view = self.view_class.as_view({'get': 'retrieve'})
response = view(request, pk=str(course_overview.id))
assert response.status_code == status.HTTP_200_OK
示例4: test_retrieve_invalid_course_id
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_retrieve_invalid_course_id(self, monkeypatch, course_test_data,
invalid_course_id):
"""Tests that invalid course ids return '404 NOT FOUND'
"""
site = course_test_data['site']
users = course_test_data['users']
if organizations_support_sites():
caller = UserFactory(is_staff=True)
map_users_to_org_site(caller=caller, site=site, users=users)
else:
caller = UserFactory(is_staff=True)
request_path = self.base_request_path
request = APIRequestFactory().get(request_path)
request.META['HTTP_HOST'] = site.domain
monkeypatch.setattr(django.contrib.sites.shortcuts,
'get_current_site',
lambda req: site)
force_authenticate(request, user=caller)
view = self.view_class.as_view({'get': 'retrieve'})
response = view(request, pk=invalid_course_id)
assert response.status_code == status.HTTP_404_NOT_FOUND
示例5: test_course_completions
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_course_completions(self, monkeypatch, user_reg_test_data):
endpoint = 'course_completions'
request_method = 'get'
site = user_reg_test_data['site']
users = user_reg_test_data['users']
dates = user_reg_test_data['dates']
months_back = user_reg_test_data['months_back']
if organizations_support_sites():
caller = UserFactory(is_staff=True)
map_users_to_org_site(caller=caller, site=site, users=users)
else:
caller = UserFactory(is_staff=True)
request = APIRequestFactory().get(self.request_path)
request.META['HTTP_HOST'] = site.domain
monkeypatch.setattr(django.contrib.sites.shortcuts,
'get_current_site',
lambda req: site)
force_authenticate(request, user=caller)
view = self.view_class.as_view({request_method: endpoint})
response = view(request)
assert self.check_response(response=response, endpoint=endpoint)
示例6: test_course_enrollments
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_course_enrollments(self, monkeypatch, user_reg_test_data):
endpoint = 'course_enrollments'
request_method = 'get'
site = user_reg_test_data['site']
users = user_reg_test_data['users']
dates = user_reg_test_data['dates']
months_back = user_reg_test_data['months_back']
if organizations_support_sites():
caller = UserFactory(is_staff=True)
map_users_to_org_site(caller=caller, site=site, users=users)
else:
caller = UserFactory(is_staff=True)
request = APIRequestFactory().get(self.request_path)
request.META['HTTP_HOST'] = site.domain
monkeypatch.setattr(django.contrib.sites.shortcuts,
'get_current_site',
lambda req: site)
force_authenticate(request, user=caller)
view = self.view_class.as_view({request_method: endpoint})
response = view(request)
assert self.check_response(response=response, endpoint=endpoint)
示例7: test_site_courses
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_site_courses(self, monkeypatch, user_reg_test_data):
endpoint = 'site_courses'
request_method = 'get'
site = user_reg_test_data['site']
users = user_reg_test_data['users']
dates = user_reg_test_data['dates']
months_back = user_reg_test_data['months_back']
if organizations_support_sites():
caller = UserFactory(is_staff=True)
map_users_to_org_site(caller=caller, site=site, users=users)
else:
caller = UserFactory(is_staff=True)
request = APIRequestFactory().get(self.request_path)
request.META['HTTP_HOST'] = site.domain
monkeypatch.setattr(django.contrib.sites.shortcuts,
'get_current_site',
lambda req: site)
force_authenticate(request, user=caller)
view = self.view_class.as_view({request_method: endpoint})
response = view(request)
assert self.check_response(response=response, endpoint=endpoint)
示例8: test_active_users
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_active_users(self, monkeypatch, user_reg_test_data):
endpoint = 'active_users'
request_method = 'get'
site = user_reg_test_data['site']
if organizations_support_sites():
caller = UserFactory(is_staff=True)
map_users_to_org_site(caller=caller, site=site, users=[])
else:
caller = UserFactory(is_staff=True)
expected_response = 'active_users history metric data'
monkeypatch.setattr('figures.views.metrics.get_site_mau_history_metrics',
lambda **_kwargs: expected_response)
request = APIRequestFactory().get(self.request_path)
request.META['HTTP_HOST'] = site.domain
monkeypatch.setattr(django.contrib.sites.shortcuts,
'get_current_site',
lambda req: site)
force_authenticate(request, user=caller)
view = self.view_class.as_view({request_method: endpoint})
response = view(request)
assert response.data['active_users'] == expected_response
示例9: test_list
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_list(self, monkeypatch, sm_test_data):
site = sm_test_data['site']
org = sm_test_data['organization']
if organizations_support_sites():
caller = UserFactory()
UserOrganizationMappingFactory(user=caller,
organization=org,
is_amc_admin=True)
else:
caller = UserFactory(is_staff=True)
request = APIRequestFactory().get(self.request_path)
request.META['HTTP_HOST'] = site.domain
monkeypatch.setattr(django.contrib.sites.shortcuts,
'get_current_site',
lambda req: site)
force_authenticate(request, user=caller)
view = self.view_class.as_view({'get': 'list'})
response = view(request)
assert response.status_code == status.HTTP_200_OK
示例10: test_site_metrics_list
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_site_metrics_list(self, monkeypatch, sm_test_data):
site = sm_test_data['site']
org = sm_test_data['organization']
if organizations_support_sites():
caller = UserFactory()
UserOrganizationMappingFactory(user=caller,
organization=org,
is_amc_admin=True)
else:
caller = UserFactory(is_staff=True)
request = APIRequestFactory().get(self.request_path)
request.META['HTTP_HOST'] = site.domain
monkeypatch.setattr(django.contrib.sites.shortcuts,
'get_current_site',
lambda req: site)
force_authenticate(request, user=caller)
view = self.view_class.as_view({'get': 'list'})
response = view(request)
assert response.status_code == status.HTTP_200_OK
示例11: test_multiple_user_orgs
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_multiple_user_orgs(self, monkeypatch, settings, username, allow):
"""
We updated `figures.permissions` so that a user can belong to multiple
organizations
"""
def test_site(request):
return self.site
request = APIRequestFactory().get('/')
request.META['HTTP_HOST'] = self.site.domain
request.user = get_user_model().objects.get(username=username)
monkeypatch.setattr(django.contrib.sites.shortcuts, 'get_current_site', test_site)
settings.FEATURES['FIGURES_IS_MULTISITE'] = True
assert figures.helpers.is_multisite()
org2 = OrganizationFactory(sites=[self.site])
UserOrganizationMappingFactory(user=request.user, organization=org2)
permission = figures.permissions.IsSiteAdminUser().has_permission(request, None)
assert permission == allow, 'User "{username}" should have access'.format(
username=username)
示例12: test_browsable_renderer_put_render
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_browsable_renderer_put_render(input_put_view, decorator):
"""
Test, that PUT method works with BrowsableAPIRenderer
This was not working in the past, because of `_get_serializer`
didn't allow `instance parameter.
"""
data = {'blah': 'blah'}
method = 'PUT'
request = rest_request.Request(APIRequestFactory().get('blah'))
output_view = decorator(input_put_view)
wrapper_cls = _get_view_class(output_view)
test_view_instance = wrapper_cls()
renderer = renderers.BrowsableAPIRenderer()
renderer.accepted_media_type = None
renderer.renderer_context = {}
response = renderer.get_raw_data_form(
data, test_view_instance, method, request,
)
assert response.data == {}
示例13: test_model_mismatch
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_model_mismatch(self):
""" cannot apply filters for one model to queryset from another """
class FooDoc(Document):
foo = fields.StringField()
class BarDoc(Document):
bar = fields.StringField()
class FooFilter(ModelFilterset):
class Meta:
model = FooDoc
class BarView(ListAPIView):
filter_backends = (MongoFilterBackend,)
filter_class = FooFilter
serializer_class = mock.Mock()
queryset = BarDoc.objects
with self.assertRaises(TypeError):
BarView.as_view()(APIRequestFactory().get("/?foo=Foo"))
示例14: test_model_subclassed
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def test_model_subclassed(self):
""" can apply filters for base model to queryset of derived """
class FooDoc(Document):
meta = { 'allow_inheritance': True}
foo = fields.StringField()
class BarDoc(FooDoc):
bar = fields.StringField()
class FooFilter(ModelFilterset):
class Meta:
model = FooDoc
class BarView(ListAPIView):
filter_backends = (MongoFilterBackend,)
filter_class = FooFilter
serializer_class = mock.Mock()
queryset = BarDoc.objects
BarView.as_view()(APIRequestFactory().get("/?foo=Foo"))
示例15: setUp
# 需要导入模块: from rest_framework import test [as 别名]
# 或者: from rest_framework.test import APIRequestFactory [as 别名]
def setUp(self):
super().setUp()
self.process = Process.objects.create(
slug="test-process", contributor=self.contributor,
)
assign_perm("view_process", self.user, self.process)
self.descriptor_schema1 = DescriptorSchema.objects.create(
slug="test-schema", contributor=self.contributor, version="1.0.0",
)
assign_perm("view_descriptorschema", self.user, self.descriptor_schema1)
self.descriptor_schema2 = DescriptorSchema.objects.create(
slug="test-schema", contributor=self.contributor, version="2.0.0",
)
assign_perm("view_descriptorschema", self.user, self.descriptor_schema2)
self.descriptor_schema3 = DescriptorSchema.objects.create(
slug="test-schema", contributor=self.contributor, version="3.0.0",
)
self.factory = APIRequestFactory()