added expected trigger and stored procedure output
[dbsrgits/SQL-Translator.git] / t / 13schema.t
index 8f787f3..f0f873e 100644 (file)
@@ -4,9 +4,10 @@
 $| = 1;
 
 use strict;
-use Test::More tests => 202;
+use Test::More tests => 238;
 use SQL::Translator::Schema::Constants;
 
+require_ok( 'SQL::Translator' );
 require_ok( 'SQL::Translator::Schema' );
 
 {
@@ -55,16 +56,28 @@ require_ok( 'SQL::Translator::Schema' );
         '... because "foo" exists' );
 
     $redundant_table = $schema->add_table(name => '');
-    is( $redundant_table, undef, qq[Can't add an anonymouse table...] );
+    is( $redundant_table, undef, qq[Can't add an anonymous table...] );
     like( $schema->error, qr/No table name/i,
-        '... because if has no name ' );
+        '... because it has no name ' );
 
     $redundant_table = SQL::Translator::Schema::Table->new(name => '');
-    is( $redundant_table, undef, qq[Can't create an anonymouse table] );
+    is( $redundant_table, undef, qq[Can't create an anonymous table] );
     like( SQL::Translator::Schema::Table->error, qr/No table name/i,
-        '... because if has no name ' );
+        '... because it has no name ' );
 
     #
+    # $schema-> drop_table
+    #
+    my $dropped_table = $schema->drop_table($foo_table->name, cascade => 1);
+    isa_ok($dropped_table, 'SQL::Translator::Schema::Table', 'Dropped table "foo"' );
+    $schema->add_table($foo_table);
+    my $dropped_table2 = $schema->drop_table($foo_table, cascade => 1);
+    isa_ok($dropped_table2, 'SQL::Translator::Schema::Table', 'Dropped table "foo" by object' );
+    my $dropped_table3 = $schema->drop_table($foo_table->name, cascade => 1);
+    like( $schema->error, qr/doesn't exist/, qq[Can't drop non-existant table "foo"] );
+
+    $schema->add_table($foo_table);
+    #
     # Table default new
     #
     is( $foo_table->name, 'foo', 'Table name is "foo"' );
@@ -155,6 +168,19 @@ require_ok( 'SQL::Translator::Schema' );
         'field_names is "foo,f2"' );
 
     #
+    # $table-> drop_field
+    #
+    my $dropped_field = $person_table->drop_field($f2->name, cascade => 1);
+    isa_ok($dropped_field, 'SQL::Translator::Schema::Field', 'Dropped field "f2"' );
+    $person_table->add_field($f2);
+    my $dropped_field2 = $person_table->drop_field($f2, cascade => 1);
+    isa_ok($dropped_field2, 'SQL::Translator::Schema::Field', 'Dropped field "f2" by object' );
+    my $dropped_field3 = $person_table->drop_field($f2->name, cascade => 1);
+    like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant field "f2"] );
+
+    $person_table->add_field($f2);
+
+    #
     # Field methods
     #
     is( $f1->name('person_name'), 'person_name',
@@ -224,6 +250,20 @@ require_ok( 'SQL::Translator::Schema' );
     is( $indices->[1]->name, 'bar', '"bar" index' );
 
     #
+    # $table-> drop_index
+    #
+    my $dropped_index = $person_table->drop_index($index1->name);
+    isa_ok($dropped_index, 'SQL::Translator::Schema::Index', 'Dropped index "foo"' );
+    $person_table->add_index($index1);
+    my $dropped_index2 = $person_table->drop_index($index1);
+    isa_ok($dropped_index2, 'SQL::Translator::Schema::Index', 'Dropped index "foo" by object' );
+    is($dropped_index2->name, $index1->name, 'Dropped correct index "foo"');
+    my $dropped_index3 = $person_table->drop_index($index1->name);
+    like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant index "foo"] );
+
+    $person_table->add_index($index1);
+
+    #
     # Constraint
     #
     my @constraints = $person_table->get_constraints;
@@ -291,6 +331,20 @@ require_ok( 'SQL::Translator::Schema' );
     is( $constraints->[1]->name, 'bar', '"bar" constraint' );
 
     #
+    # $table-> drop_constraint
+    #
+    my $dropped_con = $person_table->drop_constraint($constraint1->name);
+    isa_ok($dropped_con, 'SQL::Translator::Schema::Constraint', 'Dropped constraint "foo"' );
+    $person_table->add_constraint($constraint1);
+    my $dropped_con2 = $person_table->drop_constraint($constraint1);
+    isa_ok($dropped_con2, 'SQL::Translator::Schema::Constraint', 'Dropped constraint "foo" by object' );
+    is($dropped_con2->name, $constraint1->name, 'Dropped correct constraint "foo"');
+    my $dropped_con3 = $person_table->drop_constraint($constraint1->name);
+    like( $person_table->error, qr/doesn't exist/, qq[Can't drop non-existant constraint "foo"] );
+
+    $person_table->add_constraint($constraint1);
+
+    #
     # View
     #
     my $view = $schema->add_view( name => 'view1' ) or warn $schema->error;
@@ -308,6 +362,20 @@ require_ok( 'SQL::Translator::Schema' );
     like( $schema->error, qr/can't create view/i, '... because it exists' );
 
     #
+    # $schema-> drop_view
+    #
+    my $dropped_view = $schema->drop_view($view->name);
+    isa_ok($dropped_view, 'SQL::Translator::Schema::View', 'Dropped view "view1"' );
+    $schema->add_view($view);
+    my $dropped_view2 = $schema->drop_view($view);
+    isa_ok($dropped_view2, 'SQL::Translator::Schema::View', 'Dropped view "view1" by object' );
+    is($dropped_view2->name, $view->name, 'Dropped correct view "view1"');
+    my $dropped_view3 = $schema->drop_view($view->name);
+    like( $schema->error, qr/doesn't exist/, qq[Can't drop non-existant view "view1"] );
+
+    $schema->add_view($view);
+
+    #
     # $schema->get_*
     #
     my $bad_table = $schema->get_table;
@@ -339,6 +407,7 @@ require_ok( 'SQL::Translator::Schema' );
 
     my @views = $schema->get_views;
     is( scalar @views, 2, 'Found 1 view' );
+
 }
 
 #
@@ -349,7 +418,7 @@ require_ok( 'SQL::Translator::Schema' );
         name     => 'foo',
         database => 'PostgreSQL',
     );
-    my $t = $s->add_table( name => 'person' ) or warn $s->erro;
+    my $t = $s->add_table( name => 'person' ) or warn $s->error;
     my $f = $t->add_field( name => 'person_id' ) or warn $t->error;
     $f->data_type('serial');
 
