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