a2e868f4824d4161ca000db3252f5d5430ad7ad6
[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 => 11;
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 #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', '');
40
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');
43
44 # test primary key handling
45 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
46 ok($new->artistid, "Auto-PK worked");
47
48 # test LIMIT support
49 for (1..6) {
50     $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
51 }
52 my $it = $schema->resultset('Artist')->search( {},
53     { rows => 3,
54       offset => 2,
55       order_by => 'artistid' }
56 );
57 is( $it->count, 3, "LIMIT count ok" );  # ask for 3 rows out of 7 artists
58 is( $it->next->name, "Artist 2", "iterator->next ok" );
59 $it->next;
60 $it->next;
61 is( $it->next, undef, "next past end of resultset ok" );
62
63 my $test_type_info = {
64     'artistid' => {
65         'data_type' => 'INT',
66         'is_nullable' => 0,
67         'size' => 11,
68         'default_value' => undef,
69     },
70     'name' => {
71         'data_type' => 'VARCHAR',
72         'is_nullable' => 1,
73         'size' => 100,
74         'default_value' => undef,
75     },
76     'rank' => {
77         'data_type' => 'INT',
78         'is_nullable' => 0,
79         'size' => 11,
80         'default_value' => 13,
81     },
82     'charfield' => {
83         'data_type' => 'CHAR',
84         'is_nullable' => 1,
85         'size' => 10,
86         'default_value' => undef,
87     },
88 };
89
90 SKIP: {
91     my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
92     skip "Cannot determine MySQL server version", 1 if !$mysql_version;
93
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
99     if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) {
100         $test_type_info->{charfield}->{data_type} = 'VARCHAR';
101     }
102
103     my $type_info = $schema->storage->columns_info_for('artist');
104     is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
105 }
106
107 ## Can we properly deal with the null search problem?
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)
111
112 NULLINSEARCH: {
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     
123     TODO: {
124         local $TODO = "need to fix the row count =1 when select * from table where pk IS NULL problem";
125             is $artist2_rs->count, 0
126             => 'got no rows';           
127     }
128
129     my $artist = $artist2_rs->single;
130     
131     is $artist => undef
132     => 'Nothing Found!';
133 }
134     
135 my $cd = $schema->resultset ('CD')->create ({});
136
137 my $producer = $schema->resultset ('Producer')->create ({});
138
139 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
140
141 # clean up our mess
142 END {
143     #$dbh->do("DROP TABLE artist") if $dbh;
144 }