Remove the length limit on identifiers - it doesn't belong in DBIx::Class
[dbsrgits/DBIx-Class.git] / t / 71mysql.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 use DBI::Const::GetInfoType;
8
9 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
10
11 #warn "$dsn $user $pass";
12
13 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
14   unless ($dsn && $user);
15
16 plan tests => 10;
17
18 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
19
20 my $dbh = $schema->storage->dbh;
21
22 $dbh->do("DROP TABLE IF EXISTS artist;");
23
24 $dbh->do("CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), charfield CHAR(10));");
25
26 #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', '');
27
28 # This is in Core now, but it's here just to test that it doesn't break
29 $schema->class('Artist')->load_components('PK::Auto');
30
31 # test primary key handling
32 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
33 ok($new->artistid, "Auto-PK worked");
34
35 # test LIMIT support
36 for (1..6) {
37     $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
38 }
39 my $it = $schema->resultset('Artist')->search( {},
40     { rows => 3,
41       offset => 2,
42       order_by => 'artistid' }
43 );
44 is( $it->count, 3, "LIMIT count ok" );
45 is( $it->next->name, "Artist 2", "iterator->next ok" );
46 $it->next;
47 $it->next;
48 is( $it->next, undef, "next past end of resultset ok" );
49
50 my $test_type_info = {
51     'artistid' => {
52         'data_type' => 'INT',
53         'is_nullable' => 0,
54         'size' => 11,
55         'default_value' => undef,
56     },
57     'name' => {
58         'data_type' => 'VARCHAR',
59         'is_nullable' => 1,
60         'size' => 255,
61         'default_value' => undef,
62     },
63     'charfield' => {
64         'data_type' => 'CHAR',
65         'is_nullable' => 1,
66         'size' => 10,
67         'default_value' => undef,
68     },
69 };
70
71 SKIP: {
72     my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
73     skip "Cannot determine MySQL server version", 1 if !$mysql_version;
74
75     my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/;
76     skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2);
77
78     $v3 ||= 0;
79
80     if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) {
81         $test_type_info->{charfield}->{data_type} = 'VARCHAR';
82     }
83
84     my $type_info = $schema->storage->columns_info_for('artist');
85     is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
86 }
87
88 ## Can we properly deal with the null search problem?
89 ##
90 ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect
91 ## But I'm not sure if we should do this or not (Ash, 2008/06/03)
92
93 NULLINSEARCH: {
94     
95     ok my $artist1_rs = $schema->resultset('Artist')->search({artistid=>6666})
96     => 'Created an artist resultset of 6666';
97     
98     is $artist1_rs->count, 0
99     => 'Got no returned rows';
100     
101     ok my $artist2_rs = $schema->resultset('Artist')->search({artistid=>undef})
102     => 'Created an artist resultset of undef';
103     
104     TODO: {
105         $TODO = "need to fix the row count =1 when select * from table where pk IS NULL problem";
106             is $artist2_rs->count, 0
107             => 'got no rows';           
108     }
109
110     my $artist = $artist2_rs->single;
111     
112     is $artist => undef
113     => 'Nothing Found!';
114 }
115     
116
117 # clean up our mess
118 END {
119     $dbh->do("DROP TABLE artist") if $dbh;
120 }