Release 0.09005
[dbsrgits/SQL-Translator.git] / t / 32schema-lookups.t
1 #!/usr/bin/perl -w 
2 # vim:filetype=perl
3
4 # Before `make install' is performed this script should be runnable with
5 # `make test'. After `make install' it should work as `perl test.pl'
6 #
7 # Run script with -d for debug.
8
9 use strict;
10 use FindBin qw/$Bin/;
11
12 use Test::More;
13 use Test::SQL::Translator;
14 #use Test::Exception;
15 use Data::Dumper;
16 use SQL::Translator;
17 use SQL::Translator::Schema;
18 use SQL::Translator::Schema::Constants;
19 use UNIVERSAL qw/isa/;
20
21 # Simple options. -d for debug
22 my %opt;
23 BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
24 use constant DEBUG => (exists $opt{d} ? 1 : 0);
25
26 # Setup a (somewaht contrived!) test schema
27 #=============================================================================
28
29 my $schema = SQL::Translator::Schema->new( name => "Lookup-tests" );
30
31 my $tbl_order = $schema->add_table( name => "Order" );
32
33 # Fields
34 $tbl_order->add_field(
35     name => "order_id",
36     data_type => "INT",
37     size => "10",
38     is_primary_key => 1,
39 );
40 $tbl_order->add_field(
41     name => "customer_id",
42     data_type => "INT",
43     size => "10",
44 );
45 $tbl_order->add_field(
46     name => "invoice_number",
47     data_type => "VARCHAR",
48     size => "20",
49 );
50 $tbl_order->add_field(
51     name => "notes",
52     data_type => "TEXT",
53 );
54
55 # Constraints
56 $tbl_order->add_constraint(
57     name   => "con_pkey",
58     type   => PRIMARY_KEY,
59     fields => "order_id",
60 );
61 $tbl_order->add_constraint(
62     name   => "con_customer_fkey",
63     type   => FOREIGN_KEY,
64     fields => "customer_id",
65     reference_table  => "Customer",
66     reference_fields => "customer_id",
67 );
68 $tbl_order->add_constraint(
69     name   => "con_unique_invoice",
70     type   => UNIQUE,
71     fields => "invoice_number",
72 );
73
74 print STDERR "Test Schema:",Dumper($schema) if DEBUG;
75 die "Test is schema is invalid! : ".$schema->err unless $schema->is_valid;
76
77
78 # Testing 1,2,3,..
79 #=============================================================================
80
81 plan( tests => 14 );
82
83 my (@flds,@cons);
84
85 @flds = $tbl_order->pkey_fields;
86 is( join(",",@flds), "order_id", "pkey_fields" );
87 ok( isa($flds[0], "SQL::Translator::Schema::Field"),
88     "pkey_fields returns SQL::Translator::Schema::Field" );
89
90 @flds = $tbl_order->fkey_fields;
91 is( join(",",@flds), "customer_id", "fkey_fields" );
92 ok( isa($flds[0], "SQL::Translator::Schema::Field"),
93     "fkey_fields returns SQL::Translator::Schema::Field" );
94
95 @flds = $tbl_order->nonpkey_fields;
96 is( join(",",@flds), "customer_id,invoice_number,notes", "nonpkey_fields" );
97 ok(
98     isa($flds[0], "SQL::Translator::Schema::Field")
99     && isa($flds[1], "SQL::Translator::Schema::Field"),
100     "nonpkey_fields returns SQL::Translator::Schema::Field's" 
101 );
102
103 @flds = $tbl_order->data_fields;
104 is( join(",",@flds), "invoice_number,notes", "data_fields" );
105 ok( isa($flds[0], "SQL::Translator::Schema::Field"),
106     "data_fields returns SQL::Translator::Schema::Field" );
107
108 @flds = $tbl_order->unique_fields;
109 is( join(",",@flds), "invoice_number", "unique_fields" );
110 ok( isa($flds[0], "SQL::Translator::Schema::Field"),
111     "unique_fields returns SQL::Translator::Schema::Field" );
112
113 @cons = $tbl_order->unique_constraints;
114 is( scalar @cons, 1, "Number of unique_constraints is 1" );
115 is( $cons[0]->name, "con_unique_invoice", "unique_constraints" );
116
117 @cons = $tbl_order->fkey_constraints;
118 is( scalar @cons, 1, "Number of fkey_constraints is 1" );
119 is( $cons[0]->name, "con_customer_fkey", "fkey_constraints" );
120