Only output trigger 'scope' if it's set in YAML and JSON producers
[dbsrgits/SQL-Translator.git] / t / 32schema-lookups.t
CommitLineData
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
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;
dcb49dfe 19
20# Simple options. -d for debug
21my %opt;
22BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
23use constant DEBUG => (exists $opt{d} ? 1 : 0);
24
25# Setup a (somewaht contrived!) test schema
26#=============================================================================
27
28my $schema = SQL::Translator::Schema->new( name => "Lookup-tests" );
29
30my $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
73print STDERR "Test Schema:",Dumper($schema) if DEBUG;
74die "Test is schema is invalid! : ".$schema->err unless $schema->is_valid;
75
76
77# Testing 1,2,3,..
78#=============================================================================
79
56b9e6a5 80plan( tests => 15 );
dcb49dfe 81
82my (@flds,@cons);
83
84@flds = $tbl_order->pkey_fields;
85is( join(",",@flds), "order_id", "pkey_fields" );
56b9e6a5 86isa_ok( $flds[0], "SQL::Translator::Schema::Field" );
dcb49dfe 87
88@flds = $tbl_order->fkey_fields;
89is( join(",",@flds), "customer_id", "fkey_fields" );
56b9e6a5 90isa_ok( $flds[0], "SQL::Translator::Schema::Field" );
dcb49dfe 91
92@flds = $tbl_order->nonpkey_fields;
93is( join(",",@flds), "customer_id,invoice_number,notes", "nonpkey_fields" );
56b9e6a5 94isa_ok( $flds[0], "SQL::Translator::Schema::Field" );
95isa_ok( $flds[1], "SQL::Translator::Schema::Field" );
dcb49dfe 96
97@flds = $tbl_order->data_fields;
98is( join(",",@flds), "invoice_number,notes", "data_fields" );
56b9e6a5 99isa_ok( $flds[0], "SQL::Translator::Schema::Field" );
dcb49dfe 100
101@flds = $tbl_order->unique_fields;
102is( join(",",@flds), "invoice_number", "unique_fields" );
56b9e6a5 103isa_ok( $flds[0], "SQL::Translator::Schema::Field" );
dcb49dfe 104
105@cons = $tbl_order->unique_constraints;
106is( scalar @cons, 1, "Number of unique_constraints is 1" );
107is( $cons[0]->name, "con_unique_invoice", "unique_constraints" );
108
109@cons = $tbl_order->fkey_constraints;
110is( scalar @cons, 1, "Number of fkey_constraints is 1" );
111is( $cons[0]->name, "con_customer_fkey", "fkey_constraints" );
112