35526ff91b0e2da84424c122f214bfd4d07e54ac
[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 List::MoreUtils 'apply';
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     dsn         => $dsn,
15     user        => $user,
16     password    => $password,
17     extra       => {
18         create => [
19             q{
20                 CREATE TABLE pg_loader_test1 (
21                     id SERIAL NOT NULL PRIMARY KEY,
22                     value VARCHAR(100)
23                 )
24             },
25             q{
26                 COMMENT ON TABLE pg_loader_test1 IS 'The Table'
27             },
28             q{
29                 COMMENT ON COLUMN pg_loader_test1.value IS 'The Column'
30             },
31             q{
32                 CREATE TABLE pg_loader_test2 (
33                     id SERIAL NOT NULL PRIMARY KEY,
34                     value VARCHAR(100)
35                 )
36             },
37             q{
38                 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'
39             },
40             # Test to make sure data_types that don't need a size => don't
41             # have one and check varying types have the correct size.
42             q{
43                 CREATE TABLE pg_loader_test3 (
44                     id SERIAL NOT NULL PRIMARY KEY,
45                     a_bigint BIGINT,
46                     an_int8 INT8,
47                     a_bigserial BIGSERIAL,
48                     a_serial8 SERIAL8,
49                     a_bit BIT,
50                     a_boolean BOOLEAN,
51                     a_bool BOOL,
52                     a_box BOX,
53                     a_bytea BYTEA,
54                     a_cidr CIDR,
55                     a_circle CIRCLE,
56                     a_date DATE,
57                     a_double_precision DOUBLE PRECISION,
58                     a_float8 FLOAT8,
59                     an_inet INET,
60                     an_integer INTEGER,
61                     an_int INT,
62                     an_int4 INT4,
63                     an_interval INTERVAL,
64                     an_interval_with_precision INTERVAL(2),
65                     a_line LINE,
66                     an_lseg LSEG,
67                     a_macaddr MACADDR,
68                     a_money MONEY,
69                     a_path PATH,
70                     a_point POINT,
71                     a_polygon POLYGON,
72                     a_real REAL,
73                     a_float4 FLOAT4,
74                     a_smallint SMALLINT,
75                     an_int2 INT2,
76                     a_serial SERIAL,
77                     a_serial4 SERIAL4,
78                     a_text TEXT,
79                     a_time TIME,
80                     a_time_with_precision TIME(2),
81                     a_time_without_time_zone TIME WITHOUT TIME ZONE,
82                     a_time_without_time_zone_with_precision TIME(2) WITHOUT TIME ZONE,
83                     a_time_with_time_zone TIME WITH TIME ZONE,
84                     a_time_with_time_zone_with_precision TIME(2) WITH TIME ZONE,
85                     a_timestamp TIMESTAMP,
86                     a_timestamp_with_precision TIMESTAMP(2),
87                     a_timestamp_without_time_zone TIMESTAMP WITHOUT TIME ZONE,
88                     a_timestamp_without_time_zone_with_precision TIMESTAMP(2) WITHOUT TIME ZONE,
89                     a_timestamp_with_time_zone TIMESTAMP WITH TIME ZONE,
90                     a_timestamp_with_time_zone_with_precision TIMESTAMP(2) WITH TIME ZONE,
91                     a_bit_varying_with_precision BIT VARYING(2),
92                     a_varbit_with_precision VARBIT(2),
93                     a_character_varying_with_precision CHARACTER VARYING(2),
94                     a_varchar_with_precision VARCHAR(2),
95                     a_character_with_precision CHARACTER(2),
96                     a_char_with_precision CHAR(2),
97                     the_numeric NUMERIC(6, 3),
98                     the_decimal DECIMAL(6, 3)
99                 )
100             },
101         ],
102         drop  => [ qw/ pg_loader_test1 pg_loader_test2 pg_loader_test3 / ],
103         count => 57,
104         run   => sub {
105             my ($schema, $monikers, $classes) = @_;
106
107             my $class    = $classes->{pg_loader_test1};
108             my $filename = $schema->_loader->_get_dump_filename($class);
109
110             my $code = do {
111                 local ($/, @ARGV) = (undef, $filename);
112                 <>;
113             };
114
115             like $code, qr/^=head1 NAME\n\n^$class - The Table\n\n^=cut\n/m,
116                 'table comment';
117
118             like $code, qr/^=head2 value\n\n(.+:.+\n)+\nThe Column\n\n/m,
119                 'column comment and attrs';
120
121             $class    = $classes->{pg_loader_test2};
122             $filename = $schema->_loader->_get_dump_filename($class);
123
124             $code = do {
125                 local ($/, @ARGV) = (undef, $filename);
126                 <>;
127             };
128
129             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,
130                 'long table comment is in DESCRIPTION';
131
132             my $rsrc = $schema->resultset($monikers->{pg_loader_test3})
133                 ->result_source;
134             my @type_columns = grep /^a/, $rsrc->columns;
135             my @without_precision = grep !/_with_precision\z/, @type_columns;
136             my @with_precision    = grep  /_with_precision\z/, @type_columns;
137             my %with_precision;
138             @with_precision{
139                 apply { s/_with_precision\z// } @with_precision
140             } = ();
141
142             for my $col (@without_precision) {
143                 my ($data_type) = $col =~ /^an?_(.*)/;
144                 ($data_type = uc $data_type) =~ s/_/ /g;
145
146                 ok((not exists $rsrc->column_info($col)->{size}),
147                     "$data_type " .
148                     (exists $with_precision{$col} ? 'without precision ' : '') .
149                     "has no 'size' column_info")
150                 or diag "size is ".$rsrc->column_info($col)->{size}."\n";
151             }
152
153             for my $col (@with_precision) {
154                 my ($data_type) = $col =~ /^an?_(.*)_with_precision\z/;
155                 ($data_type = uc $data_type) =~ s/_/ /g;
156                 my $size = $rsrc->column_info($col)->{size};
157
158                 is $size, 2,
159                   "$data_type with precision has a correct 'size' column_info";
160             }
161
162             is_deeply $rsrc->column_info('the_numeric')->{size}, [6,3],
163                 'size for NUMERIC(precision, scale) is correct';
164
165             is_deeply $rsrc->column_info('the_decimal')->{size}, [6,3],
166                 'size for DECIMAL(precision, scale) is correct';
167         },
168     },
169 );
170
171 if( !$dsn || !$user ) {
172     $tester->skip_tests('You need to set the DBICTEST_PG_DSN, _USER, and _PASS environment variables');
173 }
174 else {
175     $tester->run_tests();
176 }