Add renamed_from to tables.
[dbsrgits/SQL-Translator.git] / t / 32schema-lookups.t
CommitLineData
dcb49dfe 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
9use strict;
10use FindBin qw/$Bin/;
11
12use Test::More;
13use Test::SQL::Translator;
14#use Test::Exception;
15use Data::Dumper;
16use SQL::Translator;
17use SQL::Translator::Schema;
18use SQL::Translator::Schema::Constants;
19use UNIVERSAL qw/isa/;
20
21# Simple options. -d for debug
22my %opt;
23BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
24use constant DEBUG => (exists $opt{d} ? 1 : 0);
25
26# Setup a (somewaht contrived!) test schema
27#=============================================================================
28
29my $schema = SQL::Translator::Schema->new( name => "Lookup-tests" );
30
31my $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
74print STDERR "Test Schema:",Dumper($schema) if DEBUG;
75die "Test is schema is invalid! : ".$schema->err unless $schema->is_valid;
76
77
78# Testing 1,2,3,..
79#=============================================================================
80
81plan( tests => 14 );
82
83my (@flds,@cons);
84
85@flds = $tbl_order->pkey_fields;
86is( join(",",@flds), "order_id", "pkey_fields" );
87ok( isa($flds[0], "SQL::Translator::Schema::Field"),
88 "pkey_fields returns SQL::Translator::Schema::Field" );
89
90@flds = $tbl_order->fkey_fields;
91is( join(",",@flds), "customer_id", "fkey_fields" );
92ok( isa($flds[0], "SQL::Translator::Schema::Field"),
93 "fkey_fields returns SQL::Translator::Schema::Field" );
94
95@flds = $tbl_order->nonpkey_fields;
96is( join(",",@flds), "customer_id,invoice_number,notes", "nonpkey_fields" );
97ok(
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;
104is( join(",",@flds), "invoice_number,notes", "data_fields" );
105ok( isa($flds[0], "SQL::Translator::Schema::Field"),
106 "data_fields returns SQL::Translator::Schema::Field" );
107
108@flds = $tbl_order->unique_fields;
109is( join(",",@flds), "invoice_number", "unique_fields" );
110ok( isa($flds[0], "SQL::Translator::Schema::Field"),
111 "unique_fields returns SQL::Translator::Schema::Field" );
112
113@cons = $tbl_order->unique_constraints;
114is( scalar @cons, 1, "Number of unique_constraints is 1" );
115is( $cons[0]->name, "con_unique_invoice", "unique_constraints" );
116
117@cons = $tbl_order->fkey_constraints;
118is( scalar @cons, 1, "Number of fkey_constraints is 1" );
119is( $cons[0]->name, "con_customer_fkey", "fkey_constraints" );
120