File: 82-aggregate.t

package info (click to toggle)
libtangram-perl 2.10-1.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 1,064 kB
  • ctags: 703
  • sloc: perl: 9,665; makefile: 35
file content (96 lines) | stat: -rw-r--r-- 2,409 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl -w

use strict;
use lib "t/springfield";
use Springfield;
use Test::More tests => 10;

=head1 NAME

t/aggregate.t - test aggregate tangram functions

=head1 SYNOPSIS

 perl -Mlib=lib t/springfield/00-deploy.t
 perl -Mlib=lib t/springfield/aggregate.t

=head1 DESCRIPTION

This test script tests using Tangram for aggregate functionality, such
as when no object is selected.

=cut

stdpop();

my $dbh = DBI->connect($cs, $user, $passwd)
    or die "DBI->connect failed; $DBI::errstr";

# test GROUP BY and COUNT
{
   my $storage = Springfield::connect(undef, { dbh => $dbh });
   my ($r_person, $r_child) = $storage->remote(("NaturalPerson")x2);

   #local($Tangram::TRACE)=\*STDERR;
   my $cursor = $storage->cursor
       ( undef,
	 filter => $r_person->{children}->includes($r_child),
	 group => [ $r_person ],
	 retrieve => [ $r_child->count(), $r_child->{age}->sum() ],
	 #order => [ $r_child->{id}->count() ],
       );

   my @data;
   while ( my $row = $cursor->current() ) {
       push @data, [ $cursor->residue ];
       $cursor->next();
   }
   @data = sort { $a->[0] <=> $b->[0] } @data;
   #print Data::Dumper::Dumper(\@data);
   is_deeply(\@data, [ [ 1, 38 ], [3, 19 ], [3, 19] ],
	     "GROUP BY, SUM(), COUNT()");
}
is(&leaked, 0, "leaktest");

# test GROUP BY and COUNT
{
   my $storage = Springfield::connect(undef, { dbh => $dbh });
   my ($r_legal) = $storage->remote("LegalPerson");

   my $count = $storage->count($r_legal);
   my $expected = 0;
   if ( $count == 1 ) {
       $expected = 1;
   }
   is($count, $expected, "Tangram::Storage->count(Subclass)");

   $storage->insert(LegalPerson->new(name => "Springfield Nuclear Plant"))
       unless $storage->count($r_legal);

   #local($Tangram::TRACE)=\*STDERR;
   my $cursor = $storage->cursor
       ( undef,
	 retrieve => [ $r_legal->count() ],
       );

   my @data;
   while ( my $row = $cursor->current() ) {
       push @data, [ $cursor->residue ];
       $cursor->next();
   }
   #print Data::Dumper::Dumper(\@data);
   is_deeply(\@data, [ [ 1 ] ],
	     "->COUNT() filters types");
}
is(&leaked, 0, "leaktest");

# test $storage->sum() - single, array ref, set arguments
{
    my $storage = Springfield::connect(undef, { dbh => $dbh});
    my ($r_person) = $storage->remote("NaturalPerson");

    is($storage->sum($r_person->{age}), 156,
       "Tangram::Storage->sum()");

    is(&leaked, 0, "leaktest");
}