本文整理汇总了Python中django.test方法的典型用法代码示例。如果您正苦于以下问题:Python django.test方法的具体用法?Python django.test怎么用?Python django.test使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django
的用法示例。
在下文中一共展示了django.test方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_check_custom_user_model_default_admin
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_check_custom_user_model_default_admin(self):
# Django doesn't re-register admins when using `override_settings`,
# so we have to do it manually in this test case.
admin.site.register(get_user_model(), UserAdmin)
warnings = checks.check_custom_user_model(HijackAdminConfig)
expected_warnings = [
Warning(
'django-hijack-admin does not work out the box with a custom user model.',
hint='Please mix HijackUserAdminMixin into your custom UserAdmin.',
obj=settings.AUTH_USER_MODEL,
id='hijack_admin.W001',
)
]
self.assertEqual(warnings, expected_warnings)
admin.site.unregister(get_user_model())
示例2: client
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def client():
"""Django Test Client, with some convenient overriden methods.
"""
from django.test import Client
class _Client(Client):
@property
def json(self):
"""Add json method on the client for sending json type request.
Usages:
>>> import json
>>> url = reverse("phone-verify")
>>> client.json.get(url)
>>> client.json.post(url, data=json.dumps(payload))
"""
return PartialMethodCaller(
obj=self, content_type='application/json;charset="utf-8"'
)
return _Client()
示例3: test_cookie_samesite_none_force_all
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_cookie_samesite_none_force_all(self):
with self.settings(SESSION_COOKIE_SAMESITE='None', SESSION_COOKIE_SAMESITE_FORCE_ALL=True):
response = self.client.get('/cookies-test/')
self.assertEqual(response.cookies['sessionid']['samesite'], 'None')
self.assertEqual(response.cookies['csrftoken']['samesite'], 'None')
self.assertEqual(response.cookies['custom_cookie']['samesite'], 'None')
self.assertEqual(response.cookies['zcustom_cookie']['samesite'], 'None')
cookies_string = sorted(response.cookies.output().split('\r\n'))
self.assertTrue('custom_cookie=' in cookies_string[1])
self.assertTrue('; SameSite=None' in cookies_string[1])
self.assertTrue('csrftoken=' in cookies_string[0])
self.assertTrue('; SameSite=None' in cookies_string[0])
self.assertTrue('sessionid=' in cookies_string[2])
self.assertTrue('; SameSite=None' in cookies_string[2])
self.assertTrue('zcustom_cookie=' in cookies_string[3])
self.assertTrue('; SameSite=None' in cookies_string[3])
示例4: test_cookie_samesite_django30
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_cookie_samesite_django30(self):
# Raise DeprecationWarning for newer versions of Django
with patch('django.get_version', return_value=DJANGO_SUPPORTED_VERSION):
with self.assertRaises(DeprecationWarning) as exc:
self.client.get('/cookies-test/')
self.assertEqual(exc.exception.args[0], (
'Your version of Django supports SameSite flag in the cookies mechanism. '
'You should remove django-cookies-samesite from your project.'
))
with patch('django_cookies_samesite.middleware.django.get_version', return_value=DJANGO_SUPPORTED_VERSION):
with self.assertRaises(DeprecationWarning) as exc:
self.client.get('/cookies-test/')
self.assertEqual(exc.exception.args[0], (
'Your version of Django supports SameSite flag in the cookies mechanism. '
'You should remove django-cookies-samesite from your project.'
))
示例5: test_cookie_names_changed
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_cookie_names_changed(self):
session_name = 'sessionid-test'
csrf_name = 'csrftoken-test'
with self.settings(
SESSION_COOKIE_NAME=session_name,
CSRF_COOKIE_NAME=csrf_name,
SESSION_COOKIE_SAMESITE='Lax'
):
response = self.client.get('/cookies-test/')
self.assertEqual(response.cookies[session_name]['samesite'], 'Lax')
self.assertEqual(response.cookies[csrf_name]['samesite'], 'Lax')
cookies_string = sorted(response.cookies.output().split('\r\n'))
self.assertTrue(csrf_name + '=' in cookies_string[0])
self.assertTrue('; SameSite=Lax' in cookies_string[0])
self.assertTrue(session_name + '=' in cookies_string[2])
self.assertTrue('; SameSite=Lax' in cookies_string[2])
示例6: test_unsupported_browsers
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_unsupported_browsers(self, ua_string):
session_name = 'sessionid-test'
csrf_name = 'csrftoken-test'
with self.settings(
SESSION_COOKIE_NAME=session_name,
CSRF_COOKIE_NAME=csrf_name,
SESSION_COOKIE_SAMESITE='Lax'
):
response = self.client.get(
'/cookies-test/',
HTTP_USER_AGENT=ua_string,
)
self.assertEqual(response.cookies[session_name]['samesite'], '')
self.assertEqual(response.cookies[csrf_name]['samesite'], '')
cookies_string = sorted(response.cookies.output().split('\r\n'))
self.assertTrue('; SameSite=Lax' not in cookies_string[0])
self.assertTrue('; SameSite=Lax' not in cookies_string[1])
示例7: test_supported_browsers
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_supported_browsers(self, ua_string):
session_name = 'sessionid-test'
csrf_name = 'csrftoken-test'
with self.settings(
SESSION_COOKIE_NAME=session_name,
CSRF_COOKIE_NAME=csrf_name,
SESSION_COOKIE_SAMESITE='Lax'
):
response = self.client.get(
'/cookies-test/',
HTTP_USER_AGENT=ua_string,
)
self.assertEqual(response.cookies[session_name]['samesite'], 'Lax')
self.assertEqual(response.cookies[csrf_name]['samesite'], 'Lax')
cookies_string = sorted(response.cookies.output().split('\r\n'))
self.assertTrue('; SameSite=Lax' in cookies_string[0])
self.assertTrue('; SameSite=Lax' in cookies_string[2])
示例8: test_path_conflict
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_path_conflict(self):
""" Check the crazy scenario where an existing metadata object has the same path. """
old_path = self.product_metadata._path
self.product_metadata._path = '/products/2/'
self.product_metadata.save()
self.assertEqual(self.product_metadata._object_id, self.product.pk)
# Create a new product that will take the same path
new_product = Product.objects.create()
Coverage._meta.get_model('modelinstance').objects.filter(_content_type=self.product_content_type,
_object_id=new_product.id).update(title="New Title")
# This test will not work if we have the id wrong
if new_product.id != 2:
raise Exception("Test Error: the product ID is not as expected, this test cannot work.")
# Check that the existing path was corrected
product_metadata = Coverage._meta.get_model('modelinstance').objects.get(id=self.product_metadata.id)
self.assertEqual(old_path, product_metadata._path)
# Check the new data is available under the correct path
metadata = get_metadata(path="/products/2/")
self.assertEqual(metadata.title.value, u"New Title")
示例9: test_create_error_model
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_create_error_model(self):
"""Validate that a complete Error model is created using JobError
"""
job_type_name = 'test-job'
name = 'bad-data'
title = 'Bad Data'
description = 'Error received when bad data is detected'
category = 'DATA'
error = JobError(job_type_name, name, title, description, category)
model = error.create_model()
self.assertEqual(model.name, name)
self.assertEqual(model.title, title)
self.assertEqual(model.description, description)
self.assertEqual(model.category, category)
self.assertEqual(model.job_type_name, job_type_name)
示例10: test_save_models
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_save_models(self):
"""Tests calling JobErrorMapping.save_models() successfully"""
job_type_name = 'test-job'
mapping = JobErrorMapping(job_type_name)
error_1 = JobError(job_type_name, 'mapped_error_1', title='Title', description='Description',
category='ALGORITHM')
error_2 = JobError(job_type_name, 'mapped_error_2', category='DATA')
mapping.add_mapping(1, error_1)
mapping.add_mapping(2, error_2)
# Make sure error models are created successfully
mapping.save_models()
self.assertEqual(Error.objects.filter(job_type_name=job_type_name).count(), 2)
# Make some changes
error_1.description = 'New description'
error_2.category = 'ALGORITHM'
# Make sure error models are updated successfully
mapping.save_models()
self.assertEqual(Error.objects.get(name='mapped_error_1').description, 'New description')
self.assertEqual(Error.objects.get(name='mapped_error_2').category, 'ALGORITHM')
示例11: test_scale_post_steps_successful
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_scale_post_steps_successful(self, mock_env_vars, mock_job_exe_manager):
"""Tests successfully executing scale_post_steps."""
# Set up mocks
def get_env_vars(name, *args, **kwargs):
return str(self.job.id) if name == 'SCALE_JOB_ID' else str(self.job_exe.exe_num)
mock_env_vars.side_effect = get_env_vars
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.job_type.get_job_interface.return_value.perform_post_steps.return_value = RESULTS
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.id = self.job_exe.id
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.job_id = self.job_exe.job_id
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.job_type_id = self.job_exe.job_type_id
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.exe_num = self.job_exe.exe_num
# Call method to test
cmd = PostCommand()
cmd.run_from_argv(['manage.py', 'scale_post_steps'])
# Check results
job_exe_output = JobExecutionOutput.objects.get(job_exe_id=self.job_exe.id)
self.assertDictEqual(job_exe_output.get_output().get_dict(), JOB_RESULTS.get_dict())
示例12: test_scale_post_steps_no_stderr
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_scale_post_steps_no_stderr(self, mock_env_vars, mock_job_exe_manager):
"""Tests successfully executing scale_post_steps."""
# Set up mocks
def get_env_vars(name, *args, **kwargs):
return str(self.job.id) if name == 'SCALE_JOB_ID' else str(self.job_exe.exe_num)
mock_env_vars.side_effect = get_env_vars
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.stdout = 'something'
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.stderr = None
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.job_type.get_job_interface.return_value.perform_post_steps.return_value = RESULTS
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.id = self.job_exe.id
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.job_id = self.job_exe.job_id
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.job_type_id = self.job_exe.job_type_id
mock_job_exe_manager.get_job_exe_with_job_and_job_type.return_value.exe_num = self.job_exe.exe_num
# Call method to test
cmd = PostCommand()
cmd.run_from_argv(['manage.py', 'scale_post_steps'])
# Check results
job_exe_output = JobExecutionOutput.objects.get(job_exe_id= self.job_exe.id)
self.assertDictEqual(job_exe_output.get_output().get_dict(), JOB_RESULTS.get_dict())
示例13: test_input_data_names_must_be_unique
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def test_input_data_names_must_be_unique(self):
definition = {
'command': 'test-command',
'command_arguments': '${param-1}',
'version': '1.0',
'input_data': [{
'name': 'param-1',
'type': 'file',
}, {
'name': 'param-1',
'type': 'property',
}]
}
try:
JobInterface(definition)
self.fail('Expected invalid job definition to throw an exception')
except InvalidInterfaceDefinition:
pass
示例14: setUp
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def setUp(self):
"""
Remove migration file generated by test if there is any missing.
"""
clean_migrations()
示例15: tearDown
# 需要导入模块: import django [as 别名]
# 或者: from django import test [as 别名]
def tearDown(self):
"""
Remove migration file generated by test if there is any missing.
"""
clean_migrations()