我在测试什么
我试图在我的models.py
内对annotate_active
查询集进行单元测试
class ServerQuerySet(QuerySet):
def annotate_active(self, start=None, end=None):
return self.annotate(
active=Case(
When(fqdn__in=self.get_active_fqdns(), then=True),
default=False,
output_field=BooleanField()
)
)
class Server(models.Model):
...
objects = ServerQuerySet.as_manager()
工作原理
{cd3>返回 当我在django shell中执行查询时,一切正常: 测试失败 我在 但是,当我运行测试时: 当它执行查询时,我得到一个AttributeError: 值得注意: 在我的环境中似乎存在某种冲突,在运行django的条件 你知道怎么解决这个错误吗?在test_server.py
中有以下测试用例(我希望这个测试在断言时失败)from django.test import TestCase
from reports.models import Server
class TestServerQuerySet(TestCase):
def test_annotate_active(self):
result = Server.objects.annotate_active().values_list('fqdn', 'active')
print(result)
self.assertTrue(False)
python manage.py test reports.tests.test_server --settings=piesup2.settings.test
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_annotate_active (reports.tests.test_server.TestServerQuerySet)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/jwe/Documents/piesup2/reports/tests/test_server.py", line 9, in test_annotate_active
print(result)
File "/home/jwe/Documents/piesup2/venv/lib/python3.6/site-packages/django/db/models/query.py", line 232, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/jwe/Documents/piesup2/venv/lib/python3.6/site-packages/django/db/models/query.py", line 256, in __iter__
self._fetch_all()
File "/home/jwe/Documents/piesup2/venv/lib/python3.6/site-packages/django/db/models/query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/jwe/Documents/piesup2/venv/lib/python3.6/site-packages/django/db/models/query.py", line 141, in __iter__
for row in compiler.results_iter():
File "/home/jwe/Documents/piesup2/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 790, in results_iter
fields = [s[0] for s in self.select[0:self.col_count]]
AttributeError: 'SQLCompiler' object has no attribute 'col_count'
----------------------------------------------------------------------
Ran 1 test in 0.010s
FAILED (errors=1)
Destroying test database for alias 'default'...
settings/test.py
文件。在Case
查询时,测试会中断。在
)但如果有问题的话
这是在1.9.1中修复的Django错误。在
https://code.djangoproject.com/ticket/25894
相关问题 更多 >
编程相关推荐