Adding new schema test, commiting fixes to MySQL parser test.
[dbsrgits/SQL-Translator.git] / t / 13schema.t
1 #!/usr/bin/perl
2
3 use strict;
4 use Test::More 'no_plan'; # plans => 1;
5
6 require_ok( 'SQL::Translator::Schema' );
7
8 #
9 # Schema
10 #
11 my $schema = SQL::Translator::Schema->new;
12
13 isa_ok( $schema, 'SQL::Translator::Schema' );
14
15 #
16 # Table default new
17 #
18 my $foo_table = $schema->add_table;
19 isa_ok( $foo_table, 'SQL::Translator::Schema::Table', 'Schema' );
20 is( $foo_table->name, '', 'Table name is empty' );
21 is( $foo_table->is_valid, 0, 'Table is not yet valid' );
22 my %fields = $foo_table->fields;
23 cmp_ok( scalar keys %fields, '==', 0, 'No fields' );
24
25 #
26 # Table methods
27 #
28 is( $foo_table->name('foo'), 'foo', 'Table name is "foo"' );
29
30 #
31 # New table with args
32 #
33 my $person_table = $schema->add_table( name => 'person' );
34 is( $person_table->name, 'person', 'Table name is "person"' );
35 is( $person_table->is_valid, 0, 'Table is still not valid' );
36
37 #
38 # Field default new
39 #
40 my $name = $person_table->add_field or warn $person_table->error;
41 isa_ok( $name, 'SQL::Translator::Schema::Field', 'Field' );
42 is( $name->name, '', 'Field name is blank' );
43 is( $name->data_type, '', 'Field data type is blank' );
44 is( $name->size, 0, 'Field size is "0"' );
45 is( $name->is_primary_key, '0', 'Field is_primary_key is negative' );
46
47 #
48 # Field methods
49 #
50 is( $name->name('person_name'), 'person_name', 'Field name is "person_name"' );
51 is( $name->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
52 is( $name->size(30), '30', 'Field size is "30"' );
53 is( $name->is_primary_key(0), '0', 'Field is_primary_key is negative' );
54
55 #
56 # New field with args
57 #
58 my $age       = $person_table->add_field(
59     name      => 'age',
60     data_type => 'integer',
61     size      => 3,
62 );
63 is( $age->name, 'age', 'Field name is "age"' );
64 is( $age->data_type, 'integer', 'Field data type is "integer"' );
65 is( $age->size, '3', 'Field size is "3"' );
66
67 #
68 # Index
69 #
70 my $index = $person_table->add_index or warn $person_table->error;
71 isa_ok( $index, 'SQL::Translator::Schema::Index', 'Index' );
72
73 #
74 # Constraint
75 #
76 my $constraint = $person_table->add_constraint or warn $person_table->error;
77 isa_ok( $constraint, 'SQL::Translator::Schema::Constraint', 'Constraint' );
78
79 #
80 # View
81 #
82 my $view = $schema->add_view or warn $schema->error;
83 isa_ok( $view, 'SQL::Translator::Schema::View', 'View' );