From: Rafael Kitover Date: Wed, 17 Mar 2010 21:20:14 +0000 (-0400) Subject: data type tester for common tests X-Git-Tag: 0.06000~47 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9065de345da1265ce3ccd7e2d82acebd148f6981;p=dbsrgits%2FDBIx-Class-Schema-Loader.git data type tester for common tests --- diff --git a/t/12pg_common.t b/t/12pg_common.t index b10dfa6..b09d0c4 100644 --- a/t/12pg_common.t +++ b/t/12pg_common.t @@ -2,7 +2,7 @@ use strict; use lib qw(t/lib); use dbixcsl_common_tests; use Test::More; -use List::MoreUtils 'apply'; +use File::Slurp 'slurp'; my $dsn = $ENV{DBICTEST_PG_DSN} || ''; my $user = $ENV{DBICTEST_PG_USER} || ''; @@ -17,23 +17,23 @@ my $tester = dbixcsl_common_tests->new( password => $password, data_types => { 'bigint' => { size => undef, data_type => 'bigint' }, - 'int8' => { size => undef, data_type => 'int8' }, - 'bigserial' => { size => undef, data_type => 'bigserial' }, - 'serial8' => { size => undef, data_type => 'serial8' }, + 'int8' => { size => undef, data_type => 'bigint' }, + 'bigserial' => { size => undef, data_type => 'bigint', is_auto_increment => 1 }, + 'serial8' => { size => undef, data_type => 'bigint', is_auto_increment => 1 }, 'bit' => { size => undef, data_type => 'bit' }, 'boolean' => { size => undef, data_type => 'boolean' }, - 'bool' => { size => undef, data_type => 'bool' }, + 'bool' => { size => undef, data_type => 'boolean' }, 'box' => { size => undef, data_type => 'box' }, 'bytea' => { size => undef, data_type => 'bytea' }, 'cidr' => { size => undef, data_type => 'cidr' }, 'circle' => { size => undef, data_type => 'circle' }, 'date' => { size => undef, data_type => 'date' }, 'double precision' => { size => undef, data_type => 'double precision' }, - 'float8' => { size => undef, data_type => 'float8' }, + 'float8' => { size => undef, data_type => 'double precision' }, 'inet' => { size => undef, data_type => 'inet' }, 'integer' => { size => undef, data_type => 'integer' }, - 'int' => { size => undef, data_type => 'int' }, - 'int4' => { size => undef, data_type => 'int4' }, + 'int' => { size => undef, data_type => 'integer' }, + 'int4' => { size => undef, data_type => 'integer' }, 'interval' => { size => undef, data_type => 'interval' }, 'interval(2)' => { size => 2, data_type => 'interval' }, 'line' => { size => undef, data_type => 'line' }, @@ -44,32 +44,32 @@ my $tester = dbixcsl_common_tests->new( 'point' => { size => undef, data_type => 'point' }, 'polygon' => { size => undef, data_type => 'polygon' }, 'real' => { size => undef, data_type => 'real' }, - 'float4' => { size => undef, data_type => 'float4' }, + 'float4' => { size => undef, data_type => 'real' }, 'smallint' => { size => undef, data_type => 'smallint' }, - 'int2' => { size => undef, data_type => 'int2' }, - 'serial' => { size => undef, data_type => 'serial' }, - 'serial4' => { size => undef, data_type => 'serial4' }, + 'int2' => { size => undef, data_type => 'smallint' }, + 'serial' => { size => undef, data_type => 'integer', is_auto_increment => 1 }, + 'serial4' => { size => undef, data_type => 'integer', is_auto_increment => 1 }, 'text' => { size => undef, data_type => 'text' }, - 'time' => { size => undef, data_type => 'time' }, - 'time(2)' => { size => 2, data_type => 'time' }, + 'time' => { size => undef, data_type => 'time without time zone' }, + 'time(2)' => { size => 2, data_type => 'time without time zone' }, 'time without time zone' => { size => undef, data_type => 'time without time zone' }, 'time(2) without time zone' => { size => 2, data_type => 'time without time zone' }, 'time with time zone' => { size => undef, data_type => 'time with time zone' }, 'time(2) with time zone' => { size => 2, data_type => 'time with time zone' }, - 'timestamp' => { size => undef, data_type => 'timestamp' }, - 'timestamp(2)' => { size => 2, data_type => 'timestamp' }, + 'timestamp' => { size => undef, data_type => 'timestamp without time zone' }, + 'timestamp(2)' => { size => 2, data_type => 'timestamp without time zone' }, 'timestamp without time zone' => { size => undef, data_type => 'timestamp without time zone' }, 'timestamp(2) without time zone' => { size => 2, data_type => 'timestamp without time zone' }, 'timestamp with time zone' => { size => undef, data_type => 'timestamp with time zone' }, 'timestamp(2) with time zone' => { size => 2, data_type => 'timestamp with time zone' }, 'bit varying(2)' => { size => 2, data_type => 'bit varying' }, - 'varbit(2)' => { size => 2, data_type => 'varbit' }, + 'varbit(2)' => { size => 2, data_type => 'bit varying' }, 'character varying(2)' => { size => 2, data_type => 'character varying' }, - 'varchar(2)' => { size => 2, data_type => 'varchar' }, + 'varchar(2)' => { size => 2, data_type => 'character varying' }, 'character(2)' => { size => 2, data_type => 'character' }, - 'char(2)' => { size => 2, data_type => 'char' }, + 'char(2)' => { size => 2, data_type => 'character' }, 'numeric(6, 3)' => { size => [6,3], data_type => 'numeric' }, - 'decimal(6, 3)' => { size => [6,3], data_type => 'decimal' }, + 'decimal(6, 3)' => { size => [6,3], data_type => 'numeric' }, }, extra => { create => [ @@ -94,80 +94,16 @@ my $tester = dbixcsl_common_tests->new( q{ 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' }, - # Test to make sure data_types that don't need a size => don't - # have one and check varying types have the correct size. - q{ - CREATE TABLE pg_loader_test3 ( - id SERIAL NOT NULL PRIMARY KEY, - a_bigint BIGINT, - an_int8 INT8, - a_bigserial BIGSERIAL, - a_serial8 SERIAL8, - a_bit BIT, - a_boolean BOOLEAN, - a_bool BOOL, - a_box BOX, - a_bytea BYTEA, - a_cidr CIDR, - a_circle CIRCLE, - a_date DATE, - a_double_precision DOUBLE PRECISION, - a_float8 FLOAT8, - an_inet INET, - an_integer INTEGER, - an_int INT, - an_int4 INT4, - an_interval INTERVAL, - an_interval_with_precision INTERVAL(2), - a_line LINE, - an_lseg LSEG, - a_macaddr MACADDR, - a_money MONEY, - a_path PATH, - a_point POINT, - a_polygon POLYGON, - a_real REAL, - a_float4 FLOAT4, - a_smallint SMALLINT, - an_int2 INT2, - a_serial SERIAL, - a_serial4 SERIAL4, - a_text TEXT, - a_time TIME, - a_time_with_precision TIME(2), - a_time_without_time_zone TIME WITHOUT TIME ZONE, - a_time_without_time_zone_with_precision TIME(2) WITHOUT TIME ZONE, - a_time_with_time_zone TIME WITH TIME ZONE, - a_time_with_time_zone_with_precision TIME(2) WITH TIME ZONE, - a_timestamp TIMESTAMP, - a_timestamp_with_precision TIMESTAMP(2), - a_timestamp_without_time_zone TIMESTAMP WITHOUT TIME ZONE, - a_timestamp_without_time_zone_with_precision TIMESTAMP(2) WITHOUT TIME ZONE, - a_timestamp_with_time_zone TIMESTAMP WITH TIME ZONE, - a_timestamp_with_time_zone_with_precision TIMESTAMP(2) WITH TIME ZONE, - a_bit_varying_with_precision BIT VARYING(2), - a_varbit_with_precision VARBIT(2), - a_character_varying_with_precision CHARACTER VARYING(2), - a_varchar_with_precision VARCHAR(2), - a_character_with_precision CHARACTER(2), - a_char_with_precision CHAR(2), - the_numeric NUMERIC(6, 3), - the_decimal DECIMAL(6, 3) - ) - }, ], - drop => [ qw/ pg_loader_test1 pg_loader_test2 pg_loader_test3 / ], - count => 57, + drop => [ qw/ pg_loader_test1 pg_loader_test2 / ], + count => 3, run => sub { my ($schema, $monikers, $classes) = @_; my $class = $classes->{pg_loader_test1}; my $filename = $schema->_loader->_get_dump_filename($class); - my $code = do { - local ($/, @ARGV) = (undef, $filename); - <>; - }; + my $code = slurp $filename; like $code, qr/^=head1 NAME\n\n^$class - The Table\n\n^=cut\n/m, 'table comment'; @@ -178,49 +114,10 @@ my $tester = dbixcsl_common_tests->new( $class = $classes->{pg_loader_test2}; $filename = $schema->_loader->_get_dump_filename($class); - $code = do { - local ($/, @ARGV) = (undef, $filename); - <>; - }; + $code = slurp $filename; 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, 'long table comment is in DESCRIPTION'; - - my $rsrc = $schema->resultset($monikers->{pg_loader_test3}) - ->result_source; - my @type_columns = grep /^a/, $rsrc->columns; - my @without_precision = grep !/_with_precision\z/, @type_columns; - my @with_precision = grep /_with_precision\z/, @type_columns; - my %with_precision; - @with_precision{ - apply { s/_with_precision\z// } @with_precision - } = (); - - for my $col (@without_precision) { - my ($data_type) = $col =~ /^an?_(.*)/; - ($data_type = uc $data_type) =~ s/_/ /g; - - ok((not exists $rsrc->column_info($col)->{size}), - "$data_type " . - (exists $with_precision{$col} ? 'without precision ' : '') . - "has no 'size' column_info") - or diag "size is ".$rsrc->column_info($col)->{size}."\n"; - } - - for my $col (@with_precision) { - my ($data_type) = $col =~ /^an?_(.*)_with_precision\z/; - ($data_type = uc $data_type) =~ s/_/ /g; - my $size = $rsrc->column_info($col)->{size}; - - is $size, 2, - "$data_type with precision has a correct 'size' column_info"; - } - - is_deeply $rsrc->column_info('the_numeric')->{size}, [6,3], - 'size for NUMERIC(precision, scale) is correct'; - - is_deeply $rsrc->column_info('the_decimal')->{size}, [6,3], - 'size for DECIMAL(precision, scale) is correct'; }, }, ); diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index 81a0d1d..9b1177e 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -11,6 +11,7 @@ use DBI; use Digest::MD5; use File::Find 'find'; use Class::Unload (); +use Data::Dumper::Concise; my $DUMP_DIR = './t/_common_dump'; rmtree $DUMP_DIR; @@ -45,7 +46,11 @@ sub new { $self->{default_function} ||= "CURRENT_TIMESTAMP"; $self->{default_function_def} ||= "TIMESTAMP DEFAULT $self->{default_function}"; - return bless $self => $class; + $self = bless $self, $class; + + $self->setup_data_type_tests; + + return $self; } sub skip_tests { @@ -74,7 +79,7 @@ sub run_tests { } } - plan tests => @connect_info * (174 + ($self->{extra}->{count} || 0)); + plan tests => @connect_info * (174 + ($self->{extra}{count} || 0) + ($self->{data_type_tests}{test_count} || 0)); foreach my $info_idx (0..$#connect_info) { my $info = $connect_info[$info_idx]; @@ -130,7 +135,7 @@ sub setup_schema { Class::Unload->unload($schema_class); my $file_count; - my $expected_count = 36; + my $expected_count = 36 + ($self->{data_type_tests} ? 1 : 0); { my @loader_warnings; local $SIG{__WARN__} = sub { push(@loader_warnings, $_[0]); }; @@ -860,7 +865,29 @@ sub test_schema { 'Foreign key detected'); } - $self->{extra}->{run}->($conn, $monikers, $classes) if $self->{extra}->{run}; + # test data types + if (my $data_type_tests = $self->{data_type_tests}) { + my $columns = $data_type_tests->{columns}; + + my $rsrc = $conn->resultset($data_type_tests->{table_moniker})->result_source; + + while (my ($col_name, $expected_info) = each %$columns) { + while (my ($info_key, $info_val) = each %$expected_info) { + my $text_info_val = do { + my $dd = Dumper; + $dd->Indent(0); + $dd->Values([$info_val]); + $dd->Dump; + }; + + is_deeply $rsrc->column_info($col_name)->{$info_key}, $info_val, + "column of type $col_name has $info_key => $text_info_val"; + } + } + } + + # run extra tests + $self->{extra}{run}->($conn, $monikers, $classes) if $self->{extra}{run}; $self->drop_tables unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}; @@ -1342,6 +1369,9 @@ sub create { }; $dbh->do($_) for (@statements); + + $dbh->do($self->{data_type_tests}{ddl}) if $self->{data_type_tests}; + unless($self->{skip_rels}) { # hack for now, since DB2 doesn't like inline comments, and we need # to test one for mysql, which works on everyone else... @@ -1463,6 +1493,9 @@ sub drop_tables { } $dbh->do($_) for map { $drop_auto_inc->(@$_) } @tables_auto_inc; $dbh->do("DROP TABLE $_") for (@tables, @tables_rescan); + + $dbh->do("DROP TABLE ".$self->{data_type_tests}{table_name}) if $self->{data_type_tests}; + $dbh->disconnect; # fixup for Firebird @@ -1494,6 +1527,44 @@ sub _custom_column_info { return; } +sub setup_data_type_tests { + my $self = shift; + + return unless my $types = $self->{data_types}; + + my $tests = $self->{data_type_tests} = {}; + my $cols = $tests->{columns} = {}; + + $tests->{table_name} = 'loader_test9999'; + $tests->{table_moniker} = 'LoaderTest9999'; + + my $ddl = "CREATE TABLE loader_test9999 (\n id INTEGER NOT NULL PRIMARY KEY,\n"; + + my $test_count = 0; + + while (my ($col_def, $expected_info) = each %$types) { + my $have_size = $col_def =~ /\(/ ? 1 : 0; + + (my $type_alias = $col_def) =~ s/\([^()]+\)//g; + $type_alias =~ s/\s/_/g; + + my $col_name = $type_alias . ($have_size ? '_with_size' : ''); + + $ddl .= " $col_name $col_def,\n"; + + $cols->{$col_name} = $expected_info; + + $test_count += scalar keys %$expected_info; + } + + $ddl =~ s/,\n\z/\n)/; + + $tests->{ddl} = $ddl; + $tests->{test_count} = $test_count; + + return $test_count; +} + sub DESTROY { my $self = shift; unless ($ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {