Add tests and code for update table db filtering
authorJoshua Tolley <josh@endpoint.com>
Tue, 23 Apr 2013 16:06:00 +0000 (10:06 -0600)
committerJoshua Tolley <josh@endpoint.com>
Tue, 23 Apr 2013 16:06:00 +0000 (10:06 -0600)
bucardo
t/02-bctl-table.t

diff --git a/bucardo b/bucardo
index 3dec44edbade44c76068ad464ae7dd09d2d38d6c..cf288ff13172e5e03611cc7e0977b8da9f868baf 100755 (executable)
--- a/bucardo
+++ b/bucardo
@@ -3251,33 +3251,31 @@ sub update_table {
     exit 0 if ! check_recurse($GOAT, $name, @actions);
 
     ## Make sure this table exists!
-    ## XXXJWT probably check database as well, not just name
-    #my @tables = find_goat_by_item($name, @nouns);
-    my $table = $GOAT->{by_fullname}{$name} or die qq{Could not find a table named "$name"\n}
-        . "Use 'list tables' to see all available.\n";
+    my @tables = find_goat_by_item($name, \@nouns);
 
+    if (!@tables) {
+        die qq{Didn't find any matching tables\n};
+    }
     ## If this is an array, then see how many matches we have
-    if (ref $table eq 'ARRAY') {
-        my $matches = @$table;
-        if ($matches > 1) {
-            die qq{More than one matching table: please use a schema\n};
-        }
-        $table = $table->[0];
+    if ($#tables > 0) {
+        die qq{More than one matching table: please use a schema\n};
     }
+    my $table = $tables[0];
 
     ## Store the id so we work with that alone whenever possible
     my $id = $table->{id};
 
-    ## Everything is a name=value setting after this point
+    ## Everything is a name=value setting after this point, except stuff that
+    ##   matches /^db=/
     ## We will ignore and allow noise word "set"
-    for my $arg (@actions) {
+    for my $arg (grep { ! /^db=/ } @actions) {
         next if $arg =~ /set/i;
         next if $arg =~ /\w+=\w+/o;
         usage_exit($doc_section);
     }
 
     ## Change the arguments into a hash
-    my $args = process_args(join ' ' => @actions);
+    my $args = process_args(join ' ' => ( grep { ! /^db=/ } @actions));
 
     ## Track what changes we made
     my %change;
index a50440ae2bd6d0a614bcdc33192db5b30376517f..b79dc297f5962d4f3a51e2237a18f9fb4da5230f 100644 (file)
@@ -10,7 +10,7 @@ use warnings;
 use Data::Dumper;
 use lib 't','.';
 use DBD::Pg;
-use Test::More tests => 38;
+use Test::More tests => 43;
 
 use vars qw/$t $res $expected $command $dbhX $dbhA $dbhB $SQL/;
 
@@ -281,10 +281,27 @@ $t = q{List verbose single table};
 $res = $bct->ctl('bucardo list tables -vv public.bucardo_test1');
 like ($res, qr/ghost\s+= 0/ , $t);
 
-### Tests of 'update table' usage
-#$t = q{Update table works}
-#$res = $bct->ctl('bucardo update table public.bucardo_test1 autokick=t');
+## Tests of 'update table' usage
+$t = q{Update table changes a value properly};
+$bct->ctl('bucardo update table public.bucardo_test1 ghost=1');
+$res = $bct->ctl('bucardo list tables -vv public.bucardo_test1');
+like ($res, qr/ghost\s+= 1/, $t);
+
+$t = q{Update table returns correctly when the value doesn't need changing};
+$res = $bct->ctl('bucardo update table public.bucardo_test1 ghost=1');
+like ($res, qr/No change needed for ghost/, $t);
+
+$t = q{Update table doesn't try to set "db=" actions};
+$res = $bct->ctl('bucardo update table public.bucardo_test1 db=A ghost=1');
+unlike ($res, qr/No change needed for db/, $t);
+
+$t = q{Update table correctly filters by db when table exists};
+$res = $bct->ctl('bucardo update table public.bucardo_test1 db=A ghost=1');
+like ($res, qr/No change needed for ghost/, $t);
 
+$t = q{Update table correctly filters by db when table doesn't exist};
+$res = $bct->ctl('bucardo update table public.bucardo_test1 db=B ghost=1');
+like ($res, qr/Didn't find any matching tables/, $t);
 
 END {
     $bct->stop_bucardo($dbhX);