DBIx-Custom / t / sqlite.t /
Newer Older
427 lines | 12.165kb
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-06
182

            
test cleanup
Yuki Kimoto authored on 2011-08-10
183
test 'Model class';
184
use MyDBI1;
185
$dbi = MyDBI1->connect;
186
eval { $dbi->execute('drop table book') };
187
$dbi->execute("create table book (title, author)");
188
$model = $dbi->model('book');
189
$model->insert({title => 'a', author => 'b'});
190
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
191
$dbi->execute("create table company (name)");
192
$model = $dbi->model('company');
193
$model->insert({name => 'a'});
194
is_deeply($model->list->all, [{name => 'a'}], 'basic');
195
is($dbi->models->{'book'}, $dbi->model('book'));
196
is($dbi->models->{'company'}, $dbi->model('company'));
197

            
198
$dbi = MyDBI4->connect;
199
eval { $dbi->execute('drop table book') };
200
$dbi->execute("create table book (title, author)");
201
$model = $dbi->model('book');
202
$model->insert({title => 'a', author => 'b'});
203
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
204
$dbi->execute("create table company (name)");
205
$model = $dbi->model('company');
206
$model->insert({name => 'a'});
207
is_deeply($model->list->all, [{name => 'a'}], 'basic');
test cleanup
Yuki Kimoto authored on 2011-08-10
208

            
test cleanup
Yuki Kimoto authored on 2011-08-10
209
$dbi = MyDBI5->connect;
210
eval { $dbi->execute('drop table company') };
211
eval { $dbi->execute('drop table table1') };
212
$dbi->execute("create table company (name)");
213
$dbi->execute("create table table1 (key1)");
214
$model = $dbi->model('company');
215
$model->insert({name => 'a'});
216
is_deeply($model->list->all, [{name => 'a'}], 'include all model');
217
$dbi->insert(table => 'table1', param => {key1 => 1});
218
$model = $dbi->model('book');
219
is_deeply($model->list->all, [{key1 => 1}], 'include all model');
test cleanup
Yuki Kimoto authored on 2011-08-10
220

            
test cleanup
Yuki Kimoto authored on 2011-08-10
221
test 'primary_key';
222
use MyDBI1;
223
$dbi = MyDBI1->connect;
224
$model = $dbi->model('book');
225
$model->primary_key(['id', 'number']);
226
is_deeply($model->primary_key, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
227

            
test cleanup
Yuki Kimoto authored on 2011-08-10
228
test 'columns';
229
use MyDBI1;
230
$dbi = MyDBI1->connect;
231
$model = $dbi->model('book');
232
$model->columns(['id', 'number']);
233
is_deeply($model->columns, ['id', 'number']);
test cleanup
Yuki Kimoto authored on 2011-08-10
234

            
test cleanup
Yuki Kimoto authored on 2011-08-10
235
test 'setup_model';
236
use MyDBI1;
237
$dbi = MyDBI1->connect;
238
eval { $dbi->execute('drop table book') };
239
eval { $dbi->execute('drop table company') };
240
eval { $dbi->execute('drop table test') };
test cleanup
Yuki Kimoto authored on 2011-08-10
241

            
test cleanup
Yuki Kimoto authored on 2011-08-10
242
$dbi->execute('create table book (id)');
243
$dbi->execute('create table company (id, name);');
test cleanup
Yuki Kimoto authored on 2011-08-10
244
$dbi->execute('create table test (id, name);');
test cleanup
Yuki Kimoto authored on 2011-08-10
245
$dbi->setup_model;
246
is_deeply($dbi->model('book')->columns, ['id']);
247
is_deeply($dbi->model('company')->columns, ['id', 'name']);
test cleanup
Yuki Kimoto authored on 2011-08-10
248

            
249

            
250

            
251

            
252

            
test cleanup
Yuki Kimoto authored on 2011-08-10
253
### SQLite only test
254
test 'prefix';
255
$dbi = DBIx::Custom->connect;
256
eval { $dbi->execute('drop table table1') };
257
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));');
258
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
259
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace');
260
$result = $dbi->execute('select * from table1;');
261
$rows   = $result->all;
262
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
263

            
test cleanup
Yuki Kimoto authored on 2011-08-10
264
$dbi = DBIx::Custom->connect;
265
eval { $dbi->execute('drop table table1') };
266
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));');
267
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
268
$dbi->update(table => 'table1', param => {key2 => 4},
269
  where => {key1 => 1}, prefix => 'or replace');
