Commit | Line | Data |
56b9e6a5 |
1 | #!/usr/bin/perl -w |
dcb49dfe |
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; |
dcb49dfe |
19 | |
20 | # Simple options. -d for debug |
21 | my %opt; |
22 | BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; } |
23 | use constant DEBUG => (exists $opt{d} ? 1 : 0); |
24 | |
25 | # Setup a (somewaht contrived!) test schema |
26 | #============================================================================= |
27 | |
28 | my $schema = SQL::Translator::Schema->new( name => "Lookup-tests" ); |
29 | |
30 | my $tbl_order = $schema->add_table( name => "Order" ); |
31 | |
32 | # Fields |
33 | $tbl_order->add_field( |
34 | name => "order_id", |
35 | data_type => "INT", |
36 | size => "10", |
37 | is_primary_key => 1, |
38 | ); |
39 | $tbl_order->add_field( |
40 | name => "customer_id", |
41 | data_type => "INT", |
42 | size => "10", |
43 | ); |
44 | $tbl_order->add_field( |
45 | name => "invoice_number", |
46 | data_type => "VARCHAR", |
47 | size => "20", |
48 | ); |
49 | $tbl_order->add_field( |
50 | name => "notes", |
51 | data_type => "TEXT", |
52 | ); |
53 | |
54 | # Constraints |
55 | $tbl_order->add_constraint( |
56 | name => "con_pkey", |
57 | type => PRIMARY_KEY, |
58 | fields => "order_id", |
59 | ); |
60 | $tbl_order->add_constraint( |
61 | name => "con_customer_fkey", |
62 | type => FOREIGN_KEY, |
63 | fields => "customer_id", |
64 | reference_table => "Customer", |
65 | reference_fields => "customer_id", |
66 | ); |
67 | $tbl_order->add_constraint( |
68 | name => "con_unique_invoice", |
69 | type => UNIQUE, |
70 | fields => "invoice_number", |
71 | ); |
72 | |
73 | print STDERR "Test Schema:",Dumper($schema) if DEBUG; |
74 | die "Test is schema is invalid! : ".$schema->err unless $schema->is_valid; |
75 | |
76 | |
77 | # Testing 1,2,3,.. |
78 | #============================================================================= |
79 | |
56b9e6a5 |
80 | plan( tests => 15 ); |
dcb49dfe |
81 | |
82 | my (@flds,@cons); |
83 | |
84 | @flds = $tbl_order->pkey_fields; |
85 | is( join(",",@flds), "order_id", "pkey_fields" ); |
56b9e6a5 |
86 | isa_ok( $flds[0], "SQL::Translator::Schema::Field" ); |
dcb49dfe |
87 | |
88 | @flds = $tbl_order->fkey_fields; |
89 | is( join(",",@flds), "customer_id", "fkey_fields" ); |
56b9e6a5 |
90 | isa_ok( $flds[0], "SQL::Translator::Schema::Field" ); |
dcb49dfe |
91 | |
92 | @flds = $tbl_order->nonpkey_fields; |
93 | is( join(",",@flds), "customer_id,invoice_number,notes", "nonpkey_fields" ); |
56b9e6a5 |
94 | isa_ok( $flds[0], "SQL::Translator::Schema::Field" ); |
95 | isa_ok( $flds[1], "SQL::Translator::Schema::Field" ); |
dcb49dfe |
96 | |
97 | @flds = $tbl_order->data_fields; |
98 | is( join(",",@flds), "invoice_number,notes", "data_fields" ); |
56b9e6a5 |
99 | isa_ok( $flds[0], "SQL::Translator::Schema::Field" ); |
dcb49dfe |
100 | |
101 | @flds = $tbl_order->unique_fields; |
102 | is( join(",",@flds), "invoice_number", "unique_fields" ); |
56b9e6a5 |
103 | isa_ok( $flds[0], "SQL::Translator::Schema::Field" ); |
dcb49dfe |
104 | |
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" ); |
108 | |
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" ); |
112 | |