Commit | Line | Data |
70350518 |
1 | use strict; |
34a9e8a0 |
2 | use warnings; |
70350518 |
3 | |
4 | use Test::More; |
74de7c2c |
5 | use Test::Exception; |
70350518 |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
5db49b9a |
8 | use DBI::Const::GetInfoType; |
0567538f |
9 | |
10 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; |
11 | |
12 | #warn "$dsn $user $pass"; |
13 | |
70350518 |
14 | plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test' |
0567538f |
15 | unless ($dsn && $user); |
16 | |
b5963465 |
17 | plan tests => 19; |
0567538f |
18 | |
3ff5b740 |
19 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass); |
0567538f |
20 | |
3ff5b740 |
21 | my $dbh = $schema->storage->dbh; |
0567538f |
22 | |
23 | $dbh->do("DROP TABLE IF EXISTS artist;"); |
24 | |
a0dd8679 |
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));"); |
0567538f |
26 | |
74de7c2c |
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 | |
bed3a173 |
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 | |
0567538f |
47 | #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', ''); |
48 | |
3ff5b740 |
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'); |
0567538f |
51 | |
52 | # test primary key handling |
3ff5b740 |
53 | my $new = $schema->resultset('Artist')->create({ name => 'foo' }); |
0567538f |
54 | ok($new->artistid, "Auto-PK worked"); |
55 | |
56 | # test LIMIT support |
57 | for (1..6) { |
3ff5b740 |
58 | $schema->resultset('Artist')->create({ name => 'Artist ' . $_ }); |
0567538f |
59 | } |
3ff5b740 |
60 | my $it = $schema->resultset('Artist')->search( {}, |
0567538f |
61 | { rows => 3, |
62 | offset => 2, |
63 | order_by => 'artistid' } |
64 | ); |
fb4b58e8 |
65 | is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists |
0567538f |
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 | |
a953d8d9 |
71 | my $test_type_info = { |
72 | 'artistid' => { |
103e3e03 |
73 | 'data_type' => 'INT', |
74 | 'is_nullable' => 0, |
fc22fbac |
75 | 'size' => 11, |
76 | 'default_value' => undef, |
a953d8d9 |
77 | }, |
78 | 'name' => { |
103e3e03 |
79 | 'data_type' => 'VARCHAR', |
a953d8d9 |
80 | 'is_nullable' => 1, |
a0dd8679 |
81 | 'size' => 100, |
fc22fbac |
82 | 'default_value' => undef, |
103e3e03 |
83 | }, |
39da2a2b |
84 | 'rank' => { |
85 | 'data_type' => 'INT', |
86 | 'is_nullable' => 0, |
87 | 'size' => 11, |
88 | 'default_value' => 13, |
89 | }, |
103e3e03 |
90 | 'charfield' => { |
637219ab |
91 | 'data_type' => 'CHAR', |
103e3e03 |
92 | 'is_nullable' => 1, |
fc22fbac |
93 | 'size' => 10, |
94 | 'default_value' => undef, |
103e3e03 |
95 | }, |
a953d8d9 |
96 | }; |
97 | |
bed3a173 |
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 /], |
13a2f031 |
107 | [qw/Library 1 secrets1/], |
108 | [qw/Eatery 1 secrets2/], |
109 | [qw/Library 2 secrets3/], |
bed3a173 |
110 | ]); |
111 | |
13a2f031 |
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 | # |
bed3a173 |
116 | |
d758ec36 |
117 | { |
13a2f031 |
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) { |
b5963465 |
125 | is ($_->all, 2, 'Prefetched grouped search returns correct number of rows'); |
126 | is ($_->count, 2, 'Prefetched grouped search returns correct count'); |
13a2f031 |
127 | } |
128 | |
d758ec36 |
129 | # try a ->belongs_to direction (no select collapse) |
b5963465 |
130 | my $books = $schema->resultset ('BooksInLibrary')->search ( |
131 | { 'owner.name' => 'wiggle' }, |
132 | { prefetch => 'owner', distinct => 1 } |
133 | ); |
134 | my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }}); |
135 | for ($books, $books2) { |
136 | is ($_->all, 1, 'Prefetched grouped search returns correct number of rows'); |
137 | is ($_->count, 1, 'Prefetched grouped search returns correct count'); |
13a2f031 |
138 | } |
139 | } |
bed3a173 |
140 | |
5db49b9a |
141 | SKIP: { |
142 | my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ); |
5db49b9a |
143 | skip "Cannot determine MySQL server version", 1 if !$mysql_version; |
a953d8d9 |
144 | |
f750163c |
145 | my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/; |
146 | skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2); |
147 | |
148 | $v3 ||= 0; |
149 | |
5db49b9a |
150 | if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) { |
151 | $test_type_info->{charfield}->{data_type} = 'VARCHAR'; |
152 | } |
a953d8d9 |
153 | |
3ff5b740 |
154 | my $type_info = $schema->storage->columns_info_for('artist'); |
5db49b9a |
155 | is_deeply($type_info, $test_type_info, 'columns_info_for - column data types'); |
156 | } |
a953d8d9 |
157 | |
55cfbb70 |
158 | ## Can we properly deal with the null search problem? |
a7e65bb5 |
159 | ## |
160 | ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect |
161 | ## But I'm not sure if we should do this or not (Ash, 2008/06/03) |
55cfbb70 |
162 | |
163 | NULLINSEARCH: { |
164 | |
165 | ok my $artist1_rs = $schema->resultset('Artist')->search({artistid=>6666}) |
166 | => 'Created an artist resultset of 6666'; |
167 | |
168 | is $artist1_rs->count, 0 |
169 | => 'Got no returned rows'; |
170 | |
171 | ok my $artist2_rs = $schema->resultset('Artist')->search({artistid=>undef}) |
172 | => 'Created an artist resultset of undef'; |
173 | |
374dd926 |
174 | TODO: { |
4cf8bfe6 |
175 | local $TODO = "need to fix the row count =1 when select * from table where pk IS NULL problem"; |
374dd926 |
176 | is $artist2_rs->count, 0 |
177 | => 'got no rows'; |
178 | } |
179 | |
55cfbb70 |
180 | my $artist = $artist2_rs->single; |
181 | |
182 | is $artist => undef |
183 | => 'Nothing Found!'; |
55cfbb70 |
184 | } |
185 | |
74de7c2c |
186 | my $cd = $schema->resultset ('CD')->create ({}); |
187 | |
188 | my $producer = $schema->resultset ('Producer')->create ({}); |
189 | |
c856f505 |
190 | lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die'; |
55cfbb70 |
191 | |
0567538f |
192 | # clean up our mess |
3ff5b740 |
193 | END { |
55cfbb70 |
194 | #$dbh->do("DROP TABLE artist") if $dbh; |
74de7c2c |
195 | } |