Showing 1 changed files with 184 additions and 159 deletions
+184 -159
t/sqlite.t
... ...
@@ -70,141 +70,6 @@ my $binary;
70 70
 # Prepare table
71 71
 $dbi = DBIx::Custom->connect;
72 72
 
73
-test 'Model class';
74
-use MyDBI1;
75
-$dbi = MyDBI1->connect;
76
-eval { $dbi->execute('drop table book') };
77
-$dbi->execute("create table book (title, author)");
78
-$model = $dbi->model('book');
79
-$model->insert({title => 'a', author => 'b'});
80
-is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
81
-$dbi->execute("create table company (name)");
82
-$model = $dbi->model('company');
83
-$model->insert({name => 'a'});
84
-is_deeply($model->list->all, [{name => 'a'}], 'basic');
85
-is($dbi->models->{'book'}, $dbi->model('book'));
86
-is($dbi->models->{'company'}, $dbi->model('company'));
87
-
88
-{
89
-    package MyDBI4;
90
-
91
-    use strict;
92
-    use warnings;
93
-
94
-    use base 'DBIx::Custom';
95
-
96
-    sub connect {
97
-        my $self = shift->SUPER::connect(@_);
98
-        
99
-        $self->include_model(
100
-            MyModel2 => [
101
-                'book',
102
-                {class => 'Company', name => 'company'}
103
-            ]
104
-        );
105
-    }
106
-
107
-    package MyModel2::Base1;
108
-
109
-    use strict;
110
-    use warnings;
111
-
112
-    use base 'DBIx::Custom::Model';
113
-
114
-    package MyModel2::book;
115
-
116
-    use strict;
117
-    use warnings;
118
-
119
-    use base 'MyModel2::Base1';
120
-
121
-    sub insert {
122
-        my ($self, $param) = @_;
123
-        
124
-        return $self->SUPER::insert(param => $param);
125
-    }
126
-
127
-    sub list { shift->select; }
128
-
129
-    package MyModel2::Company;
130
-
131
-    use strict;
132
-    use warnings;
133
-
134
-    use base 'MyModel2::Base1';
135
-
136
-    sub insert {
137
-        my ($self, $param) = @_;
138
-        
139
-        return $self->SUPER::insert(param => $param);
140
-    }
141
-
142
-    sub list { shift->select; }
143
-}
144
-$dbi = MyDBI4->connect;
145
-eval { $dbi->execute('drop table book') };
146
-$dbi->execute("create table book (title, author)");
147
-$model = $dbi->model('book');
148
-$model->insert({title => 'a', author => 'b'});
149
-is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
150
-$dbi->execute("create table company (name)");
151
-$model = $dbi->model('company');
152
-$model->insert({name => 'a'});
153
-is_deeply($model->list->all, [{name => 'a'}], 'basic');
154
-
155
-{
156
-     package MyDBI5;
157
-
158
-    use strict;
159
-    use warnings;
160
-
161
-    use base 'DBIx::Custom';
162
-
163
-    sub connect {
164
-        my $self = shift->SUPER::connect(@_);
165
-        
166
-        $self->include_model('MyModel4');
167
-    }
168
-}
169
-$dbi = MyDBI5->connect;
170
-eval { $dbi->execute('drop table company') };
171
-eval { $dbi->execute('drop table table1') };
172
-$dbi->execute("create table company (name)");
173
-$dbi->execute("create table table1 (key1)");
174
-$model = $dbi->model('company');
175
-$model->insert({name => 'a'});
176
-is_deeply($model->list->all, [{name => 'a'}], 'include all model');
177
-$dbi->insert(table => 'table1', param => {key1 => 1});
178
-$model = $dbi->model('book');
179
-is_deeply($model->list->all, [{key1 => 1}], 'include all model');
180
-
181
-test 'primary_key';
182
-use MyDBI1;
183
-$dbi = MyDBI1->connect;
184
-$model = $dbi->model('book');
185
-$model->primary_key(['id', 'number']);
186
-is_deeply($model->primary_key, ['id', 'number']);
187
-
188
-test 'columns';
189
-use MyDBI1;
190
-$dbi = MyDBI1->connect;
191
-$model = $dbi->model('book');
192
-$model->columns(['id', 'number']);
193
-is_deeply($model->columns, ['id', 'number']);
194
-
195
-test 'setup_model';
196
-use MyDBI1;
197
-$dbi = MyDBI1->connect;
198
-eval { $dbi->execute('drop table book') };
199
-eval { $dbi->execute('drop table company') };
200
-eval { $dbi->execute('drop table test') };
201
-
202
-$dbi->execute('create table book (id)');
203
-$dbi->execute('create table company (id, name);');
204
-$dbi->execute('create table test (id, name, primary key (id, name));');
205
-$dbi->setup_model;
206
-is_deeply($dbi->model('book')->columns, ['id']);
207
-is_deeply($dbi->model('company')->columns, ['id', 'name']);
208 73
 
