File: 01searches.t

package info (click to toggle)
libdbix-searchbuilder-perl 1.82-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 776 kB
  • sloc: perl: 10,608; makefile: 2
file content (617 lines) | stat: -rw-r--r-- 22,789 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#!/usr/bin/perl -w


use strict;
use warnings;
use Test::More;
BEGIN { require "./t/utils.pl" }
our (@AvailableDrivers);

use constant TESTS_PER_DRIVER => 157;

my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
plan tests => $total;

foreach my $d ( @AvailableDrivers ) {
SKIP: {
	unless( has_schema( 'TestApp', $d ) ) {
		skip "No schema for '$d' driver", TESTS_PER_DRIVER;
	}
	unless( should_test( $d ) ) {
		skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
	}

	my $handle = get_handle( $d );
	connect_handle( $handle );
	isa_ok($handle->dbh, 'DBI::db');

	my $ret = init_schema( 'TestApp', $handle );
	isa_ok($ret,'DBI::st', "Inserted the schema. got a statement handle back");

	my $count_all = init_data( 'TestApp::User', $handle );
	ok( $count_all,  "init users data" );

	my $users_obj = TestApp::Users->new( $handle );
	isa_ok( $users_obj, 'DBIx::SearchBuilder' );
	is( $users_obj->_Handle, $handle, "same handle as we used in constructor");

# check that new object returns 0 records in any case
	is( $users_obj->_RecordCount, 0, '_RecordCount returns 0 on not limited obj' );
	is( $users_obj->Count, 0, 'Count returns 0 on not limited obj' );
	is( $users_obj->IsLast, undef, 'IsLast returns undef on not limited obj after Count' );
	is( $users_obj->First, undef, 'First returns undef on not limited obj' );
	is( $users_obj->IsLast, undef, 'IsLast returns undef on not limited obj after First' );
	is( $users_obj->Last, undef, 'Last returns undef on not limited obj' );
	is( $users_obj->IsLast, undef, 'IsLast returns undef on not limited obj after Last' );
	$users_obj->GotoFirstItem;
	is( $users_obj->Next, undef, 'Next returns undef on not limited obj' );
	is( $users_obj->IsLast, undef, 'IsLast returns undef on not limited obj after Next' );
	# XXX TODO FIXME: may be this methods should be implemented
	# $users_obj->GotoLastItem;
	# is( $users_obj->Prev, undef, 'Prev returns undef on not limited obj' );
	my $items_ref = $users_obj->ItemsArrayRef;
	isa_ok( $items_ref, 'ARRAY', 'ItemsArrayRef always returns array reference' );
	is_deeply( $items_ref, [], 'ItemsArrayRef returns [] on not limited obj' );

# unlimit new object and check
	$users_obj->UnLimit;
	is( $users_obj->Count, $count_all, 'Count returns same number of records as was inserted' );
	isa_ok( $users_obj->First, 'DBIx::SearchBuilder::Record', 'First returns record object' );
	isa_ok( $users_obj->Last, 'DBIx::SearchBuilder::Record', 'Last returns record object' );
	$users_obj->GotoFirstItem;
	isa_ok( $users_obj->Next, 'DBIx::SearchBuilder::Record', 'Next returns record object' );
	$items_ref = $users_obj->ItemsArrayRef;
	isa_ok( $items_ref, 'ARRAY', 'ItemsArrayRef always returns array reference' );
	is( scalar @{$items_ref}, $count_all, 'ItemsArrayRef returns same number of records as was inserted' );
	$users_obj->RedoSearch;
	$items_ref = $users_obj->ItemsArrayRef;
	isa_ok( $items_ref, 'ARRAY', 'ItemsArrayRef always returns array reference' );
	is( scalar @{$items_ref}, $count_all, 'ItemsArrayRef returns same number of records as was inserted' );

# try to use $users_obj for all tests, after each call to CleanSlate it should look like new obj.
# and test $obj->new syntax
	my $clean_obj = $users_obj->new( $handle );
	isa_ok( $clean_obj, 'DBIx::SearchBuilder' );

# basic limits
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Login', VALUE => 'obra' );
	is( $users_obj->Count, 1, 'found one user with login obra' );
	TODO: {
		local $TODO = 'require discussion';
		is( $users_obj->IsLast, undef, 'IsLast returns undef before we fetch any record' );
	}
	my $first_rec = $users_obj->First;
	isa_ok( $first_rec, 'DBIx::SearchBuilder::Record', 'First returns record object' );
	is( $users_obj->IsLast, 1, '1 record in the collection then first rec is last');
	is( $first_rec->Login, 'obra', 'login is correct' );
	my $last_rec = $users_obj->Last;
	is( $last_rec, $first_rec, 'Last returns same object as First' );
	is( $users_obj->IsLast, 1, 'IsLast always returns 1 after Last call');
	$users_obj->GotoFirstItem;
	my $next_rec = $users_obj->Next;
	is( $next_rec, $first_rec, 'Next returns same object as First' );
	is( $users_obj->IsLast, 1, 'IsLast returns 1 after fetch first record with Next method');
	is( $users_obj->Next, undef, 'only one record in the collection' );
	TODO: {
		local $TODO = 'require discussion';
		is( $users_obj->IsLast, undef, 'Next returns undef, IsLast returns undef too');
	}
	$items_ref = $users_obj->ItemsArrayRef;
	isa_ok( $items_ref, 'ARRAY', 'ItemsArrayRef always returns array reference' );
	is( scalar @{$items_ref}, 1, 'ItemsArrayRef has only 1 record' );

# similar basic limit, but with different OPERATORS and less First/Next/Last tests
	# LIKE
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Name', OPERATOR => 'LIKE', VALUE => 'Glass' );
	is( $users_obj->Count, 1, "found one user with 'Glass' in the name" );
	$first_rec = $users_obj->First;
	isa_ok( $first_rec, 'DBIx::SearchBuilder::Record', 'First returns record object' );
	is( $first_rec->Login, 'glasser', 'login is correct' );

	# MATCHES
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Name', OPERATOR => 'MATCHES', VALUE => 'lass' );
	is( $users_obj->Count, 0, "found no user matching 'lass' in the name" );

	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Name', OPERATOR => 'MATCHES', VALUE => '%lass' );
	is( $users_obj->Count, 0, "found no user matching '%lass' in the name" );

	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Name', OPERATOR => 'MATCHES', VALUE => 'lass%' );
	is( $users_obj->Count, 0, "found no user matching 'lass%' in the name" );

	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Name', OPERATOR => 'MATCHES', VALUE => '%lass%' );
	is( $users_obj->Count, 1, "found one user matching '%lass%' in the name" );
	$first_rec = $users_obj->First;
	isa_ok( $first_rec, 'DBIx::SearchBuilder::Record', 'First returns record object' );
	is( $first_rec->Login, 'glasser', 'login is correct' );

	# STARTSWITH
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Name', OPERATOR => 'STARTSWITH', VALUE => 'Ruslan' );
	is( $users_obj->Count, 1, "found one user who name starts with 'Ruslan'" );
	$first_rec = $users_obj->First;
	isa_ok( $first_rec, 'DBIx::SearchBuilder::Record', 'First returns record object' );
	is( $first_rec->Login, 'cubic', 'login is correct' );

	# ENDSWITH
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Name', OPERATOR => 'ENDSWITH', VALUE => 'Tang' );
	is( $users_obj->Count, 1, "found one user who name ends with 'Tang'" );
	$first_rec = $users_obj->First;
	isa_ok( $first_rec, 'DBIx::SearchBuilder::Record', 'First returns record object' );
	is( $first_rec->Login, 'autrijus', 'login is correct' );

	# IS NULL
	# XXX TODO FIXME: FIELD => undef should be handled as NULL
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Phone', OPERATOR => 'IS', VALUE => 'NULL' );
	is( $users_obj->Count, 2, "found 2 users who has unknown phone number" );
	
	# IS NOT NULL
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Phone', OPERATOR => 'IS NOT', VALUE => 'NULL', QOUTEVALUE => 0 );
	is( $users_obj->Count, $count_all - 2, "found users who has phone number filled" );

	# IN [...] operator
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Login', OPERATOR => 'IN', VALUE => ['obra', 'cubic'] );
	is( $users_obj->Count, 2, "found two users using IN operator" );
	is_deeply(
        [ sort map $_->Login, @{ $users_obj->ItemsArrayRef } ],
        [ 'cubic', 'obra' ],
        'found correct records',
    );
	$users_obj->CleanSlate;
	$users_obj->Limit( FIELD => 'Login', OPERATOR => 'NOT IN', VALUE => ['obra', 'cubic'] );
	is( $users_obj->Count, 2, "found two users using NOT IN operator" );
	is_deeply(
        [ sort map $_->Login, @{ $users_obj->ItemsArrayRef } ],
        [ 'autrijus', 'glasser' ],
        'found correct records',
    );

	# IN $collection operator
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
    {
        my $tmp = $users_obj->Clone;
        $tmp->Limit( FIELD => 'Login', OPERATOR => 'IN', VALUE => ['obra', 'cubic'] );
        $users_obj->Limit( FIELD => 'id', OPERATOR => 'IN', VALUE => $tmp );
    }
	is( $users_obj->Count, 2, "found two users using IN operator" );
	is_deeply(
        [ sort map $_->Login, @{ $users_obj->ItemsArrayRef } ],
        [ 'cubic', 'obra' ],
        'found correct records',
    );
	$users_obj->CleanSlate;
    {
        my $tmp = $users_obj->Clone;
        $tmp->Limit( FIELD => 'Login', OPERATOR => 'IN', VALUE => ['obra', 'cubic'] );
        $users_obj->Limit( FIELD => 'id', OPERATOR => 'NOT IN', VALUE => $tmp );
    }
	is( $users_obj->Count, 2, "found two users using IN operator" );
	is_deeply(
        [ sort map $_->Login, @{ $users_obj->ItemsArrayRef } ],
        [ 'autrijus', 'glasser' ],
        'found correct records',
    );
	# IN with object and Column preselected
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
    {
        my $tmp = $users_obj->Clone;
        $tmp->Limit( FIELD => 'Login', OPERATOR => 'IN', VALUE => ['obra', 'cubic'] );
        $tmp->Column(FIELD => 'Login');
        $users_obj->Limit( FIELD => 'Login', OPERATOR => 'IN', VALUE => $tmp );
    }
	is( $users_obj->Count, 2, "found two users using IN operator" );
	is_deeply(
        [ sort map $_->Login, @{ $users_obj->ItemsArrayRef } ],
        [ 'cubic', 'obra' ],
        'found correct records',
    );

	# ORDER BY / GROUP BY
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->UnLimit;
	$users_obj->GroupByCols({FIELD => 'Login'});
	$users_obj->OrderBy(FIELD => 'Login', ORDER => 'desc');
	$users_obj->Column(FIELD => 'Login');
	is( $users_obj->Count, $count_all, "group by / order by finds right amount");
	$first_rec = $users_obj->First;
	isa_ok( $first_rec, 'DBIx::SearchBuilder::Record', 'First returns record object' );
	is( $first_rec->Login, 'obra', 'login is correct' );

	$users_obj->CleanSlate;
	TODO: {
        local $TODO = 'we leave order_by after clean slate, fixing this results in many RT failures';
        is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
    }

    {
	    $users_obj = TestApp::Users->new( $handle );
        $users_obj->UnLimit;
        $users_obj->GroupBy({FUNCTION => 'Login'});
        $users_obj->OrderBy(FIELD => 'Login', ORDER => 'desc');
        $users_obj->Column(FIELD => 'Login');
        is( $users_obj->Count, $count_all, "group by / order by finds right amount");
        $first_rec = $users_obj->First;
        isa_ok( $first_rec, 'DBIx::SearchBuilder::Record', 'First returns record object' );
        is( $first_rec->Login, 'obra', 'login is correct' );
    }
    {
	    $users_obj = TestApp::Users->new( $handle );
        $users_obj->UnLimit;
        $users_obj->GroupBy({FUNCTION => 'SUBSTR(Login, 1, 1)', });
        $users_obj->Column(FIELD => 'Login', FUNCTION => 'SUBSTR(Login, 1, 1)');
        my @list = sort map $_->Login, @{ $users_obj->ItemsArrayRef };
        is_deeply( \@list, [qw(a c g o)], 'correct values' );
    }
    {
	    $users_obj = TestApp::Users->new( $handle );
        $users_obj->UnLimit;
        $users_obj->GroupBy({FUNCTION => 'SUBSTR(?, 1, 1)', FIELD => 'Login'});
        $users_obj->Column(FIELD => 'Login', FUNCTION => 'SUBSTR(?, 1, 1)');
        my @list = sort map $_->Login, @{ $users_obj->ItemsArrayRef };
        is_deeply( \@list, [qw(a c g o)], 'correct values' );
    }
    $users_obj = TestApp::Users->new( $handle );

# Let's play a little with ENTRYAGGREGATOR
    # EA defaults to OR for the same field
	$users_obj->Limit( FIELD => 'Phone', OPERATOR => 'IS', VALUE => 'NULL', QOUTEVALUE => 0 );
	$users_obj->Limit( FIELD => 'Phone', OPERATOR => 'LIKE', VALUE => 'X' );
	is( $users_obj->Count, 4, "found users who has no phone or it has X char" );

    # set AND for the same field
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Login', OPERATOR => 'NOT LIKE', VALUE => 'c' );
	$users_obj->Limit(
        ENTRYAGGREGATOR => 'AND', FIELD => 'Login', OPERATOR => 'LIKE', VALUE => 'u'
    );
	is( $users_obj->Count, 1, "found users who has no phone or it has X char" );

    # default is AND for different fields
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
	$users_obj->Limit( FIELD => 'Phone', OPERATOR => 'IS', VALUE => 'NULL', QOUTEVALUE => 0 );
	$users_obj->Limit( FIELD => 'Login', OPERATOR => 'LIKE', VALUE => 'r' );
	is( $users_obj->Count, 2, "found users who has no phone number or login has 'r' char" );

# Let's play with RowsPerPage
    # RowsPerPage(0)
    # https://rt.cpan.org/Ticket/Display.html?id=42988
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
    $users_obj->UnLimit;
    $users_obj->RowsPerPage(0);
	is( $users_obj->Count, $count_all, "found all users" );
    ok( $users_obj->First, "fetched first user" );

    # walk all pages
	$users_obj->CleanSlate;
	is_deeply( $users_obj, $clean_obj, 'after CleanSlate looks like new object');
    $users_obj->UnLimit;
	$users_obj->OrderBy(FIELD => 'Login');
    $users_obj->RowsPerPage(2);
    is($users_obj->Count, 2, 'user count on first page' );
    {
        my %seen;
        my $saw_on_page = 0;
        my $pages = 0;
        my $prev_login = '';
        do {
            $saw_on_page = 0;
            while ( my $user = $users_obj->Next ) {
                $saw_on_page++;
                $seen{ $user->id }++;
                ok( $prev_login lt $user->Login, "order is correct" );
            }
            last unless $saw_on_page;

            $pages++;
            if ( $pages * 2 <= $count_all ) {
                is( $saw_on_page, 2, "saw only two on the page" );
            } else {
                is( $saw_on_page, $count_all - ($pages * 2), "saw slightly less users on the last page");
            }
            $users_obj->NextPage;
        } while ( $saw_on_page );

        ok( !grep( $_ != 1, values %seen ), "saw each user only once") or do { use Data::Dumper; diag Dumper(\%seen) };
        is( scalar keys %seen, $count_all, "saw all users" )
    }

    # two steps forward, on step back
    $users_obj = TestApp::Users->new( $handle );
    $users_obj->UnLimit;
	$users_obj->OrderBy(FIELD => 'Login');
    $users_obj->RowsPerPage(1);
    for ( 1 .. $count_all-1) {
        my $u = $users_obj->Next;
        ok( $u, "got a user");
        ok(!$users_obj->Next, "only on the page");

        $users_obj->NextPage;
        isnt( $users_obj->Next->id, $u->id, "got a user and he is different");
        ok(!$users_obj->Next, "only on the page");

        $users_obj->PrevPage;
        is( $users_obj->Next->id, $u->id, "got a user and he is the same");
        ok(!$users_obj->Next, "only on the page");

        $users_obj->NextPage;
    }

    # tricky variant: skip 1, but show 2
    $users_obj = TestApp::Users->new( $handle );
    $users_obj->UnLimit;
	$users_obj->OrderBy(FIELD => 'Login');
    $users_obj->RowsPerPage(2);
    $users_obj->FirstRow(2);
    {
        my $u = $users_obj->Next;
        is( $u->Login, 'cubic', "cubic is second in the list");
    }
    {
        my $u = $users_obj->Next;
        is( $u->Login, 'glasser', "glasser is third in the list");
    }

# Let's play with Column
    $users_obj = TestApp::Users->new( $handle );
    $users_obj->UnLimit;
    {
        is( $users_obj->Column(FIELD => 'id'), 'id' );
        isnt( my $id_alias = $users_obj->Column(FIELD => 'id'), 'id' );
        my $u = $users_obj->Next;
        is ( $u->_Value($id_alias), $u->id, "fetched id twice" );
    }

    $users_obj = TestApp::Users->new( $handle );
    $users_obj->UnLimit;
    {
        is( $users_obj->Column(FIELD => 'id'), 'id' );
        isnt( my $id_alias = $users_obj->Column(FIELD => 'id', FUNCTION => '? + 1'), 'id' );
        my $u = $users_obj->Next;
        is ( $u->_Value($id_alias), $u->id + 1, "fetched id and function based on id" )
            or diag "wrong SQL: ". $users_obj->BuildSelectQuery;
    }

    $users_obj = TestApp::Users->new( $handle );
    $users_obj->UnLimit;
    {
        is( $users_obj->Column(FIELD => 'id'), 'id' );
        isnt( my $id_alias = $users_obj->Column(FUNCTION => 'id + 1'), 'id' );
        my $u = $users_obj->Next;
        is ( $u->_Value($id_alias), $u->id + 1, "fetched id and function based on id" );
    }

    $users_obj = TestApp::Users->new( $handle );
    $users_obj->UnLimit;
    {
        is( $users_obj->Column(FIELD => 'id'), 'id' );
        isnt( my $id_alias = $users_obj->Column(FUNCTION => '?', FIELD => 'id'), 'id' );
        my $u = $users_obj->Next;
        is ( $u->_Value($id_alias), $u->id, "fetched with '?' function" );
    }

    $users_obj = TestApp::Users->new( $handle );
    $users_obj->UnLimit;
    {
        is( $users_obj->Column(FIELD => 'id'), "id" );
        is( my $id_alias = $users_obj->Column(FIELD => 'id', AS => 'foo'), "foo" );
        my $u = $users_obj->Next;
        is( $u->_Value($id_alias), $u->id, "fetched id with custom alias" );
    }

    $users_obj = TestApp::Users->new( $handle );
    $users_obj->UnLimit;
    {
        is( $users_obj->Column(FUNCTION => "main.*", AS => undef), undef );
        my $u = $users_obj->Next;
        ok $u->{fetched}{"\L$_"}, "fetched field $_" for keys %{$u->_ClassAccessible};
    }

    $users_obj = TestApp::Users->new( $handle );
    $users_obj->UnLimit;
    {
        is( my $id_alias = $users_obj->AdditionalColumn(FIELD => 'id', AS => 'foo'), "foo" );
        my $u = $users_obj->Next;
        is( $u->_Value($id_alias), $u->id, "fetched id with custom alias" );
        ok $u->{fetched}{"\L$_"}, "fetched normal field $_" for keys %{$u->_ClassAccessible};
    }

    # Last without running the search first
    $users_obj = TestApp::Users->new( $handle );
    $users_obj->UnLimit;
    $users_obj->OrderBy( FIELD => "Login", ORDER => "ASC" );
    is $users_obj->Last->Login, "obra", "Found last record correctly before search was run";

    # Check SQL produced by QueryHint() and QueryHintFormatted()
    $users_obj->UnLimit;
    my $hintless_sql = $users_obj->BuildSelectQuery( PreferBind => 0 );
    unlike( $hintless_sql, qr/\/\*/, "Query hint markers aren't present when QueryHint() isn't called" );

    $users_obj->UnLimit;
    $users_obj->QueryHint( '+FooBar' );
    my $hinted_sql = $users_obj->BuildSelectQuery( PreferBind => 0 );
    like( $hinted_sql, qr|/\*\+FooBar \*/|, "..but are when QueryHint() IS called" );

    $users_obj->UnLimit;
    $users_obj->Limit( FIELD => 'Comments', VALUE => 'NULL', OPERATOR => 'IS NOT' );
    if ( $d eq 'Oracle' ) {
        is( $users_obj->Count, 1, 'Found 1 user who has no comment' );
        is( $users_obj->First->Login, 'cubic', 'Found the correct user who has no comment' );
    }
    else {
        is( $users_obj->Count, 2, 'Found 2 users who have no comment' );
        is_deeply(
            [ map { $_->Login } @{ $users_obj->ItemsArrayRef } ],
            [ 'cubic', 'obra' ],
            'Found the correct users who have no comment'
        );
    }

	cleanup_schema( 'TestApp', $handle );
}} # SKIP, foreach blocks