270
$result = $dbi->execute('select * from table1;');
271
$rows   = $result->all;
272
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
test cleanup
Yuki Kimoto authored on 2011-08-10
273

            
274

            
test cleanup
Yuki Kimoto authored on 2011-08-10
275
test 'quote';
276
$dbi = DBIx::Custom->connect;
277
$dbi->quote('"');
278
eval { $dbi->execute("drop table ${q}table$p") };
279
$dbi->execute($create_table_reserved);
280
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
281
$dbi->insert(table => 'table', param => {select => 1});
282
$dbi->delete(table => 'table', where => {select => 1});
283
$result = $dbi->execute("select * from ${q}table$p");
284
$rows   = $result->all;
285
is_deeply($rows, [], "reserved word");
286

            
test cleanup
Yuki Kimoto authored on 2011-08-10
287
test 'finish statement handle';
288
$dbi = DBIx::Custom->connect;
289
$dbi->execute($create_table1);
290
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
291
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
292

            
293
$result = $dbi->select(table => 'table1');
294
$row = $result->fetch_first;
295
is_deeply($row, [1, 2], "row");
296
$row = $result->fetch;
297
ok(!$row, "finished");
298

            
299
$result = $dbi->select(table => 'table1');
300
$row = $result->fetch_hash_first;
301
is_deeply($row, {key1 => 1, key2 => 2}, "row");
302
$row = $result->fetch_hash;
303
ok(!$row, "finished");
304

            
305
$dbi->execute('create table table2 (key1, key2);');
306
$result = $dbi->select(table => 'table2');
307
$row = $result->fetch_hash_first;
308
ok(!$row, "no row fetch");
309

            
310
$dbi = DBIx::Custom->connect;
311
eval { $dbi->execute('drop table table1') };
312
$dbi->execute($create_table1);
313
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
314
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1');
315
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1');
316
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1');
317
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1');
318
$result = $dbi->select(table => 'table1');
319
$rows = $result->fetch_multi(2);
320
is_deeply($rows, [[1, 2],
321
                  [3, 4]], "fetch_multi first");
322
$rows = $result->fetch_multi(2);
323
is_deeply($rows, [[5, 6],
324
                  [7, 8]], "fetch_multi secound");
325
$rows = $result->fetch_multi(2);
326
is_deeply($rows, [[9, 10]], "fetch_multi third");
327
$rows = $result->fetch_multi(2);
328
ok(!$rows);
329

            
330
$result = $dbi->select(table => 'table1');
331
eval {$result->fetch_multi};
332
like($@, qr/Row count must be specified/, "Not specified row count");
333

            
334
$result = $dbi->select(table => 'table1');
335
$rows = $result->fetch_hash_multi(2);
336
is_deeply($rows, [{key1 => 1, key2 => 2},
337
                  {key1 => 3, key2 => 4}], "fetch_multi first");
338
$rows = $result->fetch_hash_multi(2);
339
is_deeply($rows, [{key1 => 5, key2 => 6},
340
                  {key1 => 7, key2 => 8}], "fetch_multi secound");
341
$rows = $result->fetch_hash_multi(2);
342
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third");
343
$rows = $result->fetch_hash_multi(2);
344
ok(!$rows);
345

            
346
$result = $dbi->select(table => 'table1');
347
eval {$result->fetch_hash_multi};
348
like($@, qr/Row count must be specified/, "Not specified row count");
349

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
351
test 'type option'; # DEPRECATED!
352
$dbi = DBIx::Custom->connect(
353
    data_source => 'dbi:SQLite:dbname=:memory:',
354
    dbi_option => {
355
        $DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1)
356
    }
357
);
358
$binary = pack("I3", 1, 2, 3);
359
eval { $dbi->execute('drop table table1') };
360
$dbi->execute('create table table1(key1, key2)');
361
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]);
362
$result = $dbi->select(table => 'table1');
363
$row   = $result->one;
364
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic");
365
$result = $dbi->execute('select length(key1) as key1_length from table1');
366
$row = $result->one;
367
is($row->{key1_length}, length $binary);
368

            
369
test 'type_rule from';
370
$dbi = DBIx::Custom->connect;
371
$dbi->type_rule(
372
    from1 => {
373
        date => sub { uc $_[0] }
374
    }
375
);
376
$dbi->execute("create table table1 (key1 Date, key2 datetime)");
377
$dbi->insert({key1 => 'a'}, table => 'table1');
378
$result = $dbi->select(table => 'table1');
379
is($result->fetch_first->[0], 'A');
380

            
381
$result = $dbi->select(table => 'table1');
382
is($result->one->{key1}, 'A');
383

            
test cleanup
Yuki Kimoto authored on 2011-08-10
384
# DEPRECATED! test
385
test 'filter __ expression';
386
$dbi = DBIx::Custom->connect;
387
eval { $dbi->execute('drop table company') };
388
eval { $dbi->execute('drop table location') };
389
$dbi->execute('create table company (id, name, location_id)');
390
$dbi->execute('create table location (id, name)');
391
$dbi->apply_filter('location',
392
  name => {in => sub { uc $_[0] } }
393
);
394

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

            
398
$result = $dbi->select(
399
    table => ['company', 'location'], relation => {'company.location_id' => 'location.id'},
400
    column => ['location.name as location__name']
401
);
402
is($result->fetch_first->[0], 'B');
403

            
404
$result = $dbi->select(
405
    table => 'company', relation => {'company.location_id' => 'location.id'},
406
    column => ['location.name as location__name']
407
);
408
is($result->fetch_first->[0], 'B');
409

            
410
$result = $dbi->select(
411
    table => 'company', relation => {'company.location_id' => 'location.id'},
412
    column => ['location.name as "location.name"']
413
);
414
is($result->fetch_first->[0], 'B');
test cleanup
Yuki Kimoto authored on 2011-08-10
415

            
416
test 'reserved_word_quote';
417
$dbi = DBIx::Custom->connect;
418
eval { $dbi->execute("drop table ${q}table$p") };
419
$dbi->reserved_word_quote('"');
420
$dbi->execute($create_table_reserved);
421
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
422
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
423
$dbi->insert(table => 'table', param => {select => 1});
424
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
425
$result = $dbi->execute("select * from ${q}table$p");
426
$rows   = $result->all;
427
is_deeply($rows, [{select => 2, update => 6}], "reserved word");