data type tester for common tests
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 12pg_common.t
1 use strict;
2 use lib qw(t/lib);
3 use dbixcsl_common_tests;
4 use Test::More;
5 use File::Slurp 'slurp';
6
7 my $dsn      = $ENV{DBICTEST_PG_DSN} || '';
8 my $user     = $ENV{DBICTEST_PG_USER} || '';
9 my $password = $ENV{DBICTEST_PG_PASS} || '';
10
11 my $tester = dbixcsl_common_tests->new(
12     vendor      => 'Pg',
13     auto_inc_pk => 'SERIAL NOT NULL PRIMARY KEY',
14     default_function => 'now()',
15     dsn         => $dsn,
16     user        => $user,
17     password    => $password,
18     data_types  => {
19         'bigint'    => { size => undef, data_type => 'bigint' },
20         'int8'      => { size => undef, data_type => 'bigint' },
21         'bigserial' => { size => undef, data_type => 'bigint', is_auto_increment => 1 },
22         'serial8'   => { size => undef, data_type => 'bigint', is_auto_increment => 1 },
23         'bit'       => { size => undef, data_type => 'bit' },
24         'boolean'   => { size => undef, data_type => 'boolean' },
25         'bool'      => { size => undef, data_type => 'boolean' },
26         'box'       => { size => undef, data_type => 'box' },
27         'bytea'     => { size => undef, data_type => 'bytea' },
28         'cidr'      => { size => undef, data_type => 'cidr' },
29         'circle'    => { size => undef, data_type => 'circle' },
30         'date'      => { size => undef, data_type => 'date' },
31         'double precision' => { size => undef, data_type => 'double precision' },
32         'float8'      => { size => undef, data_type => 'double precision' },
33         'inet'        => { size => undef, data_type => 'inet' },
34         'integer'     => { size => undef, data_type => 'integer' },
35         'int'         => { size => undef, data_type => 'integer' },
36         'int4'        => { size => undef, data_type => 'integer' },
37         'interval'    => { size => undef, data_type => 'interval' },
38         'interval(2)' => { size => 2, data_type => 'interval' },
39         'line'        => { size => undef, data_type => 'line' },
40         'lseg'        => { size => undef, data_type => 'lseg' },
41         'macaddr'     => { size => undef, data_type => 'macaddr' },
42         'money'       => { size => undef, data_type => 'money' },
43         'path'        => { size => undef, data_type => 'path' },
44         'point'       => { size => undef, data_type => 'point' },
45         'polygon'     => { size => undef, data_type => 'polygon' },
46         'real'        => { size => undef, data_type => 'real' },
47         'float4'      => { size => undef, data_type => 'real' },
48         'smallint'    => { size => undef, data_type => 'smallint' },
49         'int2'        => { size => undef, data_type => 'smallint' },
50         'serial'      => { size => undef, data_type => 'integer', is_auto_increment => 1 },
51         'serial4'     => { size => undef, data_type => 'integer', is_auto_increment => 1 },
52         'text'        => { size => undef, data_type => 'text' },
53         'time'        => { size => undef, data_type => 'time without time zone' },
54         'time(2)'     => { size => 2, data_type => 'time without time zone' },
55         'time without time zone'         => { size => undef, data_type => 'time without time zone' },
56         'time(2) without time zone'      => { size => 2, data_type => 'time without time zone' },
57         'time with time zone'            => { size => undef, data_type => 'time with time zone' },
58         'time(2) with time zone'         => { size => 2, data_type => 'time with time zone' },
59         'timestamp'                      => { size => undef, data_type => 'timestamp without time zone' },
60         'timestamp(2)'                   => { size => 2, data_type => 'timestamp without time zone' },
61         'timestamp without time zone'    => { size => undef, data_type => 'timestamp without time zone' },
62         'timestamp(2) without time zone' => { size => 2, data_type => 'timestamp without time zone' },
63         'timestamp with time zone'       => { size => undef, data_type => 'timestamp with time zone' },
64         'timestamp(2) with time zone'    => { size => 2, data_type => 'timestamp with time zone' },
65         'bit varying(2)'                 => { size => 2, data_type => 'bit varying' },
66         'varbit(2)'                      => { size => 2, data_type => 'bit varying' },
67         'character varying(2)'           => { size => 2, data_type => 'character varying' },
68         'varchar(2)'                     => { size => 2, data_type => 'character varying' },
69         'character(2)'                   => { size => 2, data_type => 'character' },
70         'char(2)'                        => { size => 2, data_type => 'character' },
71         'numeric(6, 3)'                  => { size => [6,3], data_type => 'numeric' },
72         'decimal(6, 3)'                  => { size => [6,3], data_type => 'numeric' },
73     },
74     extra       => {
75         create => [
76             q{
77                 CREATE TABLE pg_loader_test1 (
78                     id SERIAL NOT NULL PRIMARY KEY,
79                     value VARCHAR(100)
80                 )
81             },
82             q{
83                 COMMENT ON TABLE pg_loader_test1 IS 'The Table'
84             },
85             q{
86                 COMMENT ON COLUMN pg_loader_test1.value IS 'The Column'
87             },
88             q{
89                 CREATE TABLE pg_loader_test2 (
90                     id SERIAL NOT NULL PRIMARY KEY,
91                     value VARCHAR(100)
92                 )
93             },
94             q{
95                 COMMENT ON TABLE pg_loader_test2 IS 'very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long comment'
96             },
97         ],
98         drop  => [ qw/ pg_loader_test1 pg_loader_test2 / ],
99         count => 3,
100         run   => sub {
101             my ($schema, $monikers, $classes) = @_;
102
103             my $class    = $classes->{pg_loader_test1};
104             my $filename = $schema->_loader->_get_dump_filename($class);
105
106             my $code = slurp $filename;
107
108             like $code, qr/^=head1 NAME\n\n^$class - The Table\n\n^=cut\n/m,
109                 'table comment';
110
111             like $code, qr/^=head2 value\n\n(.+:.+\n)+\nThe Column\n\n/m,
112                 'column comment and attrs';
113
114             $class    = $classes->{pg_loader_test2};
115             $filename = $schema->_loader->_get_dump_filename($class);
116
117             $code = slurp $filename;
118
119             like $code, qr/^=head1 NAME\n\n^$class\n\n=head1 DESCRIPTION\n\n^very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long comment\n\n^=cut\n/m,
120                 'long table comment is in DESCRIPTION';
121         },
122     },
123 );
124
125 if( !$dsn || !$user ) {
126     $tester->skip_tests('You need to set the DBICTEST_PG_DSN, _USER, and _PASS environment variables');
127 }
128 else {
129     $tester->run_tests();
130 }