1#!/usr/bin/perl -w 2 3use strict; 4 5use Test::More tests => 1 + (3 * 4); 6 7BEGIN 8{ 9 require 't/test-lib.pl'; 10 use_ok('Rose::DB::Object::Loader'); 11} 12 13our %Have; 14 15# 16# Tests 17# 18 19#$Rose::DB::Object::Manager::Debug = 1; 20 21foreach my $db_type (qw(mysql pg informix sqlite)) 22{ 23 SKIP: 24 { 25 skip("$db_type tests", 3) unless($Have{$db_type}); 26 } 27 28 next unless($Have{$db_type}); 29 30 Rose::DB::Object::Metadata->unregister_all_classes; 31 Rose::DB->default_type($db_type); 32 33 my $class_prefix = ucfirst($db_type); 34 35 my $foo_class = $class_prefix . '::Foo'; 36 my $bar_class = $class_prefix . '::Bar'; 37 38 my $auto = (rand() >= 0.5) ? 'auto_initialize => [],' : 'auto => 1,'; 39 40 my $perl=<<"EOF"; 41{ 42 package $foo_class; 43 use base qw(Rose::DB::Object); 44 __PACKAGE__->meta->setup 45 ( 46 table => 'foos', 47 $auto 48 ); 49 50 package $bar_class; 51 use base qw(Rose::DB::Object); 52 __PACKAGE__->meta->setup 53 ( 54 table => 'bars', 55 $auto 56 ); 57} 58EOF 59 60 eval $perl or warn $@; 61 62 is($foo_class->meta->relationship('bar')->type, 'one to one', "check rel type - $db_type"); 63 64 my $bar = $bar_class->new; 65 my $foo = $foo_class->new(foo => 'xyz'); 66 67 #$Rose::DB::Object::Debug = 1; 68 69 $foo->bar($bar); 70 $foo->bar->bar('some text'); 71 $foo->save; 72 73 my $check_foo = $foo_class->new(id => $foo->id)->load; 74 my $check_bar = $bar_class->new(foo_id => $bar->foo_id)->load; 75 76 is($check_foo->foo, 'xyz', "check foo - $db_type"); 77 is($check_bar->bar, 'some text', "check bar - $db_type"); 78} 79 80BEGIN 81{ 82 our %Have; 83 84 # 85 # PostgreSQL 86 # 87 88 my $dbh; 89 90 eval 91 { 92 $dbh = Rose::DB->new('pg_admin')->retain_dbh() 93 or die Rose::DB->error; 94 }; 95 96 if(!$@ && $dbh) 97 { 98 $Have{'pg'} = 1; 99 $Have{'pg_with_schema'} = 1; 100 101 # Drop existing tables and create schema, ignoring errors 102 { 103 local $dbh->{'RaiseError'} = 0; 104 local $dbh->{'PrintError'} = 0; 105 106 $dbh->do('DROP TABLE bars CASCADE'); 107 $dbh->do('DROP TABLE foos CASCADE'); 108 } 109 110 $dbh->do(<<"EOF"); 111CREATE TABLE foos 112( 113 id SERIAL NOT NULL PRIMARY KEY, 114 foo VARCHAR(255) 115) 116EOF 117 118 $dbh->do(<<"EOF"); 119CREATE TABLE bars 120( 121 foo_id INT NOT NULL PRIMARY KEY REFERENCES foos (id), 122 bar VARCHAR(255) 123) 124EOF 125 126 $dbh->disconnect; 127 } 128 129 # 130 # MySQL 131 # 132 133 eval 134 { 135 my $db = Rose::DB->new('mysql_admin'); 136 $dbh = $db->retain_dbh or die Rose::DB->error; 137 138 die "No innodb support" unless(mysql_supports_innodb()); 139 140 # Drop existing tables, ignoring errors 141 { 142 local $dbh->{'RaiseError'} = 0; 143 local $dbh->{'PrintError'} = 0; 144 145 $dbh->do('DROP TABLE bars CASCADE'); 146 $dbh->do('DROP TABLE foos CASCADE'); 147 } 148 }; 149 150 if(!$@ && $dbh) 151 { 152 $Have{'mysql'} = 1; 153 154 $dbh->do(<<"EOF"); 155CREATE TABLE foos 156( 157 id INT AUTO_INCREMENT PRIMARY KEY, 158 foo VARCHAR(255) 159) 160ENGINE=InnoDB 161EOF 162 163 $dbh->do(<<"EOF"); 164CREATE TABLE bars 165( 166 foo_id INT PRIMARY KEY, 167 bar VARCHAR(255), 168 169 INDEX(foo_id), 170 171 FOREIGN KEY (foo_id) REFERENCES foos (id) 172) 173ENGINE=InnoDB 174EOF 175 176 $dbh->disconnect; 177 } 178 179 # 180 # Informix 181 # 182 183 eval 184 { 185 $dbh = Rose::DB->new('informix_admin')->retain_dbh() 186 or die Rose::DB->error; 187 }; 188 189 if(!$@ && $dbh) 190 { 191 $Have{'informix'} = 1; 192 193 # Drop existing tables and create schema, ignoring errors 194 { 195 local $dbh->{'RaiseError'} = 0; 196 local $dbh->{'PrintError'} = 0; 197 198 $dbh->do('DROP TABLE bars CASCADE'); 199 $dbh->do('DROP TABLE foos CASCADE'); 200 } 201 202 $dbh->do(<<"EOF"); 203CREATE TABLE foos 204( 205 id SERIAL NOT NULL PRIMARY KEY, 206 foo VARCHAR(255) 207) 208EOF 209 210 $dbh->do(<<"EOF"); 211CREATE TABLE bars 212( 213 foo_id INT NOT NULL PRIMARY KEY REFERENCES foos (id), 214 bar VARCHAR(255) 215) 216EOF 217 218 $dbh->disconnect; 219 } 220 221 # 222 # SQLite 223 # 224 225 eval 226 { 227 $dbh = Rose::DB->new('sqlite_admin')->retain_dbh() 228 or die Rose::DB->error; 229 }; 230 231 if(!$@ && $dbh) 232 { 233 $Have{'sqlite'} = 1; 234 235 # Drop existing tables and create schema, ignoring errors 236 { 237 local $dbh->{'RaiseError'} = 0; 238 local $dbh->{'PrintError'} = 0; 239 240 $dbh->do('DROP TABLE bars'); 241 $dbh->do('DROP TABLE foos'); 242 } 243 244 $dbh->do(<<"EOF"); 245CREATE TABLE foos 246( 247 id INTEGER PRIMARY KEY AUTOINCREMENT, 248 foo VARCHAR(255) 249) 250EOF 251 252 $dbh->do(<<"EOF"); 253CREATE TABLE bars 254( 255 foo_id INTEGER PRIMARY KEY AUTOINCREMENT REFERENCES foos (id), 256 bar VARCHAR(255) 257) 258EOF 259 260 $dbh->disconnect; 261 } 262} 263 264END 265{ 266 # Delete test table 267 268 if($Have{'pg'}) 269 { 270 # PostgreSQL 271 my $dbh = Rose::DB->new('pg_admin')->retain_dbh() 272 or die Rose::DB->error; 273 274 $dbh->do('DROP TABLE bars CASCADE'); 275 $dbh->do('DROP TABLE foos CASCADE'); 276 277 $dbh->disconnect; 278 } 279 280 if($Have{'mysql'}) 281 { 282 # MySQL 283 my $dbh = Rose::DB->new('mysql_admin')->retain_dbh() 284 or die Rose::DB->error; 285 286 $dbh->do('DROP TABLE bars CASCADE'); 287 $dbh->do('DROP TABLE foos CASCADE'); 288 289 $dbh->disconnect; 290 } 291 292 if($Have{'informix'}) 293 { 294 # Informix 295 my $dbh = Rose::DB->new('informix_admin')->retain_dbh() 296 or die Rose::DB->error; 297 298 $dbh->do('DROP TABLE bars CASCADE'); 299 $dbh->do('DROP TABLE foos CASCADE'); 300 301 $dbh->disconnect; 302 } 303 304 if($Have{'sqlite'}) 305 { 306 # Informix 307 my $dbh = Rose::DB->new('sqlite_admin')->retain_dbh() 308 or die Rose::DB->error; 309 310 $dbh->do('DROP TABLE bars'); 311 $dbh->do('DROP TABLE foos'); 312 313 $dbh->disconnect; 314 } 315} 316