File: rt_25460_numeric_aggregate.t

package info (click to toggle)
libdbd-sqlite3-perl 1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,004 kB
  • sloc: ansic: 167,715; perl: 1,788; pascal: 277; makefile: 9
file content (59 lines) | stat: -rw-r--r-- 889 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;
use lib "t/lib";
use SQLiteTest;
use Test::More;
use if -d ".git", "Test::FailWarnings";

# Create the table
my $dbh = connect_ok();
ok( $dbh->do(<<'END_SQL'), 'CREATE TABLE' );
create table foo (
	id integer primary key not null,
	mygroup varchar(255) not null,
	mynumber numeric(20,3) not null
)
END_SQL

# Fill the table
my @data = qw{
	a -2
	a 1
	b 2
	b 1
	c 3
	c -1
	d 4
	d 5
	e 6
	e 7
};
$dbh->begin_work;
while ( @data ) {
	ok $dbh->do(
		'insert into foo ( mygroup, mynumber ) values ( ?, ? )', {},
		shift(@data), shift(@data),
	);
}
$dbh->commit;

# Issue the group/sum/sort/limit query
my $rv = $dbh->selectall_arrayref(<<'END_SQL');
select mygroup, sum(mynumber) as total
from foo
group by mygroup
order by total
limit 3
END_SQL

is_deeply(
	$rv,
	[
		[ 'a', -1 ],
		[ 'c', 2  ],
		[ 'b', 3  ], 
	],
	'group/sum/sort/limit query ok'
);

done_testing;