在创建它之后,无法从DB中找到对象

问题描述 投票:0回答:2

我通过创建TestCase来测试用户创建,但在创建它之后就找不到了。

我试图通过调用.refresh_from_db()来刷新缓存,但它不起作用。

这是我的TestCase:

class SuperStrangeTest(TestCase):
    def test_super_strange(self):
        john = User.objects.create()
        john.refresh_from_db()

        print('!=====START' * 10)
        print(User.objects.count())
        print(User.objects.all())
        self.assertIsNotNone(User.objects.filter().first()) # None of assertions below would be right
        self.assertIsNotNone(User.objects.filter(id=john.id).first())
        self.assertTrue(User.objects.filter(id=john.id).exists())

我执行此测试的命令是:

./manage.py test --noinput --failfast --keepdb links.tests.SuperStrangeTest.test_super_strange

结果有时是正确的,但大多数时候它只是被打破了。

Using existing test database for alias 'default'...
/Users/oldcai/.virtualenvs/web/lib/python3.7/site-packages/grequests.py:21: MonkeyPatchWarning: Patching more than once will result in the union of all True parameters being patched
  curious_george.patch_all(thread=False, select=False)
System check identified no issues (0 silenced).
!=====START!=====START!=====START!=====START!=====START!=====START!=====START!=====START!=====START!=====START
1
<QuerySet [<User: >]>
F
======================================================================
FAIL: test_super_strange (links.tests.SuperStrangeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/oldcai/programs/python/webproject/zine/links/tests.py", line 41, in test_super_strange
    self.assertIsNotNone(User.objects.filter().first())
AssertionError: unexpectedly None

----------------------------------------------------------------------

Ran 1 test in 0.130s

FAILED (failures=1)
Preserving test database for alias 'default'...

其他行的错误:

======================================================================
FAIL: test_super_strange (links.tests.SuperStrangeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/oldcai/programs/python/webproject/links/tests.py", line 35, in test_super_strange
    self.assertTrue(User.objects.filter(id=john.id).exists())
AssertionError: False is not true

----------------------------------------------------------------------
======================================================================
FAIL: test_super_strange (links.tests.SuperStrangeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/oldcai/programs/python/webproject/links/tests.py", line 35, in test_super_strange
    self.assertTrue(User.objects.filter(id=john.id).exists())
AssertionError: False is not true

----------------------------------------------------------------------
django postgresql nose
2个回答
0
投票

尝试填写模型字段。它是Django用户模型的基础 - 它需要用户名和密码。另外要创建django用户 - 使用create_user函数,该函数将为您散列密码。尝试纠正你的代码:

...
john = User.objects.create_user(username='john', password='password')
...

0
投票

在更深入地诊断出这个问题之后,看来这个错误与我的DATABASE_ROUTERS设置有关。

我将操作的读取部分路由到生产中的随机只读从数据库,以平衡读取负载。

在TestCase中,我将slave数据库的配置设置为等于默认配置。

DATABASE = {
    'ENGINE': 'django.db.backends.postgresql',
    'ATOMIC_REQUESTS': False,
    'CONN_MAX_AGE': 0,
    'NAME': 'test',
    'USER': 'test',
    'PASSWORD': 'test',
    'HOST': '',
    'PORT': '',
}

DATABASES = {
    'default': DATABASE,
    'replica1': DATABASE,
}

但是在replica1数据库插入记录之后,它仍然无法用default查询结果。

当路由器随机选择default数据库作为要读取的数据库时,TestCase将通过,否则,它将失败。

© www.soinside.com 2019 - 2024. All rights reserved.