root/branches/release-38/t/driver-tests.pl @ 2286

Revision 2286, 25.7 kB (checked in by mpaschal, 19 months ago)

Elaborate are_objects to really be one test per call (by factoring out most of is_object instead of calling it directly)
Test some explicit cross-column joins

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl
2
3# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
4# This program is distributed under the terms of the
5# GNU General Public License, version 2.
6#
7# $Id$
8
9use strict;
10use warnings;
11use Data::Dumper;
12use English qw( -no_match_vars );
13
14$OUTPUT_AUTOFLUSH = 1;
15
16# Run this script as a symlink, in the form of 99-driver.t, ie:
17# ln -s driver-tests.pl 99-driver.t
18
19BEGIN {
20    # Set config to driver-test.cfg when run as /path/to/99-driver.t
21    $ENV{MT_CONFIG} = "$1-test.cfg"
22        if __FILE__ =~ m{ [\\/] \d+- ([^\\/]+) \.t \z }xms;
23}
24
25use Test::More;
26use Test::Deep;
27use lib 't/lib';
28use MT::Test qw(:testdb :time);
29
30BEGIN {
31    plan skip_all => "Configuration file $ENV{MT_CONFIG} not found"
32        if !-r $ENV{MT_CONFIG};
33}
34
35plan tests => 184;
36
37package Zot;
38use base 'MT::Object';
39__PACKAGE__->install_properties({
40    column_defs => {
41        'id' => 'integer not null auto_increment',
42        'x' => 'string(255)',
43    },
44    primary_key => 'id',
45    datasource => 'zot',
46});
47
48package main;
49
50my(@foo, @bar);
51my($tmp, @tmp);
52
53# Test for existing table
54ok(MT::Object->driver->dbd->ddl_class->column_defs('Foo'), "table mt_foo exists after upgrade");
55# Test for non-existent table
56ok(!MT::Object->driver->dbd->ddl_class->column_defs('Zot'), "table mt_zot does not exist after upgrade where undefined");
57
58## Test creating object with new
59##     test column access through column, then through AUTOLOAD
60$foo[0] = Foo->new;
61isa_ok($foo[0], 'Foo', 'New Foo');
62$foo[0]->column('name', 'foo');
63is($foo[0]->column('name'), 'foo', 'Setting name field with column() persists through access');
64$foo[0]->name('foo');
65is($foo[0]->name, 'foo', 'Setting name field with mutator method persists through access');
66$foo[0]->status(2);
67$foo[0]->text('bar');
68
69## Test saving created object
70ok($foo[0]->save, 'A Foo could be saved');
71is($foo[0]->id, 1, 'First Foo was given an id of 1, says accessor method');
72is($foo[0]->column('id'), $foo[0]->id, 'First Foo was given an id of 1, says column()');
73
74sub _is_object {
75    my ($got, $expected, $name) = @_;
76
77    if (!defined $got) {
78        fail($name);
79        diag('    got undef, not an object');
80        return;
81    }
82
83    if (!$got->isa(ref $expected)) {
84        fail($name);
85        diag('    got a ', ref($got), ' but expected a ', ref $expected);
86        return;
87    }
88
89    if ($got == $expected) {
90        fail($name);
91        diag('    got the exact same instance as expected, when really expected a different but equivalent object');
92        return;
93    }
94
95    # Ignore object columns that have undefined values.
96    my (%got_values, %expected_values);
97    while (my ($field, $value) = each %{ $got->{column_values} }) {
98        $got_values{$field} = $value if defined $value;
99    }
100    while (my ($field, $value) = each %{ $expected->{column_values} }) {
101        $expected_values{$field} = $value if defined $value;
102    }
103
104    if (!eq_deeply(\%got_values, \%expected_values)) {
105        # 'Test' again so the helpful failure diagnostics are output.
106        is_deeply(\%got_values, \%expected_values, $name);
107        return;
108    }
109
110    return 1;
111}
112
113sub is_object {
114    my ($got, $expected, $name) = @_;
115    pass($name) if _is_object(@_);
116}
117
118sub are_objects {
119    my ($got, $expected, $name) = @_;
120
121    my $count = scalar @$expected;
122    if ($count != scalar @$got) {
123        fail($name);
124        diag('    got ', scalar(@$got), ' objects but expected ', $count);
125        return;
126    }
127
128    for my $i (0..$count-1) {
129        return if !_is_object($$got[$i], $$expected[$i], "$name (#$i)");
130    }
131    pass($name);
132}
133
134is_object(scalar Foo->load(1), $foo[0], 'Foo #1 by id is Foo #1');
135is_object(scalar Foo->load({ id => 1 }), $foo[0], 'Foo #1 by id hash is Foo #1');
136is_object(scalar Foo->load({ id => 1, name => 'foo' }), $foo[0], 'Foo #1 by id-name hash is Foo #1');
137is_object(scalar Foo->load({ name => 'foo' }), $foo[0], 'Foo #1 by name hash is Foo #1');
138is_object(scalar Foo->load({ created_on => $foo[0]->created_on }), $foo[0], 'Foo #1 by created_on hash is Foo #1');
139is_object(scalar Foo->load({ status => 2 }), $foo[0], 'Foo #1 by status hash is Foo #1');
140
141##     Change column value, save, try to load using old value (fail?),
142##     then load again using new value
143$foo[0]->status(0);
144ok($foo[0]->save, 'Foo #1 saved with new status (0)');
145$tmp = Foo->load({ status => 2 });
146ok(!$tmp, 'Foo #1 no longer loads with old status (2)');
147$tmp = Foo->load({ status => 0 });
148is_object($tmp, $foo[0], 'Foo #1 by new status (0) is Foo #1');
149
150## Create a new object so we can do range and last/first lookups.
151## Sleep first so that they get different created_on timestamps.
152sleep(3);
153
154## Create new object for iterator testing
155$foo[1] = Foo->new;
156$foo[1]->name('baz');
157$foo[1]->text('quux');
158$foo[1]->status(1);
159$foo[1]->save;
160
161## TEST LOADING IN VARIOUS WAYS
162
163## Load all objects via iterator
164my $iter = Foo->load_iter(undef, { sort => 'created_on', direction => 'ascend' });
165isa_ok($iter, 'CODE', "Iterator for all Foos");
166ok($tmp = $iter->(), 'Iterator for our two Foos had one object');
167is_object($tmp, $foo[0], "All Foo iterator's first Foo is Foo #1");
168ok($tmp = $iter->(), 'Iterator for our two Foos had two objects');
169is_object($tmp, $foo[1], "All Foo iterator's second Foo is Foo #2");
170ok(!$iter->(), 'Iterator for our two Foos did not have a third object');
171
172## Load all objects with status == 1 via iterator
173$iter = Foo->load_iter({ status => 1 });
174isa_ok($iter, 'CODE', "Iterator for status=1 Foos");
175ok($tmp = $iter->(), 'Iterator for our status=1 Foos had one object');
176is_object($tmp, $foo[1], "Status=1 Foo iterator's first Foo is Foo #2");
177ok(!$iter->(), "Iterator for our status=1 Foos did not have a second object");
178
179## Load using non-existent ID (should fail)
180$tmp = Foo->load(3);
181ok(!$tmp, 'There is no Foo #3');
182
183## Load using descending sort (newest)
184$tmp = Foo->load(undef, {
185    sort => 'created_on',
186    direction => 'descend',
187    limit => 1 });
188is_object($tmp, $foo[1], 'Newest Foo is Foo #2');
189
190## Load using ascending sort (oldest)
191$tmp = Foo->load(undef, {
192    sort => 'created_on',
193    direction => 'ascend',
194    limit => 1 });
195is_object($tmp, $foo[0], 'Oldest Foo is Foo #1');
196
197## Load using descending sort with limit = 2
198@tmp = Foo->load(undef, {
199    sort => 'created_on',
200    direction => 'descend',
201    limit => 2 });
202are_objects(\@tmp, [ reverse @foo ], 'Two Foos newest-first load() finds Foos #2 and #1');
203
204## Load using descending sort by created_on, no limit
205@tmp = Foo->load(undef, {
206    sort => 'created_on',
207    direction => 'descend' });
208are_objects(\@tmp, [ reverse @foo ], 'All Foos newest-first load() finds Foos #2 and #1');
209
210## Load using ascending sort by status, no limit
211@tmp = Foo->load(undef, { sort => 'status', });
212are_objects(\@tmp, \@foo, 'All Foos lowest-status-first load() finds Foos #1 and #2');
213
214## Load using 'last' where status == 0
215$tmp = Foo->load({ status => 0 }, {
216    sort => 'created_on',
217    direction => 'descend',
218    limit => 1 });
219is_object($tmp, $foo[0], 'Newest status=0 Foo is Foo #1');
220
221## Load using range search, one less than foo[1]->created_on and newer
222$tmp = Foo->load(
223    { created_on => [ $foo[1]->column('created_on')-1 ] },
224    { range => { created_on => 1 } });
225is_object($tmp, $foo[1], 'Foo from open-ended date range before Foo #2 is Foo #2');
226
227## Load using EXCLUSIVE range search, up through the momment $foo[1] created
228$tmp = Foo->load(
229    { created_on => [ $foo[1]->column('created_on')-1, 
230                      $foo[1]->column('created_on') ] },
231    { range => { created_on => 1 } });
232ok(!$tmp, "Exclusive date range load() ending at Foo #1's date found no Foos");
233
234$tmp = Foo->load(
235    { created_on => [ $foo[1]->column('created_on'), 
236                      $foo[1]->column('created_on')+1 ] },
237    { range => { created_on => 1 } });
238ok(!$tmp, "Exclusive date range load() starting at Foo #1's date found no Foos");
239
240## Load using INCLUSIVE range search, up through the momment $foo[1] created
241$tmp = Foo->load(
242    { created_on => [ $foo[1]->column('created_on')-1, 
243                      $foo[1]->column('created_on') ] },
244    { range_incl => { created_on => 1 } });
245ok($tmp, 'Loaded an object based on range_incl (ts-1 to ts)');
246is_object($tmp, $foo[1], "Foo from inclusive date-range load() ending at Foo #1's date is Foo #2");
247
248$tmp = Foo->load(
249    { created_on => [ $foo[1]->column('created_on'), 
250                      $foo[1]->column('created_on')+1 ] },
251    { range_incl => { created_on => 1 } });
252ok($tmp, 'Loaded an object based on range_incl (ts to ts+1)');
253is_object($tmp, $foo[1], "Foo from inclusive date-range load() starting at Foo #1's date is Foo #2");
254
255## Check that range searches return nothing when nothing is in the range.
256$tmp = Foo->load( { created_on => [ undef, '19690101000000' ] },
257                  { range => { created_on => 1 } });
258ok(!$tmp, 'Prehistoric date range load() found no Foos');
259
260## Range search, all items with created_on less than foo[1]->created_on
261$tmp = Foo->load(
262    { created_on => [ undef, $foo[1]->column('created_on')-1 ] },
263    { range => { created_on => 1 } });
264is_object($tmp, $foo[0], "Foo from exclusive open-started date-range load() ending before Foo #1 is Foo #1");
265
266## Get count of objects
267is(Foo->count(), 2, 'Count of all Foos finds both');
268is(Foo->count({ status => 0 }), 1, 'Count of all status=0 Foos finds all one');
269my $ranged_count = Foo->count(
270    { created_on => [ $foo[1]->column('created_on')-1 ] },
271    { range => { created_on => 1 } }
272);
273is($ranged_count, 1, 'Count of all Foos in open-ended date range starting before Foo #1 finds all one');
274
275## Update status for later tests.
276$foo[0]->status(2);
277$foo[0]->save;
278
279## Test start_val loads.
280## Given the first Foo object, should load the "next" one
281## (the one with a larger created_on time)
282$tmp = Foo->load(undef, {
283    limit => 1,
284    sort => 'created_on',
285    direction => 'ascend',
286    start_val => $foo[0]->created_on });
287is_object($tmp, $foo[1], 'Next newer Foo after Foo #1 is Foo #2');
288
289## Given the first Foo object, try to load the "previous" one
290## (the one with a smaller created_on time). This should fail.
291$tmp = Foo->load(undef, {
292    limit => 1,
293    sort => 'created_on',
294    direction => 'descend',
295    start_val => $foo[0]->created_on });
296ok(!$tmp, 'Search for next older Foo before Foo #1 found none');
297
298## Given the second Foo object, try to load the "previous" one
299## (the one with a smaller created_on time). This should work.
300$tmp = Foo->load(undef, {
301    limit => 1,
302    sort => 'created_on',
303    direction => 'descend',
304    start_val => $foo[1]->created_on });
305is_object($tmp, $foo[0], 'Next older Foo before Foo #2 is Foo #1');
306
307## Given the second Foo object, try to load the "next" one
308## (the one with a larger created_on time). This should fail.
309$tmp = Foo->load(undef, {
310    limit => 1,
311    sort => 'created_on',
312    direction => 'ascend',
313    start_val => $foo[1]->created_on });
314ok(!$tmp, 'Search for next newer Foo after Foo #2 found none');
315
316## Now, given the second Foo object's created_on - 1, try to
317## load the "previous" one. This should work.
318$tmp = Foo->load(undef, {
319    limit => 1,
320    sort => 'created_on',
321    direction => 'descend',
322    start_val => $foo[1]->created_on-1 });
323is_object($tmp, $foo[0], 'Next older Foo before just before Foo #2 is Foo #1');
324
325## Now, given the second Foo object's created_on - 1, try to
326## load the "next" one. This should work.
327$tmp = Foo->load(undef, {
328    limit => 1,
329    sort => 'created_on',
330    direction => 'ascend',
331    start_val => $foo[1]->created_on-1 });
332is_object($tmp, $foo[1], 'Next newer Foo after just before Foo #2 is Foo #2');
333
334## Override created_on timestamp, make sure it works
335my $ts = substr($foo[1]->created_on, 0, -4) . '0000';
336$foo[1]->created_on($ts);
337$foo[1]->save;
338
339@tmp = Foo->load(undef, {
340    sort => 'created_on',
341    direction => 'descend',
342    limit => 2 });
343are_objects(\@tmp, \@foo, 'Time-traveled Foos newest-first are Foos #1 and #2');
344
345## Test limit of 2 with direction descend, but without
346## a sort option. This should sort by the most recently-added
347## records, ie. sorted by ID, basically.
348@tmp = Foo->load(undef, {
349    direction => 'descend',
350    limit => 2 });
351are_objects(\@tmp, [ reverse @foo ], 'Foos highest-id-first are Foos #2 and #1');
352
353## Test loading using offset.
354## Should load the second Foo object.
355$tmp = Foo->load(undef, {
356    direction => 'descend',
357    sort => 'created_on',
358    limit => 1,
359    offset => 1 });
360is_object($tmp, $foo[1], 'Second newest Foo is Foo #2');
361
362## We only have 2 Foo objects, so this should load
363## only the second Foo object (because offset is 1).
364@tmp = Foo->load(undef, {
365    direction => 'descend',
366    sort => 'created_on',
367    limit => 2,
368    offset => 1 });
369are_objects(\@tmp, [ $foo[1] ], 'Second and third newest Foos is just Foo #2');
370
371## Should load the first Foo object (ascend with offset of 1).
372$tmp = Foo->load(undef, {
373    direction => 'ascend',
374    sort => 'created_on',
375    limit => 1,
376    offset => 1 });
377is_object($tmp, $foo[0], 'Second oldest Foo is Foo #1');
378
379## Now test join loads.
380## First we need to create a couple of Bar objects.
381$bar[0] = Bar->new;
382$bar[0]->foo_id($foo[1]->id);
383$bar[0]->name('bar0');
384$bar[0]->status(0);
385ok($bar[0]->save, 'saved');
386sleep(2);  ## Sleep to ensure created_on timestamps are unique
387
388$bar[1] = Bar->new;
389$bar[1]->foo_id($foo[1]->id);
390$bar[1]->name('bar1');
391$bar[1]->status(1);
392ok($bar[1]->save, 'saved');
393sleep(2);  ## Sleep to ensure created_on timestamps are unique
394
395$bar[2] = Bar->new;
396$bar[2]->foo_id($foo[0]->id);
397$bar[2]->name('bar2');
398$bar[2]->status(0);
399ok($bar[2]->save, 'saved');
400sleep(2);  ## Sleep to ensure created_on timestamps are unique
401
402my $cgb_iter = Bar->count_group_by({
403        status => '0',
404    }, {
405        group => [ 'foo_id' ],
406        sort => 'foo_id desc',
407    });
408my ($count, $bfid, $month);
409isa_ok($cgb_iter, 'CODE');
410ok(($count, $bfid) = $cgb_iter->(), 'set');
411is($bfid, $bar[1]->id, 'id');
412is($count, 1, 'count4');
413ok(($count, $bfid) = $cgb_iter->(), 'set');
414is($bfid, $bar[0]->id, 'id');
415is($count, 1, 'count5');
416ok(!$cgb_iter->(), 'no $iter');
417
418$cgb_iter = Bar->count_group_by(undef, {
419        group => [ 'extract(month from created_on)' ],
420        sort => 'extract(month from created_on) desc',
421    });
422isa_ok($cgb_iter, 'CODE');
423ok(($count, $month) = $cgb_iter->(), 'set');
424use POSIX qw(strftime);
425is(int($month), int(strftime("%m", localtime)), 'month');
426is($count, 3, 'count6');
427ok(!$cgb_iter->(), 'no $iter');
428
429## Get a count of all Foo objects in order of most recently
430## created Bar object. No uniqueness requirement. This tests
431## the on_load_complete temporary table stuff with count.
432
433is(Foo->count(undef,
434    { join => [ 'Bar', 'foo_id',
435                undef,
436                { unique => 1,
437                  sort => 'created_on',
438                  direction => 'descend', } ] }), 2, 'count7');
439
440## Now load all Foo objects in order of most recently
441## created Bar object. Make sure they are unique.
442@tmp = Foo->load(undef,
443    { join => [ 'Bar', 'foo_id',
444                undef,
445                { sort => 'created_on',
446                  direction => 'descend',
447                  unique => 1 } ] });
448is(@tmp, 2, 'array length 2');
449is($tmp[0]->id, $foo[0]->id, 'id');
450is($tmp[1]->id, $foo[1]->id, 'id');
451
452## Load all Foo objects in order of most recently
453## created Bar object. No uniqueness requirement.
454@tmp = Foo->load(undef,
455    { join => [ 'Bar', 'foo_id',
456                undef,
457                { sort => 'created_on',
458                  direction => 'descend', } ] });
459is(@tmp, 3, 'array length 3');
460is($tmp[0]->id, $foo[0]->id, 'id');
461is($tmp[1]->id, $foo[1]->id, 'id');
462is($tmp[2]->id, $foo[1]->id, 'id');
463
464## Load last 1 Foo object in order of most recently
465## created Bar object.
466@tmp = Foo->load(undef,
467    { join => [ 'Bar', 'foo_id',
468                undef,
469                { sort => 'created_on',
470                  direction => 'descend',
471                  unique => 1,
472                  limit => 1, } ] });
473is(@tmp, 1, 'array length 1 with join');
474is($tmp[0]->id, $foo[0]->id, 'id');
475
476## Load all Foo objects where Bar.name = 'bar0'
477@tmp = Foo->load(undef,
478    { join => [ 'Bar', 'foo_id',
479                { name => 'bar0' },
480                { sort => 'created_on',
481                  direction => 'descend',
482                  unique => 1, } ] });
483is(@tmp, 1, 'array length 1 with join, limit by name');
484is($tmp[0]->id, $foo[1]->id, 'id');
485
486## foo[1] is older than foo[0] because we overrode the timestamp,
487## so this should load foo[0]
488@tmp = Foo->load(undef,
489    { sort => 'created_on', direction => 'descend', limit => 1,
490    join => [ 'Bar', 'foo_id', { status => 0 }, { unique => 1 } ] });
491is(@tmp, 1, 'array length 1');
492is($tmp[0]->id, $foo[0]->id, 'id');
493
494## This is the same join as the last one, but without the limit--so
495## we should get both Foo objects this time, in descending order.
496@tmp = Foo->load(undef,
497    { sort => 'created_on', direction => 'descend',
498      join => [ 'Bar', 'foo_id', { status => 0 }, { unique => 1 } ] });
499is(@tmp, 2, 'array length 2');
500is($tmp[0]->id, $foo[0]->id, 'id');
501is($tmp[1]->id, $foo[1]->id, 'id');
502
503## Filter join results by providing a value for 'status'; only Foo[0]
504## has a 'status' == 2, so only that record should be returned.
505@tmp = Foo->load({ status => 2 },
506    { sort => 'created_on', direction => 'descend',
507      join => [ 'Bar', 'foo_id', { status => 0 }, { unique => 1 } ] });
508is(@tmp, 1, 'array length 1');
509is($tmp[0]->id, $foo[0]->id, 'id');
510
511# Join across a column.
512@tmp = Foo->load({},
513    { sort => 'created_on', direction => 'descend',
514      join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 0 }, { unique => 1 } ] });
515are_objects(\@tmp, \@foo, 'Foos loaded by explicit join across columns');
516
517@tmp = Foo->load({ status => 2 },
518    { sort => 'created_on', direction => 'descend',
519      join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 0 }, { unique => 1 } ] });
520are_objects(\@tmp, [ $foo[0] ], 'Foos of status=2 loaded by explicit join across columns');
521
522## TEST EXISTS METHOD
523ok($foo[0]->exists, 'exists');
524$tmp = Foo->new;
525ok(!$tmp->exists, 'does not exist');
526$tmp->id(5);
527ok(!$tmp->exists, 'does not exist');
528
529## Change foo[1]->status so that its value is unique (for index)
530$foo[1]->status(5);
531ok($foo[1]->save, 'saved');
532ok(Foo->load({ status => 5 }), 'loaded' );
533
534## Test remove
535ok($foo[1]->remove, 'removed');
536ok(! Foo->load(2), 'not loaded');
537ok(! Foo->load({ status => 5 }), 'not loaded');
538ok(! Foo->load({ name => $foo[1]->name }), 'not loaded');
539ok(! Foo->load({ created_on => $foo[1]->created_on }), 'not loaded');
540
541## Test methods:
542##     * properties
543my $props1 = Foo->properties;
544is($props1->{audit}, 1, 'audit');
545is(scalar keys %{ $props1->{indexes} }, 3, 'indexes');
546is($props1->{primary_key}, 'id', 'id');
547is(scalar @{ $props1->{columns} }, 9, 'columns');
548my $props2 = $foo[0]->properties;
549is($props1, $props2, "$props1 is $props2");  ## Same address, because same hashref
550
551##     * column_names
552my $cols = $foo[0]->column_names;
553isa_ok($cols, 'ARRAY');
554my %cols = map { $_ => 1 } @$cols;
555for (qw(id name status text data created_on created_by modified_on modified_by)) {
556    ok($cols{$_}, 'cols');
557}
558
559##     * column_values
560my $vals = $foo[0]->column_values;
561isa_ok($vals, 'HASH');
562is($vals->{id}, $foo[0]->id, 'id');
563is($vals->{name}, $foo[0]->name, 'name');
564is($vals->{status}, $foo[0]->status, 'status');
565is($vals->{text}, $foo[0]->text, 'text');
566is($vals->{created_on}, $foo[0]->created_on, 'created_on');
567is($vals->{created_by}, $foo[0]->created_by, 'created_by');
568is($vals->{modified_on}, $foo[0]->modified_on, 'modified_on');
569is($vals->{modified_by}, $foo[0]->modified_by, 'modified_by');
570
571##     * set_values
572$vals = {
573    id => 5,
574    name => 'baz',
575    status => 7,
576    text => 'quux',
577    created_on => 13209,
578    created_by => 'bar',
579    #modified_on => 39023, modified_by auto-set modified_on in our new code.
580    modified_by => 'foo',
581};
582$foo[0]->set_values($vals);
583for my $col (keys %$vals) {
584    is($vals->{$col}, $foo[0]->column($col), $col);
585}
586
587##     * binary data
588
589my $binmonster = Foo->new;
590
591$vals = {
592    funky => "yes",
593    monkey => "no",
594};
595
596require MT::Serialize;
597my $srlzr = MT::Serialize->new('MT');
598$binmonster->data($srlzr->serialize(\$vals));
599my $x = $binmonster->save();
600warn 'Failed binary data test: ' . $binmonster->errstr() unless $x;
601ok($x, 'saved');
602ok($binmonster->id, 'id');
603Foo->driver->clear_cache if Foo->driver->can('clear_cache');
604my $chk = Foo->load($binmonster->id);
605if ($chk) {
606    my $chk_data = $chk->data;
607    my $chk_vals = $srlzr->unserialize($chk_data);
608    foreach (keys %$vals) {
609        is($$chk_vals->{$_}, $vals->{$_}, $_);
610    }
611} else {
612    foreach (keys %$vals) {
613        ok(0, $_);
614    }
615}
616
617##     * datasource
618is($foo[0]->table_name, 'mt_' . $foo[0]->datasource, 'datasource');
619
620##     * clone
621my $clone = $foo[0]->clone_all;
622for my $col (@$cols) {
623    is($clone->column($col), $foo[0]->column($col), $col);
624}
625
626## Sleep first so that they get different created_on timestamps.
627sleep(3);
628
629Foo->set_by_key({name => "this"});
630my $obj = Foo->load({name => "this"});
631isa_ok($obj, 'Foo');
632
633Foo->set_by_key({name => "this"}, {status => 42});
634$obj = Foo->load({name => "this"});
635is($obj && $obj->status, 42, 'status');
636
637Foo->set_by_key({name => "this"}, {status => 47});
638$obj = Foo->load({name => "this"});
639is($obj && $obj->status, 47, 'status');
640
641Foo->set_by_key({name => "this", status => 47}, {text => "spiffy"});
642$obj = Foo->load({name => "this", status => 47});
643is($obj && $obj->text, 'spiffy', 'text');
644
645sleep(3);
646
647Foo->set_by_key({name => "that"}, {text => "Once"});
648$obj = Foo->load({name => "that"});
649is($obj && $obj->text, 'Once', 'text');
650
651Foo->driver->clear_cache if Foo->driver->can('clear_cache');
652## Load use direct set of values for non-PK column
653@tmp = Foo->load({ name => [qw(foo this)] });
654@tmp = sort {$a->name cmp $b->name} @tmp;
655is(@tmp, 2, 'array length 2');
656
657is(Foo->count(), 4, 'check number of Foos');
658
659## check offsets without limits
660## Should load the third and fourth Foo objects.
661my $foo4 = Foo->load({name => "this"});
662my $foo5 = Foo->load({name => "that"});
663my $foo1 = Foo->load(undef, { 'sort' => 'created_on', 'direction' => 'ascend' });
664my @offs = Foo->load(undef, {
665    direction => 'ascend',
666    sort => 'created_on',
667    offset => 2 });
668is(@offs, 2, 'array length 2');
669isa_ok($offs[0], 'Foo');
670is($offs[0]->id, $foo4->id, 'id');
671isa_ok($offs[1], 'Foo');
672is($offs[1]->id, $foo5->id, 'id');
673
674## Should load the third and fourth Foo objects.
675@offs = Foo->load(undef, {
676    direction => 'descend',
677    sort => 'created_on',
678    offset => 1 });
679is(@offs, 3, 'array length 3');
680isa_ok($offs[0], 'Foo');
681is($offs[0]->id, $foo4->id, 'id');
682isa_ok($offs[1], 'Foo');
683is($offs[1]->id, $binmonster->id, 'id');
684isa_ok($offs[2], 'Foo');
685is($offs[2]->id, $foo1->id, 'id');
686
687#sum_group_by
688my $cnt = 0;
689my @data = Foo->load(undef, {direction => 'ascend'});
690my @foos;
691foreach my $f (@data) {
692    $f->status($cnt + 1);
693    $f->save;
694    unshift @foos, $f;
695    $cnt++;
696}
697
698my $sgb = Foo->sum_group_by(undef, { sum => 'status', group => ['id'], direction => 'ascend' });
699while (my ($status, $id2) = $sgb->()) {
700    my $f = pop @foos;
701    is($f->id, $id2, 'id sgb');
702    is($f->status, $status, 'status sgb');
703}
704
705SKIP: {
706    skip(1, '$tmp[0] undefined') unless $tmp[0];
707    ok($tmp[0] && ($tmp[0]->name eq 'foo'), 'name')
708}
709SKIP: {
710    skip(1, '$tmp[1] undefined') unless $tmp[1];
711    ok($tmp[1] && ($tmp[1]->name eq 'this'), 'name');
712}
713
714# -or
715my $newdata = Foo->new;
716$newdata->status(11);
717$newdata->name('Apple');
718$newdata->text('MacBook');
719$newdata->save;
720$newdata = Foo->new;
721$newdata->status(12);
722$newdata->name('Linux');
723$newdata->text('Ubuntu');
724$newdata->save;
725$newdata = Foo->new;
726$newdata->status(13);
727$newdata->name('Microsoft');
728$newdata->text('Vista');
729$newdata->save;
730$newdata = Foo->new;
731$newdata->status(10);
732$newdata->name('Microsoft');
733$newdata->text('XP');
734$newdata->save;
735$newdata = Foo->new;
736$newdata->status(10);
737$newdata->name('Apple');
738$newdata->text('iBook');
739$newdata->save;
740
741$count = Foo->count( [{status => 10}, -or => {name => 'Apple'}] );
742# ==> select count(*) from mt_foo where foo_status = 10 or foo_name = 'Apple'
743is($count, 3, '-or count');
744
745$count = Foo->count( [ { status => { '<=' => 20 }, name => 'Apple' }, -and_not => { status => 11 } ] );
746# ==> select count(*) from mt_foo where (foo_status <= 20 and foo_name = 'Apple') and not (foo_status = 11)
747is($count, 1, '-and_not count');
748
749$count = Foo->count( [
750    { status => 10 },
751    -or => { name => 'Apple' },
752    -or => { name => { like => '%nux' } },
753] );
754# ==> select count(*) from mt_foo where (foo_status = 10) or (foo_name = 'Apple') or (foo_name like '%nux')
755# (selects Apple+MacBook, Apple+iBook, Microsoft+XP, Linux+Ubuntu)
756is($count, 4, '-or count, 3 clauses');
757
758# alias support
759my $vista = Foo->load({name=>'Microsoft', status=>13});
760my $newbar = Bar->new;
761$newbar->foo_id($vista->id);
762$newbar->name('Silverlight');
763$newbar->status(2);
764$newbar->save;
765sleep(3);
766$newbar = Bar->new;
767$newbar->foo_id($vista->id);
768$newbar->name('IronPython');
769$newbar->status(3);
770$newbar->save;
771sleep(3);
772my $mb = Foo->load({name=>'Apple', status=>11});
773$newbar = Bar->new;
774$newbar->foo_id($mb->id);
775$newbar->name('IronRuby');
776$newbar->status(0);
777$newbar->save;
778
779# select * from foo, bar bar1, bar bar2
780# where bar1.bar_foo_id = foo_id
781# and bar2.bar_foo_id = bar1.bar_foo_id
782# and bar1.status = 2
783# and bar2.status = 3
784my @a_foos = Foo->load(
785    undef,
786    { join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 2 },
787        { join => [ 'Bar', undef, { foo_id => \'= bar1.bar_foo_id', status => 3 },
788            { alias => 'bar2' } ],
789          alias => 'bar1'
790        }
791      ],
792      sort => 'created_on', direction => 'descend',
793    }
794);
795is(scalar(@a_foos), 1, 'join the same table using alias 1');
796is($a_foos[0]->id, $vista->id, 'join the same table using alias 2');
797
798@a_foos = Foo->load(
799    undef,
800    { join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 2 },
801        { join => [ 'Bar', undef, { foo_id => \'= bar1.bar_foo_id', status => 0 },
802            { alias => 'bar2' } ],
803          alias => 'bar1'
804        }
805      ],
806      sort => 'created_on', direction => 'descend',
807    }
808);
809is(scalar(@a_foos), 0, 'join the same table using alias 3');
810 
811
8121;
Note: See TracBrowser for help on using the browser.