Changeset 2153
- Timestamp:
- 04/29/08 22:37:19 (7 months ago)
- Files:
-
- branches/release-36/t/driver-tests.pl (modified) (15 diffs)
- branches/release-36/t/lib/Bar.pm (modified) (1 diff)
- branches/release-36/t/lib/Foo.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/release-36/t/driver-tests.pl
r2144 r2153 30 30 } 31 31 32 use constant TESTS => 2 23;32 use constant TESTS => 245; 33 33 34 34 use Test::More; … … 88 88 is($foo[0]->column('id'), $foo[0]->id, 'First Foo was given an id of 1, says column()'); 89 89 90 sub is_ first_foo{91 my ($ obj, $name) = @_;92 93 isa_ok($ obj, 'Foo', $name);94 isnt($ obj, $foo[0], "$name is not the same instance as the first Foo");90 sub is_object { 91 my ($got, $expected, $name) = @_; 92 93 isa_ok($got, ref $expected, $name); 94 isnt($got, $expected, "$name is not the same instance as the expected object"); 95 95 96 96 # Ignore object columns that have undefined values. 97 # TODO: test that the objects are unequal unless we ignore undef members 98 my %obj_values; 99 while (my ($field, $value) = each %{ $obj->{column_values} }) { 100 $obj_values{$field} = $value if defined $value; 97 my (%got_values, %expected_values); 98 while (my ($field, $value) = each %{ $got->{column_values} }) { 99 $got_values{$field} = $value if defined $value; 101 100 } 102 103 is_deeply(\%obj_values, $foo[0]->{column_values}, "$name is equivalent to the first Foo"); 104 } 105 106 is_first_foo(scalar Foo->load(1), 'Foo #1 by id'); 107 is_first_foo(scalar Foo->load({ id => 1 }), 'Foo #1 by id hash'); 108 is_first_foo(scalar Foo->load({ id => 1, name => 'foo' }), 'Foo #1 by id-name hash'); 109 is_first_foo(scalar Foo->load({ name => 'foo' }), 'Foo #1 by name hash'); 110 is_first_foo(scalar Foo->load({ created_on => $foo[0]->created_on }), 'Foo #1 by created_on hash'); 111 is_first_foo(scalar Foo->load({ status => 2 }), 'Foo #1 by status hash'); 101 while (my ($field, $value) = each %{ $expected->{column_values} }) { 102 $expected_values{$field} = $value if defined $value; 103 } 104 105 is_deeply(\%got_values, \%expected_values, "$name is equivalent to the expected object"); 106 } 107 108 is_object(scalar Foo->load(1), $foo[0], 'Foo #1 by id'); 109 is_object(scalar Foo->load({ id => 1 }), $foo[0], 'Foo #1 by id hash'); 110 is_object(scalar Foo->load({ id => 1, name => 'foo' }), $foo[0], 'Foo #1 by id-name hash'); 111 is_object(scalar Foo->load({ name => 'foo' }), $foo[0], 'Foo #1 by name hash'); 112 is_object(scalar Foo->load({ created_on => $foo[0]->created_on }), $foo[0], 'Foo #1 by created_on hash'); 113 is_object(scalar Foo->load({ status => 2 }), $foo[0], 'Foo #1 by status hash'); 112 114 113 115 ## Change column value, save, try to load using old value (fail?), … … 118 120 ok(!$tmp, 'Foo #1 no longer loads with old status (2)'); 119 121 $tmp = Foo->load({ status => 0 }); 120 isa_ok($tmp, 'Foo', 'Foo #1 by new status (0)'); 121 is($tmp->id, 1, 'Foo #1 still has id of 1'); 122 is($tmp->status, 0, 'Foo #1 has its new status (0)'); 122 is_object($tmp, $foo[0], 'Foo #1 by new status (0)'); 123 123 124 124 ## Create a new object so we can do range and last/first lookups. … … 137 137 ## Load all objects via iterator 138 138 my $iter = Foo->load_iter(undef, { sort => 'created_on', direction => 'ascend' }); 139 isa_ok($iter, 'CODE', " load_iter() for all Foos returned an iterator");139 isa_ok($iter, 'CODE', "Iterator for all Foos"); 140 140 ok($tmp = $iter->(), 'Iterator for our two Foos had one object'); 141 is ($tmp->id, $foo[0]->id, "All Foo iterator's first Foo was Foo #1");141 is_object($tmp, $foo[0], "All Foo iterator's first Foo"); 142 142 ok($tmp = $iter->(), 'Iterator for our two Foos had two objects'); 143 is ($tmp->id, $foo[1]->id, "All Foo iterator's second Foo was Foo #2");143 is_object($tmp, $foo[1], "All Foo iterator's second Foo"); 144 144 ok(!$iter->(), 'Iterator for our two Foos did not have a third object'); 145 145 146 146 ## Load all objects with status == 1 via iterator 147 147 $iter = Foo->load_iter({ status => 1 }); 148 isa_ok($iter, 'CODE', " load_iter() for status=1 Foos returned an iterator");148 isa_ok($iter, 'CODE', "Iterator for status=1 Foos"); 149 149 ok($tmp = $iter->(), 'Iterator for our status=1 Foos had one object'); 150 is ($tmp->id, $foo[1]->id, "Status=1 Foo iterator's first Foo was Foo #2");150 is_object($tmp, $foo[1], "Status=1 Foo iterator's first Foo"); 151 151 ok(!$iter->(), "Iterator for our status=1 Foos did not have a second object"); 152 153 # TODO: didn't we already do these tests?154 ## Load using ID155 $tmp = Foo->load($foo[1]->id);156 isa_ok($iter, 'CODE'); # TODO: useless test157 is($foo[1]->id, $tmp->id, 'id');158 is($foo[1]->name, $tmp->name, 'name');159 is($foo[1]->text, $tmp->text, 'text');160 161 ## Load using single-column index162 $tmp = Foo->load({ name => $foo[1]->name, });163 isa_ok($iter, 'CODE'); # TODO: useless test164 is($foo[1]->id, $tmp->id, 'id');165 152 166 153 ## Load using non-existent ID (should fail) … … 173 160 direction => 'descend', 174 161 limit => 1 }); 175 is ($tmp->id, 2, 'id');162 is_object($tmp, $foo[1], 'Newest Foo'); 176 163 177 164 ## Load using ascending sort (oldest) … … 180 167 direction => 'ascend', 181 168 limit => 1 }); 182 is($tmp->id, 1, 'id'); 169 is_object($tmp, $foo[0], 'Oldest Foo'); 170 171 sub are_objects { 172 my ($got, $expected, $name) = @_; 173 174 my $count = scalar @$expected; 175 is(scalar @$got, $count, "$name got $count objects"); 176 is_object($$got[$_], $$expected[$_], "$name #$_") for (0..$count-1); 177 } 183 178 184 179 ## Load using descending sort with limit = 2 … … 187 182 direction => 'descend', 188 183 limit => 2 }); 189 is(@tmp, 2, 'array length 2'); 190 is($tmp[0]->id, 2, 'id'); 191 is($tmp[1]->id, 1, 'id'); 184 are_objects(\@tmp, [ reverse @foo ], 'Two Foos newest-first load()'); 192 185 193 186 ## Load using descending sort by created_on, no limit … … 195 188 sort => 'created_on', 196 189 direction => 'descend' }); 197 is(@tmp, 2, 'array length 2'); 198 is($tmp[0]->id, 2, 'id'); 199 is($tmp[1]->id, 1, 'id'); 190 are_objects(\@tmp, [ reverse @foo ], 'All Foos newest-first load()'); 200 191 201 192 ## Load using ascending sort by status, no limit 202 193 @tmp = Foo->load(undef, { sort => 'status', }); 203 is(@tmp, 2, 'array length 2'); 204 is($tmp[0]->id, 1, 'id'); 205 is($tmp[1]->id, 2, 'id'); 194 are_objects(\@tmp, \@foo, 'All Foos lowest-status-first load()'); 206 195 207 196 ## Load using 'last' where status == 0 … … 210 199 direction => 'descend', 211 200 limit => 1 }); 212 is ($tmp->id, 1, 'id');201 is_object($tmp, $foo[0], 'Newest status=0 Foo'); 213 202 214 203 ## Load using range search, one less than foo[1]->created_on and newer … … 216 205 { created_on => [ $foo[1]->column('created_on')-1 ] }, 217 206 { range => { created_on => 1 } }); 218 isa_ok($tmp, 'Foo'); 219 is($tmp->id, 2, 'id'); 207 is_object($tmp, $foo[1], 'Foo from open-ended date range before Foo #2'); 220 208 221 209 ## Load using EXCLUSIVE range search, up through the momment $foo[1] created … … 224 212 $foo[1]->column('created_on') ] }, 225 213 { range => { created_on => 1 } }); 226 ok(!$tmp, 'no Foo');214 ok(!$tmp, "Exclusive date range load() ending at Foo #1's date found no Foos"); 227 215 228 216 $tmp = Foo->load( … … 230 218 $foo[1]->column('created_on')+1 ] }, 231 219 { range => { created_on => 1 } }); 232 ok(!$tmp, 'no Foo');220 ok(!$tmp, "Exclusive date range load() starting at Foo #1's date found no Foos"); 233 221 234 222 ## Load using INCLUSIVE range search, up through the momment $foo[1] created … … 238 226 { range_incl => { created_on => 1 } }); 239 227 ok($tmp, 'Loaded an object based on range_incl (ts-1 to ts)'); 240 is ($tmp->id, 2, 'id');228 is_object($tmp, $foo[1], "Foo from inclusive date-range load() ending at Foo #1's date"); 241 229 242 230 $tmp = Foo->load( … … 245 233 { range_incl => { created_on => 1 } }); 246 234 ok($tmp, 'Loaded an object based on range_incl (ts to ts+1)'); 247 is ($tmp->id, 2, 'id');235 is_object($tmp, $foo[1], "Foo from inclusive date-range load() starting at Foo #1's date"); 248 236 249 237 ## Check that range searches return nothing when nothing is in the range. 250 238 $tmp = Foo->load( { created_on => [ undef, '19690101000000' ] }, 251 239 { range => { created_on => 1 } }); 252 ok(!$tmp, ' no Foo');240 ok(!$tmp, 'Prehistoric date range load() found no Foos'); 253 241 254 242 ## Range search, all items with created_on less than foo[1]->created_on … … 256 244 { created_on => [ undef, $foo[1]->column('created_on')-1 ] }, 257 245 { range => { created_on => 1 } }); 258 isa_ok($tmp, 'Foo'); 259 is($tmp->id, 1, 'id'); 260 261 ## Range search, all items with created_on >= to foo[1]->created_on 262 $tmp = Foo->load( 246 is_object($tmp, $foo[0], "Foo from exclusive open-started date-range load() ending before Foo #1"); 247 248 ## Get count of objects 249 is(Foo->count(), 2, 'Count of all Foos finds both'); 250 is(Foo->count({ status => 0 }), 1, 'Count of all status=0 Foos finds all one'); 251 my $ranged_count = Foo->count( 263 252 { created_on => [ $foo[1]->column('created_on')-1 ] }, 264 { range_incl => { created_on => 1 } }); 265 isa_ok($tmp, 'Foo'); 266 is($tmp->id, 2, 'id'); 267 268 ## Get count of objects 269 is(Foo->count, 2, 'count1'); 270 is(Foo->count({ status => 0 }), 1, 'count2'); 271 is(Foo->count( 272 { created_on => [ $foo[1]->column('created_on')-1 ] }, 273 { range => { created_on => 1 } }), 1, 'count3'); 253 { range => { created_on => 1 } } 254 ); 255 is($ranged_count, 1, 'Count of all Foos in open-ended date range starting before Foo #1 finds all one'); 274 256 275 257 ## Update status for later tests. branches/release-36/t/lib/Bar.pm
r1098 r2153 22 22 datasource => 'bar', 23 23 primary_key => 'id', 24 cacheable => 0, 24 25 }); 25 26 branches/release-36/t/lib/Foo.pm
r1098 r2153 22 22 datasource => 'foo', 23 23 primary_key => 'id', 24 cacheable => 0, 24 25 }); 25 26
