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