6 use Test::More 'no_plan'; # plans => 1;
8 require_ok( 'SQL::Translator::Schema' );
13 my $schema = SQL::Translator::Schema->new;
15 isa_ok( $schema, 'SQL::Translator::Schema' );
16 is( $schema->is_valid, undef, 'Schema not valid...' );
17 like( $schema->error, qr/no tables/i, '...because there are no tables' );
22 my $foo_table = $schema->add_table(name => 'foo') or warn $schema->error;
23 isa_ok( $foo_table, 'SQL::Translator::Schema::Table', 'Schema' );
24 is( $foo_table->name, 'foo', 'Table name is "foo"' );
25 is( $foo_table->is_valid, undef, 'Table is not yet valid' );
27 my $fields = $foo_table->get_fields;
28 is( scalar @{ $fields || [] }, 0, 'No fields' );
29 like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
34 my $person_table = $schema->add_table( name => 'person' );
35 is( $person_table->name, 'person', 'Table name is "person"' );
36 is( $person_table->is_valid, undef, 'Table is still not valid' );
41 my $name = $person_table->add_field(name => 'foo') or warn $person_table->error;
42 isa_ok( $name, 'SQL::Translator::Schema::Field', 'Field' );
43 is( $name->name, 'foo', 'Field name is "foo"' );
44 is( $name->data_type, '', 'Field data type is blank' );
45 is( $name->size, 0, 'Field size is "0"' );
46 is( $name->is_primary_key, '0', 'Field is_primary_key is false' );
51 is( $name->name('person_name'), 'person_name', 'Field name is "person_name"' );
52 is( $name->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
53 is( $name->size(30), '30', 'Field size is "30"' );
54 is( $name->is_primary_key(0), '0', 'Field is_primary_key is negative' );
59 my $age = $person_table->add_field(
61 data_type => 'integer',
64 is( $age->name, 'age', 'Field name is "age"' );
65 is( $age->data_type, 'integer', 'Field data type is "integer"' );
66 is( $age->size, '3', 'Field size is "3"' );
71 my @indices = $person_table->get_indices;
72 is( scalar @indices, 0, 'No indices' );
73 like( $person_table->error, qr/no indices/i, 'Error for no indices' );
74 my $index = $person_table->add_index( name => "foo" )
75 or warn $person_table->error;
76 isa_ok( $index, 'SQL::Translator::Schema::Index', 'Index' );
77 my $indices = $person_table->get_indices;
78 is( scalar @$indices, 1, 'One index' );
79 is( $indices->[0]->name, 'foo', '"foo" index' );
84 my @constraints = $person_table->get_constraints;
85 is( scalar @constraints, 0, 'No constraints' );
86 like( $person_table->error, qr/no constraints/i, 'Error for no constraints' );
87 my $constraint = $person_table->add_constraint( name => 'foo' )
88 or warn $person_table->error;
89 isa_ok( $constraint, 'SQL::Translator::Schema::Constraint', 'Constraint' );
90 my $constraints = $person_table->get_constraints;
91 is( scalar @$constraints, 1, 'One constraint' );
92 is( $constraints->[0]->name, 'foo', '"foo" constraint' );
97 my $view = $schema->add_view( name => 'view1' ) or warn $schema->error;
98 isa_ok( $view, 'SQL::Translator::Schema::View', 'View' );
99 my $view_sql = 'select * from table';
100 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
105 my $bad_table = $schema->get_table;
106 like( $schema->error, qr/no table/i, 'Error on no arg to get_table' );
108 $bad_table = $schema->get_table('bar');
109 like( $schema->error, qr/does not exist/i,
110 'Error on bad arg to get_table' );
112 my $bad_view = $schema->get_view;
113 like( $schema->error, qr/no view/i, 'Error on no arg to get_view' );
115 $bad_view = $schema->get_view('bar');
116 like( $schema->error, qr/does not exist/i,
117 'Error on bad arg to get_view' );
119 my $good_table = $schema->get_table('foo');
120 isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
122 my $good_view = $schema->get_view('view1');
123 isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' );
124 is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
129 my @tables = $schema->get_tables;
130 is( scalar @tables, 2, 'Found 2 tables' );
132 my @views = $schema->get_views;
133 is( scalar @views, 1, 'Found 1 view' );