Changeset 2584
- Timestamp:
- 06/16/08 21:25:31 (20 months ago)
- Location:
- branches/release-40/t
- Files:
-
- 2 modified
-
driver-tests.pl (modified) (3 diffs)
-
lib/MT/Test.pm (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/release-40/t/driver-tests.pl
r2574 r2584 24 24 25 25 use Test::More; 26 use Test::Deep;27 26 use lib 't/lib'; 28 27 … … 198 197 199 198 sub clean_db : Test(teardown) { 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 211 233 212 234 package main; 213 214 Test::GroupBy->runtests( +152 ); 235 use MT::Test; 236 237 Test::Class->runtests(qw( Test::GroupBy Test::Search +147 )); 215 238 216 239 my($foo, @foo, @bar); … … 238 261 is($foo->column('id'), 1, 'First Foo was given an id of 1, says column()'); 239 262 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 300 263 is_object(scalar Foo->load(1), $foo, 'Foo #1 by id is Foo #1'); 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');306 264 307 265 ## Change column value, save, try to load using old value (fail?), -
branches/release-40/t/lib/MT/Test.pm
r2578 r2584 1 1 package MT::Test; 2 use base qw( Exporter ); 3 4 our @EXPORT = qw( is_object are_objects ); 2 5 3 6 use strict; … … 8 11 9 12 use Test::More; 13 use Test::Deep qw( eq_deeply ); 10 14 11 15 BEGIN { … … 43 47 sub import { 44 48 my $pkg = shift; 49 my @to_export; 50 # TODO: only use these init_* calls as calls, not as import args, now that we have real functions to export. 45 51 foreach my $opt (@_) { 46 52 if ($opt =~ m{ \A : (.+) \z }xms) { … … 48 54 $pkg->$command() if $pkg->can($command); 49 55 } 50 } 56 else { 57 push @to_export, $opt; 58 } 59 } 60 # Export requested or all exportable functions. 61 $pkg->export_to_level(1, @to_export || qw( :DEFAULT )); 51 62 } 52 63 … … 926 937 } 927 938 939 sub _is_object { 940 my ($got, $expected, $name) = @_; 941 942 if (!defined $got) { 943 fail($name); 944 diag(' got undef, not an object'); 945 return; 946 } 947 948 if (!$got->isa(ref $expected)) { 949 fail($name); 950 diag(' got a ', ref($got), ' but expected a ', ref $expected); 951 return; 952 } 953 954 if ($got == $expected) { 955 fail($name); 956 diag(' got the exact same instance as expected, when really expected a different but equivalent object'); 957 return; 958 } 959 960 # Ignore object columns that have undefined values. 961 my (%got_values, %expected_values); 962 while (my ($field, $value) = each %{ $got->{column_values} }) { 963 $got_values{$field} = $value if defined $value; 964 } 965 while (my ($field, $value) = each %{ $expected->{column_values} }) { 966 $expected_values{$field} = $value if defined $value; 967 } 968 969 if (!eq_deeply(\%got_values, \%expected_values)) { 970 # 'Test' again so the helpful failure diagnostics are output. 971 is_deeply(\%got_values, \%expected_values, $name); 972 return; 973 } 974 975 return 1; 976 } 977 978 sub is_object { 979 my ($got, $expected, $name) = @_; 980 pass($name) if _is_object(@_); 981 } 982 983 sub are_objects { 984 my ($got, $expected, $name) = @_; 985 986 my $count = scalar @$expected; 987 if ($count != scalar @$got) { 988 fail($name); 989 diag(' got ', scalar(@$got), ' objects but expected ', $count); 990 return; 991 } 992 993 for my $i (0..$count-1) { 994 return if !_is_object($$got[$i], $$expected[$i], "$name (#$i)"); 995 } 996 pass($name); 997 } 998 999 sub reset_table_for { 1000 my $self = shift; 1001 for my $class (@_) { 1002 my $driver = $class->dbi_driver; 1003 my $dbh = $driver->rw_handle; 1004 my $ddl_class = $driver->dbd->ddl_class; 1005 1006 $dbh->do($ddl_class->drop_table_sql($class)) or die $dbh->errstr; 1007 $dbh->do($ddl_class->create_table_sql($class)) or die $dbh->errstr; 1008 $dbh->do($_) or die $dbh->errstr for $ddl_class->index_table_sql($class); 1009 $ddl_class->create_sequence($class); # may do nothing 1010 } 1011 } 1012 928 1013 1;
