Release commit for 1.62
[dbsrgits/SQL-Translator.git] / t / 55-oracle-producer.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3
4 use strict;
5 use warnings;
6 use Test::More;
7
8 use SQL::Translator::Schema::Constants;
9 use SQL::Translator::Schema::Table;
10 use SQL::Translator::Schema::View;
11 use SQL::Translator::Schema::Field;
12 use SQL::Translator::Schema::Constraint;
13 use SQL::Translator::Producer::Oracle;
14
15 {
16     my $table1 = SQL::Translator::Schema::Table->new( name => 'table1' );
17
18     my $table1_field1 = $table1->add_field(
19         name              => 'fk_col1',
20         data_type         => 'NUMBER',
21         size              => 6,
22         default_value     => undef,
23         is_auto_increment => 0,
24         is_nullable       => 0,
25         is_foreign_key    => 1,
26         is_unique         => 0
27     );
28
29     my $table1_field2 = $table1->add_field(
30         name              => 'fk_col2',
31         data_type         => 'VARCHAR',
32         size              => 64,
33         default_value     => undef,
34         is_auto_increment => 0,
35         is_nullable       => 0,
36         is_foreign_key    => 1,
37         is_unique         => 0
38     );
39
40     my $table2 = SQL::Translator::Schema::Table->new( name => 'table2' );
41
42     my $table2_field1 = $table2->add_field(
43         name              => 'fk_col1',
44         data_type         => 'NUMBER',
45         size              => 6,
46         default_value     => undef,
47         is_auto_increment => 0,
48         is_nullable       => 0,
49         is_foreign_key    => 0,
50         is_unique         => 0
51     );
52
53     my $table2_field2 = $table2->add_field(
54         name              => 'fk_col2',
55         data_type         => 'VARCHAR',
56         size              => 64,
57         default_value     => undef,
58         is_auto_increment => 0,
59         is_nullable       => 0,
60         is_foreign_key    => 0,
61         is_unique         => 0
62     );
63
64     my $constraint1 = $table1->add_constraint(
65         name             => 'foo',
66         fields           => [qw/ fk_col1 fk_col2 /],
67         reference_fields => [qw/ fk_col1 fk_col2 /],
68         reference_table  => 'table2',
69         type             => FOREIGN_KEY,
70     );
71
72     my ($table1_def, $fk1_def, $trigger1_def,
73         $index1_def, $constraint1_def
74     ) = SQL::Translator::Producer::Oracle::create_table($table1);
75
76     is_deeply(
77         $fk1_def,
78         [   'ALTER TABLE table1 ADD CONSTRAINT table1_fk_col1_fk_col2_fk FOREIGN KEY (fk_col1, fk_col2) REFERENCES table2 (fk_col1, fk_col2)'
79         ],
80         'correct "CREATE CONSTRAINT" SQL'
81     );
82
83     my $materialized_view = SQL::Translator::Schema::View->new(
84         name    => 'matview',
85         sql     => 'SELECT id, name FROM table3',
86         fields  => 'id, name',
87         extra   => {
88             materialized =>
89                 'REFRESH START WITH SYSDATE NEXT SYSDATE + 5/1440 FORCE WITH ROWID'
90         }
91     );
92
93     my ($materialized_view_def) = SQL::Translator::Producer::Oracle::create_view($materialized_view);
94     is_deeply(
95         $materialized_view_def,
96         [   "CREATE MATERIALIZED VIEW matview REFRESH START WITH SYSDATE NEXT SYSDATE + 5/1440 FORCE WITH ROWID AS\nSELECT id, name FROM table3"
97         ],
98         'correct "CREATE MATERIALZED VIEW" SQL'
99     );
100
101 }
102
103 done_testing();