DBIx-Custom / t / sqlite.t /
Newer Older
816 lines | 21.838kb
cleanup test
Yuki Kimoto authored on 2011-08-06
1
use Test::More;
2
use strict;
3
use warnings;
4
use utf8;
5
use Encode qw/encode_utf8 decode_utf8/;
test cleanup
Yuki Kimoto authored on 2011-08-06
6
use FindBin;
cleanup test
Yuki Kimoto authored on 2011-08-10
7
use lib "$FindBin::Bin/common";
cleanup test
Yuki Kimoto authored on 2011-08-06
8

            
9
BEGIN {
10
    eval { require DBD::SQLite; 1 }
11
        or plan skip_all => 'DBD::SQLite required';
12
    eval { DBD::SQLite->VERSION >= 1.25 }
13
        or plan skip_all => 'DBD::SQLite >= 1.25 required';
14

            
15
    plan 'no_plan';
16
    use_ok('DBIx::Custom');
17
}
18

            
test cleanup
Yuki Kimoto authored on 2011-08-06
19
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
cleanup test
Yuki Kimoto authored on 2011-08-06
20
sub test { print "# $_[0]\n" }
21

            
test cleanup
Yuki Kimoto authored on 2011-08-10
22
use DBIx::Custom;
test cleanup
Yuki Kimoto authored on 2011-08-10
23
{
24
    package DBIx::Custom;
25
    has dsn => sub { 'dbi:SQLite:dbname=:memory:' }
26
}
test cleanup
Yuki Kimoto authored on 2011-08-10
27
use MyDBI1;
28
{
29
    package MyDBI4;
30

            
31
    use strict;
32
    use warnings;
33

            
34
    use base 'DBIx::Custom';
35

            
36
    sub connect {
37
        my $self = shift->SUPER::connect(@_);
38
        
39
        $self->include_model(
40
            MyModel2 => [
41
                'book',
42
                {class => 'Company', name => 'company'}
43
            ]
44
        );
45
    }
46

            
47
    package MyModel2::Base1;
48

            
49
    use strict;
50
    use warnings;
51

            
52
    use base 'DBIx::Custom::Model';
53

            
54
    package MyModel2::book;
55

            
56
    use strict;
57
    use warnings;
58

            
59
    use base 'MyModel2::Base1';
60

            
61
    sub insert {
62
        my ($self, $param) = @_;
63
        
64
        return $self->SUPER::insert(param => $param);
65
    }
66

            
67
    sub list { shift->select; }
68

            
69
    package MyModel2::Company;
70

            
71
    use strict;
72
    use warnings;
73

            
74
    use base 'MyModel2::Base1';
75

            
76
    sub insert {
77
        my ($self, $param) = @_;
78
        
79
        return $self->SUPER::insert(param => $param);
80
    }
81

            
82
    sub list { shift->select; }
83
}
84
{
85
     package MyDBI5;
86

            
87
    use strict;
88
    use warnings;
89

            
90
    use base 'DBIx::Custom';
91

            
92
    sub connect {
93
        my $self = shift->SUPER::connect(@_);
94
        
95
        $self->include_model('MyModel4');
96
    }
97
}
98
{
99
    package MyDBI6;
100
    
101
    use base 'DBIx::Custom';
102
    
103
    sub connect {
104
        my $self = shift->SUPER::connect(@_);
105
        
106
        $self->include_model('MyModel5');
107
        
108
        return $self;
109
    }
110
}
111
{
112
    package MyDBI7;
113
    
114
    use base 'DBIx::Custom';
115
    
116
    sub connect {
117
        my $self = shift->SUPER::connect(@_);
118
        
119
        $self->include_model('MyModel6');
120
        
121
        
122
        return $self;
123
    }
124
}
125
{
126
    package MyDBI8;
127
    
128
    use base 'DBIx::Custom';
129
    
130
    sub connect {
131
        my $self = shift->SUPER::connect(@_);
132
        
133
        $self->include_model('MyModel7');
134
        
135
        return $self;
136
    }
137
}
138

            
139
{
140
    package MyDBI9;
141
    
142
    use base 'DBIx::Custom';
143
    
144
    sub connect {
145
        my $self = shift->SUPER::connect(@_);
146
        
147
        $self->include_model('MyModel8')->setup_model;
148
        
149
        return $self;
150
    }
151
}
test cleanup
Yuki Kimoto authored on 2011-08-10
152

            
cleanup test
Yuki Kimoto authored on 2011-08-06
153
# Constant
cleanup test
Yuki Kimoto authored on 2011-08-10
154
my $create_table1 = 'create table table1 (key1 varchar, key2 varchar);';
155
my $create_table1_2 = 'create table table1 (key1 varchar, key2 varchar, key3 varchar, key4 varchar, key5 varchar);';
156
my $create_table2 = 'create table table2 (key1 varchar, key3 varchar);';
157
my $create_table2_2 = "create table table2 (key1 varchar, key2 varchar, key3 varchar)";
158
my $create_table3 = "create table table3 (key1 varchar, key2 varchar, key3 varchar)";
159
my $create_table_reserved = 'create table "table" ("select" varchar, "update" varchar)';
test cleanup
Yuki Kimoto authored on 2011-08-10
160

            
test cleanup
Yuki Kimoto authored on 2011-08-10
161
my $q = '"';
162
my $p = '"';
cleanup test
Yuki Kimoto authored on 2011-08-06
163

            
cleanup test
Yuki Kimoto authored on 2011-08-06
164
# Variables
165
my $dbi;
166
my $param;
167
my $params;
168
my $sql;
169
my $result;
170
my $row;
171
my @rows;
172
my $rows;
173
my $model;
174
my $model2;
175
my $where;
cleanup test
Yuki Kimoto authored on 2011-08-10
176
my $binary;
cleanup test
Yuki Kimoto authored on 2011-08-06
177

            
178
# Prepare table
test cleanup
Yuki Kimoto authored on 2011-08-10
179
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
180

            
test cleanup
Yuki Kimoto authored on 2011-08-10
181
### a little complex test
cleanup test
Yuki Kimoto authored on 2011-08-10
182
test 'type_rule into';
test cleanup
Yuki Kimoto authored on 2011-08-10
183
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-06
184
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
185
$dbi->type_rule(
186
    into1 => {
187
        date => sub { uc $_[0] }
188
    }
189
);
cleanup test
Yuki Kimoto authored on 2011-08-10
190
$dbi->insert({key1 => 'a'}, table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-06
191
$result = $dbi->select(table => 'table1');
192
is($result->one->{key1}, 'A');
193

            
test cleanup
Yuki Kimoto authored on 2011-08-10
194
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
195
$dbi->execute("create table table1 (key1 date, key2 datetime)");
196
$dbi->type_rule(
197
    into1 => [
198
         [qw/date datetime/] => sub { uc $_[0] }
199
    ]
200
);
201
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
202
$result = $dbi->select(table => 'table1');
203
$row = $result->one;
204
is($row->{key1}, 'A');
205
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
206

            
test cleanup
Yuki Kimoto authored on 2011-08-10
207
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
208
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
209
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
210
$dbi->type_rule(
211
    into1 => [
212
        [qw/date datetime/] => sub { uc $_[0] }
213
    ]
214
);
215
$result = $dbi->execute(
216
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
217
    param => {key1 => 'a', 'table1.key2' => 'b'}
218
);
219
$row = $result->one;
220
is($row->{key1}, 'a');
221
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
222

            
test cleanup
Yuki Kimoto authored on 2011-08-10
223
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
224
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
225
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
226
$dbi->type_rule(
227
    into1 => [
228
        [qw/date datetime/] => sub { uc $_[0] }
229
    ]
230
);
231
$result = $dbi->execute(
232
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
233
    param => {key1 => 'a', 'table1.key2' => 'b'},
234
    table => 'table1'
235
);
236
$row = $result->one;
237
is($row->{key1}, 'A');
238
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
239

            
cleanup test
Yuki Kimoto authored on 2011-08-10
240
$dbi = DBIx::Custom->connect;
241
$dbi->execute("create table table1 (key1 date, key2 datetime)");
242
$dbi->register_filter(twice => sub { $_[0] * 2 });
243
$dbi->type_rule(
244
    from1 => {
245
        date => 'twice',
246
    },
247
    into1 => {
248
        date => 'twice',
249
    }
250
);
251
$dbi->insert({key1 => 2}, table => 'table1');
252
$result = $dbi->select(table => 'table1');
253
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
254

            
cleanup test
Yuki Kimoto authored on 2011-08-10
255
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
256
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
257
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
258
$dbi->type_rule(
259
    into1 => {
260
        date => sub { $_[0] . 'b' }
261
    },
262
    into2 => {
263
        date => sub { $_[0] . 'c' }
264
    },
265
    from1 => {
266
        date => sub { $_[0] . 'd' }
267
    },
268
    from2 => {
269
        date => sub { $_[0] . 'e' }
270
    }
271
);
272
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
273
$result = $dbi->select(table => 'table1');
274
$result->filter(key1 => sub { $_[0] . 'f' });
275
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
276

            
cleanup test
Yuki Kimoto authored on 2011-08-10
277
$dbi = DBIx::Custom->connect;
278
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
279
$dbi->type_rule(
280
    from1 => {
281
        date => sub { $_[0] . 'p' }
282
    },
283
    from2 => {
284
        date => sub { $_[0] . 'q' }
285
    },
286
);
287
$dbi->insert({key1 => '1'}, table => 'table1');
288
$result = $dbi->select(table => 'table1');
289
$result->type_rule(
290
    from1 => {
291
        date => sub { $_[0] . 'd' }
292
    },
293
    from2 => {
294
        date => sub { $_[0] . 'e' }
295
    }
296
);
297
$result->filter(key1 => sub { $_[0] . 'f' });
298
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
299

            
cleanup test
Yuki Kimoto authored on 2011-08-10
300
test 'type_rule_off';
301
$dbi = DBIx::Custom->connect;
302
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
303
$dbi->type_rule(
304
    from1 => {
305
        date => sub { $_[0] * 2 },
306
    },
307
    into1 => {
308
        date => sub { $_[0] * 2 },
309
    }
310
);
311
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
312
$result = $dbi->select(table => 'table1', type_rule_off => 1);
313
is($result->type_rule_off->fetch->[0], 2);
cleanup test
Yuki Kimoto authored on 2011-08-06
314

            
cleanup test
Yuki Kimoto authored on 2011-08-10
315
$dbi = DBIx::Custom->connect;
316
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
317
$dbi->type_rule(
318
    from1 => {
319
        date => sub { $_[0] * 2 },
320
    },
321
    into1 => {
322
        date => sub { $_[0] * 3 },
323
    }
324
);
325
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
326
$result = $dbi->select(table => 'table1', type_rule_off => 1);
327
is($result->one->{key1}, 4);
cleanup test
Yuki Kimoto authored on 2011-08-06
328

            
cleanup test
Yuki Kimoto authored on 2011-08-10
329
$dbi = DBIx::Custom->connect;
330
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
331
$dbi->type_rule(
332
    from1 => {
333
        date => sub { $_[0] * 2 },
334
    },
335
    into1 => {
336
        date => sub { $_[0] * 3 },
337
    }
338
);
339
$dbi->insert({key1 => 2}, table => 'table1');
340
$result = $dbi->select(table => 'table1');
341
is($result->one->{key1}, 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
342

            
cleanup test
Yuki Kimoto authored on 2011-08-10
343
$dbi = DBIx::Custom->connect;
344
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
345
$dbi->type_rule(
346
    from1 => {
347
        date => sub { $_[0] * 2 },
348
    },
349
    into1 => {
350
        date => sub { $_[0] * 3 },
351
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
352
);
cleanup test
Yuki Kimoto authored on 2011-08-10
353
$dbi->insert({key1 => 2}, table => 'table1');
354
$result = $dbi->select(table => 'table1');
355
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
356

            
cleanup test
Yuki Kimoto authored on 2011-08-10
357
$dbi = DBIx::Custom->connect;
358
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
359
$dbi->register_filter(ppp => sub { uc $_[0] });
360
$dbi->type_rule(
361
    into1 => {
362
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
363
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
364
);
365
$dbi->insert({key1 => 'a'}, table => 'table1');
366
$result = $dbi->select(table => 'table1');
367
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
368

            
cleanup test
Yuki Kimoto authored on 2011-08-10
369
eval{$dbi->type_rule(
370
    into1 => {
371
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
372
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
373
)};
374
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
375

            
test cleanup
Yuki Kimoto authored on 2011-08-10
376
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
377
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
378
eval {
379
    $dbi->type_rule(
380
        from1 => {
381
            Date => sub { $_[0] * 2 },
382
        }
383
    );
384
};
385
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
386

            
cleanup test
Yuki Kimoto authored on 2011-08-10
387
eval {
388
    $dbi->type_rule(
389
        into1 => {
390
            Date => sub { $_[0] * 2 },
391
        }
392
    );
393
};
394
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
395

            
cleanup test
Yuki Kimoto authored on 2011-08-10
396
$dbi = DBIx::Custom->connect;
397
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
398
$dbi->type_rule(
399
    from1 => {
400
        date => sub { $_[0] * 2 },
401
    },
402
    into1 => {
403
        date => sub { $_[0] * 3 },
404
    }
405
);
406
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
407
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
408
$result->type_rule_off;
409
is($result->one->{key1}, 6);
clenup test
Yuki Kimoto authored on 2011-08-06
410

            
cleanup test
Yuki Kimoto authored on 2011-08-10
411
$dbi = DBIx::Custom->connect;
412
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
413
$dbi->type_rule(
414
    from1 => {
415
        date => sub { $_[0] * 2 },
416
        datetime => sub { $_[0] * 4 },
417
    },
418
);
419
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
420
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
421
$result->type_rule(
422
    from1 => {
423
        date => sub { $_[0] * 3 }
424
    }
425
);
426
$row = $result->one;
427
is($row->{key1}, 6);
428
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
429

            
430
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
431
$result->type_rule(
432
    from1 => {
433
        date => sub { $_[0] * 3 }
434
    }
435
);
436
$row = $result->one;
437
is($row->{key1}, 6);
438
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
439

            
440
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
441
$result->type_rule(
442
    from1 => {
443
        date => sub { $_[0] * 3 }
444
    }
445
);
446
$row = $result->one;
447
is($row->{key1}, 6);
448
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
449
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
450
$result->type_rule(
451
    from1 => [date => sub { $_[0] * 3 }]
452
);
453
$row = $result->one;
454
is($row->{key1}, 6);
455
is($row->{key2}, 2);
456
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
457
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
458
$result->type_rule(
459
    from1 => [date => 'fivetimes']
460
);
461
$row = $result->one;
462
is($row->{key1}, 10);
463
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
464
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
465
$result->type_rule(
466
    from1 => [date => undef]
467
);
468
$row = $result->one;
469
is($row->{key1}, 2);
470
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
471

            
test cleanup
Yuki Kimoto authored on 2011-08-10
472
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
473
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
474
$dbi->type_rule(
475
    from1 => {
476
        date => sub { $_[0] * 2 },
477
    },
478
);
479
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
480
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
481
$result->filter(key1 => sub { $_[0] * 3 });
482
is($result->one->{key1}, 12);
clenup test
Yuki Kimoto authored on 2011-08-06
483

            
cleanup test
Yuki Kimoto authored on 2011-08-10
484
$dbi = DBIx::Custom->connect;
485
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
486
$dbi->type_rule(
487
    from1 => {
488
        date => sub { $_[0] * 2 },
489
    },
490
);
491
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
492
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
493
$result->filter(key1 => sub { $_[0] * 3 });
494
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
495

            
cleanup test
Yuki Kimoto authored on 2011-08-10
496
$dbi = DBIx::Custom->connect;
497
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
498
$dbi->type_rule(
499
    into1 => {
500
        date => sub { $_[0] . 'b' }
501
    },
502
    into2 => {
503
        date => sub { $_[0] . 'c' }
504
    },
505
    from1 => {
506
        date => sub { $_[0] . 'd' }
507
    },
508
    from2 => {
509
        date => sub { $_[0] . 'e' }
510
    }
511
);
512
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
513
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
514
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
515
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
516
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
517

            
cleanup test
Yuki Kimoto authored on 2011-08-10
518
$dbi = DBIx::Custom->connect;
519
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
520
$dbi->type_rule(
521
    into1 => {
522
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
523
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
524
    into2 => {
525
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
526
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
527
    from1 => {
528
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
529
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
530
    from2 => {
531
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
532
    }
533
);
cleanup test
Yuki Kimoto authored on 2011-08-10
534
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
535
$result = $dbi->select(table => 'table1');
536
is($result->type_rule1_off->fetch_first->[0], '1ce');
537
$result = $dbi->select(table => 'table1');
538
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
539

            
cleanup test
Yuki Kimoto authored on 2011-08-10
540
$dbi = DBIx::Custom->connect;
541
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
542
$dbi->type_rule(
543
    into1 => {
544
        date => sub { $_[0] . 'b' }
545
    },
546
    into2 => {
547
        date => sub { $_[0] . 'c' }
548
    },
549
    from1 => {
550
        date => sub { $_[0] . 'd' }
551
    },
552
    from2 => {
553
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
554
    }
555
);
cleanup test
Yuki Kimoto authored on 2011-08-10
556
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
557
$result = $dbi->select(table => 'table1');
558
is($result->type_rule2_off->fetch_first->[0], '1bd');
559
$result = $dbi->select(table => 'table1');
560
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
561

            
test cleanup
Yuki Kimoto authored on 2011-08-10
562
test 'Model class';
563
use MyDBI1;
564
$dbi = MyDBI1->connect;
565
eval { $dbi->execute('drop table book') };
566
$dbi->execute("create table book (title, author)");
567
$model = $dbi->model('book');
568
$model->insert({title => 'a', author => 'b'});
569
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
570
$dbi->execute("create table company (name)");
571
$model = $dbi->model('company');
572
$model->insert({name => 'a'});
573
is_deeply($model->list->all, [{name => 'a'}], 'basic');
574
is($dbi->models->{'book'}, $dbi->model('book'));
575
is($dbi->models->{'company'}, $dbi->model('company'));
576

            
577
$dbi = MyDBI4->connect;
578
eval { $dbi->execute('drop table book') };
579
$dbi->execute("create table book (title, author)");
580
$model = $dbi->model('book');
581
$model->insert({title => 'a', author => 'b'});
582
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
583
$dbi->execute("create table company (name)");
584
$model = $dbi->model('company');
585
$model->insert({name => 'a'});
586
is_deeply($model->list->all, [{name => 'a'}], 'basic');
test cleanup
Yuki Kimoto authored on 2011-08-10
587

            
test cleanup
Yuki Kimoto authored on 2011-08-10
588
$dbi = MyDBI5->connect;
589
eval { $dbi->execute('drop table company') };
590
eval { $dbi->execute('drop table table1') };
591
$dbi->execute("create table company (name)");
592
$dbi->execute("create table table1 (key1)");
593
$model = $dbi->model('company');
594
$model->insert({name => 'a'});
595
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
596
$dbi->insert(table => 'table1', param => {key1 => 1});
597
$model = $dbi->model('book');
598
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
test cleanup
Yuki Kimoto authored on 2011-08-10
599

            
test cleanup
Yuki Kimoto authored on 2011-08-10
600
test 'primary_key';
601
use MyDBI1;
602
$dbi = MyDBI1->connect;
603
$model = $dbi->model('book');
604
$model->primary_key(['id', 'number']);
605
is_deeply($model->primary_key, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
606

            
test cleanup
Yuki Kimoto authored on 2011-08-10
607
test 'columns';
608
use MyDBI1;
609
$dbi = MyDBI1->connect;
610
$model = $dbi->model('book');
611
$model->columns(['id', 'number']);
612
is_deeply($model->columns, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
613

            
test cleanup
Yuki Kimoto authored on 2011-08-10
614
test 'setup_model';
615
use MyDBI1;
616
$dbi = MyDBI1->connect;
617
eval { $dbi->execute('drop table book') };
618
eval { $dbi->execute('drop table company') };
619
eval { $dbi->execute('drop table test') };
test cleanup
Yuki Kimoto authored on 2011-08-10
620

            
test cleanup
Yuki Kimoto authored on 2011-08-10
621
$dbi->execute('create table book (id)');
622
$dbi->execute('create table company (id, name);');
test cleanup
Yuki Kimoto authored on 2011-08-10
623
$dbi->execute('create table test (id, name);');
test cleanup
Yuki Kimoto authored on 2011-08-10
624
$dbi->setup_model;
625
is_deeply($dbi->model('book')->columns, ['id']);
626
is_deeply($dbi->model('company')->columns, ['id', 'name']);
test cleanup
Yuki Kimoto authored on 2011-08-10
627

            
628

            
629

            
630

            
631

            
632

            
633

            
634

            
635

            
636

            
637

            
test cleanup
Yuki Kimoto authored on 2011-08-10
638
### SQLite only test
639
test 'prefix';
640
$dbi = DBIx::Custom->connect;
641
eval { $dbi->execute('drop table table1') };
642
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));');
643
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
644
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
645
$result = $dbi->execute('select * from table1;');
646
$rows   = $result->all;
647
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
648

            
test cleanup
Yuki Kimoto authored on 2011-08-10
649
$dbi = DBIx::Custom->connect;
650
eval { $dbi->execute('drop table table1') };
651
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));');
652
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
653
$dbi->update(table => 'table1', param => {key2 => 4},
654
  where => {key1 => 1}, prefix => 'or replace');
655
$result = $dbi->execute('select * from table1;');
656
$rows   = $result->all;
657
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
658

            
659

            
test cleanup
Yuki Kimoto authored on 2011-08-10
660
test 'quote';
661
$dbi = DBIx::Custom->connect;
662
$dbi->quote('"');
663
eval { $dbi->execute("drop table ${q}table$p") };
664
$dbi->execute($create_table_reserved);
665
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
666
$dbi->insert(table => 'table', param => {select => 1});
667
$dbi->delete(table => 'table', where => {select => 1});
668
$result = $dbi->execute("select * from ${q}table$p");
669
$rows   = $result->all;
670
is_deeply($rows, [], "reserved word");
671

            
test cleanup
Yuki Kimoto authored on 2011-08-10
672
test 'finish statement handle';
673
$dbi = DBIx::Custom->connect;
674
$dbi->execute($create_table1);
675
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
676
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
677

            
678
$result = $dbi->select(table => 'table1');
679
$row = $result->fetch_first;
680
is_deeply($row, [1, 2], "row");
681
$row = $result->fetch;
682
ok(!$row, "finished");
683

            
684
$result = $dbi->select(table => 'table1');
685
$row = $result->fetch_hash_first;
686
is_deeply($row, {key1 => 1, key2 => 2}, "row");
687
$row = $result->fetch_hash;
688
ok(!$row, "finished");
689

            
690
$dbi->execute('create table table2 (key1, key2);');
691
$result = $dbi->select(table => 'table2');
692
$row = $result->fetch_hash_first;
693
ok(!$row, "no row fetch");
694

            
695
$dbi = DBIx::Custom->connect;
696
eval { $dbi->execute('drop table table1') };
697
$dbi->execute($create_table1);
698
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
699
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
700
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
701
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
702
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
703
$result = $dbi->select(table => 'table1');
704
$rows = $result->fetch_multi(2);
705
is_deeply($rows, [[1, 2],
706
                  [3, 4]], "fetch_multi first");
707
$rows = $result->fetch_multi(2);
708
is_deeply($rows, [[5, 6],
709
                  [7, 8]], "fetch_multi secound");
710
$rows = $result->fetch_multi(2);
711
is_deeply($rows, [[9, 10]], "fetch_multi third");
712
$rows = $result->fetch_multi(2);
713
ok(!$rows);
714

            
715
$result = $dbi->select(table => 'table1');
716
eval {$result->fetch_multi};
717
like($@, qr/Row count must be specified/, "Not specified row count");
718

            
719
$result = $dbi->select(table => 'table1');
720
$rows = $result->fetch_hash_multi(2);
721
is_deeply($rows, [{key1 => 1, key2 => 2},
722
                  {key1 => 3, key2 => 4}], "fetch_multi first");
723
$rows = $result->fetch_hash_multi(2);
724
is_deeply($rows, [{key1 => 5, key2 => 6},
725
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
726
$rows = $result->fetch_hash_multi(2);
727
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
728
$rows = $result->fetch_hash_multi(2);
729
ok(!$rows);
730

            
731
$result = $dbi->select(table => 'table1');
732
eval {$result->fetch_hash_multi};
733
like($@, qr/Row count must be specified/, "Not specified row count");
734

            
test cleanup
Yuki Kimoto authored on 2011-08-10
735

            
cleanup test
Yuki Kimoto authored on 2011-08-10
736
test 'type option'; # DEPRECATED!
737
$dbi = DBIx::Custom->connect(
738
    data_source => 'dbi:SQLite:dbname=:memory:',
739
    dbi_option => {
740
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
741
    }
742
);
743
$binary = pack("I3", 1, 2, 3);
744
eval { $dbi->execute('drop table table1') };
745
$dbi->execute('create table table1(key1, key2)');
746
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
747
$result = $dbi->select(table => 'table1');
748
$row   = $result->one;
749
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
750
$result = $dbi->execute('select length(key1) as key1_length from table1');
751
$row = $result->one;
752
is($row->{key1_length}, length $binary);
753

            
754
test 'type_rule from';
755
$dbi = DBIx::Custom->connect;
756
$dbi->type_rule(
757
    from1 => {
758
        date => sub { uc $_[0] }
759
    }
760
);
761
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
762
$dbi->insert({key1 => 'a'}, table => 'table1');
763
$result = $dbi->select(table => 'table1');
764
is($result->fetch_first->[0], 'A');
765

            
766
$result = $dbi->select(table => 'table1');
767
is($result->one->{key1}, 'A');
768

            
769

            
770

            
771

            
772

            
test cleanup
Yuki Kimoto authored on 2011-08-10
773
# DEPRECATED! test
774
test 'filter __ expression';
775
$dbi = DBIx::Custom->connect;
776
eval { $dbi->execute('drop table company') };
777
eval { $dbi->execute('drop table location') };
778
$dbi->execute('create table company (id, name, location_id)');
779
$dbi->execute('create table location (id, name)');
780
$dbi->apply_filter('location',
781
  name => {in => sub { uc $_[0] } }
782
);
783

            
784
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2});
785
$dbi->insert(table => 'location', param => {id => 2, name => 'b'});
786

            
787
$result = $dbi->select(
788
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
789
    column => ['location.name as location__name']
790
);
791
is($result->fetch_first->[0], 'B');
792

            
793
$result = $dbi->select(
794
    table => 'company', relation => {'company.location_id' => 'location.id'},
795
    column => ['location.name as location__name']
796
);
797
is($result->fetch_first->[0], 'B');
798

            
799
$result = $dbi->select(
800
    table => 'company', relation => {'company.location_id' => 'location.id'},
801
    column => ['location.name as "location.name"']
802
);
803
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
804

            
805
test 'reserved_word_quote';
806
$dbi = DBIx::Custom->connect;
807
eval { $dbi->execute("drop table ${q}table$p") };
808
$dbi->reserved_word_quote('"');
809
$dbi->execute($create_table_reserved);
810
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
811
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
812
$dbi->insert(table => 'table', param => {select => 1});
813
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
814
$result = $dbi->execute("select * from ${q}table$p");
815
$rows   = $result->all;
816
is_deeply($rows, [{select => 2, update => 6}], "reserved word");