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:', {
24 f_ext => ".csv/r", # /r is a flag (see https://metacpan.org/module/DBD::File#f_ext)
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 dbdfile.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 $ENV{DBIC_DBDFILE_TXN_NOWARN} = 1;
110 $schema->populate ('Owners', [
117 $schema->populate ('BooksInLibrary', [
118 [qw/id source owner title /],
119 [qw/1 Library 1 secrets1/],
120 [qw/2 Eatery 1 secrets2/],
121 [qw/3 Library 2 secrets3/],
125 # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed)
126 my $owners = $schema->resultset('Owners')->search(
127 { 'books.id' => { '!=', undef }},
128 { prefetch => 'books', cache => 1 }
130 is($owners->all, 2, 'Prefetched grouped search returns correct number of rows');
132 # only works here because of the full cache
133 # S:S would croak on a subselect otherwise
134 is($owners->count, 2, 'Prefetched grouped search returns correct count');
136 # try a ->belongs_to direction (no select collapse)
137 my $books = $schema->resultset('BooksInLibrary')->search (
138 { 'owner.name' => 'wiggle' },
139 { prefetch => 'owner', distinct => 1 }
143 local $TODO = 'populate does not subtract the non-Library INSERTs here...';
144 is($owners->all, 1, 'Prefetched grouped search returns correct number of rows');
145 is($owners->count, 1, 'Prefetched grouped search returns correct count');
149 my $type_info = $schema->storage->columns_info_for('artist');
150 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
152 my $cd = $schema->resultset('CD')->create({ cdid => 1 });
153 my $producer = $schema->resultset('Producer')->create({ producerid => 1 });
154 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
157 my $artist = $schema->resultset('Artist')->next;
158 my $cd = $schema->resultset('CD')->next;
159 $cd->set_from_related('artist', $artist);
162 my $rs = $schema->resultset('CD')->search ({}, { prefetch => 'artist' });
166 is ($cd->artist->name, $artist->name, 'Prefetched artist');
167 }, 'join does not throw';
169 local $schema->storage->sql_maker->{_default_jointype} = 'inner';
174 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
175 artist.artistid, artist.name, artist.rank, artist.charfield
177 INNER JOIN artist artist ON artist.artistid = me.artist
180 'overriden default join type works',
185 # Test support for straight joins
186 my $cdsrc = $schema->source('CD');
187 my $artrel_info = $cdsrc->relationship_info ('artist');
188 $cdsrc->add_relationship(
190 $artrel_info->{class},
191 $artrel_info->{cond},
192 { %{$artrel_info->{attrs}}, join_type => 'straight' },
195 $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
198 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
199 straight_artist.artistid, straight_artist.name, straight_artist.rank, straight_artist.charfield
201 STRAIGHT JOIN artist straight_artist ON straight_artist.artistid = me.artist
204 'straight joins correctly supported'
208 # Can we properly deal with the null search problem?
210 $schema->resultset('Artist')->create({ artistid => 2222, name => 'last created artist' });
212 ok my $artist1_rs = $schema->resultset('Artist')->search({artistid=>6666})
213 => 'Created an artist resultset of 6666';
215 is $artist1_rs->count, 0
216 => 'Got no returned rows';
218 ok my $artist2_rs = $schema->resultset('Artist')->search({artistid=>undef})
219 => 'Created an artist resultset of undef';
221 is $artist2_rs->count, 0
224 my $artist = $artist2_rs->single;
237 # kill the scalar ref here
238 $schema->source('CD')->name('cd');
240 my $rs = $schema->resultset('CD');
243 foreach my $y (keys %$cds_per_year) {
244 foreach my $c (1 .. $cds_per_year->{$y} ) {
245 $rs->create({ cdid => $cdid++, title => "CD $y-$c", artist => 1, year => "$y-01-01" });
249 is ($rs->count, 6, 'CDs created successfully');