2nd Pass
[dbsrgits/DBIx-Class.git] / t / 86ss_dbm.t
CommitLineData
ac50f57b 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6
7use lib qw(t/lib);
8use DBICTest;
9use DBIC::SqlMakerTest;
10use DBIx::Class::Optional::Dependencies ();
11
12use Path::Class;
13
14plan skip_all =>
15 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_ss_dbm')
16 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_ss_dbm');
17
18my $db_dir = dir(qw/t var/, "ss_dbm-$$");
19$db_dir->mkpath unless -d $db_dir;
20
21my ($dsn, $opts) = ('dbi:DBM:', {
22 f_dir => "$db_dir",
23 f_lockfile => '.lock',
24
25 dbm_type => 'BerkeleyDB',
26 dbm_mldbm => 'Storable',
27 dbm_store_metadata => 1,
28});
29
30my $schema = DBICTest::Schema->connect($dsn, '', '', $opts);
31is ($schema->storage->sqlt_type, 'DBM', 'sqlt_type correct pre-connection');
32isa_ok($schema->storage->sql_maker, 'DBIx::Class::SQLMaker::SQLStatement');
33
34# Custom deployment
35my $dbh = $schema->storage->dbh;
7606fd62 36my @cmds = split /\s*\;\s*/, scalar file(qw/t lib dbdfile.sql/)->slurp;
ac50f57b 37$dbh->do($_) for @cmds;
38
39### S:S doesn't have any sort of AUTOINCREMENT support, so IDs will have to be generated by hand ###
40
41# test primary key handling
42my $new = $schema->resultset('Artist')->create({
43 artistid => 1,
44 name => 'foo'
45});
46ok($new->artistid, "Create worked");
47
48# test LIMIT support
49for (1..6) {
50 $schema->resultset('Artist')->create({
51 artistid => $_+1,
52 name => 'Artist '.$_,
53 });
54}
55my $it = $schema->resultset('Artist')->search( {}, {
56 rows => 3,
57 offset => 2,
58 order_by => 'artistid'
59});
60is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists
61is( $it->next->name, "Artist 2", "iterator->next ok" );
62$it->next;
63$it->next;
64is( $it->next, undef, "next past end of resultset ok" );
65
66# Limit with select-lock (which is silently thrown away)
67lives_ok {
68 isa_ok (
69 $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
70 'DBICTest::Schema::Artist',
71 );
72} 'Limited FOR UPDATE select works';
73
74# shared-lock (which is silently thrown away)
75lives_ok {
76 isa_ok (
77 $schema->resultset('Artist')->find({artistid => 1}, {for => 'shared'}),
78 'DBICTest::Schema::Artist',
79 );
80} 'LOCK IN SHARE MODE select works';
81
82# (No nullables with DBD::DBM)
83my $test_type_info = {
84 'artistid' => {
85 'data_type' => 'VARCHAR',
86 'is_nullable' => 0,
87 'size' => 0,
88 },
89 'name' => {
90 'data_type' => 'VARCHAR',
91 'is_nullable' => 0,
92 'size' => 100,
93 },
94 'rank' => {
95 'data_type' => 'VARCHAR',
96 'is_nullable' => 0,
97 'size' => 0,
98 },
99 'charfield' => {
100 'data_type' => 'VARCHAR',
101 'is_nullable' => 0,
102 'size' => 10,
103 },
104};
105
7606fd62 106$ENV{DBIC_DBDFILE_TXN_NOWARN} = 1;
107
ac50f57b 108$schema->populate ('Owners', [
109 [qw/id name /],
110 [qw/1 wiggle/],
111 [qw/2 woggle/],
112 [qw/3 boggle/],
113]);
114
115$schema->populate ('BooksInLibrary', [
116 [qw/id source owner title /],
117 [qw/1 Library 1 secrets1/],
118 [qw/2 Eatery 1 secrets2/],
119 [qw/3 Library 2 secrets3/],
120]);
121
122{
123 # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed)
124 my $owners = $schema->resultset('Owners')->search(
125 { 'books.id' => { '!=', undef }},
126 { prefetch => 'books', cache => 1 }
127 );
128 is($owners->all, 2, 'Prefetched grouped search returns correct number of rows');
129
130 # only works here because of the full cache
131 # S:S would croak on a subselect otherwise
132 is($owners->count, 2, 'Prefetched grouped search returns correct count');
133
134 # try a ->belongs_to direction (no select collapse)
135 my $books = $schema->resultset('BooksInLibrary')->search (
136 { 'owner.name' => 'wiggle' },
137 { prefetch => 'owner', distinct => 1 }
138 );
139
140 {
141 local $TODO = 'populate does not subtract the non-Library INSERTs here...';
142 is($owners->all, 1, 'Prefetched grouped search returns correct number of rows');
143 is($owners->count, 1, 'Prefetched grouped search returns correct count');
144 }
145}
146
147my $type_info = $schema->storage->columns_info_for('artist');
148is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
149
150my $cd = $schema->resultset('CD')->create({ cdid => 1 });
151my $producer = $schema->resultset('Producer')->create({ producerid => 1 });
152lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
153
154{
155 my $artist = $schema->resultset('Artist')->next;
156 my $cd = $schema->resultset('CD')->next;
157 $cd->set_from_related('artist', $artist);
158 $cd->update;
159
160 my $rs = $schema->resultset('CD')->search ({}, { prefetch => 'artist' });
161
162 lives_ok sub {
163 my $cd = $rs->next;
164 is ($cd->artist->name, $artist->name, 'Prefetched artist');
165 }, 'join does not throw';
166
167 local $schema->storage->sql_maker->{_default_jointype} = 'inner';
168 is_same_sql_bind (
169 $rs->as_query,
170 '(
171 SELECT
172 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
173 artist.artistid, artist.name, artist.rank, artist.charfield
174 FROM cd me
175 INNER JOIN artist artist ON artist.artistid = me.artist
176 )',
177 [],
178 'overriden default join type works',
179 );
180}
181
182{
183 # Test support for straight joins
184 my $cdsrc = $schema->source('CD');
185 my $artrel_info = $cdsrc->relationship_info ('artist');
186 $cdsrc->add_relationship(
187 'straight_artist',
188 $artrel_info->{class},
189 $artrel_info->{cond},
190 { %{$artrel_info->{attrs}}, join_type => 'straight' },
191 );
192 is_same_sql_bind (
193 $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
194 '(
195 SELECT
196 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
197 straight_artist.artistid, straight_artist.name, straight_artist.rank, straight_artist.charfield
198 FROM cd me
199 STRAIGHT JOIN artist straight_artist ON straight_artist.artistid = me.artist
200 )',
201 [],
202 'straight joins correctly supported'
203 );
204}
205
206# Can we properly deal with the null search problem?
207{
208 $schema->resultset('Artist')->create({ artistid => 2222, name => 'last created artist' });
209
210 ok my $artist1_rs = $schema->resultset('Artist')->search({artistid=>6666})
211 => 'Created an artist resultset of 6666';
212
213 is $artist1_rs->count, 0
214 => 'Got no returned rows';
215
216 ok my $artist2_rs = $schema->resultset('Artist')->search({artistid=>undef})
217 => 'Created an artist resultset of undef';
218
219 is $artist2_rs->count, 0
220 => 'got no rows';
221
222 my $artist = $artist2_rs->single;
223
224 is $artist => undef
225 => 'Nothing Found!';
226}
227
228{
229 my $cds_per_year = {
230 2001 => 2,
231 2002 => 1,
232 2005 => 3,
233 };
234
235 # kill the scalar ref here
236 $schema->source('CD')->name('cd');
237
238 my $rs = $schema->resultset('CD');
239 $rs->delete;
240 my $cdid = 1;
241 foreach my $y (keys %$cds_per_year) {
242 foreach my $c (1 .. $cds_per_year->{$y} ) {
243 $rs->create({ cdid => $cdid++, title => "CD $y-$c", artist => 1, year => "$y-01-01" });
244 }
245 }
246
247 is ($rs->count, 6, 'CDs created successfully');
248}
249
250done_testing;