多数据库和非托管模型 - 测试用例失败

Posted

技术标签:

【中文标题】多数据库和非托管模型 - 测试用例失败【英文标题】:Multi db and unmanged models - Test case are failing 【发布时间】:2020-01-29 22:50:42 【问题描述】:

我使用非托管(只读)模型设置了 mutlidb。这些模型没有迁移。我试图测试 view.py 的功能。在 sqlite3 数据库中,测试表的这些模式不会导致导致测试用例失败的问题。 在 view.py 中,我导入的非托管(只读)模型失败了。

我已经点击链接进行测试Testing against unmanaged models

提到了多数据库设置

test_runner.py

from django.test.runner import DiscoverRunner


class DisableMigrations(object):
    def __contains__(self, item):
        return True

    def __getitem__(self, item):
        return None


class UnManagedModelTestRunner(DiscoverRunner):

    def setup_test_environment(self, *args, **kwargs):
        from django.apps import apps
        # app_name = apps.get_app_config('core_esp')
        self.unmanaged_models = [m for m in apps.get_models() if not m._meta.managed and m._meta.app_label is 'core_esp']
        for m in self.unmanaged_models:
            m._meta.managed = True
        super(UnManagedModelTestRunner, self).setup_test_environment(*args, **kwargs)

    def teardown_test_environment(self, *args, **kwargs):
        super(UnManagedModelTestRunner, self).teardown_test_environment(*args, **kwargs)
        # reset un managed models
        for m in self.unmanaged_models:
            m._meta.managed = False

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']
# Skip the migrations by setting "MIGRATION_MODULES"
# to the DisableMigrations class defined above
#
MIGRATION_MODULES = DisableMigrations()

# Set Django's test runner to the custom class defined above
TEST_RUNNER = 'apps.settings.test_runner.UnManagedModelTestRunner'
DATABASE_ROUTERS = ('apps.tests.test_dbrouter.TestApiRouter', )

models.py

from django.db import models

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

class TestModelB(models.Model):
    testid = models.CharField(max_length=200)
    class Meta:
        managed = False
        db_table = 'test_model_b'
        app_label = 'application_b'

test.py

class MyTestCase(TestCase):

    def test_my_function(self):
       # view is called
       # serializer called with read only model
       pass

错误

Traceback (most recent call last):                                                                                 
File "C:\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)                                                                                                                                   
File "C:\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute return Database.Cursor.execute(self, query, params)                                                                                                            
sqlite3.OperationalError: no such table: TestD 
The above exception was the direct cause of the following exception:                                                                                                                                              Traceback (most recent call last): 
File "manage.py", line 21, in <module> main() 
File "manage.py", line 17, in main execute_from_command_line(sys.argv)
File "C:\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line utility.execute()
File "C:\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "C:\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\lib\site-packages\django\core\management\base.py", line execute
output = self.handle(*args, **options)
File "C:\lib\site-packages\django\core\management\commands\test.py", line 53, in handle failures = test_runner.run_tests(test_labels)
File "C:\lib\site-packages\django\test\runner.py", line 627, in run_tests  
suite = self.build_suite(test_labels, extra_tests)
File "C:\lib\site-packages\django\test\runner.py", line 488, in build_suites   
tests = self.test_loader.loadTestsFromName(label)                                                                                                    
File "c:\program files\python37\Lib\unittest\loader.py", line 154, in loadTestsFromName 
module = __import__(module_name)                                                                                                                                           
File "C:\tests\tests.py", line 5, in <module>       
from .views import (                                                                                                                             
File "C:\views.py", line 6, in <module>    
from .serializers import (                                                                                                                       
File "C:\serializers.py", line 9, in <module>   
class TestSerializer(serializers.Serializer):                                                                                                                           
File "C:\serializers.py", line 13, in TestSerializer     
required=False, style='base_template': 'select_multiple.html'                                                                                                           
File "C:\lib\site-packages\rest_framework\fields.py", line 1476, in __init__                                                                      
super(MultipleChoiceField, self).__init__(*args, **kwargs)                                                                                                              
File "C:\lib\site-packages\rest_framework\fields.py", line 1417, in __init__                                                                         
self.choices = choices                                                                                                                        
File "C:\lib\site-packages\rest_framework\fields.py", line 1453, in _set_choices                                                                                                                                                                                                                         
for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):  
File "C:\lib\site-packages\django\db\models\sql\compiler.py", line 1052, in results_iter                                          
results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)                                       
File "C:\lib\site-packages\django\db\models\sql\compiler.py", line 1100, in execute_sql                                                             
cursor.execute(sql, params)                                                                                
File "C:\lib\site-packages\django\db\backends\utils.py", line 67, in execute 
return self._execute_with_wrappers(sql, params, many=False, 
executor=self._execute)
File "C:\lib\site-packages\django\db\backends\utils.py", line 76, in 
_execute_with_wrappers                                               
return executor(sql, params, many, context)                                                                                                                               
File "C:\lib\site-packages\django\db\backends\utils.py", line 84, in _execute                                                   
return self.cursor.execute(sql, params)                                                                                                                                   
File "C:\lib\site-packages\django\db\utils.py", line 89, in __exit__  
raise dj_exc_value.with_traceback(traceback) from exc_value           
File "C:\lib\site-packages\django\db\backends\utils.py", line 84, in _execute                                                                  
return self.cursor.execute(sql, params)                                                                                                                                   
File "C:\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute                                                                
return Database.Cursor.execute(self, query, params)                                                                                                                     
django.db.utils.OperationalError: no such table: TESTD

                    