@@ -512,15 +581,16 @@ require_ok( 'SQL::Translator::Schema' );
 #
 {
     my $s                   = SQL::Translator::Schema->new(name => 'TrigTest');
+    $s->add_table(name=>'foo') or die "Couldn't create table: ", $s->error;
     my $name                = 'foo_trigger';
     my $perform_action_when = 'after';
-    my $database_event      = 'insert';
+    my $database_events     = 'insert';
     my $on_table            = 'foo';
     my $action              = 'update modified=timestamp();';
     my $t                   = $s->add_trigger(
         name                => $name,
         perform_action_when => $perform_action_when,
-        database_event      => $database_event,
+        database_events     => $database_events,
         on_table            => $on_table,
         action              => $action,
     ) or die $s->error;
@@ -531,9 +601,9 @@ require_ok( 'SQL::Translator::Schema' );
     is( $t->name, $name, qq[Name is "$name"] );
     is( $t->perform_action_when, $perform_action_when, 
         qq[Perform action when is "$perform_action_when"] );
-    is( $t->database_event, $database_event, 
-        qq[Database event is "$database_event"] );
-    is( $t->on_table, $on_table, qq[Table is "$on_table"] );
+    is( join(',', $t->database_events), $database_events, 
+        qq[Database event is "$database_events"] );
+    isa_ok( $t->table, 'SQL::Translator::Schema::Table', qq[table is a Table"] );
     is( $t->action, $action, qq[Action is "$action"] );
 
     my @triggs = $s->get_triggers;
@@ -542,8 +612,62 @@ require_ok( 'SQL::Translator::Schema' );
     my $t1 = $s->get_trigger( $name );
     isa_ok( $t1, 'SQL::Translator::Schema::Trigger', 'Trigger' );
     is( $t1->name, $name, qq[Name is "$name"] );
-}
 
+
+
+       my $s2                   = SQL::Translator::Schema->new(name => 'TrigTest2');
+    $s2->add_table(name=>'foo') or die "Couldn't create table: ", $s2->error;
+    my $t2                   = $s2->add_trigger(
+        name                => 'foo_trigger',
+        perform_action_when => 'after',
+        database_events     => [qw/insert update/],
+        on_table            => 'foo',
+        action              => 'update modified=timestamp();',
+    ) or die $s2->error;
+       isa_ok( $t2, 'SQL::Translator::Schema::Trigger', 'Trigger' );
+    isa_ok( $t2->schema, 'SQL::Translator::Schema', 'Schema' );
+    is( $t2->schema->name, 'TrigTest2', qq[Schema name is "'TrigTest2'"] );
+    is( $t2->name, 'foo_trigger', qq[Name is "foo_trigger"] );
+       is_deeply(
+        [$t2->database_events],
+        [qw/insert update/],
+        "Database events are [qw/insert update/] "
+    );
+       isa_ok($t2->database_events,'ARRAY','Database events');
+       
+       #
+       # Trigger equal tests
+       #
+       isnt(
+        $t1->equals($t2),
+        1,
+        'Compare two Triggers with database_event and database_events'
+    );
+
+       $t1->database_events($database_events);
+       $t2->database_events($database_events);
+       is($t1->equals($t2),1,'Compare two Triggers with database_event');
+
+       $t2->database_events('');
+       $t1->database_events([qw/update insert/]);
+       $t2->database_events([qw/insert update/]);
+       is($t1->equals($t2),1,'Compare two Triggers with database_events');
+       
+    #
+    # $schema-> drop_trigger
+    #
+    my $dropped_trig = $s->drop_trigger($t->name);
+    isa_ok($dropped_trig, 'SQL::Translator::Schema::Trigger', 'Dropped trigger "foo_trigger"' );
+    $s->add_trigger($t);
+    my $dropped_trig2 = $s->drop_trigger($t);
+    isa_ok($dropped_trig2, 'SQL::Translator::Schema::Trigger', 'Dropped trigger "foo_trigger" by object' );
+    is($dropped_trig2->name, $t->name, 'Dropped correct trigger "foo_trigger"');
+    my $dropped_trig3 = $s->drop_trigger($t->name);
+    like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant trigger "foo_trigger"] );
+
+    $s->add_trigger($t);
+}
 #
 # Procedure
 #
@@ -576,4 +700,18 @@ require_ok( 'SQL::Translator::Schema' );
     my $p1 = $s->get_procedure( $name );
     isa_ok( $p1, 'SQL::Translator::Schema::Procedure', 'Procedure' );
     is( $p1->name, $name, qq[Name is "$name"] );
+
+    #
+    # $schema-> drop_procedure
+    #
+    my $dropped_proc = $s->drop_procedure($p->name);
+    isa_ok($dropped_proc, 'SQL::Translator::Schema::Procedure', 'Dropped procedure "foo_proc"' );
+    $s->add_procedure($p);
+    my $dropped_proc2 = $s->drop_procedure($p);
+    isa_ok($dropped_proc2, 'SQL::Translator::Schema::Procedure', 'Dropped procedure "foo_proc" by object' );
+    is($dropped_proc2->name, $p->name, 'Dropped correct procedure "foo_proc"');
+    my $dropped_proc3 = $s->drop_procedure($p->name);
+    like( $s->error, qr/doesn't exist/, qq[Can't drop non-existant procedure "foo_proc"] );
+
+    $s->add_procedure($p);
 }