From 22cf860615dcc891a37e5ce7e3fdfabb9b2df97b Mon Sep 17 00:00:00 2001 From: John Gravatt Date: Sat, 29 Nov 2014 10:23:54 -0600 Subject: [PATCH] more unit tests --- lib/MySQL/Util.pm | 57 +++++++++++--------- t/03-main.t | 2 + t/lib/MySQL/Util/Test/DropFks.pm | 80 +++++++++++++++++++++++++++++ t/lib/MySQL/Util/Test/GetColumns.pm | 9 ++++ 4 files changed, 124 insertions(+), 24 deletions(-) create mode 100644 t/lib/MySQL/Util/Test/DropFks.pm diff --git a/lib/MySQL/Util.pm b/lib/MySQL/Util.pm index 5811cb3..55cd2c6 100644 --- a/lib/MySQL/Util.pm +++ b/lib/MySQL/Util.pm @@ -5,7 +5,7 @@ use warnings FATAL => 'all'; use Moose; use namespace::autoclean; use DBI; -use Carp; +use Carp::Always; use DBIx::DataFactory; use Data::Dumper; $Data::Dumper::Sortkeys = 1; @@ -227,7 +227,7 @@ sub BUILD { # uncoverable branch true if ( $ENV{VERBOSE} ) { - $self->_config_verbose_funcs(); # uncoverable statement + $self->_config_verbose_funcs(); # uncoverable statement } my $netrc_error; @@ -607,7 +607,7 @@ sub drop_fk { return [ $sql, $fk_ddl ]; } - return [ $fk_ddl ]; + return [$fk_ddl]; } =item drop_fks([$table]) @@ -794,34 +794,42 @@ sub dup_table { my $show_create = $self->_show_create_table($table); $self->_verbose($show_create); + my $is_temp = 0; if ( $show_create =~ /create temporary table/i ) { $show_create =~ s/CREATE TEMPORARY TABLE `[\w_]+`/CREATE TEMPORARY TABLE `$new_table`/i; + $is_temp = 1; } else { $show_create =~ s/CREATE TABLE `[\w_]+`/CREATE TABLE `$new_table`/i; } - if ( !$dst_util - or $self->host eq $dst_util->host - or $self->database eq $dst_util->database ) + my $strip_names = 0; + if ( !$dst_util ) { + $strip_names = 1; + } + elsif ( $self->host eq $dst_util->host + or $self->mysql_socket eq $dst_util->mysql_socket ) { + if ( $self->database ) { + if ( $self->database eq $dst_util->database ) { + $strip_names = 1; + } + } + } + + if ($strip_names) { # can't have dup key or constraint names $show_create =~ s/key `*[\w_]+`* \(/key \(/ig; # remove key names $show_create =~ s/constraint `*[\w_]+`* F/constraint f/ig; # remove constraint names } - + $self->_verbose($show_create); - my $dbh = $self->_dbh; - if ($dst_util) { - $dbh = $dst_util->_dbh; - $dst_util->_temp_tables->{$new_table} = 1; - } - else { - $self->_temp_tables->{$new_table} = 1; - } + + my $dbh = $dst_util ? $dst_util->_dbh : $self->_dbh; + $dst_util->_temp_tables->{$new_table} = 1 if $is_temp; $dbh->do($show_create); } @@ -2289,8 +2297,8 @@ sub upsert { my $into_table => { optional => 1, isa => 'Str|Undef' }; $self->_verbose("table = $table"); - $self->_verbose(Dumper $values_href); - + $self->_verbose( Dumper $values_href); + $util = $self if !$util; my $lc_values_href = $self->_hashkeys_to_lc($values_href); @@ -2833,13 +2841,13 @@ sub set_fk_rules { ); my $undo_ddl_aref = $self->drop_fk( - table => $table, - fk_name => $fk_name, - include_drop_stmt => 1 + table => $table, + fk_name => $fk_name, + include_drop_stmt => 1 ); $self->_verbose($new_ddl); - $self->_verbose(Dumper $undo_ddl_aref); + $self->_verbose( Dumper $undo_ddl_aref); $self->_dbh->do($new_ddl); return $undo_ddl_aref; @@ -2895,9 +2903,10 @@ Returns true if table exists. Otherwise returns false. sub temp_table_exists { args_pos - # required - my $self => 'Object', - my $table => 'Str'; + + # required + my $self => 'Object', + my $table => 'Str'; my $desc; eval { $desc = $self->describe_table( $table, 1 ); }; diff --git a/t/03-main.t b/t/03-main.t index ca98f5f..4fc7ccb 100644 --- a/t/03-main.t +++ b/t/03-main.t @@ -22,6 +22,7 @@ use MySQL::Util::Test::SetFkRules; use MySQL::Util::Test::TempTableExists; use MySQL::Util::Test::IsFkConstraint; use MySQL::Util::Test::CreateData; +use MySQL::Util::Test::DropFks; ###### CONSTANTS ###### @@ -58,6 +59,7 @@ MySQL::Util::Test::AddColumn->new( util => $Util, load_db => 0 )->run; MySQL::Util::Test::SetFkRules->new( load_db => 0 )->run; MySQL::Util::Test::TempTableExists->new( load_db => 0 )->run; MySQL::Util::Test::IsFkConstraint->new( load_db => 0 )->run; +MySQL::Util::Test::DropFks->new(load_db => 0)->run; # dont drop any tables until last MySQL::Util::Test::DropTable->new( util => $Util, load_db => 0 )->run; diff --git a/t/lib/MySQL/Util/Test/DropFks.pm b/t/lib/MySQL/Util/Test/DropFks.pm new file mode 100644 index 0000000..6f15d48 --- /dev/null +++ b/t/lib/MySQL/Util/Test/DropFks.pm @@ -0,0 +1,80 @@ +package MySQL::Util::Test::DropFks; + +use Modern::Perl; +use Carp; +use Data::Dumper; +use Test::More; +use MySQL::Util; +use Smart::Args; +use lib '.', './t'; +use MySQL::Util::Test::Common qw( + load_db + get_mysql_util +); + +sub run { + my $self = shift; + $self->no_table; + $self->with_table; +} + +sub with_table { + my $self = shift; + my $util = $self->{util}; + + my $aref = $util->drop_fks('depth_2a'); + ok( ref $aref eq 'ARRAY'); + ok( scalar @$aref == 2); + + my $href = $util->get_fk_constraints('depth_2a'); + ok( ( scalar keys %$href ) == 0); + $href = $util->get_fk_constraints; + ok( ( scalar keys %$href ) == 5); + + ok( $util->apply_ddl($aref) ); + + $href = $util->get_fk_constraints('depth_2a'); + ok( ( scalar keys %$href ) == 2); + $href = $util->get_fk_constraints; + ok( ( scalar keys %$href ) == 7); +} + +sub no_table { + my $self = shift; + my $util = $self->{util}; + + my $aref = $util->drop_fks; + ok( ref $aref eq 'ARRAY'); + ok( scalar @$aref == 7); + + my $href = $util->get_fk_constraints; + ok( ( scalar keys %$href ) == 0); + + ok( $util->apply_ddl($aref) ); + + $href = $util->get_fk_constraints; + ok( ( scalar keys %$href ) == 7); +} + +sub new { + args + + # required + my $class => 'Str', + + # optional + my $util => { optional => 1, isa => 'MySQL::Util' }, + my $load_db => { optional => 1, isa => 'Bool', default => 1 }; + + my $self = {}; + + if ($load_db) { + load_db(); + } + + $self->{util} = $util ? $util : get_mysql_util(); + + return bless $self, $class; +} + +1; diff --git a/t/lib/MySQL/Util/Test/GetColumns.pm b/t/lib/MySQL/Util/Test/GetColumns.pm index 1b04081..5597b73 100644 --- a/t/lib/MySQL/Util/Test/GetColumns.pm +++ b/t/lib/MySQL/Util/Test/GetColumns.pm @@ -32,10 +32,19 @@ sub test { ok( @cols == 2 ); } +sub should_fail { + my $self = shift; + my $util = $self->{util}; + + eval { $util->get_columns( table => 'table_a', uc => 1, lc => 1 ); }; + ok($@); +} + sub run { my $self = shift; $self->test; + $self->should_fail; } sub new {