1;

package TestApp;

sub schema_mysql {[
	"DROP TABLE IF EXISTS Users",
<<EOF
CREATE TABLE Users (
        id integer AUTO_INCREMENT,
        Login varchar(18) NOT NULL,
        Name varchar(36),
	Phone varchar(18),
    Comments blob,
  	PRIMARY KEY (id))
EOF
]}
sub cleanup_schema_mysql { [
    "DROP TABLE Users", 
] }

sub schema_mariadb {[
	"DROP TABLE IF EXISTS Users",
<<EOF
CREATE TABLE Users (
    id integer AUTO_INCREMENT,
    Login varchar(18) NOT NULL,
    Name varchar(36),
    Phone varchar(18),
    Comments blob,
    PRIMARY KEY (id))
EOF
]}
sub cleanup_schema_mariadb { [
    "DROP TABLE Users",
] }

sub schema_pg {
<<EOF;
CREATE TEMPORARY TABLE Users (
        id serial PRIMARY KEY,
        Login varchar(18) NOT NULL,
        Name varchar(36),
        Phone varchar(18),
        Comments text
)
EOF

}

sub schema_sqlite {

<<EOF;
CREATE TABLE Users (
	id integer primary key,
	Login varchar(18) NOT NULL,
	Name varchar(36),
	Phone varchar(18),
    Comments blob)
EOF

}

