我正在测试
我试图对annotate_active
内的models.py
查询集进行单元测试,
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()
工作原理
get_active_fqdns
只返回一组fqdn值{'va1', 'val2', ...}
当我在django shell中执行查询时,一切正常:
~/Documents/piesup2 master* ⇡ 1m 15s
venv ❯ python manage.py shell --settings=piesup2.settings.test
>>> from reports.models import Server
>>> Server.objects.annotate_active().values_list('fqdn', 'active')
<QuerySet [('101.104.243.11', False), ('101.203.185.232', False), ('103.110.68.247', False), ('103.201.105.197', False), ('103.47.83.167', False), ('1.119.79.204', False), ('1.152.209.127', False), ('120.14.125.221', False), ('120.14.95.12', False), ('127.239.228.52', False), ('127.94.245.121', False), ('133.107.3.133', False), ('143.65.89.141', False), ('151.161.235.135', False), ('162.54.208.170', False), ('170.252.228.176', False), ('170.55.42.194', False), ('171.210.89.116', False), ('175.166.243.88', False), ('175.62.108.95', False), '...(remaining elements truncated)...']>
测试失败
我在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
我在执行查询时遇到AttributeError:
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
文件。在我的环境中似乎存在某种冲突,因此在运行django的条件Case
查询时我的测试会中断。
知道如何解决此错误吗?
答案 0 :(得分:1)