Django测试匹配查询不存在



我是Django的新手。我正在构建一个应用程序,需要对用户访问各种用途和功能进行大量控制。

为此,我创建了一个名为"UserType"的模型,其中有一些用户组作为对象,即扫描仪,守望先锋,枪骑兵和管理员。

另一个模型AssignedType将用户分配给一个或多个UserType。它有两个字段。'user'与user模型相关,'user_type'与UserType模型相关。

控制访问权限的文件(rights.py)如下:

from .models import User, UserType, AssignedType 
# Get all user types in the database as model objects
scanner = UserType.objects.get(id=1)
overwatch = UserType.objects.get(id=2)
lancer = UserType.objects.get(id=3)
admin = UserType.objects.get(id=4)
# Rights of each type of user. Keys are the views and values are the user types allowed to access a particular view
group_rights = {
'scanner':[scanner, admin],
}
# Function to determine whether the logged user may access a view. Returns True if allowed. False otherwise.
def is_user_permitted(logged_user, view):

# Get a list of users types assigned to the user
assigned_types_objects = AssignedType.objects.all().filter(user=logged_user)
assigned_types = []
for assigned_type in assigned_types_objects:
assigned_types.append(assigned_type.user_type)

# Get a list of users types permitted to access the view.
view_rights = group_rights[view]
# Check whether there is any user type is common in the two lists. If so, user is authorized to access the view. 
if set(assigned_types) & set(view_rights):
return True
else:
return False

调用上述文件中函数的视图如下:

@login_required
def scanner(request):
if not is_user_permitted(request.user, 'scanner'):
raise PermissionDenied
return render(request, "DataMech/scanner.html")

一切似乎都很好。但是,当我尝试创建和运行测试时,我收到了一个错误。下面是我的test .py文件。

from django.test import TestCase, Client
from django.urls import reverse
from DataMech.models import User, UserType, AssignedType 
class TestScanner(TestCase):
def setUp(self):
User.objects.create(username='scanner1', password='abc')
User.objects.create(username='scanner2', password='abc')
User.objects.create(username='overwatch1', password='abc')
User.objects.create(username='overwatch2', password='abc')
User.objects.create(username='lancer1', password='abc')
User.objects.create(username='lancer2', password='abc')
User.objects.create(username='lancer3', password='abc')
User.objects.create(username='lancer4', password='abc')
User.objects.create(username='admin1', password='abc')
UserType.objects.create(name='scanner')
UserType.objects.create(name='overwatch')
UserType.objects.create(name='lancer')
UserType.objects.create(name='admin')
AssignedType.objects.create(user='scanner1', user_type='scanner')
AssignedType.objects.create(user='scanner2', user_type='scanner')
AssignedType.objects.create(user='overwatch1', user_type='overwatch')
AssignedType.objects.create(user='overwatch2', user_type='overwatch')
AssignedType.objects.create(user='lancer1', user_type='lancer')
AssignedType.objects.create(user='lancer2', user_type='lancer')
AssignedType.objects.create(user='lancer3', user_type='lancer')
AssignedType.objects.create(user='lancer4', user_type='lancer')
AssignedType.objects.create(user='admin1', user_type='admin')
def test_allow_access_only_if_user_is_authorized_GET(self):
c = Client()
c.login(username='scanner1', password='abc')
response = c.get(reverse('scanner'))
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'DataMech/scanner.html')
c.logout()
c = Client()
c.login(username='scanner2', password='abc')
response = c.get(reverse('scanner'))
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'DataMech/scanner.html')
c.logout()
c = Client()
c.login(username='overwatch1', password='abc')
response = c.get(reverse('scanner'))
self.assertEquals(response.status_code, 403)
c.logout()

运行此测试将导致以下错误。

Traceback (most recent call last):
File "C:UsersDellDesktopFinancial reports projectFinDataFinDatamanage.py", line 22, in <module>
main()
File "C:UsersDellDesktopFinancial reports projectFinDataFinDatamanage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:Python39libsite-packagesdjangocoremanagement__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:Python39libsite-packagesdjangocoremanagement__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:Python39libsite-packagesdjangocoremanagementcommandstest.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "C:Python39libsite-packagesdjangocoremanagementbase.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:Python39libsite-packagesdjangocoremanagementbase.py", line 371, in execute
output = self.handle(*args, **options)
File "C:Python39libsite-packagesdjangocoremanagementcommandstest.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "C:Python39libsite-packagesdjangotestrunner.py", line 698, in run_tests
self.run_checks(databases)
File "C:Python39libsite-packagesdjangotestrunner.py", line 636, in run_checks
call_command('check', verbosity=self.verbosity, databases=databases)
File "C:Python39libsite-packagesdjangocoremanagement__init__.py", line 168, in call_command
return command.execute(*args, **defaults)
File "C:Python39libsite-packagesdjangocoremanagementbase.py", line 371, in execute
output = self.handle(*args, **options)
File "C:Python39libsite-packagesdjangocoremanagementcommandscheck.py", line 63, in handle
self.check(
File "C:Python39libsite-packagesdjangocoremanagementbase.py", line 392, in check
all_issues = checks.run_checks(
File "C:Python39libsite-packagesdjangocorechecksregistry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:Python39libsite-packagesdjangocorechecksurls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:Python39libsite-packagesdjangocorechecksurls.py", line 23, in check_resolver
return check_method()
File "C:Python39libsite-packagesdjangourlsresolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:Python39libsite-packagesdjangoutilsfunctional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:Python39libsite-packagesdjangourlsresolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:Python39libsite-packagesdjangoutilsfunctional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:Python39libsite-packagesdjangourlsresolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:Python39libimportlib__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:UsersDellDesktopFinancial reports projectFinDataFinDataFinDataurls.py", line 21, in <module>
path("", include("DataMech.urls")),
File "C:Python39libsite-packagesdjangourlsconf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:Python39libimportlib__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:UsersDellDesktopFinancial reports projectFinDataFinDataDataMechurls.py", line 4, in <module>
from . import views
File "C:UsersDellDesktopFinancial reports projectFinDataFinDataDataMechviews.py", line 12, in <module>
from .rights import is_user_permitted
File "C:UsersDellDesktopFinancial reports projectFinDataFinDataDataMechrights.py", line 4, in <module>
scanner = UserType.objects.get(id=1)
File "C:Python39libsite-packagesdjangodbmodelsmanager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:Python39libsite-packagesdjangodbmodelsquery.py", line 429, in get
raise self.model.DoesNotExist(
DataMech.models.DoesNotExist: UserType matching query does not exist.

我在这里做错了什么?

在您的视图中导入rights.py文件。当测试开始时,没有创建UserType实例。当Django试图设置以便它可以开始测试时,它会因为这个事实而得到这个错误。您应该使用get_or_create方法来确保创建对象:

scanner, created = UserType.objects.get_or_create(name='scanner')

以此类推。您还应该为此调整您的测试,不要在那里创建对象或使用此方法。

相关内容

  • 没有找到相关文章

最新更新