root/lang/perl/Data-Model/trunk/t/lib/Mock/Tests/Basic.pm @ 29539

Revision 29539, 13.1 kB (checked in by yappo, 6 years ago)

add multi key test case for lookup/lookup_multi

Line 
1package Mock::Tests::Basic;
2use t::Utils;
3use base 'Test::Class';
4use Mock::Tests;
5use Test::More;
6
7sub t_01_basic : Tests {
8    my $ret1 = mock->set( user => 'yappo', { name => 'Kazuhiro Osawa' } );
9    isa_ok $ret1, mock_class."::user";
10    is $ret1->id, 'yappo';
11    is $ret1->name, 'Kazuhiro Osawa';
12
13    my($ret2) = mock->get( user => 'yappo' );
14    isa_ok $ret2, mock_class."::user";
15    is $ret2->id, 'yappo';
16    is $ret2->name, 'Kazuhiro Osawa';
17
18    ok mock->delete( user => 'yappo' ), 'delete ok';
19    ($ret2) = mock->get( user => 'yappo' );
20    ok !$ret2, 'get error';
21    ok !mock->delete( user => 'yappo' ), 'delete error';
22}
23
24sub t_02_insert_bookmark_user : Tests {
25    my $ret1 = mock->set( bookmark_user => [qw/ 1 yappo /] );
26    isa_ok $ret1, mock_class."::bookmark_user";
27    is $ret1->bookmark_id, 1, 'bookmark_id';
28    is $ret1->user_id, 'yappo';
29
30    $ret1 = mock->set( bookmark_user => [qw/ 1 lopnor /] );
31    is $ret1->bookmark_id, 1;
32    is $ret1->user_id, 'lopnor';
33
34    $ret1 = mock->set( bookmark_user => [qw/ 2 yappo /] );
35    is $ret1->bookmark_id, 2;
36    is $ret1->user_id, 'yappo';
37
38    $ret1 = mock->set( bookmark_user => [qw/ 2 lopnor /] );
39    is $ret1->bookmark_id, 2;
40    is $ret1->user_id, 'lopnor';
41}
42
43sub t_03_get : Tests {
44    my($ret2) = mock->get( bookmark_user => [qw/ 1 yappo /] );
45    isa_ok $ret2, mock_class."::bookmark_user";
46    is $ret2->bookmark_id, 1;
47    is $ret2->user_id, 'yappo';
48}
49       
50sub t_03_order : Tests {
51    my($ret3) = mock->get( bookmark_user => '1', { order => [ { user_id => 'DESC' } ] } );
52    isa_ok $ret3, mock_class."::bookmark_user";
53    is $ret3->bookmark_id, 1;
54    is $ret3->user_id, 'yappo';
55}
56
57sub t_03_index : Tests {
58    my($ret4) = mock->get( bookmark_user => {
59        index => { user_id => 'lopnor' },
60        order => [{ bookmark_id => 'DESC' }],
61    });
62    isa_ok $ret4, mock_class."::bookmark_user";
63    is $ret4->bookmark_id, 2;
64    is $ret4->user_id, 'lopnor';
65}
66   
67sub t_04_delete : Tests {
68    ok mock->delete( bookmark_user => [qw/ 1 yappo /] ), 'delete bookmark_user';
69    ok !mock->get( bookmark_user => [qw/ 1 yappo /] ), 'get error bookmark_user';
70    ok !mock->delete( bookmark_user => [qw/ 1 yappo /] ), 'delete error bookmark_user';
71}
72
73sub t_05_select_all_iterator : Tests(5) {
74    my $itr = mock->get('bookmark_user');
75    isa_ok $itr, 'Data::Model::Iterator';
76    my $i = 0;
77    while (my $row = $itr->next) {
78        $i++;
79        isa_ok $row, mock_class."::bookmark_user";
80    }
81    is $i, 3;
82}
83
84sub t_05_select_all_iterator_with_reset : Tests(8) {
85    my $itr = mock->get('bookmark_user');
86    isa_ok $itr, 'Data::Model::Iterator';
87    my $i = 0;
88    while (my $row = $itr->next) {
89        $i++;
90        isa_ok $row, mock_class."::bookmark_user";
91    }
92    $itr->reset;
93    while (my $row = $itr->next) {
94        $i++;
95        isa_ok $row, mock_class."::bookmark_user";
96    }
97    is $i, 6;
98}
99
100sub t_05_select_all_iterator_limit : Tests(4) {
101    my $itr = mock->get('bookmark_user', { limit => 2 });
102    isa_ok $itr, 'Data::Model::Iterator';
103    my $i = 0;
104    while (my $row = $itr->next) {
105        $i++;
106        isa_ok $row, mock_class."::bookmark_user";
107    }
108    is $i, 2;
109}
110
111sub t_05_select_all_iterator_limit_offset : Tests(3) {
112    my $itr = mock->get('bookmark_user', { limit => 1, offset => 2 });
113    isa_ok $itr, 'Data::Model::Iterator';
114    my $i = 0;
115    while (my $row = $itr->next) {
116        $i++;
117        isa_ok $row, mock_class."::bookmark_user";
118    }
119    is $i, 1;
120}
121
122sub t_06_update : Tests {
123    my($set) = mock->set( user => 'yappo' => { name => '-' } );
124    is $set->name, '-', 'is -';
125    my($obj) = mock->get( user => 'yappo' );
126
127    $obj->name('Kazuhiro Osawa');
128    $obj->update;
129    my($obj2) = mock->get( user => 'yappo' );
130    is $obj2->name, 'Kazuhiro Osawa', 'is Kazuhiro Osawa';
131
132    $obj->name('Kazuhiro');
133    mock->set($obj);
134    my($obj3) = mock->get( user => 'yappo' );
135    is $obj3->name, 'Kazuhiro', 'is Kazuhiro';
136
137    $obj->name('Kazuhiro Osawa');
138    mock->replace($obj);
139    my($obj4) = mock->get( user => 'yappo' );
140    is $obj4->name, 'Kazuhiro Osawa', 'is Kazuhiro Osawa';
141
142
143    $obj->name('Osawa');
144    mock->replace($obj);
145    my($obj5) = mock->get( user => 'yappo' );
146    is $obj5->name, 'Osawa', 'is Osawa';
147}
148
149sub t_06_update_2ndidx : Tests {
150    my $set1 = mock->set( bookmark_user => [qw/ 10 jyappo /] );
151    isa_ok $set1, mock_class."::bookmark_user";
152    my $set2 = mock->set( bookmark_user => [qw/ 11 jyappo /] );
153    isa_ok $set2, mock_class."::bookmark_user";
154
155    my $row;
156    my $it = mock->get( bookmark_user => { index => { user_id => 'jyappo' }, order => [{ bookmark_id => 'ASC' }] } );
157    $row = $it->next;
158    isa_ok $row, mock_class."::bookmark_user";
159    is $row->bookmark_id, 10, '10 jyappo';
160    is $row->user_id, 'jyappo', '10 jyappo';
161    $row = $it->next;
162    isa_ok $row, mock_class."::bookmark_user";
163    is $row->bookmark_id, 11, '11 jyappo';
164    is $row->user_id, 'jyappo', '11 jyappo';
165    ok !$it->next;
166
167    $row->user_id('iyappo');
168    $row->update;
169
170    $it = mock->get( bookmark_user => { index => { user_id => 'jyappo' }, order => [{ bookmark_id => 'ASC' }] } );
171    $row = $it->next;
172    isa_ok $row, mock_class."::bookmark_user";
173    is $row->bookmark_id, 10, '10 jyappo';
174    is $row->user_id, 'jyappo', '10 jyappo';
175    ok !$it->next;
176
177    $it = mock->get( bookmark_user => { index => { user_id => 'iyappo' }, order => [{ bookmark_id => 'ASC' }] } );
178    $row = $it->next;
179    isa_ok $row, mock_class."::bookmark_user";
180    is $row->bookmark_id, 11, '11 iyappo';
181    is $row->user_id, 'iyappo', '11 iyappo';
182    ok !$it->next;
183}
184
185sub t_07_replace : Tests {
186    my $set1  = mock->set( user => 'yappologs' => { name => 'blog' } );
187    is $set1->name, 'blog', 'is blog';
188    my($obj1) = mock->get( user => 'yappologs' );
189    is $obj1->name, 'blog', 'is blog';
190
191    my $set2  = mock->replace( user => 'yappologs' => { name => "yappo's blog" } );
192    is $set2->name, "yappo's blog", "is yappo's blog";
193    my($obj2) = mock->get( user => 'yappologs' );
194    is $obj2->name, "yappo's blog", "is yappo's blog";
195}
196
197sub t_08_autoincrement : Tests {
198    my $set1 = mock->set( bookmark => { url => 'url1' });
199    is $set1->id, 1, 'set id1';
200    is $set1->url, 'url1';
201
202    my $set2 = mock->set( bookmark => { url => 'url2' });
203    is $set2->id, 2, 'set id2';
204    is $set2->url, 'url2';
205
206    my $set3 = mock->set( bookmark => { url => 'url3' });
207    is $set3->id, 3, 'set id3';
208    is $set3->url, 'url3';
209
210
211    my($key1) = mock->get( bookmark => 1 );
212    is $key1->id, 1, 'key id1';
213    is $key1->url, 'url1';
214
215    my($key2) = mock->get( bookmark => 2 );
216    is $key2->id, 2, 'key id2';
217    is $key2->url, 'url2';
218
219    my($key3) = mock->get( bookmark => 3 );
220    is $key3->id, 3, 'key id3';
221    is $key3->url, 'url3';
222
223
224    my($idx1) = mock->get( bookmark => { index => { url => 'url1' } } );
225    is $idx1->id, 1, 'idx id1';
226    is $idx1->url, 'url1';
227
228    my($idx2) = mock->get( bookmark => { index => { url => 'url2' } } );
229    is $idx2->id, 2, 'idx id2';
230    is $idx2->url, 'url2';
231
232    my($idx3) = mock->get( bookmark => { index => { url => 'url3' } } );
233    is $idx3->id, 3, 'idx id3';
234    is $idx3->url, 'url3';
235}
236
237sub t_09_get_delete : Tests {
238    my $set = mock->set( user => 'select-delete', { name => 'Kazuhiro Osawa' } );
239    isa_ok $set, mock_class."::user";
240
241    my($get) = mock->get( user => 'select-delete' );
242    isa_ok $get, mock_class."::user";
243    ok($get->delete, 'delete by row');
244
245    ok(!mock->get( user => 'select-delete' ));
246}
247
248sub t_10_direct_update : Tests {
249    my $set1 = mock->set( user => 'direct_update-1', { name => 'direct_update 1' } );
250    isa_ok $set1, mock_class."::user";
251    my $set2 = mock->set( user => 'direct_update-2', { name => 'direct_update 2' } );
252    isa_ok $set2, mock_class."::user";
253
254    my($get1) = mock->get( user => 'direct_update-1' );
255    isa_ok $get1, mock_class."::user";
256    is $get1->id, 'direct_update-1';
257    is $get1->name, 'direct_update 1';
258    my($get2) = mock->get( user => 'direct_update-2' );
259    isa_ok $get2, mock_class."::user";
260    is $get2->id, 'direct_update-2';
261    is $get2->name, 'direct_update 2';
262
263    ok mock->update(
264        user => 'direct_update-1',
265        undef, +{
266            name => 'updated direct_update 1',
267        },
268    ), 'update 1';
269    my($get3) = mock->get( user => 'direct_update-1' );
270    isa_ok $get3, mock_class."::user";
271    is $get3->id, 'direct_update-1';
272    is $get3->name, 'updated direct_update 1';
273
274    ok mock->update(
275        user => ['direct_update-1'],
276        undef, +{
277            name => 'updated 2 direct_update 1',
278        },
279    ), 'update 2';
280    my($get4) = mock->get( user => 'direct_update-1' );
281    isa_ok $get4, mock_class."::user";
282    is $get4->id, 'direct_update-1';
283    is $get4->name, 'updated 2 direct_update 1';
284
285    my($get5) = mock->get( user => 'direct_update-2' );
286    isa_ok $get5, mock_class."::user";
287    is $get5->id, 'direct_update-2';
288    is $get5->name, 'direct_update 2';
289
290
291    ok mock->update(
292        user => +{
293            where => [
294                name => { LIKE => '%2' },
295            ],
296        }, +{
297            name => 'updated direct_update 2',
298        },
299    ), 'update 3';
300    my($get6) = mock->get( user => 'direct_update-1' );
301    isa_ok $get6, mock_class."::user";
302    is $get6->id, 'direct_update-1';
303    is $get6->name, 'updated 2 direct_update 1';
304    my($get7) = mock->get( user => 'direct_update-2' );
305    isa_ok $get7, mock_class."::user";
306    is $get7->id, 'direct_update-2';
307    is $get7->name, 'updated direct_update 2';
308
309
310    ok mock->update(
311        user => ['direct_update-1'],
312        undef, +{
313            id   => 'direct_update-3',
314            name => 'direct_update 3',
315        },
316    ), 'update 4';
317    ok(!mock->get( user => 'direct_update-1' ));
318    my($get8) = mock->get( user => 'direct_update-3' );
319    isa_ok $get8, mock_class."::user";
320    is $get8->id, 'direct_update-3';
321    is $get8->name, 'direct_update 3';
322    my($get9) = mock->get( user => 'direct_update-2' );
323    isa_ok $get9, mock_class."::user";
324    is $get9->id, 'direct_update-2';
325    is $get9->name, 'updated direct_update 2';
326}
327
328sub t_11_obj_delete : Tests {
329    my $set = mock->set( user => 'obj-delete', { name => 'Kazuhiro Osawa' } );
330    isa_ok $set, mock_class."::user";
331
332    my($get) = mock->get( user => 'obj-delete' );
333    isa_ok $get, mock_class."::user";
334    ok(mock->delete($get), 'mock->delete( $obj )');
335
336    ok(!mock->get( user => 'obj-delete' ));
337}
338
339sub t_12_lookup : Tests {
340    my $lookup = mock->lookup( user => 'yappo' );
341    isa_ok $lookup, mock_class."::user";
342    is $lookup->id, 'yappo', 'id is yappo';
343    is $lookup->name, 'Osawa', 'name is Osawa';
344}
345
346sub t_13_lookup_multi : Tests {
347    my @lookup = mock->lookup_multi( user => [ 'yappo', 'yappologs' ] );
348
349    isa_ok $lookup[0], mock_class."::user";
350    is $lookup[0]->id, 'yappo', 'id is yappo';
351    is $lookup[0]->name, 'Osawa', 'name is Osawa';
352    isa_ok $lookup[1], mock_class."::user";
353    is $lookup[1]->id, 'yappologs', 'id is yappologs';
354    is $lookup[1]->name, "yappo's blog", "name is yappo's blog";
355
356    my @lookup_rev = mock->lookup_multi( user => [ 'yappo', 'yappologs' ] );
357    isa_ok $lookup_rev[0], mock_class."::user";
358    is $lookup_rev[0]->id, 'yappo', 'id is yappo';
359    is $lookup_rev[0]->name, 'Osawa', 'name is Osawa';
360    isa_ok $lookup_rev[1], mock_class."::user";
361    is $lookup_rev[1]->id, 'yappologs', 'id is yappologs';
362    is $lookup_rev[1]->name, "yappo's blog", "name is yappo's blog";
363}
364
365sub t_14_prepere : Test {
366    ok(mock->set( bookmark_user => [qw/ 101 yappo /] ));
367    ok(mock->set( bookmark_user => [qw/ 102 osawa /] ));
368    ok(mock->set( bookmark_user => [qw/ 103 kazuhiro /] ));
369}
370
371sub t_15_lookup_multikey : Tests {
372    eval { mock->lookup( bookmark_user => 'yappo' ) };
373    like $@, qr/The number of key is wrong at /;
374
375    my $lookup;
376    $lookup = mock->lookup( bookmark_user => [qw/ 101 yappo /] );
377    isa_ok $lookup, mock_class."::bookmark_user";
378    is $lookup->bookmark_id, 101, 'id';
379    is $lookup->user_id, 'yappo', 'user_id';
380
381    $lookup = mock->lookup( bookmark_user => [qw/ 102 osawa /] );
382    isa_ok $lookup, mock_class."::bookmark_user";
383    is $lookup->bookmark_id, 102, 'id';
384    is $lookup->user_id, 'osawa', 'user_id';
385
386    $lookup = mock->lookup( bookmark_user => [qw/ 103 kazuhiro /] );
387    isa_ok $lookup, mock_class."::bookmark_user";
388    is $lookup->bookmark_id, 103, 'id';
389    is $lookup->user_id, 'kazuhiro', 'user_id';
390}
391
392sub t_16_lookup_multi_multikey : Tests {
393    eval { mock->lookup_multi( bookmark_user => 'yappo' ) };
394    like $@, qr/The number of key is wrong at /;
395    eval { mock->lookup_multi( bookmark_user => ['yappo'] ) };
396    like $@, qr/The number of key is wrong at /;
397
398    my($lookup) = mock->lookup_multi( bookmark_user => [ [qw/ 101 yappo /] ]);
399    isa_ok $lookup, mock_class."::bookmark_user";
400    is $lookup->bookmark_id, 101, 'id';
401    is $lookup->user_id, 'yappo', 'user_id';
402
403    my @lookup = mock->lookup_multi( bookmark_user => [ [qw/ 102 osawa /], [qw/ 103 kazuhiro /] ] );
404    isa_ok $lookup[0], mock_class."::bookmark_user";
405    is $lookup[0]->bookmark_id, 102, 'id';
406    is $lookup[0]->user_id, 'osawa', 'user_id';
407    isa_ok $lookup[1], mock_class."::bookmark_user";
408    is $lookup[1]->bookmark_id, 103, 'id';
409    is $lookup[1]->user_id, 'kazuhiro', 'user_id';
410}
411
4121;
Note: See TracBrowser for help on using the browser.