gpt4 book ai didi

perl - 有没有办法在催化剂中使用测试数据库

转载 作者:行者123 更新时间:2023-12-02 15:26:11 26 4
gpt4 key购买 nike

使用 Catalyst 时,有没有办法像在 Rails 中一样指定开发、测试和生产数据库?我已经查看了文档,但没有找到答案。

最佳答案

引用cpan Catalyst Testing Tutorial

You may wish to maintain both a "production database" for your live application and a "testing database" for your test cases.

DATABASE CONFIG SWITCHING IN YOUR MODEL CLASS

One solution is to allow the database specification to be overridden with an environment variable. For example, open lib/MyApp/Model/MyAppDB.pm in your editor and change the __PACKAGE__->config(... declaration to resemble:

my $dsn = $ENV{MYAPP_DSN} ||= 'dbi:SQLite:myapp.db';
__PACKAGE__->config(
schema_class => 'MyAppDB',
connect_info => [
$dsn,
'',
'',
{ AutoCommit => 1 },

],
);

Then, when you run your test case, you can use commands such as:

$ cp myapp.db myappTEST.db
$ CATALYST_DEBUG=0 MYAPP_DSN="dbi:SQLite:myappTEST.db" prove --lib lib -v t/live_app01.t

This will modify the DSN only while the test case is running. If you launch your normal application without the MYAPP_DSN environment variable defined, it will default to the same dbi:SQLite:myapp.db as before.

DATABASE CONFIG SWITCHING USING MULTIPLE CONFIG FILES

By utilizing Catalyst::Plugin::ConfigLoaders functionality for loading multiple config files based on environment variables you can override your default (production) database connection settings.

Setting $ENV{ MYAPP_CONFIG_LOCAL_SUFFIX } to 'testing' in your test script results in loading of an additional config file named myapp_testing.conf after myapp.conf which will override any parameters in myapp.conf.

You should set the environment variable in the BEGIN block of your test script to make sure it's set before your Catalyst application is started.

The following is an example for a config and test script for a DBIx::Class model named MyDB and a controller named Foo:

myapp_testing.conf:

<Model::MyDB>
<connect_info>
dsn dbi:SQLite:myapp.db
</connect_info>
</Model::MyDB>

另请检查this使用单独的数据库时

关于perl - 有没有办法在催化剂中使用测试数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14740424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com