patch for parser/producer args courtesy of darren (w/ embellishments by me)
[dbsrgits/SQL-Translator.git] / t / 13schema.t
index 2025d75..8f787f3 100644 (file)
@@ -1,9 +1,10 @@
 #!/usr/bin/perl
+# vim:set ft=perl:
 
 $| = 1;
 
 use strict;
-use Test::More 'no_plan'; # plans => 1;
+use Test::More tests => 202;
 use SQL::Translator::Schema::Constants;
 
 require_ok( 'SQL::Translator::Schema' );
@@ -50,24 +51,45 @@ require_ok( 'SQL::Translator::Schema' );
 
     my $redundant_table = $schema->add_table(name => 'foo');
     is( $redundant_table, undef, qq[Can't create another "foo" table...] );
-    like( $schema->error, qr/can't use table name/i, '... because "foo" exists' );
+    like( $schema->error, qr/can't use table name/i, 
+        '... because "foo" exists' );
+
+    $redundant_table = $schema->add_table(name => '');
+    is( $redundant_table, undef, qq[Can't add an anonymouse table...] );
+    like( $schema->error, qr/No table name/i,
+        '... because if has no name ' );
+
+    $redundant_table = SQL::Translator::Schema::Table->new(name => '');
+    is( $redundant_table, undef, qq[Can't create an anonymouse table] );
+    like( SQL::Translator::Schema::Table->error, qr/No table name/i,
+        '... because if has no name ' );
 
     #
     # Table default new
     #
     is( $foo_table->name, 'foo', 'Table name is "foo"' );
+    is( "$foo_table", 'foo', 'Table stringifies to "foo"' );
     is( $foo_table->is_valid, undef, 'Table "foo" is not yet valid' );
 
     my $fields = $foo_table->get_fields;
     is( scalar @{ $fields || [] }, 0, 'Table "foo" has no fields' );
     like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
 
+    is( $foo_table->comments, undef, 'No comments' );
+
     #
     # New table with args
     #
-    my $person_table = $schema->add_table( name => 'person' );
+    my $person_table = $schema->add_table( 
+        name     => 'person', 
+        comments => 'foo',
+    );
     is( $person_table->name, 'person', 'Table name is "person"' );
     is( $person_table->is_valid, undef, 'Table is not yet valid' );
+    is( $person_table->comments, 'foo', 'Comments = "foo"' );
+    is( join(',', $person_table->comments('bar')), 'foo,bar', 
+        'Table comments = "foo,bar"' );
+    is( $person_table->comments, "foo\nbar", 'Table comments = "foo,bar"' );
 
     #
     # Field default new
@@ -76,21 +98,32 @@ require_ok( 'SQL::Translator::Schema' );
         warn $person_table->error;
     isa_ok( $f1, 'SQL::Translator::Schema::Field', 'Field' );
     is( $f1->name, 'foo', 'Field name is "foo"' );
+    is( $f1->full_name, 'person.foo', 'Field full_name is "person.foo"' );
+    is( "$f1", 'foo', 'Field stringifies to "foo"' );
     is( $f1->data_type, '', 'Field data type is blank' );
     is( $f1->size, 0, 'Field size is "0"' );
     is( $f1->is_primary_key, '0', 'Field is_primary_key is false' );
-    is( $f1->nullable, 1, 'Field can be NULL' );
+    is( $f1->is_nullable, 1, 'Field can be NULL' );
     is( $f1->default_value, undef, 'Field default is undefined' );
-
-    my $f2 = SQL::Translator::Schema::Field->new (name => 'f2') or 
-        warn SQL::Translator::Schema::Field->error;
+    is( $f1->comments, '', 'No comments' );
+    is( $f1->table, 'person', 'Field table is person' );
+    is( $f1->schema->database, 'PostgreSQL', 'Field schema shortcut works' );
+
+    my $f2 = SQL::Translator::Schema::Field->new (
+        name     => 'f2',
+        comments => 'foo',
+    ) or warn SQL::Translator::Schema::Field->error;
     $f2 = $person_table->add_field( $f2 );
     isa_ok( $f1, 'SQL::Translator::Schema::Field', 'f2' );
     is( $f2->name, 'f2', 'Add field "f2"' );
-    is( $f2->nullable(0), 0, 'Field cannot be NULL' );
-    is( $f2->nullable(''), 0, 'Field cannot be NULL' );
-    is( $f2->nullable('0'), 0, 'Field cannot be NULL' );
-    is( $f1->default_value(''), '', 'Field default is empty string' );
+    is( $f2->is_nullable(0), 0, 'Field cannot be NULL' );
+    is( $f2->is_nullable(''), 0, 'Field cannot be NULL' );
+    is( $f2->is_nullable('0'), 0, 'Field cannot be NULL' );
+    is( $f2->default_value(''), '', 'Field default is empty string' );
+    is( $f2->comments, 'foo', 'Field comment = "foo"' );
+    is( join(',', $f2->comments('bar')), 'foo,bar', 
+        'Field comment = "foo,bar"' );
+    is( $f2->comments, "foo\nbar", 'Field comment = "foo,bar"' );
 
     $person_table = $f2->table( $person_table );
     isa_ok( $person_table, 'SQL::Translator::Schema::Table', 'person_table' );
@@ -100,18 +133,42 @@ require_ok( 'SQL::Translator::Schema' );
 
     my $redundant_field = $person_table->add_field(name => 'f2');
     is( $redundant_field, undef, qq[Didn't create another "f2" field...] );
-    like( $person_table->error, qr/can't create field/i, 
+    like( $person_table->error, qr/can't use field/i, 
         '... because it exists' );
 
+    $redundant_field = $person_table->add_field(name => '');
+    is( $redundant_field, undef, qq[Didn't add a "" field...] );
+    like( $person_table->error, qr/No field name/i,
+        '... because it has no name' );
+
+    $redundant_field = SQL::Translator::Schema::Field->new(name => '');
+    is( $redundant_field, undef, qq[Didn't create a "" field...] );
+    like( SQL::Translator::Schema::Field->error, qr/No field name/i,
+        '... because it has no name' );
+
+    my @fields = $person_table->get_fields;
+    is( scalar @fields, 2, 'Table "foo" has 2 fields' );
+
+    is( $fields[0]->name, 'foo', 'First field is "foo"' );
+    is( $fields[1]->name, 'f2', 'Second field is "f2"' );
+    is( join(",",$person_table->field_names), 'foo,f2',
+        'field_names is "foo,f2"' );
+
     #
     # Field methods
     #
-    is( $f1->name('person_name'), 'person_name', 
+    is( $f1->name('person_name'), 'person_name',
         'Field name is "person_name"' );
     is( $f1->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
     is( $f1->size('30'), '30', 'Field size is "30"' );
     is( $f1->is_primary_key(0), '0', 'Field is_primary_key is negative' );
 
+    $f1->extra( foo => 'bar' );
+    $f1->extra( { baz => 'quux' } );
+    my %extra = $f1->extra;
+    is( $extra{'foo'}, 'bar', 'Field extra "foo" is "bar"' );
+    is( $extra{'baz'}, 'quux', 'Field extra "baz" is "quux"' );
+
     #
     # New field with args
     #
@@ -126,6 +183,7 @@ require_ok( 'SQL::Translator::Schema' );
     is( $age->size(10,2), '10,2', 'Field size still "10,2"' );
     is( $age->size([10,2]), '10,2', 'Field size still "10,2"' );
     is( $age->size(qw[ 10 2 ]), '10,2', 'Field size still "10,2"' );
+    is( join(':', $age->size), '10:2', 'Field size returns array' );
 
     #
     # Index
@@ -138,6 +196,22 @@ require_ok( 'SQL::Translator::Schema' );
     isa_ok( $index1, 'SQL::Translator::Schema::Index', 'Index' );
     is( $index1->name, 'foo', 'Index name is "foo"' );
 
+    is( $index1->is_valid, undef, 'Index name is not valid...' );
+    like( $index1->error, qr/no fields/i, '...because it has no fields' );
+
+    is( join(':', $index1->fields('foo,bar')), 'foo:bar', 
+        'Index accepts fields');
+
+    is( $index1->is_valid, undef, 'Index name is not valid...' );
+    like( $index1->error, qr/does not exist in table/i, 
+        '...because it used fields not in the table' );
+
+    is( join(':', $index1->fields(qw[foo age])), 'foo:age', 
+        'Index accepts fields');
+    is( $index1->is_valid, 1, 'Index name is now valid' );
+
+    is( $index1->type, NORMAL, 'Index type is "normal"' );
+
     my $index2 = SQL::Translator::Schema::Index->new( name => "bar" ) 
         or warn SQL::Translator::Schema::Index->error;
     $index2    = $person_table->add_index( $index2 );
@@ -161,34 +235,58 @@ require_ok( 'SQL::Translator::Schema' );
     isa_ok( $constraint1, 'SQL::Translator::Schema::Constraint', 'Constraint' );
     is( $constraint1->name, 'foo', 'Constraint name is "foo"' );
 
-    $fields = join(',', $constraint1->fields('id') );
-    is( $fields, 'id', 'Constraint field = "id"' );
+    $fields = join(',', $constraint1->fields('age') );
+    is( $fields, 'age', 'Constraint field = "age"' );
 
-    $fields = join(',', $constraint1->fields('id,id') );
-    is( $fields, 'id', 'Constraint field = "id"' );
+    $fields = $constraint1->fields;
+    ok( ref $fields[0] && $fields[0]->isa("SQL::Translator::Schema::Field"),
+        'Constraint fields returns a SQL::Translator::Schema::Field' );
 
-    $fields = join(',', $constraint1->fields('id', 'name') );
-    is( $fields, 'id,name', 'Constraint field = "id,name"' );
+    $fields = join(',', $constraint1->fields('age,age') );
+    is( $fields, 'age', 'Constraint field = "age"' );
 
-    $fields = join(',', $constraint1->fields( 'id,name,id' ) );
-    is( $fields, 'id,name', 'Constraint field = "id,name"' );
+    $fields = join(',', $constraint1->fields('age', 'name') );
+    is( $fields, 'age,name', 'Constraint field = "age,name"' );
 
-    $fields = join(',', $constraint1->fields( 'id, name' ) );
-    is( $fields, 'id,name', 'Constraint field = "id,name"' );
+    $fields = join(',', $constraint1->fields( 'age,name,age' ) );
+    is( $fields, 'age,name', 'Constraint field = "age,name"' );
 
-    $fields = join(',', $constraint1->fields( [ 'id', 'name' ] ) );
-    is( $fields, 'id,name', 'Constraint field = "id,name"' );
+    $fields = join(',', $constraint1->fields( 'age, name' ) );
+    is( $fields, 'age,name', 'Constraint field = "age,name"' );
 
-    $fields = join(',', $constraint1->fields( qw[ id name ] ) );
-    is( $fields, 'id,name', 'Constraint field = "id,name"' );
+    $fields = join(',', $constraint1->fields( [ 'age', 'name' ] ) );
+    is( $fields, 'age,name', 'Constraint field = "age,name"' );
+
+    $fields = join(',', $constraint1->fields( qw[ age name ] ) );
+    is( $fields, 'age,name', 'Constraint field = "age,name"' );
+
+    $fields = join(',', $constraint1->field_names );
+    is( $fields, 'age,name', 'Constraint field_names = "age,name"' );
+
+    is( $constraint1->match_type, '', 'Constraint match type is empty' );
+    is( $constraint1->match_type('foo'), undef, 
+        'Constraint match type rejects bad arg...' );
+    like( $constraint1->error, qr/invalid match type/i,
+        '...because it is invalid');
+    is( $constraint1->match_type('FULL'), 'full', 
+        'Constraint match type = "full"' );
 
     my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'bar' );
     $constraint2    = $person_table->add_constraint( $constraint2 );
     isa_ok( $constraint2, 'SQL::Translator::Schema::Constraint', 'Constraint' );
     is( $constraint2->name, 'bar', 'Constraint name is "bar"' );
 
+    my $constraint3 = $person_table->add_constraint(
+        type       => 'check',
+        expression => 'foo bar',
+    ) or die $person_table->error;
+    isa_ok( $constraint3, 'SQL::Translator::Schema::Constraint', 'Constraint' );
+    is( $constraint3->type, CHECK_C, 'Constraint type is "CHECK"' );
+    is( $constraint3->expression, 'foo bar', 
+        'Constraint expression is "foo bar"' );
+
     my $constraints = $person_table->get_constraints;
-    is( scalar @$constraints, 2, 'Two constraints' );
+    is( scalar @$constraints, 3, 'Three constraints' );
     is( $constraints->[0]->name, 'foo', '"foo" constraint' );
     is( $constraints->[1]->name, 'bar', '"bar" constraint' );
 
@@ -328,9 +426,7 @@ require_ok( 'SQL::Translator::Schema' );
     is( $pet_id->name, 'pet_id', 'Added field "pet_id"' );
     
     is( $c->is_valid, 1, 'Constraint now valid' );
-
 }
-exit;
 
 #
 # $table->primary_key test
@@ -340,7 +436,6 @@ exit;
     my $t = $s->add_table( name => 'person' );
 
     is( $t->primary_key, undef, 'No primary key' );
-    like( $t->error, qr/no primary key/i, 'No primary key error' );
 
     is( $t->primary_key('person_id'), undef, 
             q[Can't make PK on "person_id"...] );
@@ -381,3 +476,104 @@ exit;
 
     is( join('', $c2->reference_fields), 'id', 'FK found PK "person.id"' );
 }
+
+#
+# View
+#
+{
+    my $s      = SQL::Translator::Schema->new( name => 'ViewTest' );
+    my $name   = 'foo_view';
+    my $sql    = 'select name, age from person';
+    my $fields = 'name, age';
+    my $v      = $s->add_view(
+        name   => $name,
+        sql    => $sql,
+        fields => $fields,
+        schema => $s,
+    );
+
+    isa_ok( $v, 'SQL::Translator::Schema::View', 'View' );
+    isa_ok( $v->schema, 'SQL::Translator::Schema', 'Schema' );
+    is( $v->schema->name, 'ViewTest', qq[Schema name is "'ViewTest'"] );
+    is( $v->name, $name, qq[Name is "$name"] );
+    is( $v->sql, $sql, qq[Name is "$sql"] );
+    is( join(':', $v->fields), 'name:age', qq[Fields are "$fields"] );
+
+    my @views = $s->get_views;
+    is( scalar @views, 1, 'Number of views is 1' );
+
+    my $v1 = $s->get_view( $name );
+    isa_ok( $v1, 'SQL::Translator::Schema::View', 'View' );
+    is( $v1->name, $name, qq[Name is "$name"] );
+}
+
+#
+# Trigger
+#
+{
+    my $s                   = SQL::Translator::Schema->new(name => 'TrigTest');
+    my $name                = 'foo_trigger';
+    my $perform_action_when = 'after';
+    my $database_event      = '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,
+        on_table            => $on_table,
+        action              => $action,
+    ) or die $s->error;
+
+    isa_ok( $t, 'SQL::Translator::Schema::Trigger', 'Trigger' );
+    isa_ok( $t->schema, 'SQL::Translator::Schema', 'Schema' );
+    is( $t->schema->name, 'TrigTest', qq[Schema name is "'TrigTest'"] );
+    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( $t->action, $action, qq[Action is "$action"] );
+
+    my @triggs = $s->get_triggers;
+    is( scalar @triggs, 1, 'Number of triggers is 1' );
+
+    my $t1 = $s->get_trigger( $name );
+    isa_ok( $t1, 'SQL::Translator::Schema::Trigger', 'Trigger' );
+    is( $t1->name, $name, qq[Name is "$name"] );
+}
+
+#
+# Procedure
+#
+{
+    my $s          = SQL::Translator::Schema->new( name => 'ProcTest' );
+    my $name       = 'foo_proc';
+    my $sql        = 'select foo from bar';
+    my $parameters = 'foo, bar';
+    my $owner      = 'Nomar';
+    my $comments   = 'Go Sox!';
+    my $p          = $s->add_procedure(
+        name       => $name,
+        sql        => $sql,
+        parameters => $parameters,
+        owner      => $owner,
+        comments   => $comments,
+    ) or die $s->error;
+
+    isa_ok( $p, 'SQL::Translator::Schema::Procedure', 'Procedure' );
+    isa_ok( $p->schema, 'SQL::Translator::Schema', 'Schema' );
+    is( $p->schema->name, 'ProcTest', qq[Schema name is "'ProcTest'"] );
+    is( $p->name, $name, qq[Name is "$name"] );
+    is( $p->sql, $sql, qq[SQL is "$sql"] );
+    is( join(',', $p->parameters), 'foo,bar', qq[Params = 'foo,bar'] );
+    is( $p->comments, $comments, qq[Comments = "$comments"] );
+
+    my @procs = $s->get_procedures;
+    is( scalar @procs, 1, 'Number of procedures is 1' );
+
+    my $p1 = $s->get_procedure( $name );
+    isa_ok( $p1, 'SQL::Translator::Schema::Procedure', 'Procedure' );
+    is( $p1->name, $name, qq[Name is "$name"] );
+}