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
|
#!/usr/bin/perl -w
use strict;
use warnings;
use Test::More;
BEGIN { require "./t/utils.pl" }
our (@AvailableDrivers);
use constant TESTS_PER_DRIVER => 363;
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 $lowest = ($d ne 'Pg' && $d ne 'Oracle')? '-': 'z';
diag "generate data" if $ENV{TEST_VERBOSE};
{
my @tags = qw(a b c d);
foreach my $i ( 1..30 ) {
my $number_of_tags = int(rand(4));
my @t;
push @t, $tags[int rand scalar @tags] while $number_of_tags--;
my %seen = ();
@t = grep !$seen{$_}++, @t;
my $obj = TestApp::Object->new($handle);
my ($oid) = $obj->Create( Name => join(",", sort @t) || $lowest );
ok($oid,"Created record ". $oid);
ok($obj->Load($oid), "Loaded the record");
my $tags_ok = 1;
foreach my $t( @t ) {
my $tag = TestApp::Tag->new($handle);
my ($tid) = $tag->Create( Object => $oid, Name => $t );
$tags_ok = 0 unless $tid;
}
ok($tags_ok, "Added tags");
}
}
my @fields = (
'Name',
$d eq 'Oracle' ? 'TO_CHAR(Name)' : ($d eq 'mysql' || $d eq 'MariaDB') ? 'BINARY(Name)' : 'CAST(Name AS TEXT)'
);
diag "test ordering objects by fields on Tags table" if $ENV{TEST_VERBOSE};
foreach my $direction ( qw(ASC DESC) ) {
foreach my $field (@fields ) {
foreach my $combine_search_and_count ( 0, 1 ) {
foreach my $per_page (0, 5) {
foreach my $page (0, 2) {
my $objs = TestApp::Objects->new($handle);
$objs->CombineSearchAndCount($combine_search_and_count);
$objs->UnLimit;
my $tags_alias = $objs->Join(
TYPE => 'LEFT',
ALIAS1 => 'main',
FIELD1 => 'id',
TABLE2 => 'Tags',
FIELD2 => 'Object',
);
ok($tags_alias, "joined tags table");
# Generated SQL is MIN(Name) or nested functions like MIN(CAST(Name AS TEXT))
$objs->OrderBy( ALIAS => $tags_alias, FIELD => $field, ORDER => $direction );
$objs->RowsPerPage($per_page) if $per_page;
$objs->GotoPage($page) if $page;
ok($objs->First, 'ok, we have at least one result');
$objs->GotoFirstItem;
my ($order_ok, $last) = (1, $direction eq 'ASC'? '-': 'zzzz');
while ( my $obj = $objs->Next ) {
my $tmp;
if ( $direction eq 'ASC' ) {
$tmp = (substr($last, 0, 1) cmp substr($obj->Name, 0, 1));
} else {
$tmp = -(substr($last, -1, 1) cmp substr($obj->Name, -1, 1));
}
if ( $tmp > 0 ) {
$order_ok = 0; last;
}
$last = $obj->Name;
}
ok($order_ok, "$direction order is correct") or do {
diag "Wrong $direction query: ". $objs->BuildSelectQuery;
$objs->GotoFirstItem;
while ( my $obj = $objs->Next ) {
diag($obj->id .":". $obj->Name);
}
};
my $got_count = $objs->CountAll;
is ($got_count, 30, "CountAll is expected");
}}}}} # foreach variants blocks
my $expected_count = 0;
diag "test ordering objects by object's fields with limit by tags" if $ENV{TEST_VERBOSE};
foreach my $direction ( qw(ASC DESC) ) {
foreach my $field ('Name', 'id') {
foreach my $combine_search_and_count ( 0, 1 ) {
foreach my $per_page (0, 5, 30) {
foreach my $page (0, 2) {
my $objs = TestApp::Objects->new($handle);
$objs->CombineSearchAndCount($combine_search_and_count);
my $tags_alias = $objs->Join(
ALIAS1 => 'main',
FIELD1 => 'id',
TABLE2 => 'Tags',
FIELD2 => 'Object',
);
ok($tags_alias, "joined tags table");
$objs->OrderBy( FIELD => $field, ORDER => $direction );
$objs->RowsPerPage($per_page) if $per_page;
$objs->GotoPage($page) if $page;
$objs->Limit( ALIAS => $tags_alias, FIELD => 'Name', OPERATOR => 'IN', VALUE => ['c', 'a'] );
my @list;
while ( my $obj = $objs->Next ) {
push @list, $field eq 'Name'? $obj->Name : $obj->Id;
}
my @correct = sort {$field eq 'Name'? $a cmp $b : $a <=> $b } @list;
@correct = reverse @correct if $direction eq 'DESC';
is_deeply(\@list, \@correct, "correct order");
my $got_count = $objs->CountAll;
if ($expected_count) {
is($got_count, $expected_count, "count is correct");
} else {
$expected_count = $got_count;
}
}}}}} # foreach variants blocks
cleanup_schema( 'TestApp', $handle );
}} # SKIP, foreach driver block
1;
package TestApp;
sub schema_mysql { [
"CREATE TEMPORARY TABLE Objects (
id integer AUTO_INCREMENT,
Name varchar(36),
PRIMARY KEY (id)
)",
"CREATE TEMPORARY TABLE Tags (
id integer AUTO_INCREMENT,
Object integer NOT NULL,
Name varchar(36),
PRIMARY KEY (id)
)",
"CREATE INDEX Tags1 ON Tags (Name)"
] }
sub schema_mariadb { [
"CREATE TEMPORARY TABLE Objects (
id integer AUTO_INCREMENT,
Name varchar(36),
PRIMARY KEY (id)
)",
"CREATE TEMPORARY TABLE Tags (
id integer AUTO_INCREMENT,
Object integer NOT NULL,
Name varchar(36),
PRIMARY KEY (id)
)",
"CREATE INDEX Tags1 ON Tags (Name)"
] }
sub schema_pg { [
"CREATE TEMPORARY TABLE Objects (
id serial PRIMARY KEY,
Name varchar(36)
)",
"CREATE TEMPORARY TABLE Tags (
id serial PRIMARY KEY,
Object integer NOT NULL,
Name varchar(36)
)",
"CREATE INDEX Tags1 ON Tags (Name)"
]}
sub schema_sqlite {[
"CREATE TABLE Objects (
id integer primary key,
Name varchar(36)
)",
"CREATE TABLE Tags (
id integer primary key,
Object integer NOT NULL,
Name varchar(36)
)",
"CREATE INDEX Tags1 ON Tags (Name)"
]}
sub schema_oracle { [
"CREATE SEQUENCE Objects_seq",
"CREATE TABLE Objects (
id integer CONSTRAINT Objects_Key PRIMARY KEY,
Name varchar(36)
)",
"CREATE SEQUENCE Tags_seq",
"CREATE TABLE Tags (
id integer CONSTRAINT Tags_Key PRIMARY KEY,
Object integer NOT NULL,
Name varchar(36)
)",
"CREATE INDEX Tags1 ON Tags (Name)"
] }
sub cleanup_schema_oracle { [
"DROP SEQUENCE Objects_seq",
"DROP TABLE Objects",
"DROP SEQUENCE Tags_seq",
"DROP TABLE Tags",
] }
1;
package TestApp::Object;
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('Objects');
$self->_Handle($handle);
}
sub _ClassAccessible {
{
id =>
{read => 1, type => 'int(11)' },
Name =>
{read => 1, write => 1, type => 'varchar(36)' },
}
}
1;
package TestApp::Objects;
use base qw/DBIx::SearchBuilder/;
sub _Init {
my $self = shift;
$self->SUPER::_Init( Handle => shift );
$self->Table('Objects');
}
sub NewItem
{
my $self = shift;
return TestApp::Object->new( $self->_Handle );
}
1;
package TestApp::Tag;
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('Tags');
$self->_Handle($handle);
}
sub _ClassAccessible {
{
id =>
{read => 1, type => 'int(11)' },
Object =>
{read => 1, type => 'int(11)' },
Name =>
{read => 1, write => 1, type => 'varchar(36)' },
}
}
1;
package TestApp::Tags;
# use TestApp::User;
use base qw/DBIx::SearchBuilder/;
sub _Init {
my $self = shift;
$self->SUPER::_Init( Handle => shift );
$self->Table('Tags');
}
sub NewItem
{
my $self = shift;
return TestApp::Tag->new( $self->_Handle );
}
1;
|