209 74
 test 'delete_at';
210 75
 $dbi = DBIx::Custom->connect;
... ...
@@ -1940,7 +1805,33 @@ $source = "a {= {}";
1940 1805
 eval{$builder->build_query($source)};
1941 1806
 like($@, qr/unexpected "{"/, "error : 2");
1942 1807
 
1943
-### SQLite test
1808
+
1809
+
1810
+
1811
+
1812
+
1813
+
1814
+
1815
+
1816
+
1817
+
1818
+
1819
+
1820
+
1821
+
1822
+
1823
+
1824
+
1825
+
1826
+
1827
+
1828
+
1829
+
1830
+
1831
+
1832
+
1833
+
1834
+### a little complex test
1944 1835
 test 'type option'; # DEPRECATED!
1945 1836
 $dbi = DBIx::Custom->connect(
1946 1837
     data_source => 'dbi:SQLite:dbname=:memory:',
... ...
@@ -2375,39 +2266,141 @@ $result = $dbi->execute('select * from table1;');
2375 2266
 $rows   = $result->all;
2376 2267
 is_deeply($rows, [{key1 => 1, key2 => 4}], "basic");
2377 2268
 
2269
+test 'Model class';
2270
+use MyDBI1;
2271
+$dbi = MyDBI1->connect;
2272
+eval { $dbi->execute('drop table book') };
2273
+$dbi->execute("create table book (title, author)");
2274
+$model = $dbi->model('book');
2275
+$model->insert({title => 'a', author => 'b'});
2276
+is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
2277
+$dbi->execute("create table company (name)");
2278
+$model = $dbi->model('company');
2279
+$model->insert({name => 'a'});
2280
+is_deeply($model->list->all, [{name => 'a'}], 'basic');
2281
+is($dbi->models->{'book'}, $dbi->model('book'));
2282
+is($dbi->models->{'company'}, $dbi->model('company'));
2283
+
2284
+{
2285
+    package MyDBI4;
2378 2286
 
2379
-test 'reserved_word_quote';
2380
-$dbi = DBIx::Custom->connect;
2381
-eval { $dbi->execute("drop table ${q}table$p") };
2382
-$dbi->reserved_word_quote('"');
2383
-$dbi->execute($create_table_reserved);
2384
-$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2385
-$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
2386
-$dbi->insert(table => 'table', param => {select => 1});
2387
-$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
2388
-$result = $dbi->execute("select * from ${q}table$p");
2389
-$rows   = $result->all;
2390
-is_deeply($rows, [{select => 2, update => 6}], "reserved word");
2287
+    use strict;
2288
+    use warnings;
2391 2289
 
2392
-test 'quote';
2393
-$dbi = DBIx::Custom->connect;
2394
-$dbi->quote('"');
2395
-eval { $dbi->execute("drop table ${q}table$p") };
2396
-$dbi->execute($create_table_reserved);
2397
-$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2398
-$dbi->insert(table => 'table', param => {select => 1});
2399
-$dbi->delete(table => 'table', where => {select => 1});
2400
-$result = $dbi->execute("select * from ${q}table$p");
2401
-$rows   = $result->all;
2402
-is_deeply($rows, [], "reserved word");
2290
+    use base 'DBIx::Custom';
2291
+
2292
+    sub connect {
2293
+        my $self = shift->SUPER::connect(@_);
2294
+        
2295
+        $self->include_model(
2296
+            MyModel2 => [
2297
+                'book',
2298
+                {class => 'Company', name => 'company'}
2299
+            ]
2300
+        );
2301
+    }
2302
+
2303
+    package MyModel2::Base1;
2304
+
2305
+    use strict;
2306
+    use warnings;
2307
+
2308
+    use base 'DBIx::Custom::Model';
2309
+
2310
+    package MyModel2::book;
2311
+
2312
+    use strict;
2313
+    use warnings;
2314
+
2315
+    use base 'MyModel2::Base1';
2316
+
2317
+    sub insert {
2318
+        my ($self, $param) = @_;
2319
+        
2320
+        return $self->SUPER::insert(param => $param);
2321
+    }
2322
+
2323
+    sub list { shift->select; }
2324
+
2325
+    package MyModel2::Company;
2326
+
2327
+    use strict;
2328
+    use warnings;
2329
+
2330
+    use base 'MyModel2::Base1';
2331
+
2332
+    sub insert {
2333
+        my ($self, $param) = @_;
2334
+        
2335
+        return $self->SUPER::insert(param => $param);
2336
+    }
2337
+
2338
+    sub list { shift->select; }
2339
+}
2340
+$dbi = MyDBI4->connect;
2341
+eval { $dbi->execute('drop table book') };
2342
+$dbi->execute("create table book (title, author)");
2343
+$model = $dbi->model('book');
2344
+$model->insert({title => 'a', author => 'b'});
2345
+is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic');
2346
+$dbi->execute("create table company (name)");
2347
+$model = $dbi->model('company');
2348
+$model->insert({name => 'a'});
2349
+is_deeply($model->list->all, [{name => 'a'}], 'basic');
2403 2350
 
2351
+{
2352
+     package MyDBI5;
2404 2353
 
2354
+    use strict;
2355
+    use warnings;
2405 2356
 
2357
+    use base 'DBIx::Custom';
2406 2358
 
2359
+    sub connect {
2360
+        my $self = shift->SUPER::connect(@_);
2361
+        
2362
+        $self->include_model('MyModel4');
2363
+    }
2364
+}
2365
+$dbi = MyDBI5->connect;
2366
+eval { $dbi->execute('drop table company') };
2367
+eval { $dbi->execute('drop table table1') };
2368
+$dbi->execute("create table company (name)");
2369
+$dbi->execute("create table table1 (key1)");
2370
+$model = $dbi->model('company');
2371
+$model->insert({name => 'a'});
2372
+is_deeply($model->list->all, [{name => 'a'}], 'include all model');
2373
+$dbi->insert(table => 'table1', param => {key1 => 1});
2374
+$model = $dbi->model('book');
2375
+is_deeply($model->list->all, [{key1 => 1}], 'include all model');
2407 2376
 
2377
+test 'primary_key';
2378
+use MyDBI1;
2379
+$dbi = MyDBI1->connect;
2380
+$model = $dbi->model('book');
2381
+$model->primary_key(['id', 'number']);
2382
+is_deeply($model->primary_key, ['id', 'number']);
2408 2383
 
2384
+test 'columns';
2385
+use MyDBI1;
2386
+$dbi = MyDBI1->connect;
2387
+$model = $dbi->model('book');
2388
+$model->columns(['id', 'number']);
2389
+is_deeply($model->columns, ['id', 'number']);
2409 2390
 
2391
+test 'setup_model';
2392
+use MyDBI1;
2393
+$dbi = MyDBI1->connect;
2394
+eval { $dbi->execute('drop table book') };
2395
+eval { $dbi->execute('drop table company') };
2396
+eval { $dbi->execute('drop table test') };
2410 2397
 
2398
+$dbi->execute('create table book (id)');
2399
+$dbi->execute('create table company (id, name);');
2400
+$dbi->execute('create table test (id, name, primary key (id, name));');
2401
+$dbi->setup_model;
2402
+is_deeply($dbi->model('book')->columns, ['id']);
2403
+is_deeply($dbi->model('company')->columns, ['id', 'name']);
2411 2404
 
2412 2405
 
2413 2406
 
... ...
@@ -2422,6 +2415,25 @@ is_deeply($rows, [], "reserved word");
2422 2415
 
2423 2416
 
2424 2417
 
2418
+### SQLite only test
2419
+test 'quote';
2420
+$dbi = DBIx::Custom->connect;
2421
+$dbi->quote('"');
2422
+eval { $dbi->execute("drop table ${q}table$p") };
2423
+$dbi->execute($create_table_reserved);
2424
+$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2425
+$dbi->insert(table => 'table', param => {select => 1});
2426
+$dbi->delete(table => 'table', where => {select => 1});
2427
+$result = $dbi->execute("select * from ${q}table$p");
2428
+$rows   = $result->all;
2429
+is_deeply($rows, [], "reserved word");
2430
+
2431
+
2432
+
2433
+
2434
+
2435
+
2436
+
2425 2437
 
2426 2438
 
2427 2439
 
... ...
@@ -2463,3 +2475,16 @@ $result = $dbi->select(
2463 2475
     column => ['location.name as "location.name"']
2464 2476
 );
2465 2477
 is($result->fetch_first->[0], 'B');
2478
+
2479
+test 'reserved_word_quote';
2480
+$dbi = DBIx::Custom->connect;
2481
+eval { $dbi->execute("drop table ${q}table$p") };
2482
+$dbi->reserved_word_quote('"');
2483
+$dbi->execute($create_table_reserved);
2484
+$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}});
2485
+$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}});
2486
+$dbi->insert(table => 'table', param => {select => 1});
2487
+$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2});
2488
+$result = $dbi->execute("select * from ${q}table$p");
2489
+$rows   = $result->all;
2490
+is_deeply($rows, [{select => 2, update => 6}], "reserved word");