FAQ
Hi,

I'm currently writing tests for an application. To be able to test with
a live database, I wrote a test database package along the lines of the
example in chapter 8 (page 165) of Johnathan's Catalyst book:

package MyApp::Test::Database::Live;

use strict;
use warnings;

use MyAppDB;
use MyApp::Model::MyAppDB;
use Directory::Scratch;
use YAML qw( DumpFile );
use FindBin qw( $Bin );
use Digest::SHA1 qw( sha1_hex );
use HTTP::Request;

use base 'Exporter';
our @EXPORT = qw /schema log_in /;
my $schema;
my $config;

BEGIN {
my $tmp = Directory::Scratch->new;
my $db = $tmp->touch('db');
my $dsn = "DBI:SQLite:$db";
$schema = MyAppDB->connect($dsn);
$schema->deploy;
$config = "$Bin/../myapp_local.yml";
DumpFile($config, { 'MyApp::Model::MyAppDB' => { connect_info =>
[$dsn]}});
DumpFile('/home/jochen/location.txt', { 'path' => $config });
}

sub schema { $schema };

sub log_in {
my $mech = shift;

my $obj = '{ "login": "testuser", "password": "testing"}' . "\n";
my $dummy = [ 'Content-type' => 'application/json', 'Content-Length'
=> length($obj) ];
my $user = schema()->resultset('MyAppDB::User')->create({
login => 'testuser',
password => sha1_hex('testing'),
#password => 'testing',
mail => 'testuser@mydomain.com',
first_name => 'Max',
last_name => 'Mustermann',
company => 'mycompany',
customer_id => '002096',
});
my $req = HTTP::Request->new('POST',
'http://localhost/rest/login/login', $dummy, $obj);
$mech->request($req);
return $user;
}

END { unlink $config };

1;

When the login request failed, I first thought it had to do with the
authentication process. But when there was a change in the db schema
that I hadn't made accordingly in my local mysql database, the test
script complained about the columns not present in the database. So the
myapp_local.yml seems to get ignored. OTOH, the test user is not created
in that database.
According to the ConfigLoader docs, 'local' is the default suffix for
files used to override the main config, so AFAIU the above code is
supposed to "just work".
Maybe I'm just not seeing the wood for the trees here, but are there any
prerequisites for this to work that I may have overlooked?

Jochen

