DBIx-Custom / t / sqlite.t /
Newer Older
830 lines | 22.276kb
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 'table_alias';
183
$dbi = DBIx::Custom->connect;
184
eval { $dbi->execute('drop table table1') };
185
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
186
$dbi->type_rule(
187
    into1 => {
188
        date => sub { uc $_[0] }
189
    }
190
);
191
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
192
  table_alias => {table2 => 'table1'});
193
$result = $dbi->select(table => 'table1');
194
is($result->one->{key1}, 'A');
195

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

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

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
237
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
238
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
239
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
240
$dbi->type_rule(
241
    into1 => [
242
        [qw/date datetime/] => sub { uc $_[0] }
243
    ]
244
);
245
$result = $dbi->execute(
246
    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
247
    param => {key1 => 'a', 'table1.key2' => 'b'},
248
    table => 'table1'
249
);
250
$row = $result->one;
251
is($row->{key1}, 'A');
252
is($row->{key2}, 'B');
cleanup test
Yuki Kimoto authored on 2011-08-06
253

            
cleanup test
Yuki Kimoto authored on 2011-08-10
254
$dbi = DBIx::Custom->connect;
255
$dbi->execute("create table table1 (key1 date, key2 datetime)");
256
$dbi->register_filter(twice => sub { $_[0] * 2 });
257
$dbi->type_rule(
258
    from1 => {
259
        date => 'twice',
260
    },
261
    into1 => {
262
        date => 'twice',
263
    }
264
);
265
$dbi->insert({key1 => 2}, table => 'table1');
266
$result = $dbi->select(table => 'table1');
267
is($result->fetch->[0], 8);
cleanup test
Yuki Kimoto authored on 2011-08-06
268

            
cleanup test
Yuki Kimoto authored on 2011-08-10
269
test 'type_rule and filter order';
test cleanup
Yuki Kimoto authored on 2011-08-10
270
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
271
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
272
$dbi->type_rule(
273
    into1 => {
274
        date => sub { $_[0] . 'b' }
275
    },
276
    into2 => {
277
        date => sub { $_[0] . 'c' }
278
    },
279
    from1 => {
280
        date => sub { $_[0] . 'd' }
281
    },
282
    from2 => {
283
        date => sub { $_[0] . 'e' }
284
    }
285
);
286
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }});
287
$result = $dbi->select(table => 'table1');
288
$result->filter(key1 => sub { $_[0] . 'f' });
289
is($result->fetch_first->[0], '1abcdef');
cleanup test
Yuki Kimoto authored on 2011-08-06
290

            
cleanup test
Yuki Kimoto authored on 2011-08-10
291
$dbi = DBIx::Custom->connect;
292
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
293
$dbi->type_rule(
294
    from1 => {
295
        date => sub { $_[0] . 'p' }
296
    },
297
    from2 => {
298
        date => sub { $_[0] . 'q' }
299
    },
300
);
301
$dbi->insert({key1 => '1'}, table => 'table1');
302
$result = $dbi->select(table => 'table1');
303
$result->type_rule(
304
    from1 => {
305
        date => sub { $_[0] . 'd' }
306
    },
307
    from2 => {
308
        date => sub { $_[0] . 'e' }
309
    }
310
);
311
$result->filter(key1 => sub { $_[0] . 'f' });
312
is($result->fetch_first->[0], '1def');
cleanup test
Yuki Kimoto authored on 2011-08-06
313

            
cleanup test
Yuki Kimoto authored on 2011-08-10
314
test 'type_rule_off';
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] * 2 },
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->type_rule_off->fetch->[0], 2);
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', type_rule_off => 1);
340
$result = $dbi->select(table => 'table1', type_rule_off => 1);
341
is($result->one->{key1}, 4);
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
    }
352
);
353
$dbi->insert({key1 => 2}, table => 'table1');
354
$result = $dbi->select(table => 'table1');
355
is($result->one->{key1}, 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->type_rule(
360
    from1 => {
361
        date => sub { $_[0] * 2 },
362
    },
363
    into1 => {
364
        date => sub { $_[0] * 3 },
365
    }
cleanup test
Yuki Kimoto authored on 2011-08-06
366
);
cleanup test
Yuki Kimoto authored on 2011-08-10
367
$dbi->insert({key1 => 2}, table => 'table1');
368
$result = $dbi->select(table => 'table1');
369
is($result->fetch->[0], 12);
cleanup test
Yuki Kimoto authored on 2011-08-06
370

            
cleanup test
Yuki Kimoto authored on 2011-08-10
371
$dbi = DBIx::Custom->connect;
372
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
373
$dbi->register_filter(ppp => sub { uc $_[0] });
374
$dbi->type_rule(
375
    into1 => {
376
        date => 'ppp'
cleanup test
Yuki Kimoto authored on 2011-08-06
377
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
378
);
379
$dbi->insert({key1 => 'a'}, table => 'table1');
380
$result = $dbi->select(table => 'table1');
381
is($result->one->{key1}, 'A');
cleanup test
Yuki Kimoto authored on 2011-08-06
382

            
cleanup test
Yuki Kimoto authored on 2011-08-10
383
eval{$dbi->type_rule(
384
    into1 => {
385
        date => 'pp'
cleanup test
Yuki Kimoto authored on 2011-08-06
386
    }
cleanup test
Yuki Kimoto authored on 2011-08-10
387
)};
388
like($@, qr/not registered/);
cleanup test
Yuki Kimoto authored on 2011-08-06
389

            
test cleanup
Yuki Kimoto authored on 2011-08-10
390
$dbi = DBIx::Custom->connect;
cleanup test
Yuki Kimoto authored on 2011-08-10
391
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
392
eval {
393
    $dbi->type_rule(
394
        from1 => {
395
            Date => sub { $_[0] * 2 },
396
        }
397
    );
398
};
399
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
400

            
cleanup test
Yuki Kimoto authored on 2011-08-10
401
eval {
402
    $dbi->type_rule(
403
        into1 => {
404
            Date => sub { $_[0] * 2 },
405
        }
406
    );
407
};
408
like($@, qr/lower/);
clenup test
Yuki Kimoto authored on 2011-08-06
409

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
425
$dbi = DBIx::Custom->connect;
426
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
427
$dbi->type_rule(
428
    from1 => {
429
        date => sub { $_[0] * 2 },
430
        datetime => sub { $_[0] * 4 },
431
    },
432
);
433
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
434
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
435
$result->type_rule(
436
    from1 => {
437
        date => sub { $_[0] * 3 }
438
    }
439
);
440
$row = $result->one;
441
is($row->{key1}, 6);
442
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
443

            
444
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
445
$result->type_rule(
446
    from1 => {
447
        date => sub { $_[0] * 3 }
448
    }
449
);
450
$row = $result->one;
451
is($row->{key1}, 6);
452
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
453

            
454
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
455
$result->type_rule(
456
    from1 => {
457
        date => sub { $_[0] * 3 }
458
    }
459
);
460
$row = $result->one;
461
is($row->{key1}, 6);
462
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
463
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
464
$result->type_rule(
465
    from1 => [date => sub { $_[0] * 3 }]
466
);
467
$row = $result->one;
468
is($row->{key1}, 6);
469
is($row->{key2}, 2);
470
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
clenup test
Yuki Kimoto authored on 2011-08-06
471
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
472
$result->type_rule(
473
    from1 => [date => 'fivetimes']
474
);
475
$row = $result->one;
476
is($row->{key1}, 10);
477
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
478
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
479
$result->type_rule(
480
    from1 => [date => undef]
481
);
482
$row = $result->one;
483
is($row->{key1}, 2);
484
is($row->{key2}, 2);
clenup test
Yuki Kimoto authored on 2011-08-06
485

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
498
$dbi = DBIx::Custom->connect;
499
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
500
$dbi->type_rule(
501
    from1 => {
502
        date => sub { $_[0] * 2 },
503
    },
504
);
505
$dbi->insert({key1 => 2}, table => 'table1');
clenup test
Yuki Kimoto authored on 2011-08-06
506
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
507
$result->filter(key1 => sub { $_[0] * 3 });
508
is($result->fetch->[0], 12);
clenup test
Yuki Kimoto authored on 2011-08-06
509

            
cleanup test
Yuki Kimoto authored on 2011-08-10
510
$dbi = DBIx::Custom->connect;
511
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
512
$dbi->type_rule(
513
    into1 => {
514
        date => sub { $_[0] . 'b' }
515
    },
516
    into2 => {
517
        date => sub { $_[0] . 'c' }
518
    },
519
    from1 => {
520
        date => sub { $_[0] . 'd' }
521
    },
522
    from2 => {
523
        date => sub { $_[0] . 'e' }
524
    }
525
);
526
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1);
clenup test
Yuki Kimoto authored on 2011-08-06
527
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
528
is($result->type_rule_off->fetch_first->[0], '1');
clenup test
Yuki Kimoto authored on 2011-08-06
529
$result = $dbi->select(table => 'table1');
cleanup test
Yuki Kimoto authored on 2011-08-10
530
is($result->type_rule_on->fetch_first->[0], '1de');
clenup test
Yuki Kimoto authored on 2011-08-06
531

            
cleanup test
Yuki Kimoto authored on 2011-08-10
532
$dbi = DBIx::Custom->connect;
533
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
534
$dbi->type_rule(
535
    into1 => {
536
        date => sub { $_[0] . 'b' }
cleanup test
Yuki Kimoto authored on 2011-08-06
537
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
538
    into2 => {
539
        date => sub { $_[0] . 'c' }
cleanup test
Yuki Kimoto authored on 2011-08-06
540
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
541
    from1 => {
542
        date => sub { $_[0] . 'd' }
cleanup test
Yuki Kimoto authored on 2011-08-06
543
    },
cleanup test
Yuki Kimoto authored on 2011-08-10
544
    from2 => {
545
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
546
    }
547
);
cleanup test
Yuki Kimoto authored on 2011-08-10
548
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1);
549
$result = $dbi->select(table => 'table1');
550
is($result->type_rule1_off->fetch_first->[0], '1ce');
551
$result = $dbi->select(table => 'table1');
552
is($result->type_rule1_on->fetch_first->[0], '1cde');
cleanup test
Yuki Kimoto authored on 2011-08-06
553

            
cleanup test
Yuki Kimoto authored on 2011-08-10
554
$dbi = DBIx::Custom->connect;
555
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
556
$dbi->type_rule(
557
    into1 => {
558
        date => sub { $_[0] . 'b' }
559
    },
560
    into2 => {
561
        date => sub { $_[0] . 'c' }
562
    },
563
    from1 => {
564
        date => sub { $_[0] . 'd' }
565
    },
566
    from2 => {
567
        date => sub { $_[0] . 'e' }
cleanup test
Yuki Kimoto authored on 2011-08-06
568
    }
569
);
cleanup test
Yuki Kimoto authored on 2011-08-10
570
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1);
571
$result = $dbi->select(table => 'table1');
572
is($result->type_rule2_off->fetch_first->[0], '1bd');
573
$result = $dbi->select(table => 'table1');
574
is($result->type_rule2_on->fetch_first->[0], '1bde');
test cleanup
Yuki Kimoto authored on 2011-08-10
575

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

            
591
$dbi = MyDBI4->connect;
592
eval { $dbi->execute('drop table book') };
593
$dbi->execute("create table book (title, author)");
594
$model = $dbi->model('book');
595
$model->insert({title => 'a', author => 'b'});
596
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
597
$dbi->execute("create table company (name)");
598
$model = $dbi->model('company');
599
$model->insert({name => 'a'});
600
is_deeply($model->list->all, [{name => 'a'}], 'basic');
test cleanup
Yuki Kimoto authored on 2011-08-10
601

            
test cleanup
Yuki Kimoto authored on 2011-08-10
602
$dbi = MyDBI5->connect;
603
eval { $dbi->execute('drop table company') };
604
eval { $dbi->execute('drop table table1') };
605
$dbi->execute("create table company (name)");
606
$dbi->execute("create table table1 (key1)");
607
$model = $dbi->model('company');
608
$model->insert({name => 'a'});
609
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
610
$dbi->insert(table => 'table1', param => {key1 => 1});
611
$model = $dbi->model('book');
612
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
test cleanup
Yuki Kimoto authored on 2011-08-10
613

            
test cleanup
Yuki Kimoto authored on 2011-08-10
614
test 'primary_key';
615
use MyDBI1;
616
$dbi = MyDBI1->connect;
617
$model = $dbi->model('book');
618
$model->primary_key(['id', 'number']);
619
is_deeply($model->primary_key, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
620

            
test cleanup
Yuki Kimoto authored on 2011-08-10
621
test 'columns';
622
use MyDBI1;
623
$dbi = MyDBI1->connect;
624
$model = $dbi->model('book');
625
$model->columns(['id', 'number']);
626
is_deeply($model->columns, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
627

            
test cleanup
Yuki Kimoto authored on 2011-08-10
628
test 'setup_model';
629
use MyDBI1;
630
$dbi = MyDBI1->connect;
631
eval { $dbi->execute('drop table book') };
632
eval { $dbi->execute('drop table company') };
633
eval { $dbi->execute('drop table test') };
test cleanup
Yuki Kimoto authored on 2011-08-10
634

            
test cleanup
Yuki Kimoto authored on 2011-08-10
635
$dbi->execute('create table book (id)');
636
$dbi->execute('create table company (id, name);');
test cleanup
Yuki Kimoto authored on 2011-08-10
637
$dbi->execute('create table test (id, name);');
test cleanup
Yuki Kimoto authored on 2011-08-10
638
$dbi->setup_model;
639
is_deeply($dbi->model('book')->columns, ['id']);
640
is_deeply($dbi->model('company')->columns, ['id', 'name']);
test cleanup
Yuki Kimoto authored on 2011-08-10
641

            
642

            
643

            
644

            
645

            
646

            
647

            
648

            
649

            
650

            
651

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
663
$dbi = DBIx::Custom->connect;
664
eval { $dbi->execute('drop table table1') };
665
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));');
666
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
667
$dbi->update(table => 'table1', param => {key2 => 4},
668
  where => {key1 => 1}, prefix => 'or replace');
669
$result = $dbi->execute('select * from table1;');
670
$rows   = $result->all;
671
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
672

            
673

            
test cleanup
Yuki Kimoto authored on 2011-08-10
674
test 'quote';
675
$dbi = DBIx::Custom->connect;
676
$dbi->quote('"');
677
eval { $dbi->execute("drop table ${q}table$p") };
678
$dbi->execute($create_table_reserved);
679
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
680
$dbi->insert(table => 'table', param => {select => 1});
681
$dbi->delete(table => 'table', where => {select => 1});
682
$result = $dbi->execute("select * from ${q}table$p");
683
$rows   = $result->all;
684
is_deeply($rows, [], "reserved word");
685

            
test cleanup
Yuki Kimoto authored on 2011-08-10
686
test 'finish statement handle';
687
$dbi = DBIx::Custom->connect;
688
$dbi->execute($create_table1);
689
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
690
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
691

            
692
$result = $dbi->select(table => 'table1');
693
$row = $result->fetch_first;
694
is_deeply($row, [1, 2], "row");
695
$row = $result->fetch;
696
ok(!$row, "finished");
697

            
698
$result = $dbi->select(table => 'table1');
699
$row = $result->fetch_hash_first;
700
is_deeply($row, {key1 => 1, key2 => 2}, "row");
701
$row = $result->fetch_hash;
702
ok(!$row, "finished");
703

            
704
$dbi->execute('create table table2 (key1, key2);');
705
$result = $dbi->select(table => 'table2');
706
$row = $result->fetch_hash_first;
707
ok(!$row, "no row fetch");
708

            
709
$dbi = DBIx::Custom->connect;
710
eval { $dbi->execute('drop table table1') };
711
$dbi->execute($create_table1);
712
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
713
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
714
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
715
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
716
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
717
$result = $dbi->select(table => 'table1');
718
$rows = $result->fetch_multi(2);
719
is_deeply($rows, [[1, 2],
720
                  [3, 4]], "fetch_multi first");
721
$rows = $result->fetch_multi(2);
722
is_deeply($rows, [[5, 6],
723
                  [7, 8]], "fetch_multi secound");
724
$rows = $result->fetch_multi(2);
725
is_deeply($rows, [[9, 10]], "fetch_multi third");
726
$rows = $result->fetch_multi(2);
727
ok(!$rows);
728

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

            
733
$result = $dbi->select(table => 'table1');
734
$rows = $result->fetch_hash_multi(2);
735
is_deeply($rows, [{key1 => 1, key2 => 2},
736
                  {key1 => 3, key2 => 4}], "fetch_multi first");
737
$rows = $result->fetch_hash_multi(2);
738
is_deeply($rows, [{key1 => 5, key2 => 6},
739
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
740
$rows = $result->fetch_hash_multi(2);
741
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
742
$rows = $result->fetch_hash_multi(2);
743
ok(!$rows);
744

            
745
$result = $dbi->select(table => 'table1');
746
eval {$result->fetch_hash_multi};
747
like($@, qr/Row count must be specified/, "Not specified row count");
748

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
750
test 'type option'; # DEPRECATED!
751
$dbi = DBIx::Custom->connect(
752
    data_source => 'dbi:SQLite:dbname=:memory:',
753
    dbi_option => {
754
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
755
    }
756
);
757
$binary = pack("I3", 1, 2, 3);
758
eval { $dbi->execute('drop table table1') };
759
$dbi->execute('create table table1(key1, key2)');
760
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
761
$result = $dbi->select(table => 'table1');
762
$row   = $result->one;
763
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
764
$result = $dbi->execute('select length(key1) as key1_length from table1');
765
$row = $result->one;
766
is($row->{key1_length}, length $binary);
767

            
768
test 'type_rule from';
769
$dbi = DBIx::Custom->connect;
770
$dbi->type_rule(
771
    from1 => {
772
        date => sub { uc $_[0] }
773
    }
774
);
775
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
776
$dbi->insert({key1 => 'a'}, table => 'table1');
777
$result = $dbi->select(table => 'table1');
778
is($result->fetch_first->[0], 'A');
779

            
780
$result = $dbi->select(table => 'table1');
781
is($result->one->{key1}, 'A');
782

            
783

            
784

            
785

            
786

            
test cleanup
Yuki Kimoto authored on 2011-08-10
787
# DEPRECATED! test
788
test 'filter __ expression';
789
$dbi = DBIx::Custom->connect;
790
eval { $dbi->execute('drop table company') };
791
eval { $dbi->execute('drop table location') };
792
$dbi->execute('create table company (id, name, location_id)');
793
$dbi->execute('create table location (id, name)');
794
$dbi->apply_filter('location',
795
  name => {in => sub { uc $_[0] } }
796
);
797

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

            
801
$result = $dbi->select(
802
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
803
    column => ['location.name as location__name']
804
);
805
is($result->fetch_first->[0], 'B');
806

            
807
$result = $dbi->select(
808
    table => 'company', relation => {'company.location_id' => 'location.id'},
809
    column => ['location.name as location__name']
810
);
811
is($result->fetch_first->[0], 'B');
812

            
813
$result = $dbi->select(
814
    table => 'company', relation => {'company.location_id' => 'location.id'},
815
    column => ['location.name as "location.name"']
816
);
817
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
818

            
819
test 'reserved_word_quote';
820
$dbi = DBIx::Custom->connect;
821
eval { $dbi->execute("drop table ${q}table$p") };
822
$dbi->reserved_word_quote('"');
823
$dbi->execute($create_table_reserved);
824
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
825
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
826
$dbi->insert(table => 'table', param => {select => 1});
827
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
828
$result = $dbi->execute("select * from ${q}table$p");
829
$rows   = $result->all;
830
is_deeply($rows, [{select => 2, update => 6}], "reserved word");