/
36-pg-array.t
82 lines (68 loc) · 1.73 KB
/
36-pg-array.t
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
#!/usr/bin/env perl6
use v6;
use DBIish;
use Test;
plan 7;
my %con-parms;
# If env var set, no parameter needed.
%con-parms<database> = 'dbdishtest' unless %*ENV<PGDATABASE>;
%con-parms<user> = 'postgres' unless %*ENV<PGUSER>;
my $dbh;
try {
$dbh = DBIish.connect('Pg', |%con-parms);
CATCH {
when X::DBIish::LibraryMissing | X::DBDish::ConnectionFailed {
diag "$_\nCan't continue.";
}
default { .throw; }
}
}
without $dbh {
skip-rest 'prerequisites failed';
exit;
}
my $sth = $dbh.do(q:to/STATEMENT/);
DROP TABLE IF EXISTS sal_emp;
STATEMENT
$sth = $dbh.do(q:to/STATEMENT/);
CREATE TABLE sal_emp (
name text,
pay_by_quarter integer[],
schedule text[][],
salary_by_month float[]
);
STATEMENT
$sth = $dbh.do(q:to/STATEMENT/);
INSERT INTO sal_emp
VALUES (
'Bill',
'{10000, 10000, 10000, 10000}',
'{{"meeting", "lunch"}, {"training day", "presentation"}}',
'{511.123, 622.345,1}'
);
STATEMENT
$sth = $dbh.prepare(q:to/STATEMENT/);
SELECT name, pay_by_quarter, schedule, salary_by_month
FROM sal_emp
STATEMENT
$sth.execute;
my %h = $sth.row(:hash);;
class SalEmp {
has $.name;
has Int @.pay_by_quarter;
has @.schedule;
has Num @.salary_by_month;
submethod BUILD(:$!name, :@!pay_by_quarter, :@!schedule, :@!salary_by_month) { }
};
is %h.elems, 4, "Contain 4 elements";
my $obj;
lives-ok {
$obj = SalEmp.new(|%h);
}, "Can create class";
isa-ok $obj.pay_by_quarter, Array[Int], 'Array[Int]';
isa-ok $obj.salary_by_month, Array[Num], 'Array[Num]';
isa-ok $obj.schedule, Array, 'schedule is array';
is $obj.schedule.elems, 2, 'schedule with 2';
isa-ok $obj.schedule[0], Array[Str];
# Cleanup
$dbh.dispose;