Too many changes.
[dbsrgits/SQL-Translator.git] / t / 13schema.t
CommitLineData
4d878d2f 1#!/usr/bin/perl
2
586fba3f 3$| = 1;
4
4d878d2f 5use strict;
6use Test::More 'no_plan'; # plans => 1;
7
8require_ok( 'SQL::Translator::Schema' );
9
10#
11# Schema
12#
13my $schema = SQL::Translator::Schema->new;
14
15isa_ok( $schema, 'SQL::Translator::Schema' );
586fba3f 16is( $schema->is_valid, undef, 'Schema not valid...' );
17like( $schema->error, qr/no tables/i, '...because there are no tables' );
4d878d2f 18
19#
20# Table default new
21#
586fba3f 22my $foo_table = $schema->add_table(name => 'foo') or warn $schema->error;
4d878d2f 23isa_ok( $foo_table, 'SQL::Translator::Schema::Table', 'Schema' );
586fba3f 24is( $foo_table->name, 'foo', 'Table name is "foo"' );
25is( $foo_table->is_valid, undef, 'Table is not yet valid' );
4d878d2f 26
586fba3f 27my $fields = $foo_table->get_fields;
28is( scalar @{ $fields || [] }, 0, 'No fields' );
29like( $foo_table->error, qr/no fields/i, 'Error for no fields' );
4d878d2f 30
31#
32# New table with args
33#
34my $person_table = $schema->add_table( name => 'person' );
35is( $person_table->name, 'person', 'Table name is "person"' );
586fba3f 36is( $person_table->is_valid, undef, 'Table is still not valid' );
4d878d2f 37
38#
39# Field default new
40#
586fba3f 41my $name = $person_table->add_field(name => 'foo') or warn $person_table->error;
4d878d2f 42isa_ok( $name, 'SQL::Translator::Schema::Field', 'Field' );
586fba3f 43is( $name->name, 'foo', 'Field name is "foo"' );
4d878d2f 44is( $name->data_type, '', 'Field data type is blank' );
45is( $name->size, 0, 'Field size is "0"' );
586fba3f 46is( $name->is_primary_key, '0', 'Field is_primary_key is false' );
4d878d2f 47
48#
49# Field methods
50#
51is( $name->name('person_name'), 'person_name', 'Field name is "person_name"' );
52is( $name->data_type('varchar'), 'varchar', 'Field data type is "varchar"' );
53is( $name->size(30), '30', 'Field size is "30"' );
54is( $name->is_primary_key(0), '0', 'Field is_primary_key is negative' );
55
56#
57# New field with args
58#
59my $age = $person_table->add_field(
60 name => 'age',
61 data_type => 'integer',
62 size => 3,
63);
64is( $age->name, 'age', 'Field name is "age"' );
65is( $age->data_type, 'integer', 'Field data type is "integer"' );
66is( $age->size, '3', 'Field size is "3"' );
67
68#
69# Index
70#
586fba3f 71my @indices = $person_table->get_indices;
72is( scalar @indices, 0, 'No indices' );
73like( $person_table->error, qr/no indices/i, 'Error for no indices' );
74my $index = $person_table->add_index( name => "foo" )
75 or warn $person_table->error;
4d878d2f 76isa_ok( $index, 'SQL::Translator::Schema::Index', 'Index' );
586fba3f 77my $indices = $person_table->get_indices;
78is( scalar @$indices, 1, 'One index' );
79is( $indices->[0]->name, 'foo', '"foo" index' );
4d878d2f 80
81#
82# Constraint
83#
586fba3f 84my @constraints = $person_table->get_constraints;
85is( scalar @constraints, 0, 'No constraints' );
86like( $person_table->error, qr/no constraints/i, 'Error for no constraints' );
87my $constraint = $person_table->add_constraint( name => 'foo' )
88 or warn $person_table->error;
4d878d2f 89isa_ok( $constraint, 'SQL::Translator::Schema::Constraint', 'Constraint' );
586fba3f 90my $constraints = $person_table->get_constraints;
91is( scalar @$constraints, 1, 'One constraint' );
92is( $constraints->[0]->name, 'foo', '"foo" constraint' );
4d878d2f 93
94#
95# View
96#
586fba3f 97my $view = $schema->add_view( name => 'view1' ) or warn $schema->error;
4d878d2f 98isa_ok( $view, 'SQL::Translator::Schema::View', 'View' );
586fba3f 99my $view_sql = 'select * from table';
100is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
101
102#
103# $schema->get_*
104#
105my $bad_table = $schema->get_table;
106like( $schema->error, qr/no table/i, 'Error on no arg to get_table' );
107
108$bad_table = $schema->get_table('bar');
109like( $schema->error, qr/does not exist/i,
110 'Error on bad arg to get_table' );
111
112my $bad_view = $schema->get_view;
113like( $schema->error, qr/no view/i, 'Error on no arg to get_view' );
114
115$bad_view = $schema->get_view('bar');
116like( $schema->error, qr/does not exist/i,
117 'Error on bad arg to get_view' );
118
119my $good_table = $schema->get_table('foo');
120isa_ok( $good_table, 'SQL::Translator::Schema::Table', 'Table "foo"' );
121
122my $good_view = $schema->get_view('view1');
123isa_ok( $good_view, 'SQL::Translator::Schema::View', 'View "view1"' );
124is( $view->sql( $view_sql ), $view_sql, 'View SQL is good' );
125
126#
127# $schema->get_*s
128#
129my @tables = $schema->get_tables;
130is( scalar @tables, 2, 'Found 2 tables' );
131
132my @views = $schema->get_views;
133is( scalar @views, 1, 'Found 1 view' );