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;
20 # Simple options. -d for debug
22 BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
23 use constant DEBUG => (exists $opt{d} ? 1 : 0);
25 # Setup a (somewaht contrived!) test schema
26 #=============================================================================
28 my $schema = SQL::Translator::Schema->new( name => "Lookup-tests" );
30 my $tbl_order = $schema->add_table( name => "Order" );
33 $tbl_order->add_field(
39 $tbl_order->add_field(
40 name => "customer_id",
44 $tbl_order->add_field(
45 name => "invoice_number",
46 data_type => "VARCHAR",
49 $tbl_order->add_field(
55 $tbl_order->add_constraint(
60 $tbl_order->add_constraint(
61 name => "con_customer_fkey",
63 fields => "customer_id",
64 reference_table => "Customer",
65 reference_fields => "customer_id",
67 $tbl_order->add_constraint(
68 name => "con_unique_invoice",
70 fields => "invoice_number",
73 print STDERR "Test Schema:",Dumper($schema) if DEBUG;
74 die "Test is schema is invalid! : ".$schema->err unless $schema->is_valid;
78 #=============================================================================
84 @flds = $tbl_order->pkey_fields;
85 is( join(",",@flds), "order_id", "pkey_fields" );
86 isa_ok( $flds[0], "SQL::Translator::Schema::Field" );
88 @flds = $tbl_order->fkey_fields;
89 is( join(",",@flds), "customer_id", "fkey_fields" );
90 isa_ok( $flds[0], "SQL::Translator::Schema::Field" );
92 @flds = $tbl_order->nonpkey_fields;
93 is( join(",",@flds), "customer_id,invoice_number,notes", "nonpkey_fields" );
94 isa_ok( $flds[0], "SQL::Translator::Schema::Field" );
95 isa_ok( $flds[1], "SQL::Translator::Schema::Field" );
97 @flds = $tbl_order->data_fields;
98 is( join(",",@flds), "invoice_number,notes", "data_fields" );
99 isa_ok( $flds[0], "SQL::Translator::Schema::Field" );
101 @flds = $tbl_order->unique_fields;
102 is( join(",",@flds), "invoice_number", "unique_fields" );
103 isa_ok( $flds[0], "SQL::Translator::Schema::Field" );
105 @cons = $tbl_order->unique_constraints;
106 is( scalar @cons, 1, "Number of unique_constraints is 1" );
107 is( $cons[0]->name, "con_unique_invoice", "unique_constraints" );
109 @cons = $tbl_order->fkey_constraints;
110 is( scalar @cons, 1, "Number of fkey_constraints is 1" );
111 is( $cons[0]->name, "con_customer_fkey", "fkey_constraints" );