简体   繁体   English

使用非托管外部只读数据库进行单元测试

[英]Unit tests with an unmanaged external read-only database

I'm working on a project which involves a huge external dataset (~490Gb) loaded in an external database (MS SQL through django-pyodbc-azure). 我正在一个项目中,该项目涉及在外部数据库(通过django-pyodbc-azure的MS SQL)中加载的巨大外部数据集(〜490Gb)。 I've generated the Django models marked managed=False in their meta. 我已经在其元数据中生成了标记为managed=False的Django模型。 In my application this works fine, but I can't seem to figure out how to run my unit tests. 在我的应用程序中,这可以正常工作,但是我似乎无法弄清楚如何运行单元测试。 I can think of two approaches: mocking the data in a test database, and giving the unit tests (and CI) read-only access to the production dataset. 我可以想到两种方法:在测试数据库中模拟数据,并为单元测试(和CI)授予对生产数据集的只读访问权限。 Both options are acceptable, but I can't figure out either of them: 这两个选项都是可以接受的,但是我无法弄清楚它们中的任何一个:

Option 1: Mocked data Because my models are marked managed=False , there are no migrations, and as a result, the test runner fails to create the database. 选项1:模拟数据因为我的模型被标记为manage managed=False ,所以没有迁移,因此,测试运行器无法创建数据库。

Option 2: Live data django-pyodbc-azure will attempt to create a test database, which fails because it has a read-only connection. 选项2:实时数据 django-pyodbc-azure将尝试创建测试数据库,该数据库由于具有只读连接而失败。 Also I suspect that even if it were allowed to do so, the resulting database would be missing the required tables. 我也怀疑即使允许这样做,结果数据库也会缺少必需的表。

Q How can I run my unittests? 问:如何进行单元测试? Installing additional packages, or reconfiguring the database is acceptable. 可以安装其他软件包或重新配置数据库。 My setup uses django 1.9 with postgresql for the main DB. 我的设置使用django 1.9和postgresql作为主数据库。

After a day of staring at my screen, I found a solution: 盯着屏幕看了一天之后,我找到了解决方法:

I removed the managed=True from the models, and generated migrations. 我从模型中删除了managed=True ,并生成了迁移。 To prevent actual migrations against the production database, I used my database router to prevent the migrations. 为了防止针对生产数据库的实际迁移,我使用了数据库路由器来防止迁移。 (return False in allow_migrate when for the appropriate app and database). (对于相应的应用和数据库,在allow_migrate返回False)。

In my settings I detect whether unittests are being run, and then just don't define the database router or the external database. 在我的设置中,我检测是否正在运行单元测试,然后仅不定义数据库路由器或外部数据库。 With the migrations present, the unit tests. 在存在迁移的情况下,单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM