Allow for tests to run in parallel (simultaneously from multiple checkouts)
[dbsrgits/DBIx-Class.git] / t / sqlmaker / oracle.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 BEGIN {
6   require DBIx::Class::Optional::Dependencies;
7   plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('id_shortener')
8     unless DBIx::Class::Optional::Dependencies->req_ok_for ('id_shortener');
9 }
10
11 use Test::Exception;
12 use Data::Dumper::Concise;
13 use lib qw(t/lib);
14 use DBICTest;
15 use DBIC::SqlMakerTest;
16 use DBIx::Class::SQLMaker::Oracle;
17
18 #
19 #  Offline test for connect_by
20 #  ( without active database connection)
21 #
22 my @handle_tests = (
23     {
24         connect_by  => { 'parentid' => { '-prior' => \'artistid' } },
25         stmt        => '"parentid" = PRIOR artistid',
26         bind        => [],
27         msg         => 'Simple: "parentid" = PRIOR artistid',
28     },
29     {
30         connect_by  => { 'parentid' => { '!=' => { '-prior' => { -ident => 'artistid' } } } },
31         stmt        => '"parentid" != ( PRIOR "artistid" )',
32         bind        => [],
33         msg         => 'Simple: "parentid" != ( PRIOR "artistid" )',
34     },
35     # Examples from http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries003.htm
36
37     # CONNECT BY last_name != 'King' AND PRIOR employee_id = manager_id ...
38     {
39         connect_by  => [
40             last_name => { '!=' => 'King' },
41             manager_id => { '-prior' => { -ident => 'employee_id' } },
42         ],
43         stmt        => '( "last_name" != ? OR "manager_id" = PRIOR "employee_id" )',
44         bind        => ['King'],
45         msg         => 'oracle.com example #1',
46     },
47     # CONNECT BY PRIOR employee_id = manager_id and
48     #            PRIOR account_mgr_id = customer_id ...
49     {
50         connect_by  => {
51             manager_id => { '-prior' => { -ident => 'employee_id' } },
52             customer_id => { '>', { '-prior' => \'account_mgr_id' } },
53         },
54         stmt        => '( "customer_id" > ( PRIOR account_mgr_id ) AND "manager_id" = PRIOR "employee_id" )',
55         bind        => [],
56         msg         => 'oracle.com example #2',
57     },
58     # CONNECT BY NOCYCLE PRIOR employee_id = manager_id AND LEVEL <= 4;
59     # TODO: NOCYCLE parameter doesn't work
60 );
61
62 my $sqla_oracle = DBIx::Class::SQLMaker::Oracle->new( quote_char => '"', name_sep => '.' );
63 isa_ok($sqla_oracle, 'DBIx::Class::SQLMaker::Oracle');
64
65
66 for my $case (@handle_tests) {
67     my ( $stmt, @bind );
68     my $msg = sprintf("Offline: %s",
69         $case->{msg} || substr($case->{stmt},0,25),
70     );
71     lives_ok(
72         sub {
73             ( $stmt, @bind ) = $sqla_oracle->_recurse_where( $case->{connect_by} );
74             is_same_sql_bind( $stmt, \@bind, $case->{stmt}, $case->{bind},$msg )
75               || diag "Search term:\n" . Dumper $case->{connect_by};
76         }
77     ,sprintf("lives is ok from '%s'",$msg));
78 }
79
80 is (
81   $sqla_oracle->_shorten_identifier('short_id'),
82   'short_id',
83   '_shorten_identifier for short id without keywords ok'
84 );
85
86 is (
87   $sqla_oracle->_shorten_identifier('short_id', [qw/ foo /]),
88   'short_id',
89   '_shorten_identifier for short id with one keyword ok'
90 );
91
92 is (
93   $sqla_oracle->_shorten_identifier('short_id', [qw/ foo bar baz /]),
94   'short_id',
95   '_shorten_identifier for short id with keywords ok'
96 );
97
98 is (
99   $sqla_oracle->_shorten_identifier('very_long_identifier_which_exceeds_the_30char_limit'),
100   'VryLngIdntfrWhchExc_72M8CIDTM7',
101   '_shorten_identifier without keywords ok',
102 );
103
104 is (
105   $sqla_oracle->_shorten_identifier('very_long_identifier_which_exceeds_the_30char_limit',[qw/ foo /]),
106   'Foo_72M8CIDTM7KBAUPXG48B22P4E',
107   '_shorten_identifier with one keyword ok',
108 );
109 is (
110   $sqla_oracle->_shorten_identifier('very_long_identifier_which_exceeds_the_30char_limit',[qw/ foo bar baz /]),
111   'FooBarBaz_72M8CIDTM7KBAUPXG48B',
112   '_shorten_identifier with keywords ok',
113 );
114
115 # test SQL generation for INSERT ... RETURNING
116
117 sub UREF { \do { my $x } };
118
119 $sqla_oracle->{bindtype} = 'columns';
120
121 for my $q ('', '"') {
122   local $sqla_oracle->{quote_char} = $q;
123
124   my ($sql, @bind) = $sqla_oracle->insert(
125     'artist',
126     {
127       'name' => 'Testartist',
128     },
129     {
130       'returning' => 'artistid',
131       'returning_container' => [],
132     },
133   );
134
135   is_same_sql_bind(
136     $sql, \@bind,
137     "INSERT INTO ${q}artist${q} (${q}name${q}) VALUES (?) RETURNING ${q}artistid${q} INTO ?",
138     [ [ name => 'Testartist' ], [ artistid => UREF ] ],
139     'sql_maker generates insert returning for one column'
140   );
141
142   ($sql, @bind) = $sqla_oracle->insert(
143     'artist',
144     {
145       'name' => 'Testartist',
146     },
147     {
148       'returning' => \'artistid',
149       'returning_container' => [],
150     },
151   );
152
153   is_same_sql_bind(
154     $sql, \@bind,
155     "INSERT INTO ${q}artist${q} (${q}name${q}) VALUES (?) RETURNING artistid INTO ?",
156     [ [ name => 'Testartist' ], [ artistid => UREF ] ],
157     'sql_maker generates insert returning for one column'
158   );
159
160
161   ($sql, @bind) = $sqla_oracle->insert(
162     'computed_column_test',
163     {
164       'a_timestamp' => '2010-05-26 18:22:00',
165     },
166     {
167       'returning' => [ 'id', 'a_computed_column', 'charfield' ],
168       'returning_container' => [],
169     },
170   );
171
172   is_same_sql_bind(
173     $sql, \@bind,
174     "INSERT INTO ${q}computed_column_test${q} (${q}a_timestamp${q}) VALUES (?) RETURNING ${q}id${q}, ${q}a_computed_column${q}, ${q}charfield${q} INTO ?, ?, ?",
175     [ [ a_timestamp => '2010-05-26 18:22:00' ], [ id => UREF ], [ a_computed_column => UREF ], [ charfield => UREF ] ],
176     'sql_maker generates insert returning for multiple columns'
177   );
178 }
179
180 done_testing;