Django数据库:测试数据库的问题



我正在学习Django测试。我写了一个带有简单模型的简单应用程序,并想运行测试来检查模型方法的有效性,但在运行测试时收到了一条错误消息:

这是型号.py

from django.db import models

class Trip(models.Model):
origin = models.CharField(max_length=20)
destination = models.CharField(max_length=20)
def __str__(self):
return self.origin
def is_valid(self):
return self.origin != self.destination

这是test.py

from django.test import TestCase
from .models import Trip

# Create your tests here.
class TripModelTests(TestCase):
def test_trip(self):
a = Trip.objects.create(origin='a', destination='a')
self.assertIs(a.is_valid(), True)

这是设置.py

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd57r9kcrhthdc7',
'USER': 'sdqxaruartlvrd',
'PASSWORD': 'e7b8f85611596ed125fe3ed4ea590f821f65e317c17ee7871be75b8130d72378',
'HOST': 'ec2-3-214-46-194.compute-1.amazonaws.com',
'PORT': '5432',
'TEST': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
}

这是我运行python manage.py测试传输时收到的错误消息

Creating test database for alias 'default'...
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocoremanagement__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocoremanagement__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocoremanagementcommandstest.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocoremanagementbase.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocoremanagementbase.py", line 371, in execute
output = self.handle(*args, **options)
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocoremanagementcommandstest.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangotestrunner.py", line 695, in run_tests
old_config = self.setup_databases(aliases=databases)
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangotestrunner.py", line 614, in setup_databases
return _setup_databases(
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangotestutils.py", line 170, in setup_databases
connection.creation.create_test_db(
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangodbbackendsbasecreation.py", line 55, in create_test_db
self._create_test_db(verbosity, autoclobber, keepdb)
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangodbbackendsbasecreation.py", line 172, in _create_test_db
'dbname': self.connection.ops.quote_name(test_database_name),
File "C:UsersfabiaAppDataLocalProgramsPythonPython38-32libsite-packagesdjangodbbackendspostgresqloperations.py", line 113, in quote_name
if name.startswith('"') and name.endswith('"'):
AttributeError: 'WindowsPath' object has no attribute 'startswith'

如果我只使用默认的django设置并使用sqlite数据库,测试会很好。。。。

错误可能是由于BASE_DIR路径造成的,在settings.py中,您需要删除斜线/并将其切换到以下

'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

您可能需要根据您的BASE_DIR内容验证它是否指向正确的位置。调试的一种方法是在数据库dictionary之后设置ipdb(),因此一旦使用python manage.py runserver,就可以轻松地检查DATABASES结构。

import ipdb; ipdb.set_trace()

来源:https://pypi.org/project/ipdb/