cf02a6136a0e992a5fe655fc9d9c912c18c26a83
[dbsrgits/DBIx-Class.git] / t / 71mysql.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use DBI::Const::GetInfoType;
9
10 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
11
12 #warn "$dsn $user $pass";
13
14 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
15   unless ($dsn && $user);
16
17 plan tests => 23;
18
19 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
20
21 my $dbh = $schema->storage->dbh;
22
23 $dbh->do("DROP TABLE IF EXISTS artist;");
24
25 $dbh->do("CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10));");
26
27 $dbh->do("DROP TABLE IF EXISTS cd;");
28
29 $dbh->do("CREATE TABLE cd (cdid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, artist INTEGER, title TEXT, year INTEGER, genreid INTEGER, single_track INTEGER);");
30
31 $dbh->do("DROP TABLE IF EXISTS producer;");
32
33 $dbh->do("CREATE TABLE producer (producerid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT);");
34
35 $dbh->do("DROP TABLE IF EXISTS cd_to_producer;");
36
37 $dbh->do("CREATE TABLE cd_to_producer (cd INTEGER,producer INTEGER);");
38
39 $dbh->do("DROP TABLE IF EXISTS owners;");
40
41 $dbh->do("CREATE TABLE owners (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL);");
42
43 $dbh->do("DROP TABLE IF EXISTS books;");
44
45 $dbh->do("CREATE TABLE books (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, source VARCHAR(100) NOT NULL, owner integer NOT NULL, title varchar(100) NOT NULL,  price integer);");
46
47 #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', '');
48
49 # This is in Core now, but it's here just to test that it doesn't break
50 $schema->class('Artist')->load_components('PK::Auto');
51
52 # test primary key handling
53 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
54 ok($new->artistid, "Auto-PK worked");
55
56 # test LIMIT support
57 for (1..6) {
58     $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
59 }
60 my $it = $schema->resultset('Artist')->search( {},
61     { rows => 3,
62       offset => 2,
63       order_by => 'artistid' }
64 );
65 is( $it->count, 3, "LIMIT count ok" );  # ask for 3 rows out of 7 artists
66 is( $it->next->name, "Artist 2", "iterator->next ok" );
67 $it->next;
68 $it->next;
69 is( $it->next, undef, "next past end of resultset ok" );
70
71 my $test_type_info = {
72     'artistid' => {
73         'data_type' => 'INT',
74         'is_nullable' => 0,
75         'size' => 11,
76         'default_value' => undef,
77     },
78     'name' => {
79         'data_type' => 'VARCHAR',
80         'is_nullable' => 1,
81         'size' => 100,
82         'default_value' => undef,
83     },
84     'rank' => {
85         'data_type' => 'INT',
86         'is_nullable' => 0,
87         'size' => 11,
88         'default_value' => 13,
89     },
90     'charfield' => {
91         'data_type' => 'CHAR',
92         'is_nullable' => 1,
93         'size' => 10,
94         'default_value' => undef,
95     },
96 };
97
98 $schema->populate ('Owners', [
99   [qw/id  name  /],
100   [qw/1   wiggle/],
101   [qw/2   woggle/],
102   [qw/3   boggle/],
103 ]);
104
105 $schema->populate ('BooksInLibrary', [
106   [qw/source  owner title   /],
107   [qw/Library 1     secrets1/],
108   [qw/Eatery  1     secrets2/],
109   [qw/Library 2     secrets3/],
110 ]);
111
112 #
113 # try a distinct + prefetch on tables with identically named columns 
114 # (mysql doesn't seem to like subqueries with equally named columns)
115 #
116
117 SKIP: {
118   # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed)
119   my $owners = $schema->resultset ('Owners')->search (
120     { 'books.id' => { '!=', undef }},
121     { prefetch => 'books', distinct => 1 }
122   );
123   my $owners2 = $schema->resultset ('Owners')->search ({ id => { -in => $owners->get_column ('me.id')->as_query }});
124   for ($owners, $owners2) {
125     lives_ok { is ($_->all, 2, 'Prefetched grouped search returns correct number of rows') }
126       || skip ('No test due to exception', 1);
127     lives_ok { is ($_->count, 2, 'Prefetched grouped search returns correct count') }
128       || skip ('No test due to exception', 1);
129   }
130
131   TODO: {
132     # try a ->prefetch direction (no select collapse)
133     my $books = $schema->resultset ('BooksInLibrary')->search (
134       { 'owner.name' => 'wiggle' },
135       { prefetch => 'owner', distinct => 1 }
136     );
137
138     local $TODO = 'MySQL is crazy - there seems to be no way to make this work';
139     # error thrown is:
140     # Duplicate column name 'id' [for Statement "
141     #   SELECT COUNT( * )
142     #     FROM (
143     #       SELECT me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name
144     #         FROM books me
145     #         JOIN owners owner ON owner.id = me.owner 
146     #       WHERE ( ( owner.name = ? AND source = ? ) ) 
147     #       GROUP BY me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name
148     #     ) count_subq
149     # " with ParamValues: 0='wiggle', 1='Library']
150     #
151     # go fucking figure
152
153     my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }});
154     for ($books, $books2) {
155       lives_ok { is ($_->all, 1, 'Prefetched grouped search returns correct number of rows') }
156         || skip ('No test due to exception', 1);
157       lives_ok { is ($_->count, 1, 'Prefetched grouped search returns correct count') }
158         || skip ('No test due to exception', 1);
159     }
160   }
161 }
162
163 SKIP: {
164     my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
165     skip "Cannot determine MySQL server version", 1 if !$mysql_version;
166
167     my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/;
168     skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2);
169
170     $v3 ||= 0;
171
172     if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) {
173         $test_type_info->{charfield}->{data_type} = 'VARCHAR';
174     }
175
176     my $type_info = $schema->storage->columns_info_for('artist');
177     is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
178 }
179
180 ## Can we properly deal with the null search problem?
181 ##
182 ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect
183 ## But I'm not sure if we should do this or not (Ash, 2008/06/03)
184
185 NULLINSEARCH: {
186     
187     ok my $artist1_rs = $schema->resultset('Artist')->search({artistid=>6666})
188     => 'Created an artist resultset of 6666';
189     
190     is $artist1_rs->count, 0
191     => 'Got no returned rows';
192     
193     ok my $artist2_rs = $schema->resultset('Artist')->search({artistid=>undef})
194     => 'Created an artist resultset of undef';
195     
196     TODO: {
197         local $TODO = "need to fix the row count =1 when select * from table where pk IS NULL problem";
198             is $artist2_rs->count, 0
199             => 'got no rows';           
200     }
201
202     my $artist = $artist2_rs->single;
203     
204     is $artist => undef
205     => 'Nothing Found!';
206 }
207     
208 my $cd = $schema->resultset ('CD')->create ({});
209
210 my $producer = $schema->resultset ('Producer')->create ({});
211
212 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
213
214 # clean up our mess
215 END {
216     #$dbh->do("DROP TABLE artist") if $dbh;
217 }