【问题讨论】:

How to create table during Django tests with managed = False的可能重复 【参考方案1】:

我可以告诉你我是如何解决这个问题的;

所以我创建了一个信号,它或多或少看起来像这样:

def create_test_models(**kwargs):
    if "test" in sys.argv:
        Organization = apps.get_model("organizations.Organization")
        ...  # other models here too
        # as we do not manage it - we need to create it manually;
        with connection.schema_editor() as schema_editor:
            sid = transaction.savepoint()
            try:
                schema_editor.create_model(Organization)
                ... # different models here if needed
                transaction.savepoint_commit(sid)
            except ProgrammingError:  # tables already exists;
                transaction.savepoint_rollback(sid)

此信号在配置中连接为pre_migrate 信号:

class OrganizationsConfig(AppConfig):
    name = "engine.organizations"

    def ready(self):
        # run after each migration; so each deploy, but this method can handle the
        # incremental updates.
        pre_migrate.connect(create_test_models, sender=self)

这可能不是超级解决方案 - 但它正在运行,并且您在测试期间创建了模型,您可以使用它们或创建测试数据等。

希望这将帮助您继续前进。

【讨论】:

@MichelSamia 任何堆栈跟踪? 哎呀抱歉,我没有完全实现它,我只是在setUp中直接尝试了模式编辑器。我想删除我的反对票,但在您编辑答案之前我不允许这样做。【参考方案2】:

我在当前的 django 2.2 中找到了一个很好的解决方案。与 pytest-django 完美配合,但即使没有 pytest 也可能工作。

要使其正常工作,您需要:

    将您的项目拆分为两个 django 应用程序。一个将仅包含非托管模型,另一个将包含其余模型。不要在这两个应用中指定 managed = False

    在 DATABASES 的 settings.py 中,您将拥有两个数据库,一个是默认数据库,一个是您的外部数据库

    在您有设置的同一文件夹中,创建 routers.py 并实现一个路由器,该路由器将根据应用标签路由到给定的 DB(cca 45 行,包括您永远不会写入这些外部 DB 的检查)。然后将此路由器添加到 settings.py 的 ROUTERS 列表中。

当使用 pytest 运行测试时,它确实会为没有迁移文件夹的应用创建所有表

【讨论】:

我会尽量简化它,并将路由器放在这里。可能当使用 managed = False 时,路由器会更简单 即使没有路由器,你也可以使用它,但它会在默认测试数据库中创建非托管模型。这与实际环境中有些不同,例如外键可以在这两者之间进行。

以上是关于多数据库和非托管模型 - 测试用例失败的主要内容,如果未能解决你的问题,请参考以下文章

涉及多部手机的用例的自动化移动测试策略 - 例如蓝牙数据传输等

测试用例又双叒叕失败了,NLP帮你

自动装配失败:不是托管类型

测试设计

如何插入多对多记录数据?

Uiautomator--断言的使用