Pytest-Django不特别支持Django的multi-db,并且失败,不允许数据库访问

时间:2019-10-03 06:17:51

标签: python django pytest pytest-django

我试图使用非托管(只读)模型通过多数据库设置运行测试。当我进行测试时,serailizers.py中的导入功能失败,并且无法访问测试数据库。

settings.py

 if 'test' in sys.argv or 'test_coverage' in sys.argv: 
DATABASES = {                                                       
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'db.sqlite3',
    },
    'test_db': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'db.sqlite3',
    }
}
INSTALLED_APPS += ['test']

pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE=apps.settings

models.py

from django.db import models

class TestModelA(models.Model):
    testid = models.CharField(max_length=200)
    class Meta:
        managed = False
        db_table = 'TestD'

serializers.py

from rest_framework import serializers

from apps.services import get_testid

class SearchSerializer(serializers.Serializer):

application_names = serializers.MultipleChoiceField(
    choices=get_testid(),
    required=False, style={'base_template': 'select_multiple.html'}
)

services.py

from apps.models import TestModelA

def get_testid():
    return TestModelA.objects.values_list('testid ', flat=True)

tests.py

import pytest

from django.test import RequestFactory, TestCase

from appsviews import (
    details
)

class MyTestCase(TestCase):

    def setUp(self):
        self.request = RequestFactory().get('/details/')

    @pytest.mark.django_db
    def test_my_function(self):
        response = details(self.request)
        self.assertEqual(response.status_code, 200)

错误

apps/tests/tests.py:4: in <module>
from apps.views import (
<frozen importlib._bootstrap>:971: in _find_and_load???
<frozen importlib._bootstrap>:955: in _find_and_load_unlocked ???
<frozen importlib._bootstrap>:665: in _load_unlocked ???
/tmp/lib/python3.6/site-packages/_pytest/assertion/rewrite.py:149: in exec_module exec(co, module.__dict__)
apps/views.py:6: in <module>
from apps.serializers import (
<frozen importlib._bootstrap>:971: in _find_and_load ???
<frozen importlib._bootstrap>:955: in _find_and_load_unlocked ???
<frozen importlib._bootstrap>:665: in _load_unlocked ???
/tmp/lib/python3.6/site-packages/_pytest/assertion/rewrite.py:149: in exec_module exec(co, module.__dict__)
apps/serializers.py:9: in <module>
class Serializer(serializers.Serializer):
apps/serializers.py:13: in SearchSerializer
required=False, style={'base_template': 'select_multiple.html'}
/tmp/lib/python3.6/site-packages/rest_framework/fields.py:1476: in __init__
super(MultipleChoiceField, self).__init__(*args, **kwargs)
/tmp/lib/python3.6/site-packages/rest_framework/fields.py:1417: in __init__
self.choices = choices
/tmp/lib/python3.6/site-packages/rest_framework/fields.py:1453: in _set_choices
self.grouped_choices = to_choices_dict(choices)
/tmp/lib/python3.6/site-packages/rest_framework/fields.py:150: in to_choices_dict
for choice in choices:
/tmp/lib/python3.6/site-packages/django/db/models/query.py:274: in __iter__ self._fetch_all()
/tmp/lib/python3.6/site-packages/django/db/models/query.py:1242: in _fetch_all
self._result_cache = list(self._iterable_class(self))
/tmp/lib/python3.6/site-packages/django/db/models/query.py:181: in __iter__
compiler = queryset.query.get_compiler(queryset.db)
/tmp/lib/python3.6/site-packages/django/db/models/sql/query.py:289: in get_compiler
return connection.ops.compiler(self.compiler)(self, connection, using)
/tmp/lib/python3.6/site-packages/django/db/backends/base/operations.py:330: in compiler
self._cache = import_module(self.compiler_module)
/tmp/lib/python3.6/site-packages/django/utils/functional.py:80: in __get__
res = instance.__dict__[self.name] = self.func(instance)
/tmp/lib/python3.6/site-packages/django/db/backends/oracle/operations.py:587: in compiler_module
if self.connection.features.has_fetch_offset_support:
/tmp/lib/python3.6/site-packages/django/utils/functional.py:80: in __get__
res = instance.__dict__[self.name] = self.func(instance)
/tmp/lib/python3.6/site-packages/django/db/backends/oracle/features.py:63: in has_fetch_offset_support
return self.connection.oracle_version >= (12, 2)
/tmp/lib/python3.6/site-packages/django/utils/functional.py:80: in __get__
res = instance.__dict__[self.name] = self.func(instance)
/tmp/lib/python3.6/site-packages/django/db/backends/oracle/base.py:311: in oracle_version
with self.temporary_connection():
/usr/local/lib/python3.6/contextlib.py:81: in __enter__
return next(self.gen)
/tmp/lib/python3.6/site-packages/django/db/backends/base/base.py:593: in temporary_connection
with self.cursor() as cursor:
/tmp/lib/python3.6/site-packages/django/db/backends/base/base.py:256: in cursor
return self._cursor()
/tmp/lib/python3.6/site-packages/django/db/backends/base/base.py:233: in _cursor
self.ensure_connection()
E   Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

0 个答案:

没有答案