9 use DBIC::SqlMakerTest;
10 use DBIx::Class::Optional::Dependencies ();
15 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_ss_csv')
16 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_ss_csv');
18 my $db_dir = dir(qw/t var/, "ss_csv-$$");
19 $db_dir->mkpath unless -d $db_dir;
21 my ($dsn, $opts) = ('dbi:CSV:', {
32 my $schema = DBICTest::Schema->connect($dsn, '', '', $opts);
33 is ($schema->storage->sqlt_type, 'CSV', 'sqlt_type correct pre-connection');
34 isa_ok($schema->storage->sql_maker, 'DBIx::Class::SQLMaker::SQLStatement');
37 my $dbh = $schema->storage->dbh;
38 my @cmds = split /\s*\;\s*/, scalar file(qw/t lib test_deploy DBICTest-Schema-1.x-SQL-Statement.sql/)->slurp;
39 $dbh->do($_) for @cmds;
41 ### S:S doesn't have any sort of AUTOINCREMENT support, so IDs will have to be generated by hand ###
43 # test primary key handling
44 my $new = $schema->resultset('Artist')->create({
48 ok($new->artistid, "Create worked");
52 $schema->resultset('Artist')->create({
57 my $it = $schema->resultset('Artist')->search( {}, {
60 order_by => 'artistid'
62 is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists
63 is( $it->next->name, "Artist 2", "iterator->next ok" );
66 is( $it->next, undef, "next past end of resultset ok" );
68 # Limit with select-lock (which is silently thrown away)
71 $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
72 'DBICTest::Schema::Artist',
74 } 'Limited FOR UPDATE select works';
76 # shared-lock (which is silently thrown away)
79 $schema->resultset('Artist')->find({artistid => 1}, {for => 'shared'}),
80 'DBICTest::Schema::Artist',
82 } 'LOCK IN SHARE MODE select works';
84 # (everything seems to be a VARCHAR with S:S)
85 my $test_type_info = {
87 'data_type' => 'VARCHAR',
92 'data_type' => 'VARCHAR',
97 'data_type' => 'VARCHAR',
102 'data_type' => 'VARCHAR',
108 $schema->populate ('Owners', [
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/],
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 }
128 is($owners->all, 2, 'Prefetched grouped search returns correct number of rows');
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');
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 }
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');
147 my $type_info = $schema->storage->columns_info_for('artist');
148 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
150 my $cd = $schema->resultset('CD')->create({ cdid => 1 });
151 my $producer = $schema->resultset('Producer')->create({ producerid => 1 });
152 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
155 my $artist = $schema->resultset('Artist')->next;
156 my $cd = $schema->resultset('CD')->next;
157 $cd->set_from_related('artist', $artist);
160 my $rs = $schema->resultset('CD')->search ({}, { prefetch => 'artist' });
164 is ($cd->artist->name, $artist->name, 'Prefetched artist');
165 }, 'join does not throw';
167 local $schema->storage->sql_maker->{_default_jointype} = 'inner';
172 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
173 artist.artistid, artist.name, artist.rank, artist.charfield
175 INNER JOIN artist artist ON artist.artistid = me.artist
178 'overriden default join type works',
183 # Test support for straight joins
184 my $cdsrc = $schema->source('CD');
185 my $artrel_info = $cdsrc->relationship_info ('artist');
186 $cdsrc->add_relationship(
188 $artrel_info->{class},
189 $artrel_info->{cond},
190 { %{$artrel_info->{attrs}}, join_type => 'straight' },
193 $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
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
199 STRAIGHT JOIN artist straight_artist ON straight_artist.artistid = me.artist
202 'straight joins correctly supported'
206 # Can we properly deal with the null search problem?
208 $schema->resultset('Artist')->create({ artistid => 2222, name => 'last created artist' });
210 ok my $artist1_rs = $schema->resultset('Artist')->search({artistid=>6666})
211 => 'Created an artist resultset of 6666';
213 is $artist1_rs->count, 0
214 => 'Got no returned rows';
216 ok my $artist2_rs = $schema->resultset('Artist')->search({artistid=>undef})
217 => 'Created an artist resultset of undef';
219 is $artist2_rs->count, 0
222 my $artist = $artist2_rs->single;
235 # kill the scalar ref here
236 $schema->source('CD')->name('cd');
238 my $rs = $schema->resultset('CD');
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" });
247 is ($rs->count, 6, 'CDs created successfully');