Search Discussions

  • Ben Vinnerd at Jun 17, 2009 at 2:58 pm

    On 17/06/09 14:50, Jochen Luig wrote:
    <snip> So the
    myapp_local.yml seems to get ignored. OTOH, the test user is not created
    in that database.
    According to the ConfigLoader docs, 'local' is the default suffix for
    files used to override the main config, so AFAIU the above code is
    supposed to "just work".
    Maybe I'm just not seeing the wood for the trees here, but are there any
    prerequisites for this to work that I may have overlooked?
    This looks like the same problem i'm having (see thread "ConfigLoader
    trouble").

    I did try myapp_local.conf and it was ignored.

    Ben
  • Carl Vincent at Jun 17, 2009 at 3:13 pm

    -----Original Message-----
    From: Ben Vinnerd
    Sent: 17 June 2009 15:59
    To: The elegant MVC web framework
    On 17/06/09 14:50, Jochen Luig wrote:
    <snip> So the
    myapp_local.yml seems to get ignored. OTOH, the test user is not created
    in that database.
    According to the ConfigLoader docs, 'local' is the default suffix for
    files used to override the main config, so AFAIU the above code is
    supposed to "just work".
    Maybe I'm just not seeing the wood for the trees here, but are there any
    prerequisites for this to work that I may have overlooked?
    This looks like the same problem i'm having (see thread "ConfigLoader
    trouble").

    I did try myapp_local.conf and it was ignored.

    Are you sure that your config files are parsing correctly? I've had problems in the past (admittedly with YAML config files) with the files being silently ignored when there's a syntax error in them. This may be handled better in the latest versions.

    Regards

    Carl


    --
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Carl Vincent http://www.netskills.ac.uk/ (URL)
    Systems Manager 0191 222 5003 (voice)
    Netskills, Newcastle University 0191 222 5001 (fax)
    Training ? Accreditation - Consultancy ? Development
  • Ben Vinnerd at Jun 17, 2009 at 3:20 pm

    On 17/06/09 16:13, Carl Vincent wrote:
    Are you sure that your config files are parsing correctly? I've had problems in the past (admittedly with YAML config files) with the files being silently ignored when there's a syntax error in them. This may be handled better in the latest versions.
    Yeah i'm sure - if i load the config file manually from within
    lib/Myapp.pm, it works fine:

    __PACKAGE__->config->{'Plugin::ConfigLoader'} = {file =>
    '/home/motoring/Motoring/motoring_beta.conf'};

    ($ENV{MOTORING_CONFIG_LOCAL_SUFFIX} is set to "beta")

    Ben
  • Jochen Luig at Jun 17, 2009 at 3:28 pm

    On Wed, 2009-06-17 at 16:13 +0100, Carl Vincent wrote:

    Are you sure that your config files are parsing correctly? I've had problems in the past (admittedly with YAML config files) with the files being silently ignored when there's a syntax error in them. This may be handled better in the latest versions.
    As mine is written via the DumpFile function of the YAML module, I think
    it will parse correctly. Anyway, here's a sample myapp_local.yml

    ---
    MyApp::Model::MyAppDB:
    connect_info:
    - DBI:SQLite:/tmp/8UwA7IjIPZ/db

    Looks OK to me.


    @Ben: Sorry, I found your thread only after I had written my message.
    But yes, looks like it's the same problem.

    Regards,

    Jochen
  • Tomas Doran at Jun 18, 2009 at 7:50 am

    On 17 Jun 2009, at 14:50, Jochen Luig wrote:
    According to the ConfigLoader docs, 'local' is the default suffix for
    files used to override the main config, so AFAIU the above code is
    supposed to "just work".
    Maybe I'm just not seeing the wood for the trees here, but are
    there any
    prerequisites for this to work that I may have overlooked?
    Erm, no. It should just work.

    I generated an example app with: catalyst.pl TestConfig, and applied
    the attached patch - this shows the local config happening.

    At a wild stab in the dark - is this an issue with the lifecycle of
    your application? I.e. is your app starting before you actually write
    the config file out?

    I guess that starting your test with strace -f and then searching for
    instances of the string 'myapp_local' in the output would be
    instructive about what is going wrong for you..

    Cheers
    t0m


    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: 0001-Test-local-config-works.patch
    Type: application/octet-stream
    Size: 2576 bytes
    Desc: not available
    Url : http://lists.scsys.co.uk/pipermail/catalyst/attachments/20090618/2bcf1e11/0001-Test-local-config-works.obj
  • Jochen Luig at Jun 19, 2009 at 7:27 am

    On Thu, 2009-06-18 at 08:50 +0100, Tomas Doran wrote:

    At a wild stab in the dark - is this an issue with the lifecycle of
    your application? I.e. is your app starting before you actually write
    the config file out?
    It may be a lifecycle issue. The db connection is configured in
    MyApp::Model::MyAppDB. I don't know enough about the initialization
    process to decide if that's a problem, so I'll just try it.
    I guess that starting your test with strace -f and then searching for
    instances of the string 'myapp_local' in the output would be
    instructive about what is going wrong for you..
    As far as I understand the attached snippet of the strace output I think
    the myapp_local.yml is actually being read during the startup process.

    Jochen
    -------------- next part --------------
    [pid 7844] select(8, [4 6], NULL, NULL, NULL <unfinished ...>
    [pid 7845] <... write resumed> ) = 61
    [pid 7845] gettimeofday({1245317519, 875530}, NULL) = 0
    [pid 7845] stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size#09, ...}) = 0
    [pid 7845] write(2, "2009/06/18 11:31:59 Loaded Config"..., 74 <unfinished ...>
    [pid 7844] <... select resumed> ) = 1 (in [6])
    [pid 7844] read(6, "2009/06/18 11:31:59 Loaded Config"..., 65536) = 74
    [pid 7844] write(2, "2009/06/18 11:31:59 Loaded Config"..., 742009/06/18 11:31:59 Loaded Config "/home/jochen/projects/MyApp/myapp.yml"
    ) = 74
    [pid 7844] select(8, [4 6], NULL, NULL, NULL <unfinished ...>
    [pid 7845] <... write resumed> ) = 74
    [pid 7845] gettimeofday({1245317519, 876155}, NULL) = 0
    [pid 7845] stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size#09, ...}) = 0
    [pid 7845] write(2, "2009/06/18 11:31:59 Loaded Config"..., 80 <unfinished ...>
    [pid 7844] <... select resumed> ) = 1 (in [6])
    [pid 7844] read(6, "2009/06/18 11:31:59 Loaded Config"..., 65536) = 80
    [pid 7844] write(2, "2009/06/18 11:31:59 Loaded Config"..., 802009/06/18 11:31:59 Loaded Config "/home/jochen/projects/MyApp/myapp_local.yml"
    ) = 80
    [pid 7844] select(8, [4 6], NULL, NULL, NULL <unfinished ...>
    [pid 7845] <... write resumed> ) = 80
    [pid 7845] gettimeofday({1245317519, 877341}, NULL) = 0
    [pid 7845] stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size#09, ...}) = 0
    [pid 7845] write(2, "2009/06/18 11:31:59 Setting up au"..., 48 <unfinished ...>
    [pid 7844] <... select resumed> ) = 1 (in [6])
    [pid 7844] read(6, "2009/06/18 11:31:59 Setting up au"..., 65536) = 48
    [pid 7844] write(2, "2009/06/18 11:31:59 Setting up au"..., 482009/06/18 11:31:59 Setting up auth realm users
    ) = 48
    [pid 7844] select(8, [4 6], NULL, NULL, NULL <unfinished ...>
    [pid 7845] <... write resumed> ) = 48
    [pid 7845] gettimeofday({1245317519, 878556}, NULL) = 0
    [pid 7845] stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size#09, ...}) = 0
    [pid 7845] write(2, "2009/06/18 11:31:59 Initialized i"..., 51 <unfinished ...>
    [pid 7844] <... select resumed> ) = 1 (in [6])
    [pid 7844] read(6, "2009/06/18 11:31:59 Initialized i"..., 65536) = 51
    [pid 7844] write(2, "2009/06/18 11:31:59 Initialized i"..., 512009/06/18 11:31:59 Initialized i18n "MyApp::I18N"
    ) = 51
    [pid 7844] select(8, [4 6], NULL, NULL, NULL <unfinished ...>
    [pid 7845] <... write resumed> ) = 51
    [pid 7845] gettimeofday({1245317519, 879233}, NULL) = 0
    [pid 7845] stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size#09, ...}) = 0
    [pid 7845] brk(0xbc72000) = 0xbc72000
    [pid 7845] write(2, "2009/06/18 11:31:59 Loaded compon"..., 4096 <unfinished ...>
    [pid 7844] <... select resumed> ) = 1 (in [6])
    [pid 7844] read(6, "2009/06/18 11:31:59 Loaded compon"..., 65536) = 4096
    [pid 7844] write(2, "2009/06/18 11:31:59 Loaded compon"..., 40962009/06/18 11:31:59 Loaded components:
    .-----------------------------------------------------------------+----------.
    Class | Type |
    +-----------------------------------------------------------------+----------+
  • Ben Vinnerd at Jun 18, 2009 at 2:27 pm

    On 17/06/09 14:50, Jochen Luig wrote:
    When the login request failed, I first thought it had to do with the
    authentication process. But when there was a change in the db schema
    that I hadn't made accordingly in my local mysql database, the test
    script complained about the columns not present in the database. So the
    myapp_local.yml seems to get ignored. OTOH, the test user is not created
    in that database.
    According to the ConfigLoader docs, 'local' is the default suffix for
    files used to override the main config, so AFAIU the above code is
    supposed to "just work".
    Maybe I'm just not seeing the wood for the trees here, but are there any
    prerequisites for this to work that I may have overlooked?
    Jochen - have you deleted the Makefile.PL in the app dir? If so, put it
    back :)

    Ben
  • Jochen Luig at Jun 19, 2009 at 7:20 am

    On Thu, 2009-06-18 at 15:27 +0100, Ben Vinnerd wrote:

    Jochen - have you deleted the Makefile.PL in the app dir? If so, put it
    back :)
    No, it's still there. I'm not familiar with the startup process of
    Catalyst apps and couldn't find anything beyond this part of the manual:

    http://search.cpan.org/~hkclark/Catalyst-Manual-5.8000/lib/Catalyst/Manual/Internals.pod#Initialization

    but I suspect my problem is that the db connection is configured in
    MyApp::Model::MyAppDB in an if statement that looks at the hostname and
    then sets it according to the present environment. I'll try it with %ENV
    and config files the way you're doing it next.

    Jochen

    P.S.: If there's a more in depth documentation on the initialization
    process, I'd appreciate a link.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedJun 17, '09 at 1:50p
activeJun 19, '09 at 7:27a
posts9
users4
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase