forked from movabletype/movabletype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver-tests.pl
2100 lines (1837 loc) · 63.7 KB
/
driver-tests.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# Movable Type (r) Open Source (C) 2001-2011 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id: driver-tests.pl 3531 2009-03-12 09:11:52Z fumiakiy $
use strict;
use warnings;
use Data::Dumper;
use English qw( -no_match_vars );
$OUTPUT_AUTOFLUSH = 1;
# Run this script as a symlink, in the form of 99-driver.t, ie:
# ln -s driver-tests.pl 99-driver.t
BEGIN {
# Set config to driver-test.cfg when run as /path/to/99-driver.t
$ENV{MT_CONFIG} = "$1-test.cfg"
if __FILE__ =~ m{ [\\/] \d+- ([^\\/]+) \.t \z }xms;
}
use Test::More;
use lib 't/lib';
BEGIN {
plan skip_all => "Configuration file $ENV{MT_CONFIG} not found"
if !-r "t/$ENV{MT_CONFIG}";
}
use MT::Test qw(:testdb :time);
package Zot;
use base 'MT::Object';
__PACKAGE__->install_properties({
column_defs => {
'id' => 'integer not null auto_increment',
'x' => 'string(255)',
},
primary_key => 'id',
datasource => 'zot',
});
package Test::GroupBy;
use base qw( Test::Class MT::Test );
use Test::More;
use POSIX qw(strftime);
sub reset_db : Test(setup) {
my $self = shift;
$self->clean_db();
my @obj_data = (
{ class => 'Foo',
id => 1,
name => 'foo',
text => 'bar',
status => 2, },
{ class => 'Foo',
id => 2,
name => 'baz',
text => 'quux',
status => 1, },
{ class => 'Bar',
__wait => 1,
id => 1,
foo_id => 2,
name => 'bar0',
status => 0, },
{ class => 'Bar',
__wait => 1,
id => 2,
foo_id => 2,
name => 'bar1',
status => 1, },
{ class => 'Bar',
__wait => 1,
id => 3,
foo_id => 1,
name => 'bar2',
status => 0, },
);
for my $data (@obj_data) {
my $class = delete $data->{class};
my $wait = delete $data->{__wait};
my $obj = $class->new;
$obj->set_values($data);
sleep($wait) if $wait;
$obj->save();
}
}
sub count_group_by : Tests(34) {
# legacy way of specifying sort direction
my $cgb_iter = Bar->count_group_by({
status => '0',
}, {
group => [ 'foo_id' ],
sort => 'foo_id desc',
});
my ($count, $bfid, $month);
isa_ok($cgb_iter, 'CODE');
ok(($count, $bfid) = $cgb_iter->(), 'set');
is($bfid, 2, 'id');
is($count, 1, 'count4');
ok(($count, $bfid) = $cgb_iter->(), 'set');
is($bfid, 1, 'id');
is($count, 1, 'count5');
ok(!$cgb_iter->(), 'no $iter');
# new way of specifying sort direction
my $cgb_iter2 = Bar->count_group_by({
status => '0',
}, {
group => [ 'foo_id' ],
sort => 'foo_id',
direction => 'descend'
});
isa_ok($cgb_iter2, 'CODE');
ok(($count, $bfid) = $cgb_iter2->(), 'set');
is($bfid, 2, 'id');
is($count, 1, 'count4');
ok(($count, $bfid) = $cgb_iter2->(), 'set');
is($bfid, 1, 'id');
is($count, 1, 'count5');
ok(!$cgb_iter2->(), 'no $iter');
# legacy way of specifying sort direction
my $cgb_iter3 = Bar->count_group_by(undef, {
group => [ 'extract(month from created_on)' ],
sort => 'extract(month from created_on) desc',
});
isa_ok($cgb_iter3, 'CODE');
ok(($count, $month) = $cgb_iter3->(), 'set');
is(int($month), int(strftime("%m", localtime)), 'month');
is($count, 3, 'count6');
ok(!$cgb_iter3->(), 'no $iter');
# new way of specifying sort direction
my $cgb_iter4 = Bar->count_group_by(undef, {
group => [ 'extract(month from created_on)' ],
sort => [{ column => 'extract(month from created_on)',
desc => 'desc' }]
});
isa_ok($cgb_iter4, 'CODE');
ok(($count, $month) = $cgb_iter4->(), 'set');
is(int($month), int(strftime("%m", localtime)), 'month');
is($count, 3, 'count6');
ok(!$cgb_iter4->(), 'no $iter');
# Sort by count
my $cgb_iter5 = Bar->count_group_by(undef, {
group => [ 'foo_id' ],
sort => 'cnt',
});
isa_ok($cgb_iter5, 'CODE');
ok(($count, $bfid) = $cgb_iter5->(), 'set');
is($bfid, 1, 'id-7');
is($count, 1, 'count-7');
ok(($count, $bfid) = $cgb_iter5->(), 'set');
is($bfid, 2, 'id-8');
is($count, 2, 'count-8');
ok(!$cgb_iter2->(), 'no $iter');
}
sub sum_group_by : Tests(7) {
# Sum status values across groups of ids (that is, a group for each Foo).
my $sgb = Foo->sum_group_by(undef, {
sum => 'status',
group => ['id'],
direction => 'ascend',
});
my ($status, $id) = $sgb->();
ok($status && $id, 'sum_group_by results had a first result');
is($status, 1, q{sum_group_by result #1's status is 1});
is($id, 2, 'sum_group_by result #1 was for Foo #2');
($status, $id) = $sgb->();
ok($status && $id, 'sum_group_by results had a second result');
is($status, 2, q{sum_group_by result #2's status is 2});
is($id, 1, 'sum_group_by result #2 was for Foo #1');
($status, $id) = $sgb->();
ok(!$status, 'sum_group_by only had two results');
}
sub avg_group_by : Tests(7) {
my $agb = Foo->avg_group_by(undef, {
avg => 'status',
group => ['id'],
direction => 'ascend',
});
my ($status, $id) = $agb->();
ok($status && $id, 'avg_group_by results had a first result');
# Compare numerically; is() will compare stringwise.
ok($status == 1, q{avg_group_by result #1's status is 1});
is($id, 2, 'avg_group_by result #1 was for Foo #2');
($status, $id) = $agb->();
ok($status && $id, 'avg_group_by results had a second result');
# Compare numerically; is() will compare stringwise.
ok($status == 2, q{avg_group_by result #2's status is 2});
is($id, 1, 'avg_group_by result #2 was for Foo #1');
($status, $id) = $agb->();
ok(!$status, 'avg_group_by only had two results');
}
sub max_group_by : Tests(7) {
my $mgb = Bar->max_group_by(undef, {
join => Foo->join_on(undef,
{
'id' => \'=bar_foo_id',
}),
group => ['foo_id'],
max => 'created_on',
});
my ($created_on, $foo_id) = $mgb->();
my $f1 = Foo->load(1);
my $b3 = Bar->load(3);
is($foo_id, $f1->id, 'max_group_by had a second result');
#is($created_on, $b3->created_on, 'max_group_by had a second result');
my $f2 = Foo->load(2);
my $b2 = Bar->load(2);
($created_on, $foo_id) = $mgb->();
is($foo_id, $f2->id, 'max_group_by had a first result');
#is($created_on, $b2->created_on, 'max_group_by had a first result');
($created_on, $foo_id) = $mgb->();
ok(!$created_on, 'max_group_by only had two results');
my $mgb2 = Bar->max_group_by(undef, {
join => Foo->join_on(undef,
{ 'id' => \'=bar_foo_id' },
{ limit => 1 },
),
group => ['foo_id'],
max => 'created_on',
});
($created_on, $foo_id) = $mgb2->();
is($foo_id, $f1->id, 'max_group_by with limit had a first result');
#is($created_on, $b3->created_on, 'max_group_by with limit had a first result');
($created_on, $foo_id) = $mgb2->();
ok(!$created_on, 'max_group_by with limit only had one result');
my $mgb3 = Bar->max_group_by(undef, {
join => Foo->join_on(undef,
{ 'id' => \'=bar_foo_id' },
{ limit => 1, offset => 1 },
),
group => ['foo_id'],
max => 'created_on',
});
($created_on, $foo_id) = $mgb3->();
is($foo_id, $f2->id, 'max_group_by with limit and offset had a first result');
#is($created_on, $b2->created_on, 'max_group_by with limit and offset had a first result');
($created_on, $foo_id) = $mgb3->();
ok(!$created_on, 'max_group_by with limit and offset only had one result');
}
sub clean_db : Test(teardown) {
MT::Test->reset_table_for(qw( Foo Bar ));
}
package Test::Joins;
use Test::More;
use MT::Test;
use base qw( Test::Class MT::Test );
sub reset_db : Test(setup) {
MT::Test->reset_table_for(qw( Foo Bar Baz ));
}
sub make_pc_data {
my $self = shift;
$self->make_objects(
{ __class => 'Foo',
name => 'Apple',
text => 'MacBook',
status => 11, },
{ __class => 'Foo',
name => 'Linux',
text => 'Ubuntu',
status => 12, },
{ __class => 'Foo',
name => 'Microsoft',
text => 'Vista',
status => 13, },
{ __class => 'Foo',
name => 'Microsoft',
text => 'XP',
status => 10, },
{ __class => 'Foo',
name => 'Apple',
text => 'iBook',
status => 10, },
{ __class => 'Bar',
__wait => 1,
name => 'Silverlight',
status => 2,
foo_id => 3, },
{ __class => 'Bar',
__wait => 1,
name => 'IronPython',
status => 3,
foo_id => 4, },
{ __class => 'Bar',
__wait => 1,
name => 'IronRuby',
status => 1,
foo_id => 4, },
{ __class => 'Bar',
__wait => 1,
name => 'Visual C++',
status => 4,
foo_id => 3, },
{ __class => 'Bar',
__wait => 1,
name => 'Visual Basic',
status => 4,
foo_id => 4, },
{ __class => 'Baz',
__wait => 1,
name => 'Home',
status => 1,
bar_id => 3, },
{ __class => 'Baz',
__wait => 1,
name => 'Professional',
status => 1,
bar_id => 3, },
{ __class => 'Baz',
__wait => 1,
name => 'Ultimate',
status => 1,
bar_id => 3, },
{ __class => 'Baz',
__wait => 1,
name => 'Kubuntu',
status => 1,
bar_id => 2, },
{ __class => 'Baz',
__wait => 1,
name => 'Edubuntu',
status => 3,
bar_id => 2, },
{ __class => 'Baz',
__wait => 1,
name => 'zubuntu',
status => 4,
bar_id => 2, },
{ __class => 'Baz',
__wait => 1,
name => 'Enterprise',
status => 3,
bar_id => 5, },
);
}
sub joins : Tests(1) {
my $self = shift;
$self->make_pc_data();
my $vista = Foo->load(4); # not a search
my @data = Foo->load(
undef,
{
joins => [
[ 'Baz', undef, { bar_id => \'= bar1.bar_id', status => 3 }, { } ],
[ 'Bar', 'foo_id', { status => 3 }, { alias => 'bar1', to => 'foo' } ],
[ 'Bar', undef, { foo_id => \'= bar1.bar_foo_id', status => 4 }, { alias => 'bar2' } ]
],
sort => 'created_on', direction => 'descend',
}
);
are_objects(\@data, [ $vista ], 'Has bar_status = 3, bar_status = 4, baz_status = 3 (only joins)');
}
sub count_with_joins: Tests(2) {
my $self = shift;
$self->make_pc_data();
my $count_args1 = {
joins => [
[ 'Baz', undef, { bar_id => \'= bar_id', status => 1 }, { unique => 1 } ],
],
};
my $count_args2 = {
joins => [
[ 'Baz', undef, { bar_id => \'= bar_id', status => 1 }, { unique => 1 } ],
],
};
my $count_args3 = {
joins => [
[ 'Baz', undef, { bar_id => \'= bar_id', status => 1 }, { unique => 1 } ],
],
};
is( Bar->count( undef, $count_args1 ), 2, 'count method must looks up unique options of joins.' );
my @bars = Bar->load(undef, $count_args2 );
is( Bar->count( undef, $count_args3 ), scalar @bars, 'count method must returns same as number of objects that load method returns.' );
}
sub count_group_by_with_joins: Tests(3) {
my $self = shift;
$self->make_objects(
{ __class => 'Foo',
name => 'Apple',
text => 'Snow Leopard',
status => 1, },
{ __class => 'Foo',
name => 'Apple',
text => 'iOS',
status => 1, },
{ __class => 'Foo',
name => 'Microsoft',
text => 'Windows7',
status => 1, },
{ __class => 'Bar',
__wait => 1,
name => 'Silverlight',
status => 1,
foo_id => 1, },
{ __class => 'Bar',
__wait => 1,
name => 'IronPython',
status => 3,
foo_id => 1, },
{ __class => 'Bar',
__wait => 1,
name => 'IronRuby',
status => 1,
foo_id => 2, },
{ __class => 'Bar',
__wait => 1,
name => 'Visual C++',
status => 1,
foo_id => 3, },
);
my $args = {
joins => [
[
'Bar',
undef,
{ foo_id => \'= foo_id', status => 1 },
{ unique => 1, limit => 2, offset => 1, },
],
],
group => ['name'],
sort => 'cnt',
direction => 'ascend',
limit => 1,
offset => 0,
};
my $iter = Foo->count_group_by( undef, $args );
my @counts;
my @names;
while ( my ( $cnt, $name ) = $iter->() ) {
push @counts, $cnt;
push @names, $name;
}
is( scalar @counts, 1 );
is( $counts[0], 2 );
is( $names[0], 'Apple' );
}
sub joins_with_join : Tests(1) {
my $self = shift;
$self->make_pc_data();
my $original = Foo->load(4); # not a search
my @data = Foo->load(
undef,
{
join => [
'Bar',
'foo_id',
{
status => 3
},
{
alias => 'bar1',
join => [ 'Baz', undef, { bar_id => \'= bar1.bar_id', status => 3 }, { } ],
},
],
joins => [
[ 'Bar', undef, { foo_id => \'= foo_id', status => 4 }, { alias => 'bar2' } ]
],
sort => 'created_on', direction => 'descend',
}
);
are_objects(\@data, [ $original ], 'Has bar_status = 3, bar_status = 4, baz_status = 3 (joins with join)');
}
sub only_join : Tests(1) {
my $self = shift;
$self->make_pc_data();
my $original = Foo->load(4); # not a search
my @data = Foo->load(
undef,
{
join => [
'Bar',
'foo_id',
{
status => 3
},
{
alias => 'bar1',
join => [ 'Baz', undef, { bar_id => \'= bar1.bar_id', status => 3 }, {
join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 4 }, { alias => 'bar2' } ]
} ]
},
],
sort => 'created_on', direction => 'descend',
}
);
are_objects(\@data, [ $original ], 'Has bar_status = 3, bar_status = 4, baz_status = 3 (only join)');
}
sub clean_db : Test(teardown) {
MT::Test->reset_table_for(qw( Foo Bar Baz ));
}
package Test::Search;
use Test::More;
use MT::Test;
use base qw( Test::Class MT::Test );
sub reset_db : Test(setup) {
MT::Test->reset_table_for(qw( Foo Bar ));
}
sub make_basic_data {
my $self = shift;
$self->make_objects(
{ __class => 'Foo',
__wait => 1,
name => 'foo',
text => 'bar',
id => 1,
status => 2, },
{ __class => 'Foo',
__wait => 3,
name => 'baz',
text => 'quux',
id => 2,
status => 1, },
);
}
sub make_pc_data {
my $self = shift;
$self->make_objects(
{ __class => 'Foo',
name => 'Apple',
text => 'MacBook',
status => 11, },
{ __class => 'Foo',
name => 'Linux',
text => 'Ubuntu',
status => 12, },
{ __class => 'Foo',
name => 'Microsoft',
text => 'Vista',
status => 13, },
{ __class => 'Foo',
name => 'Microsoft',
text => 'XP',
status => 10, },
{ __class => 'Foo',
name => 'Apple',
text => 'iBook',
status => 10, },
{ __class => 'Bar',
__wait => 1,
name => 'Silverlight',
status => 2,
foo_id => 3, },
{ __class => 'Bar',
__wait => 1,
name => 'IronPython',
status => 3,
foo_id => 3, },
{ __class => 'Bar',
__wait => 1,
name => 'IronRuby',
status => 0,
foo_id => 1, },
);
}
sub basic : Tests(5) {
my $self = shift;
$self->make_basic_data();
my $foo = Foo->load(1); # not a search
is_object(scalar Foo->load({ id => 1 }), $foo, 'Foo #1 by id hash is Foo #1');
is_object(scalar Foo->load({ id => 1, name => 'foo' }), $foo, 'Foo #1 by id-name hash is Foo #1');
is_object(scalar Foo->load({ name => 'foo' }), $foo, 'Foo #1 by name hash is Foo #1');
is_object(scalar Foo->load({ created_on => $foo->created_on }), $foo, 'Foo #1 by created_on hash is Foo #1');
is_object(scalar Foo->load({ status => 2 }), $foo, 'Foo #1 by status hash is Foo #1');
}
sub sorting : Tests(6) {
my $self = shift;
$self->make_basic_data();
my ($tmp, @tmp);
my @foo = map { Foo->load($_) } (1..2);
## Load using descending sort (newest)
$tmp = Foo->load(undef, {
sort => 'created_on',
direction => 'descend',
limit => 1 });
is_object($tmp, $foo[1], 'Newest Foo is Foo #2');
## Load using ascending sort (oldest)
$tmp = Foo->load(undef, {
sort => 'created_on',
direction => 'ascend',
limit => 1 });
is_object($tmp, $foo[0], 'Oldest Foo is Foo #1');
## Load using descending sort with limit = 2
@tmp = Foo->load(undef, {
sort => 'created_on',
direction => 'descend',
limit => 2 });
are_objects(\@tmp, [ reverse @foo ], 'Two Foos newest-first load() finds Foos #2 and #1');
## Load using descending sort by created_on, no limit
@tmp = Foo->load(undef, {
sort => 'created_on',
direction => 'descend' });
are_objects(\@tmp, [ reverse @foo ], 'All Foos newest-first load() finds Foos #2 and #1');
## Load using ascending sort by status, no limit
@tmp = Foo->load(undef, { sort => 'status', });
are_objects(\@tmp, [ reverse @foo ], 'All Foos lowest-status-first load() finds Foos #2 and #1');
## Load using 'last' where status == 2
$tmp = Foo->load({ status => 2 }, {
sort => 'created_on',
direction => 'descend',
limit => 1 });
is_object($tmp, $foo[0], 'Newest status=2 Foo is Foo #1');
}
sub ranges : Tests(9) {
my $self = shift;
$self->make_basic_data();
my $tmp;
my @foo = map { Foo->load($_) } (1..2);
## Load using range search, one less than foo[1]->created_on and newer
$tmp = Foo->load(
{ created_on => [ $foo[1]->column('created_on')-1 ] },
{ range => { created_on => 1 } });
is_object($tmp, $foo[1], 'Foo from open-ended date range before Foo #2 is Foo #2');
## Load using EXCLUSIVE range search, up through the momment $foo[1] created
$tmp = Foo->load(
{ created_on => [ $foo[1]->column('created_on')-1,
$foo[1]->column('created_on') ] },
{ range => { created_on => 1 } });
ok(!$tmp, "Exclusive date range load() ending at Foo #1's date found no Foos");
$tmp = Foo->load(
{ created_on => [ $foo[1]->column('created_on'),
$foo[1]->column('created_on')+1 ] },
{ range => { created_on => 1 } });
ok(!$tmp, "Exclusive date range load() starting at Foo #1's date found no Foos");
## Load using INCLUSIVE range search, up through the momment $foo[1] created
$tmp = Foo->load(
{ created_on => [ $foo[1]->column('created_on')-1,
$foo[1]->column('created_on') ] },
{ range_incl => { created_on => 1 } });
ok($tmp, 'Loaded an object based on range_incl (ts-1 to ts)');
is_object($tmp, $foo[1], "Foo from inclusive date-range load() ending at Foo #1's date is Foo #2");
$tmp = Foo->load(
{ created_on => [ $foo[1]->column('created_on'),
$foo[1]->column('created_on')+1 ] },
{ range_incl => { created_on => 1 } });
ok($tmp, 'Loaded an object based on range_incl (ts to ts+1)');
is_object($tmp, $foo[1], "Foo from inclusive date-range load() starting at Foo #1's date is Foo #2");
## Check that range searches return nothing when nothing is in the range.
$tmp = Foo->load( { created_on => [ undef, '19690101000000' ] },
{ range => { created_on => 1 } });
ok(!$tmp, 'Prehistoric date range load() found no Foos');
## Range search, all items with created_on less than foo[1]->created_on
$tmp = Foo->load(
{ created_on => [ undef, $foo[1]->column('created_on')-1 ] },
{ range => { created_on => 1 } });
is_object($tmp, $foo[0], "Foo from exclusive open-started date-range load() ending before Foo #1 is Foo #1");
}
sub alias : Tests(2) {
my $self = shift;
$self->make_pc_data();
my $vista = Foo->load(3); # not a search
# select * from foo, bar bar1, bar bar2
# where bar1.bar_foo_id = foo_id
# and bar2.bar_foo_id = bar1.bar_foo_id
# and bar1.status = 2
# and bar2.status = 3
my @a_foos = Foo->load(
undef,
{ join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 2 },
{ join => [ 'Bar', undef, { foo_id => \'= bar1.bar_foo_id', status => 3 },
{ alias => 'bar2' } ],
alias => 'bar1'
}
],
sort => 'created_on', direction => 'descend',
}
);
are_objects(\@a_foos, [ $vista ], 'Has Bars with status=2 and status=3 (alias)');
@a_foos = Foo->load(
undef,
{ join => [ 'Bar', undef, { foo_id => \'= foo_id', status => 2 },
{ join => [ 'Bar', undef, { foo_id => \'= bar1.bar_foo_id', status => 0 },
{ alias => 'bar2' } ],
alias => 'bar1'
}
],
sort => 'created_on', direction => 'descend',
}
);
is_deeply(\@a_foos, [], 'No Foo has Bars with status=2 and status=0 (alias)');
}
sub conjunctions : Tests(5) {
my $self = shift;
$self->make_pc_data();
my @foos = map { Foo->load($_) } (1..5); # not a search
my @res = Foo->load([
{ status => 10 },
-or => { name => 'Apple' },
]);
@res = sort { $a->id <=> $b->id } @res;
are_objects(\@res, [ @foos[0,3,4] ], '-or results');
@res = Foo->load([
{ name => 'Microsoft' },
-and => { status => 10 },
]);
are_objects(\@res, [ $foos[3] ], '-and results');
@res = Foo->load([
{ status => { '<=' => 20 },
name => 'Apple' },
-and_not => { status => 11 },
]);
# where (foo_status <= 20 and foo_name = 'Apple') and not (foo_status = 11)
are_objects(\@res, [ $foos[4] ], '-and_not results');
@res = Foo->load([
{ status => 10 },
-or => { name => 'Apple' },
-or => { name => { like => '%nux' } },
]);
@res = sort { $a->id <=> $b->id } @res;
# where (foo_status = 10) or (foo_name = 'Apple') or (foo_name like '%nux')
# (selects Apple+MacBook, Apple+iBook, Microsoft+XP, Linux+Ubuntu)
are_objects(\@res, [ @foos[0,1,3,4] ], 'big -or results');
@res = Foo->load(
[
[
{ status => 10 },
-or => { status => 12 },
],
-and => [
{ name => { like => '%nux' } },
-or => { name => 'Apple' },
],
]
);
@res = sort { $a->id <=> $b->id } @res;
# where ((foo_status = 10) or (foo_status = 12)) and ((foo_name like '%nux') or (foo_name = 'Apple'))
# (selects Apple+iBook, Linux+Ubuntu)
are_objects(\@res, [ @foos[1,4] ], 'grouping -or, -and');
}
sub early_ending_iterators: Tests(4) {
my $self = shift;
$self->make_pc_data();
my ($iter, $tmp, @tmp);
my @foo = map { Foo->load($_) } (1..5);
## Load using descending sort (newest)
$iter = Foo->load_iter(undef,
{ join => [ 'Bar', 'foo_id',
undef,
{ sort => 'created_on',
direction => 'descend',
unique => 1 } ] });
$tmp = $iter->();
is_object($tmp, $foo[0], '(early ending iterator) Foo associated with the newest Bar is Foo #1');
eval { $iter->end(); };
is($@, q(), 'Iterator can be ended #1');
## Load using ascending sort (oldest)
$iter = Foo->load_iter(undef,
{ join => [ 'Bar', 'foo_id',
undef,
{ sort => 'created_on',
direction => 'ascend',
unique => 1 } ] });
$tmp = $iter->();
SKIP: {
skip('sort with unique does not run correctly on PostgreSQL.', 1)
if $ENV{MT_CONFIG} =~ m/postgres/i;
is_object($tmp, $foo[2], '(early ending iterator) Foo associated with the oldest Bar is Foo #3');
}
eval { $iter->end(); };
is($@, q(), 'Iterator can be ended #2');
}
sub clean_db : Test(teardown) {
MT::Test->reset_table_for(qw( Foo Bar ));
}
package Test::Classy;
use Test::More;
use MT::Test;
use base qw( Test::Class MT::Test );
use Sock;
sub reset_db : Test(setup) {
MT::Test->reset_table_for(qw( Sock ));
}
sub a_plain_old_sock : Tests(3) {
my $s = Sock->new();
$s->text('asf dasf');
ok($s->save(), 'A regular old sock could be saved');
is($s->class, 'sock', q{A regular old sock's class is sock});
my $g = Classfree::Sock->load($s->id);
is($g->class, 'sock', q{Class-unaware object says a regular old sock's class is sock too});
}
sub sock_monkey_fish : Tests(8) {
my $monkey = Sock::Monkey->new();
$monkey->text('asf dasf');
ok($monkey->save(), 'A sock monkey could be saved');
is($monkey->class, 'monkey', q{A sock monkey's class is monkey});
my $fish = Sock::Fish->new();
$fish->text('asf dasf');
ok($fish->save(), 'A sock fish could be saved');
is($fish->class, 'fish', q{A sock fish's class is fish});
my @socks = Sock->load();
is(scalar @socks, 0, 'Plain load looks for plain old socks and finds none');
@socks = Sock->load({ class => '*' });
is(scalar @socks, 2, 'Search for all Socks finds a pair of socks');
@socks = sort { ref($a) cmp ref($b) } @socks;
is(ref $socks[0], 'Sock::Fish', 'One of the discovered Socks is a fish');
is(ref $socks[1], 'Sock::Monkey', 'The other discovered Sock is a monkey');
}
sub sock_combined_terms : Tests(9) {
my $monkey = Sock::Monkey->new();
$monkey->text('ABC');
ok($monkey->save(), 'A sock monkey could be saved');
is($monkey->class, 'monkey', q{A sock monkey's class is monkey});
$monkey = Sock::Monkey->new();
$monkey->text('DEF');
ok($monkey->save(), 'A sock monkey could be saved (2)');
is($monkey->class, 'monkey', q{A sock monkey's class is monkey (2)});
my $fish = Sock::Fish->new();
$fish->text('ABC');
ok($fish->save(), 'A sock fish could be saved');
is($fish->class, 'fish', q{A sock fish's class is fish});
my @socks = Sock->load({
class => '*',
text => 'ABC',
});
is(scalar @socks, 2, 'Search for all Socks finds a pair of socks');
@socks = sort { ref($a) cmp ref($b) } @socks;
is(ref $socks[0], 'Sock::Fish', 'One of the discovered Socks is a fish');
is(ref $socks[1], 'Sock::Monkey', 'The other discovered Sock is a monkey');
}
sub sock_no_class : Tests(9) {
my $monkey = Sock::Monkey->new();
$monkey->text('ABC');
ok($monkey->save(), 'A sock monkey could be saved');
is($monkey->class, 'monkey', q{A sock monkey's class is monkey});
$monkey = Sock::Monkey->new();
$monkey->text('DEF');
ok($monkey->save(), 'A sock monkey could be saved (2)');
is($monkey->class, 'monkey', q{A sock monkey's class is monkey (2)});
my $fish = Sock::Fish->new();
$fish->text('ABC');
ok($fish->save(), 'A sock fish could be saved');
is($fish->class, 'fish', q{A sock fish's class is fish});
my @socks = Sock->load({
text => 'ABC',
}, {
no_class => 1,
});
is(scalar @socks, 2, 'Search for all Socks finds a pair of socks');
@socks = sort { ref($a) cmp ref($b) } @socks;
is(ref $socks[0], 'Sock::Fish', 'One of the discovered Socks is a fish');
is(ref $socks[1], 'Sock::Monkey', 'The other discovered Sock is a monkey');
}
sub sock_array_combined_terms : Tests(9) {
my $monkey = Sock::Monkey->new();
$monkey->text('ABC');
ok($monkey->save(), 'A sock monkey could be saved');
is($monkey->class, 'monkey', q{A sock monkey's class is monkey});
$monkey = Sock::Monkey->new();
$monkey->text('DEF');
ok($monkey->save(), 'A sock monkey could be saved (2)');
is($monkey->class, 'monkey', q{A sock monkey's class is monkey (2)});
my $fish = Sock::Fish->new();
$fish->text('ABC');
ok($fish->save(), 'A sock fish could be saved');
is($fish->class, 'fish', q{A sock fish's class is fish});
my @socks = Sock->load(
[
{
class => '*',
},
'-and',
{
text => 'ABC',
},
]
);
is(scalar @socks, 2, 'Search for all Socks finds a pair of socks');
@socks = sort { ref($a) cmp ref($b) } @socks;
is(ref $socks[0], 'Sock::Fish', 'One of the discovered Socks is a fish');
is(ref $socks[1], 'Sock::Monkey', 'The other discovered Sock is a monkey');
}
sub sock_array_combined_terms_ex : Tests(9) {
my $monkey = Sock::Monkey->new();
$monkey->text('ABC');
ok($monkey->save(), 'A sock monkey could be saved');
is($monkey->class, 'monkey', q{A sock monkey's class is monkey});
$monkey = Sock::Monkey->new();
$monkey->text('DEF');
ok($monkey->save(), 'A sock monkey could be saved (2)');
is($monkey->class, 'monkey', q{A sock monkey's class is monkey (2)});
my $fish = Sock::Fish->new();
$fish->text('ABC');
ok($fish->save(), 'A sock fish could be saved');