5 use Test::More 'no_plan'; # plans => 1;
7 use SQL::Translator::Parser::PostgreSQL qw(parse);
9 my $t = SQL::Translator->new( trace => 0 );
11 create table t_test1 (
12 f_serial serial NOT NULL primary key,
13 f_varchar character varying (255),
14 f_double double precision,
18 f_bin binary data(100),
21 f_fk1 integer not null references t_test2 (f_id),
25 create table t_test2 (
26 f_id integer NOT NULL,
27 f_varchar varchar(25),
31 alter table only t_test1 add constraint c_u1 unique (f_varchar);
33 alter table only t_test1 add constraint "$1" foreign key (f_fk2)
34 references t_test2 (f_id) on update no action on delete cascade;
39 my $data = parse( $t, $sql );
40 #print Dumper($data),"\n";
42 is( ref $data, 'HASH', 'Data is a hashref' );
43 is( scalar keys %{ $data || {} }, 2, 'Two tables' );
45 ok( defined $data->{'t_test1'}, 'Table t_test1 exists' );
46 ok( defined $data->{'t_test2'}, 'Table t_test2 exists' );
48 my $t1 = $data->{'t_test1'};
49 my $t1_fields = $t1->{'fields'};
50 is( scalar keys %{ $t1_fields }, 11, '11 fields in t_test1' );
51 is( $t1_fields->{'f_serial'}{'data_type'}, 'integer', 'Field is an integer' );
52 is( $t1_fields->{'f_varchar'}{'data_type'}, 'varchar', 'Field is a varchar' );
53 is( $t1_fields->{'f_double'}{'data_type'}, 'float', 'Field is a float' );
54 is( $t1_fields->{'f_bigint'}{'data_type'}, 'integer', 'Field is an integer' );
55 is( $t1_fields->{'f_char'}{'data_type'}, 'char', 'Field is char' );
56 is( $t1_fields->{'f_bool'}{'data_type'}, 'boolean', 'Field is a boolean' );
57 is( $t1_fields->{'f_bin'}{'data_type'}, 'binary', 'Field is binary' );
58 is( $t1_fields->{'f_tz'}{'data_type'}, 'timestamp', 'Field is a timestamp' );
59 is( $t1_fields->{'f_text'}{'data_type'}, 'text', 'Field is text' );
60 is( $t1_fields->{'f_fk1'}{'data_type'}, 'integer', 'Field is an integer' );
61 is( $t1_fields->{'f_fk2'}{'data_type'}, 'integer', 'Field is an integer' );
63 is( $t1_fields->{'f_serial'}{'is_primary_key'}, 1,
64 'Field "f_serial" is primary key' );
66 my $t1_constraints = $t1->{'constraints'};
67 #print Dumper($t1_constraints),"\n";
68 is( scalar @{ $t1_constraints || [] }, 6, '6 constraints on t_test1' );
69 is( $t1_constraints->[-2]{'type'}, 'unique', 'Constraint is unique' );
70 is( $t1_constraints->[-2]{'fields'}[0], 'f_varchar',
71 'Constraint is on field "f_varchar"' );
73 is( $t1_constraints->[-1]{'type'}, 'foreign_key', 'Constraint is foreign key' );
74 is( $t1_constraints->[-1]{'fields'}[0], 'f_fk2',
75 'Constraint is on field "f_fk2"' );
76 is( $t1_constraints->[-1]{'reference_table'}, 't_test2',
77 'Constraint is to table "t_test2"' );
78 is( $t1_constraints->[-1]{'reference_fields'}[0], 'f_id',
79 'Constraint is to field "f_id"' );
80 is( $t1_constraints->[-1]{'on_update_do'}, 'no_action',
81 'No action on update' );
82 is( $t1_constraints->[-1]{'on_delete_do'}, 'cascade',
83 'Cascade on delete' );
85 my $t2 = $data->{'t_test2'};
86 my $t2_fields = $t2->{'fields'};
87 is( scalar keys %{ $t2_fields }, 2, '2 fields in t_test2' );
88 is( $t2_fields->{'f_id'}{'data_type'}, 'integer', 'Field is an integer' );
89 is( $t2_fields->{'f_varchar'}{'data_type'}, 'varchar', 'Field is an varchar' );