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'
7 # Run script with -d for debug.
13 use Test::SQL::Translator;
17 use SQL::Translator::Schema;
18 use SQL::Translator::Schema::Constants;
19 use UNIVERSAL qw/isa/;
21 # Simple options. -d for debug
23 BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
24 use constant DEBUG => (exists $opt{d} ? 1 : 0);
26 # Setup a (somewaht contrived!) test schema
27 #=============================================================================
29 my $schema = SQL::Translator::Schema->new( name => "Lookup-tests" );
31 my $tbl_order = $schema->add_table( name => "Order" );
34 $tbl_order->add_field(
40 $tbl_order->add_field(
41 name => "customer_id",
45 $tbl_order->add_field(
46 name => "invoice_number",
47 data_type => "VARCHAR",
50 $tbl_order->add_field(
56 $tbl_order->add_constraint(
61 $tbl_order->add_constraint(
62 name => "con_customer_fkey",
64 fields => "customer_id",
65 reference_table => "Customer",
66 reference_fields => "customer_id",
68 $tbl_order->add_constraint(
69 name => "con_unique_invoice",
71 fields => "invoice_number",
74 print STDERR "Test Schema:",Dumper($schema) if DEBUG;
75 die "Test is schema is invalid! : ".$schema->err unless $schema->is_valid;
79 #=============================================================================
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" );
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" );
95 @flds = $tbl_order->nonpkey_fields;
96 is( join(",",@flds), "customer_id,invoice_number,notes", "nonpkey_fields" );
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"
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" );
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" );
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" );
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" );