File: QueryInCondition.t

package info (click to toggle)
znuny 6.5.18-1
  • links: PTS
  • area: non-free
  • in suites: forky, sid
  • size: 205,344 kB
  • sloc: perl: 1,038,694; xml: 74,551; javascript: 65,276; sql: 23,574; sh: 417; makefile: 63
file content (358 lines) | stat: -rw-r--r-- 11,665 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --

use strict;
use warnings;
use utf8;

use vars (qw($Self));

my $DBObject = $Kernel::OM->Get('Kernel::System::DB');

my @Tests = (
    {
        Description      => 'Test does not contain all necessary data for QueryInCondition',
        Fails            => 1,
        QueryInCondition => {},
        ReferenceData    => undef,
    },
    {
        Description      => 'Test does not contain any values for QueryInCondition',
        Fails            => 1,
        QueryInCondition => {
            Key => 't.id',
        },
        ReferenceData => undef,
    },
    {
        Description      => "Test QueryInCondition with some ids and quote type 'Integer' (BindMode 0)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1, 2, 3, 4, 6, ],
            QuoteType => 'Integer',
            BindMode  => 0,
        },
        ReferenceData => 't.id IN (1, 2, 3, 4, 6)',
    },
    {
        Description      => "Test QueryInCondition with some ids and quote type 'Integer' (BindMode 1)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1, 2, 3, 4, 6, ],
            QuoteType => 'Integer',
            BindMode  => 1,
        },
        ReferenceData => {
            SQL    => 't.id IN (?, ?, ?, ?, ?)',
            Values => [ \1, \2, \3, \4, \6, ],
        },
    },
    {
        Description => "Test QueryInCondition with some ids and quote type 'Integer', but one wrong value (BindMode 0)",
        Fails       => 1,
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1, 2, 3, 4, 'test' ],
            QuoteType => 'Integer',
            BindMode  => 0,
        },
    },
    {
        Description      => "Test QueryInCondition with some numners and quote type 'Number' (BindMode 0)",
        QueryInCondition => {
            Key       => 'ta.time_unit',
            Values    => [ 1.5, 2.75, 3, 444.4, 6, ],
            QuoteType => 'Number',
            BindMode  => 0,
        },
        ReferenceData => 'ta.time_unit IN (1.5, 2.75, 3, 6, 444.4)',
    },
    {
        Description      => "Test QueryInCondition with some numbers and quote type 'Number' (BindMode 1)",
        QueryInCondition => {
            Key       => 'ta.time_unit',
            Values    => [ 1.5, 2.75, 3, 444.4, 6, ],
            QuoteType => 'Number',
            BindMode  => 1,
        },
        ReferenceData => {
            SQL    => 'ta.time_unit IN (?, ?, ?, ?, ?)',
            Values => [ \1.5, \2.75, \3, \6, \444.4, ],
        },
    },
    {
        Description      => "Test QueryInCondition with some strings and quote type 'String' (BindMode 0)",
        QueryInCondition => {
            Key      => 't.tn',
            Values   => [ 'aaa', 'bbb', 'ccc', 'zzz', 'test', ],
            BindMode => 0,
        },
        ReferenceData => "t.tn IN ('aaa', 'bbb', 'ccc', 'test', 'zzz')",
    },
    {
        Description      => "Test QueryInCondition with some strings and quote type 'String' (BindMode 1)",
        QueryInCondition => {
            Key      => 't.tn',
            Values   => [ 'aaa', 'bbb', 'ccc', 'test', 'zzz' ],
            BindMode => 1,
        },
        ReferenceData => {
            SQL    => 't.tn IN (?, ?, ?, ?, ?)',
            Values => [ \'aaa', \'bbb', \'ccc', \'test', \'zzz' ],
        },
    },
    {
        Description      => "Test QueryInCondition with some strings and not allowed quote type 'Like' (BindMode 0)",
        Fails            => 1,
        QueryInCondition => {
            Key       => 't.tn',
            Values    => ['*aaa*'],
            QuoteType => 'Like',
            BindMode  => 0,
        },
    },
    {
        Description      => "Test QueryInCondition with 1000 ids and quote type 'Integer' (BindMode 0)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1 .. 1000 ],
            QuoteType => 'Integer',
            BindMode  => 0,
        },
        ReferenceData => 't.id IN (' . ( join ', ', 1 .. 1000 ) . ')',
    },
    {
        Description      => "Test QueryInCondition with 1000 ids and quote type 'Integer' (BindMode 1)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1 .. 1000 ],
            QuoteType => 'Integer',
            BindMode  => 1,
        },
        ReferenceData => {
            SQL    => 't.id IN (' . ( join ', ', ('?') x 1000 ) . ')',
            Values => [ 1 .. 1000 ],
        },
    },
    {
        Description      => "Test QueryInCondition with over 1000 ids and quote type 'Integer' (BindMode 0)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1 .. 1200 ],
            QuoteType => 'Integer',
            BindMode  => 0,
        },
        ReferenceData       => 't.id IN (' . ( join ', ', 1 .. 1200 ) . ')',
        ReferenceDataOracle => '( t.id IN ('
            . ( join ', ', 1 .. 1000 )
            . ') OR t.id IN ('
            . ( join ', ', 1001 .. 1200 ) . ') )',
    },
    {
        Description      => "Test QueryInCondition with over 1000 ids and quote type 'Integer' (BindMode 1)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1 .. 1200 ],
            QuoteType => 'Integer',
            BindMode  => 1,
        },
        ReferenceData => {
            SQL    => 't.id IN (' . ( join ', ', ('?') x 1200 ) . ')',
            Values => [ 1 .. 1200 ],
        },
        ReferenceDataOracle => {
            SQL => '( t.id IN (' . ( join ', ', ('?') x 1000 ) . ') OR t.id IN (' . ( join ', ', ('?') x 200 ) . ') )',
            Values => [ 1 .. 1200 ],
        },
    },
    {
        Description      => "Test QueryInCondition with 2001 ids and quote type 'Integer' (BindMode 0)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1 .. 2001 ],
            QuoteType => 'Integer',
            BindMode  => 0,
        },
        ReferenceData       => 't.id IN (' . ( join ', ', 1 .. 2001 ) . ')',
        ReferenceDataOracle => '( t.id IN ('
            . ( join ', ', 1 .. 1000 )
            . ') OR t.id IN ('
            . ( join ', ', 1001 .. 2000 )
            . ') OR t.id IN ('
            . ( join ', ', 2001 .. 2001 ) . ') )',
    },

    # Some tests for the negation.
    {
        Description      => "Test QueryInCondition with some ids, quote type 'Integer' and negated (BindMode 0)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1, 2, 3, 4, 6, ],
            QuoteType => 'Integer',
            BindMode  => 0,
            Negate    => 1,
        },
        ReferenceData => 't.id NOT IN (1, 2, 3, 4, 6)',
    },
    {
        Description      => "Test QueryInCondition with some ids, quote type 'Integer' and negated (BindMode 1)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1, 2, 3, 4, 6, ],
            QuoteType => 'Integer',
            BindMode  => 1,
            Negate    => 1,
        },
        ReferenceData => {
            SQL    => 't.id NOT IN (?, ?, ?, ?, ?)',
            Values => [ \1, \2, \3, \4, \6, ],
        },
    },
    {
        Description      => "Test QueryInCondition with over 1000 ids, quote type 'Integer' and negated (BindMode 0)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1 .. 1200 ],
            QuoteType => 'Integer',
            BindMode  => 0,
            Negate    => 1,
        },
        ReferenceData       => 't.id NOT IN (' . ( join ', ', 1 .. 1200 ) . ')',
        ReferenceDataOracle => '( t.id NOT IN ('
            . ( join ', ', 1 .. 1000 )
            . ') AND t.id NOT IN ('
            . ( join ', ', 1001 .. 1200 ) . ') )',
    },
    {
        Description      => "Test QueryInCondition with over 1000 ids, quote type 'Integer' and negated (BindMode 1)",
        QueryInCondition => {
            Key       => 't.id',
            Values    => [ 1 .. 1200 ],
            QuoteType => 'Integer',
            BindMode  => 1,
            Negate    => 1,
        },
        ReferenceData => {
            SQL    => 't.id NOT IN (' . ( join ', ', ('?') x 1200 ) . ')',
            Values => [ 1 .. 1200 ],
        },
        ReferenceDataOracle => {
            SQL => '( t.id NOT IN ('
                . ( join ', ', ('?') x 1000 )
                . ') AND t.id NOT IN ('
                . ( join ', ', ('?') x 200 ) . ') )',
            Values => [ 1 .. 1200 ],
        },
    },
);

my $TestCount = 1;

TEST:
for my $Test (@Tests) {

    if ( !$Test->{QueryInCondition} || ref $Test->{QueryInCondition} ne 'HASH' ) {

        $Self->True(
            0,
            "Test $TestCount: No QueryInCondition found for this test.",
        );

        next TEST;
    }

    my $ReferenceData = $Test->{ReferenceData};

    if ( $DBObject->GetDatabaseFunction('Type') eq 'oracle' && $Test->{ReferenceDataOracle} ) {
        $ReferenceData = $Test->{ReferenceDataOracle};
    }

    # print test case description
    if ( $Test->{Description} ) {
        $Self->True(
            1,
            "Test $TestCount: $Test->{Description}",
        );
    }

    if ( $Test->{QueryInCondition}->{BindMode} ) {

        my %Result = $DBObject->QueryInCondition(
            %{ $Test->{QueryInCondition} },
        );

        if ( $Test->{Fails} ) {
            $Self->False(
                %Result,
                "Test $TestCount: QueryInCondition() - should fail.",
            );
        }
        else {

            $Self->Is(
                $Result{SQL},
                $ReferenceData->{SQL},
                "Test $TestCount: QueryInCondition() - SQL - test the sql string (BindMode 1)",
            );

            $Self->Is(
                scalar @{ $Result{Values} },
                scalar @{ $ReferenceData->{Values} },
                "Test $TestCount: QueryInCondition() - Values - test the value count (BindMode 1)",
            );

            my $Success = $DBObject->Prepare(
                SQL   => 'SELECT t.id, t.tn, ta.time_unit FROM ticket t, time_accounting ta WHERE ' . $Result{SQL},
                Bind  => $Result{Values},
                Limit => 1,
            );

            $Self->True(
                $Success,
                "Test $TestCount: SQL from QueryInCondition - Executed with True (BindMode 1)",
            );
        }
    }
    else {

        my $SQL = $DBObject->QueryInCondition(
            %{ $Test->{QueryInCondition} },
        );

        if ( $Test->{Fails} ) {
            $Self->False(
                $SQL,
                "Test $TestCount: QueryInCondition() - should fail.",
            );
        }
        else {

            $Self->Is(
                $SQL,
                $ReferenceData,
                "Test $TestCount: QueryInCondition() - test the result  (BindMode 0)",
            );

            my $Success = $DBObject->Prepare(
                SQL   => 'SELECT t.id, t.tn, ta.time_unit FROM ticket t, time_accounting ta WHERE ' . $SQL,
                Limit => 1,
            );

            $Self->True(
                $Success,
                "Test $TestCount: SQL from QueryInCondition - Executed with True (BindMode 0)",
            );
        }
    }
}
continue {
    $TestCount++;
}

1;