File: 02-multiple-schemas.t

package info (click to toggle)
libdancer-plugin-dbic-perl 0.1506-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 148 kB
  • sloc: perl: 74; makefile: 12
file content (78 lines) | stat: -rw-r--r-- 1,995 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use strict;
use warnings;
use Test::More tests => 9, import => ['!pass'];
use Test::Exception;

use Dancer qw(:syntax);
use Dancer::Plugin::DBIC;
use DBI;
use File::Temp qw(tempfile);

eval { require DBD::SQLite };
if ($@) {
    plan skip_all => 'DBD::SQLite required to run these tests';
}

my (undef, $dbfile1) = tempfile(SUFFIX => '.db');
my (undef, $dbfile2) = tempfile(SUFFIX => '.db');

set plugins => {
    DBIC => {
        foo => {
            dsn =>  "dbi:SQLite:dbname=$dbfile1",
        },
        bar => {
            dsn =>  "dbi:SQLite:dbname=$dbfile2",
        },
    }
};

my $dbh1 = DBI->connect("dbi:SQLite:dbname=$dbfile1");
my $dbh2 = DBI->connect("dbi:SQLite:dbname=$dbfile2");

ok $dbh1->do(q{
    create table user (name varchar(100) primary key, age int)
}), 'Created sqlite test db.';

my @users = ( ['bob', 30] );
for my $user (@users) { $dbh1->do('insert into user values(?,?)', {}, @$user) }

ok $dbh2->do(q{
    create table user (name varchar(100) primary key, age int)
}), 'Created sqlite test db.';

@users = ( ['sue', 20] );
for my $user (@users) {
    $dbh2->do(q{ insert into user values(?,?) }, {}, @$user);
}

my $user = schema('foo')->resultset('User')->find('bob');
ok $user, 'Found bob.';
is $user->age => '30', 'Bob is getting old.';

$user = schema('bar')->resultset('User')->find('sue');
ok $user, 'Found sue.';
is $user->age => '20', 'Sue is the right age.';

throws_ok { schema('poo')->resultset('User')->find('bob') }
    qr/schema poo is not configured/, 'Missing schema error thrown';

throws_ok { schema->resultset('User')->find('bob') }
    qr/The schema default is not configured/, 'Missing default schema error thrown';

set plugins => {
    DBIC => {
        default => {
            dsn =>  "dbi:SQLite:dbname=$dbfile1",
        },
        bar => {
            dsn =>  "dbi:SQLite:dbname=$dbfile2",
        },
    }
};

lives_and {
    ok schema->resultset('User')->find('bob'), 'Found bob.'
} 'Default schema';

unlink $dbfile1, $dbfile2;