Merge 0.09's savepoint branch
[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 use DBICTest::Stats;
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 => 9;
18
19 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
20
21 my $dbh = $schema->storage->dbh;
22 my $stats = new DBICTest::Stats();
23 $schema->storage->debugobj($stats);
24 $schema->storage->debug(1);
25
26 $dbh->do("DROP TABLE IF EXISTS artist;");
27
28 $dbh->do("CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), charfield CHAR(10)) ENGINE=InnoDB;");
29
30 #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', '');
31
32 # This is in Core now, but it's here just to test that it doesn't break
33 $schema->class('Artist')->load_components('PK::Auto');
34
35 # test primary key handling
36 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
37 ok($new->artistid, "Auto-PK worked");
38
39 # test LIMIT support
40 for (1..6) {
41     $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
42 }
43 my $it = $schema->resultset('Artist')->search( {},
44     { rows => 3,
45       offset => 2,
46       order_by => 'artistid' }
47 );
48 is( $it->count, 3, "LIMIT count ok" );
49 is( $it->next->name, "Artist 2", "iterator->next ok" );
50 $it->next;
51 $it->next;
52 is( $it->next, undef, "next past end of resultset ok" );
53
54 my $test_type_info = {
55     'artistid' => {
56         'data_type' => 'INT',
57         'is_nullable' => 0,
58         'size' => 11,
59         'default_value' => undef,
60     },
61     'name' => {
62         'data_type' => 'VARCHAR',
63         'is_nullable' => 1,
64         'size' => 255,
65         'default_value' => undef,
66     },
67     'charfield' => {
68         'data_type' => 'CHAR',
69         'is_nullable' => 1,
70         'size' => 10,
71         'default_value' => undef,
72     },
73 };
74
75 $schema->txn_begin();
76
77 my $arty = $schema->resultset('Artist')->find(1);
78
79 my $name = $arty->name();
80
81 $schema->svp_begin('savepoint1');
82
83 cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled');
84
85 $arty->update({ name => 'Jheephizzy' });
86
87 $arty->discard_changes();
88
89 cmp_ok($arty->name(), 'eq', 'Jheephizzy', 'Name changed');
90
91 $schema->svp_rollback('savepoint1');
92
93 cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled');
94
95 $arty->discard_changes();
96
97 cmp_ok($arty->name(), 'eq', $name, 'Name rolled back');
98
99 $schema->txn_commit();
100
101 SKIP: {
102     my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
103     skip "Cannot determine MySQL server version", 1 if !$mysql_version;
104
105     my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/;
106     skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2);
107
108     $v3 ||= 0;
109
110     if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) {
111         $test_type_info->{charfield}->{data_type} = 'VARCHAR';
112     }
113
114     my $type_info = $schema->storage->columns_info_for('artist');
115     is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
116 }
117
118 # clean up our mess
119 END {
120     $dbh->do("DROP TABLE artist") if $dbh;
121 }