Switch as_query testing to direct specification in is_same_sql_bind
[dbsrgits/DBIx-Class-Historic.git] / t / 71mysql.t
CommitLineData
70350518 1use strict;
34a9e8a0 2use warnings;
70350518 3
4use Test::More;
74de7c2c 5use Test::Exception;
70350518 6use lib qw(t/lib);
7use DBICTest;
5db49b9a 8use DBI::Const::GetInfoType;
0567538f 9
10my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
11
12#warn "$dsn $user $pass";
13
70350518 14plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
0567538f 15 unless ($dsn && $user);
16
74de7c2c 17plan tests => 11;
0567538f 18
3ff5b740 19my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
0567538f 20
3ff5b740 21my $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
0567538f 39#'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', '');
40
3ff5b740 41# This is in Core now, but it's here just to test that it doesn't break
42$schema->class('Artist')->load_components('PK::Auto');
0567538f 43
44# test primary key handling
3ff5b740 45my $new = $schema->resultset('Artist')->create({ name => 'foo' });
0567538f 46ok($new->artistid, "Auto-PK worked");
47
48# test LIMIT support
49for (1..6) {
3ff5b740 50 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
0567538f 51}
3ff5b740 52my $it = $schema->resultset('Artist')->search( {},
0567538f 53 { rows => 3,
54 offset => 2,
55 order_by => 'artistid' }
56);
fb4b58e8 57is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists
0567538f 58is( $it->next->name, "Artist 2", "iterator->next ok" );
59$it->next;
60$it->next;
61is( $it->next, undef, "next past end of resultset ok" );
62
a953d8d9 63my $test_type_info = {
64 'artistid' => {
103e3e03 65 'data_type' => 'INT',
66 'is_nullable' => 0,
fc22fbac 67 'size' => 11,
68 'default_value' => undef,
a953d8d9 69 },
70 'name' => {
103e3e03 71 'data_type' => 'VARCHAR',
a953d8d9 72 'is_nullable' => 1,
a0dd8679 73 'size' => 100,
fc22fbac 74 'default_value' => undef,
103e3e03 75 },
39da2a2b 76 'rank' => {
77 'data_type' => 'INT',
78 'is_nullable' => 0,
79 'size' => 11,
80 'default_value' => 13,
81 },
103e3e03 82 'charfield' => {
637219ab 83 'data_type' => 'CHAR',
103e3e03 84 'is_nullable' => 1,
fc22fbac 85 'size' => 10,
86 'default_value' => undef,
103e3e03 87 },
a953d8d9 88};
89
5db49b9a 90SKIP: {
91 my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
5db49b9a 92 skip "Cannot determine MySQL server version", 1 if !$mysql_version;
a953d8d9 93
f750163c 94 my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/;
95 skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2);
96
97 $v3 ||= 0;
98
5db49b9a 99 if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) {
100 $test_type_info->{charfield}->{data_type} = 'VARCHAR';
101 }
a953d8d9 102
3ff5b740 103 my $type_info = $schema->storage->columns_info_for('artist');
5db49b9a 104 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
105}
a953d8d9 106
55cfbb70 107## Can we properly deal with the null search problem?
a7e65bb5 108##
109## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect
110## But I'm not sure if we should do this or not (Ash, 2008/06/03)
55cfbb70 111
112NULLINSEARCH: {
113
114 ok my $artist1_rs = $schema->resultset('Artist')->search({artistid=>6666})
115 => 'Created an artist resultset of 6666';
116
117 is $artist1_rs->count, 0
118 => 'Got no returned rows';
119
120 ok my $artist2_rs = $schema->resultset('Artist')->search({artistid=>undef})
121 => 'Created an artist resultset of undef';
122
374dd926 123 TODO: {
4cf8bfe6 124 local $TODO = "need to fix the row count =1 when select * from table where pk IS NULL problem";
374dd926 125 is $artist2_rs->count, 0
126 => 'got no rows';
127 }
128
55cfbb70 129 my $artist = $artist2_rs->single;
130
131 is $artist => undef
132 => 'Nothing Found!';
55cfbb70 133}
134
74de7c2c 135my $cd = $schema->resultset ('CD')->create ({});
136
137my $producer = $schema->resultset ('Producer')->create ({});
138
c856f505 139lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
55cfbb70 140
0567538f 141# clean up our mess
3ff5b740 142END {
55cfbb70 143 #$dbh->do("DROP TABLE artist") if $dbh;
74de7c2c 144}