more robust $dbh->tables parsing for sometimes-quoted Pg tables
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 12pg_common.t
CommitLineData
a78e3fed 1use strict;
c2849787 2use lib qw(t/lib);
fbd83464 3use dbixcsl_common_tests;
fdd8ff16 4use Test::More;
baff904e 5use List::MoreUtils 'apply';
a78e3fed 6
9e978a19 7my $dsn = $ENV{DBICTEST_PG_DSN} || '';
8my $user = $ENV{DBICTEST_PG_USER} || '';
9my $password = $ENV{DBICTEST_PG_PASS} || '';
a78e3fed 10
fbd83464 11my $tester = dbixcsl_common_tests->new(
a78e3fed 12 vendor => 'Pg',
13 auto_inc_pk => 'SERIAL NOT NULL PRIMARY KEY',
9e978a19 14 dsn => $dsn,
a78e3fed 15 user => $user,
16 password => $password,
fdd8ff16 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 },
4b9fb838 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 },
baff904e 40 # Test to make sure data_types that don't need a size => don't
d4d1a665 41 # have one and check varying types have the correct size.
baff904e 42 q{
4b9fb838 43 CREATE TABLE pg_loader_test3 (
baff904e 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,
baff904e 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,
afb4c5bc 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),
d4d1a665 96 a_char_with_precision CHAR(2),
97 the_numeric NUMERIC(6, 3),
98 the_decimal DECIMAL(6, 3)
baff904e 99 )
100 },
fdd8ff16 101 ],
4b9fb838 102 drop => [ qw/ pg_loader_test1 pg_loader_test2 pg_loader_test3 / ],
103 count => 57,
fdd8ff16 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
79a00530 118 like $code, qr/^=head2 value\n\n(.+:.+\n)+\nThe Column\n\n/m,
119 'column comment and attrs';
baff904e 120
4b9fb838 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
6ecee584 132 my $rsrc = $schema->resultset($monikers->{pg_loader_test3})
133 ->result_source;
d4d1a665 134 my @type_columns = grep /^a/, $rsrc->columns;
baff904e 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 ' : '') .
4b9fb838 149 "has no 'size' column_info")
150 or diag "size is ".$rsrc->column_info($col)->{size}."\n";
baff904e 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;
afb4c5bc 156 my $size = $rsrc->column_info($col)->{size};
baff904e 157
afb4c5bc 158 is $size, 2,
159 "$data_type with precision has a correct 'size' column_info";
baff904e 160 }
d4d1a665 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';
fdd8ff16 167 },
168 },
a78e3fed 169);
170
9e978a19 171if( !$dsn || !$user ) {
172 $tester->skip_tests('You need to set the DBICTEST_PG_DSN, _USER, and _PASS environment variables');
a78e3fed 173}
174else {
175 $tester->run_tests();
176}