|
Lines 129-134
subtest 'CRUD' => sub {
Link Here
|
| 129 |
teardown(); |
129 |
teardown(); |
| 130 |
}; |
130 |
}; |
| 131 |
|
131 |
|
|
|
132 |
subtest 'Owner filter logic' => sub { |
| 133 |
plan tests => 16; |
| 134 |
|
| 135 |
my ( @where_strs, @args ); |
| 136 |
|
| 137 |
my $test_logic = sub { |
| 138 |
my ($owner) = @_; |
| 139 |
@where_strs = (); |
| 140 |
@args = (); |
| 141 |
|
| 142 |
if ( defined $owner and $owner ne '' ) { |
| 143 |
$owner =~ s/^\s+|\s+$//g; # Trim leading/trailing spaces |
| 144 |
$owner =~ s/\s+/ /g; # Normalize internal whitespace |
| 145 |
|
| 146 |
return ( [], [] ) if $owner eq ''; |
| 147 |
|
| 148 |
my @name_parts = split ' ', $owner; |
| 149 |
|
| 150 |
if ( @name_parts == 2 ) { |
| 151 |
push @where_strs, '( bo.firstname LIKE ? AND bo.surname LIKE ? )'; |
| 152 |
push @args, "%$name_parts[0]%", "%$name_parts[1]%"; |
| 153 |
} else { |
| 154 |
push @where_strs, '(bo.firstname LIKE ? OR bo.surname LIKE ?)'; |
| 155 |
push @args, "%$owner%", "%$owner%"; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return ( \@where_strs, \@args ); |
| 160 |
}; |
| 161 |
|
| 162 |
my ( $where, $args ); |
| 163 |
|
| 164 |
( $where, $args ) = $test_logic->(undef); |
| 165 |
is_deeply( $where, [], 'No where clause when owner is undef' ); |
| 166 |
is_deeply( $args, [], 'No args when owner is undef' ); |
| 167 |
|
| 168 |
( $where, $args ) = $test_logic->(''); |
| 169 |
is_deeply( $where, [], 'No where clause when owner is empty' ); |
| 170 |
is_deeply( $args, [], 'No args when owner is empty' ); |
| 171 |
|
| 172 |
( $where, $args ) = $test_logic->('John Smith'); |
| 173 |
is_deeply( $where, ['( bo.firstname LIKE ? AND bo.surname LIKE ? )'], 'AND clause for two name parts' ); |
| 174 |
is_deeply( $args, [ '%John%', '%Smith%' ], 'Args match both names' ); |
| 175 |
|
| 176 |
( $where, $args ) = $test_logic->(' John Smith '); |
| 177 |
is_deeply( $where, ['( bo.firstname LIKE ? AND bo.surname LIKE ? )'], 'Trimmed whitespace around two name parts' ); |
| 178 |
is_deeply( $args, [ '%John%', '%Smith%' ], 'Args match trimmed names' ); |
| 179 |
|
| 180 |
( $where, $args ) = $test_logic->('John Smith'); |
| 181 |
is_deeply( $where, ['( bo.firstname LIKE ? AND bo.surname LIKE ? )'], 'Normalized internal whitespace' ); |
| 182 |
is_deeply( $args, [ '%John%', '%Smith%' ], 'Args match normalized names' ); |
| 183 |
|
| 184 |
( $where, $args ) = $test_logic->('John '); |
| 185 |
is_deeply( $where, ['(bo.firstname LIKE ? OR bo.surname LIKE ?)'], 'Single name with trailing space' ); |
| 186 |
is_deeply( $args, [ '%John%', '%John%' ], 'Args match single name trimmed' ); |
| 187 |
|
| 188 |
( $where, $args ) = $test_logic->(' '); |
| 189 |
is_deeply( $where, [], 'No where clause when owner is only whitespace' ); |
| 190 |
is_deeply( $args, [], 'No args when owner is only whitespace' ); |
| 191 |
|
| 192 |
( $where, $args ) = $test_logic->(' John '); |
| 193 |
is_deeply( $where, ['(bo.firstname LIKE ? OR bo.surname LIKE ?)'], 'Single name with leading/trailing spaces' ); |
| 194 |
is_deeply( $args, [ '%John%', '%John%' ], 'Args match cleaned single name' ); |
| 195 |
}; |
| 196 |
|
| 132 |
subtest 'Sharing' => sub { |
197 |
subtest 'Sharing' => sub { |
| 133 |
plan tests => 21; |
198 |
plan tests => 21; |
| 134 |
my $patron_wants_to_share = $builder->build( |
199 |
my $patron_wants_to_share = $builder->build( |
| 135 |
- |
|
|