Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gravattj committed Nov 29, 2014
1 parent 2cdfa7e commit 22cf860
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 24 deletions.
57 changes: 33 additions & 24 deletions lib/MySQL/Util.pm
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -607,7 +607,7 @@ sub drop_fk {
return [ $sql, $fk_ddl ];
}

return [ $fk_ddl ];
return [$fk_ddl];
}

=item drop_fks([$table])
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ); };
Expand Down
2 changes: 2 additions & 0 deletions t/03-main.t
Expand Up @@ -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 ######

Expand Down Expand Up @@ -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;
Expand Down
80 changes: 80 additions & 0 deletions 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;
9 changes: 9 additions & 0 deletions t/lib/MySQL/Util/Test/GetColumns.pm
Expand Up @@ -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 {
Expand Down

0 comments on commit 22cf860

Please sign in to comment.