-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-dbic_wrapper.t
161 lines (127 loc) · 5.27 KB
/
01-dbic_wrapper.t
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!perl -T
use Test::More;
use Test::Fatal qw/dies_ok lives_ok/;
# use File::Temp;
use DBI;
use DBD::SQLite;
use JCOM::BM::DBICWrapper;
package My::Schema;
use base qw/DBIx::Class::Schema::Loader/;
__PACKAGE__->naming('current');
1;
package My::Model;
use Moose;
with qw/JCOM::BM::DBICWrapper/;
has 'colour' => ( is => 'rw' , isa => 'Str' , default => 'green' , required => 1 );
1;
package My::Model::O::Product;
use Moose;
extends 'JCOM::BM::DBICObject';
has 'o' => ( isa => 'My::Schema::Product' , is => 'ro' , required => 1 , handles => [ 'id' , 'name' ] );
sub turn_on{
my ($self) = @_;
return "Turning on $self";
}
sub activate{
shift->o()->update({ active => 1});
}
sub deactivate{
shift->o()->update({ active => 0});
}
1;
package My::Model::DBICFactory::Product;
use Moose;
extends qw/JCOM::BM::DBICFactory/ ;
sub wrap{
my ($self , $o) = @_;
return My::Model::O::Product->new({o => $o , factory => $self });
}
1;
package My::Model::DBICFactory::ActiveProduct;
use Moose;
extends qw/My::Model::DBICFactory::Product/;
sub _build_dbic_rs{
my ($self) = @_;
return $self->bm->jcom_schema->resultset('Product')->search_rs({ active => 1});
}
1;
package My::Model::DBICFactory::ColouredProduct;
use Moose;
extends qw/My::Model::DBICFactory::Product/;
sub _build_dbic_rs{
my ($self) = @_;
my $bm = $self->bm();
return $bm->jcom_schema->resultset('Product')->search_rs({ colour => $bm->colour() });
}
1;
package main;
## Connect to a DB and dynamically build the DBIC model.
ok( my $dbh = DBI->connect("dbi:SQLite::memory:" , "" , "") , "Ok connected as a DBI");
ok( $dbh->{AutoCommit} = 1 , "Ok autocommit set");
ok( $dbh->do("PRAGMA foreign_keys = ON") , "Ok set foreign keys");
ok( $dbh->do('CREATE TABLE builder(id INTEGER PRIMARY KEY AUTOINCREMENT, bname VARCHAR(255) UNIQUE NOT NULL)') , "Ok creating builder table");
ok( $dbh->do('CREATE TABLE product(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), active BOOLEAN DEFAULT FALSE, colour VARCHAR(10) NOT NULL DEFAULT \'blue\', builder_id INTEGER,FOREIGN KEY (builder_id) REFERENCES builder(id))') , "Ok creating product table");
## Build a schema dynamically.
ok( my $schema = My::Schema->connect(sub{ return $dbh ;} , { unsafe => 1 } ), "Ok built schema with dbh");
## Just to check
ok( $schema->resultset('Builder') , "Builder resultset is there");
ok( $schema->resultset('Product') , "Product resultset is there");
## Build a My::Model using it
ok( my $bm = My::Model->new({ jcom_schema => $schema }) , "Ok built a model");
## And test a few stuff.
ok( my $pf = $bm->dbic_factory('Product') , "Ok got product factory");
ok( my $pf2 = $bm->dbic_factory('ActiveProduct') , "Ok got another product factory");
ok( my $bf = $bm->dbic_factory('Builder') , "Ok got builder factory");
## Object creation.
ok( my $b = $bf->create( { bname => 'Builder1' }) , "Ok built the first builder");
ok( my $ob = $bf->find_or_create( { bname => 'Builder1' }) , "Ok found or create builder");
cmp_ok( $b->id() , '==' , $ob->id() , "Both builders are the same");
## Object loopback
ok( $b = $bf->find($b->id()) , "Ok found it by id");
cmp_ok( $b->bname , 'eq' , 'Builder1' , "Good data");
{
ok( my $other_builder = $bf->find_or_create( { bname => 'Something never heard of' } ) , "Ok could create a new one");
}
## Now a product
ok( my $p = $pf->create( { name => 'Hoover' , builder => $b }) , "Ok could make a product");
ok( $p->id() , "Hoover product has got an ID");
ok( $p->turn_on() , "Can be turned on as well");
## Another product. This one is active
## Note that it's created via the ActiveProduct resultset.
ok( my $ap = $pf2->create({ name => 'Kettle' , builder => $b , active => 1 }) , "Ok made an active product");
ok( ! $pf2->find($p->id()), "We cannot find the first product because it's not active");
ok( $pf2->find($ap->id()), "We can find the second product because it's active");
## Now some searching.
ok( my $search_rs = $pf->search() , "Ok got a resultset");
isa_ok( $search_rs , 'My::Model::DBICFactory::Product' , "And its a Product factory");
cmp_ok( $search_rs->count() , '==' , 2 , "Got two products");
my $seen_p = 0;
while( my $next_p = $search_rs->next() ){
$seen_p++;
isa_ok( $next_p , 'My::Model::O::Product' , "Ok next is a product");
}
cmp_ok( $seen_p , '==' , 2 , "Seen two products thanks to next");
## Same thing on the active only products.
ok( my $act_search = $pf2->search() , "Ok got active product search");
isa_ok( $act_search , 'My::Model::DBICFactory::Product' , "And its a Product factory");
cmp_ok( $act_search->count() , '==' , 1 , "Got one product");
$seen_p = 0;
while( my $next_p = $act_search->next() ){
$seen_p++;
isa_ok( $next_p , 'My::Model::O::Product' , "Ok next is a product");
}
cmp_ok( $seen_p , '==' , 1 , "Seen one product thanks to next");
$p->activate();
cmp_ok( $pf2->search()->count() , '==' , 2 , "Now two products in the active resultset");
## Now some colour testing
{
ok( my $cr = $bm->dbic_factory('ColouredProduct') , "Ok coloured product resultset");
cmp_ok( $cr->count() , '==' , 0 , "No coloured in green product found");
$bm->colour('blue');
cmp_ok( $bm->dbic_factory('ColouredProduct')->count() , '==' , 2 , 'Now two coloured product');
}
## Test a non existing factory
{
dies_ok { $bm->dbic_factory('BoudinBlanc') } "No boudin blanc factory";
}
done_testing();