Trying to add tests as I write methods, so lots of new stuff to mirror
[dbsrgits/SQL-Translator.git] / t / 13schema.t
1 #!/usr/bin/perl
2
3 $| = 1;
4
5 use strict;
6 use Test::More 'no_plan'; # plans => 1;
7
8 require_ok( 'SQL::Translator::Schema' );
9
10 #
11 # Schema
12 #
13 my $schema = SQL::Translator::Schema->new;
14
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' );
18
19 #
20 # Table default new
21 #
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' );
26
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' );
30
31 #
32 # New table with args
33 #
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' );
37
38 #
39 # Field default new
40 #
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' );
47
48 #
49 # Field methods
50 #
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' );
55
56 #
57 # New field with args
58 #
59 my $age       = $person_table->add_field(
60     name      => 'age',
61     data_type => 'integer',
62     size      => 3,
63 );
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"' );
67
68 #
69 # Index
70 #
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' );
80
81 #
82 # Constraint
83 #
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' );
93
94 #
95 # View
96 #
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' );
101
102 #
103 # $schema->get_*
104 #
105 my $bad_table = $schema->get_table;
106 like( $schema->error, qr/no table/i, 'Error on no arg to get_table' );
107
108 $bad_table = $schema->get_table('bar');
109 like( $schema->error, qr/does not exist/i, 
110     'Error on bad arg to get_table' );
111
112 my $bad_view = $schema->get_view;
113 like( $schema->error, qr/no view/i, 'Error on no arg to get_view' );
114
115 $bad_view = $schema->get_view('bar');
116 like( $schema->error, qr/does not exist/i, 
117     'Error on bad arg to get_view' );
118
119 my $good_table = $schema->get_table('foo');
120 isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
121
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' );
125
126 #
127 # $schema->get_*s
128 #
129 my @tables = $schema->get_tables;
130 is( scalar @tables, 2, 'Found 2 tables' );
131
132 my @views = $schema->get_views;
133 is( scalar @views, 1, 'Found 1 view' );