sub schema_oracle { [
    "CREATE SEQUENCE Users_seq",
    "CREATE TABLE Users (
        id integer CONSTRAINT Users_Key PRIMARY KEY,
        Login varchar(18) NOT NULL,
        Name varchar(36),
        Phone varchar(18),
        Comments clob
    )",
] }

sub cleanup_schema_oracle { [
    "DROP SEQUENCE Users_seq",
    "DROP TABLE Users", 
] }


1;

package TestApp::User;

use base $ENV{SB_TEST_CACHABLE}?
    qw/DBIx::SearchBuilder::Record::Cachable/:
    qw/DBIx::SearchBuilder::Record/;

sub _Init {
    my $self = shift;
    my $handle = shift;
    $self->Table('Users');
    $self->_Handle($handle);
}

sub _ClassAccessible {
    {   
        id =>
        {read => 1, type => 'int(11)' }, 
        Login =>
        {read => 1, write => 1, type => 'varchar(18)' },
        Name =>
        {read => 1, write => 1, type => 'varchar(36)' },
        Phone =>
        {read => 1, write => 1, type => 'varchar(18)', default => ''},
        Comments =>
        {read => 1, write => 1, type => 'blob', default => ''},
    }
}

sub init_data {
    return (
	[ 'Login',	'Name',			'Phone', 'Comments' ],
	[ 'cubic',	'Ruslan U. Zakirov',	'+7-903-264-XX-XX', 'one comment' ],
	[ 'obra',	'Jesse Vincent',	undef, '' ],
	[ 'glasser',	'David Glasser',	undef, undef ],
	[ 'autrijus',	'Autrijus Tang',	'+X-XXX-XXX-XX-XX' ],
    );
}

1;

package TestApp::Users;

# use TestApp::User;
use base qw/DBIx::SearchBuilder/;

sub _Init {
    my $self = shift;
    $self->SUPER::_Init( Handle => shift );
    $self->Table('Users');
}

sub NewItem
{
	my $self = shift;
	return TestApp::User->new( $self->_Handle );
}

1;