| 200 | | for my $class (qw( Foo Bar )) { |
| 201 | | my $driver = $class->dbi_driver; |
| 202 | | my $dbh = $driver->rw_handle; |
| 203 | | my $ddl_class = $driver->dbd->ddl_class; |
| 204 | | |
| 205 | | $dbh->do($ddl_class->drop_table_sql($class)) or die $dbh->errstr; |
| 206 | | $dbh->do($ddl_class->create_table_sql($class)) or die $dbh->errstr; |
| 207 | | $dbh->do($_) or die $dbh->errstr for $ddl_class->index_table_sql($class); |
| 208 | | $ddl_class->create_sequence($class); # may do nothing |
| 209 | | } |
| 210 | | } |
| | 199 | MT::Test->reset_table_for(qw( Foo Bar )); |
| | 200 | } |
| | 201 | |
| | 202 | |
| | 203 | package Test::Search; |
| | 204 | use MT::Test; |
| | 205 | use base qw( Test::Class MT::Test ); |
| | 206 | |
| | 207 | sub make_foos : Test(setup) { |
| | 208 | MT::Test->reset_table_for('Foo'); |
| | 209 | |
| | 210 | my $foo = Foo->new; |
| | 211 | $foo->set_values({ |
| | 212 | name => 'foo', |
| | 213 | status => 2, |
| | 214 | text => 'bar', |
| | 215 | }); |
| | 216 | $foo->save or die "Could not save test Foo: ", $foo->errstr, "\n"; |
| | 217 | } |
| | 218 | |
| | 219 | sub basic : Tests(5) { |
| | 220 | my $foo = Foo->load(1); # not a search |
| | 221 | |
| | 222 | is_object(scalar Foo->load({ id => 1 }), $foo, 'Foo #1 by id hash is Foo #1'); |
| | 223 | is_object(scalar Foo->load({ id => 1, name => 'foo' }), $foo, 'Foo #1 by id-name hash is Foo #1'); |
| | 224 | is_object(scalar Foo->load({ name => 'foo' }), $foo, 'Foo #1 by name hash is Foo #1'); |
| | 225 | is_object(scalar Foo->load({ created_on => $foo->created_on }), $foo, 'Foo #1 by created_on hash is Foo #1'); |
| | 226 | is_object(scalar Foo->load({ status => 2 }), $foo, 'Foo #1 by status hash is Foo #1'); |
| | 227 | } |
| | 228 | |
| | 229 | sub unmake_foos : Test(teardown) { |
| | 230 | MT::Test->reset_table_for('Foo'); |
| | 231 | } |
| | 232 | |
| 240 | | sub _is_object { |
| 241 | | my ($got, $expected, $name) = @_; |
| 242 | | |
| 243 | | if (!defined $got) { |
| 244 | | fail($name); |
| 245 | | diag(' got undef, not an object'); |
| 246 | | return; |
| 247 | | } |
| 248 | | |
| 249 | | if (!$got->isa(ref $expected)) { |
| 250 | | fail($name); |
| 251 | | diag(' got a ', ref($got), ' but expected a ', ref $expected); |
| 252 | | return; |
| 253 | | } |
| 254 | | |
| 255 | | if ($got == $expected) { |
| 256 | | fail($name); |
| 257 | | diag(' got the exact same instance as expected, when really expected a different but equivalent object'); |
| 258 | | return; |
| 259 | | } |
| 260 | | |
| 261 | | # Ignore object columns that have undefined values. |
| 262 | | my (%got_values, %expected_values); |
| 263 | | while (my ($field, $value) = each %{ $got->{column_values} }) { |
| 264 | | $got_values{$field} = $value if defined $value; |
| 265 | | } |
| 266 | | while (my ($field, $value) = each %{ $expected->{column_values} }) { |
| 267 | | $expected_values{$field} = $value if defined $value; |
| 268 | | } |
| 269 | | |
| 270 | | if (!eq_deeply(\%got_values, \%expected_values)) { |
| 271 | | # 'Test' again so the helpful failure diagnostics are output. |
| 272 | | is_deeply(\%got_values, \%expected_values, $name); |
| 273 | | return; |
| 274 | | } |
| 275 | | |
| 276 | | return 1; |
| 277 | | } |
| 278 | | |
| 279 | | sub is_object { |
| 280 | | my ($got, $expected, $name) = @_; |
| 281 | | pass($name) if _is_object(@_); |
| 282 | | } |
| 283 | | |
| 284 | | sub are_objects { |
| 285 | | my ($got, $expected, $name) = @_; |
| 286 | | |
| 287 | | my $count = scalar @$expected; |
| 288 | | if ($count != scalar @$got) { |
| 289 | | fail($name); |
| 290 | | diag(' got ', scalar(@$got), ' objects but expected ', $count); |
| 291 | | return; |
| 292 | | } |
| 293 | | |
| 294 | | for my $i (0..$count-1) { |
| 295 | | return if !_is_object($$got[$i], $$expected[$i], "$name (#$i)"); |
| 296 | | } |
| 297 | | pass($name); |
| 298 | | } |
| 299 | | |
| 301 | | is_object(scalar Foo->load({ id => 1 }), $foo, 'Foo #1 by id hash is Foo #1'); |
| 302 | | is_object(scalar Foo->load({ id => 1, name => 'foo' }), $foo, 'Foo #1 by id-name hash is Foo #1'); |
| 303 | | is_object(scalar Foo->load({ name => 'foo' }), $foo, 'Foo #1 by name hash is Foo #1'); |
| 304 | | is_object(scalar Foo->load({ created_on => $foo->created_on }), $foo, 'Foo #1 by created_on hash is Foo #1'); |
| 305 | | is_object(scalar Foo->load({ status => 2 }), $foo, 'Foo #1 by status hash is Foo #1'); |