LP1968754: address a few more bugs in instructor search user/sandbergja/lp1968754_instructor_search
authorJane Sandberg <js7389@princeton.edu>
Tue, 9 May 2023 23:29:23 +0000 (16:29 -0700)
committerJane Sandberg <js7389@princeton.edu>
Sat, 13 May 2023 15:48:09 +0000 (08:48 -0700)
Signed-off-by: Jane Sandberg <sandbergja@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Course.pm
Open-ILS/src/perlmods/t/26-course-opac-search.t

index 866eb7e..8b7838c 100644 (file)
@@ -310,6 +310,8 @@ sub _create_where_clause {
         my $search_query;
         if ($type eq 'instructor') {
             $query =~ s/\*$/:*/; # postgres prefix matching syntax ends with :* (e.g. string:*, not string*)
+            $query =~ s/^\s+|\s+$//g; # preceding and trailing spaces have the potential to make to_tsquery mad
+            $query =~ s/\s/ \& /g; # postgres to_tsquery wants & characters between words
             my $in = ($contains eq 'nocontains') ? "not in" : "in";
             $search_query = {'id' => {$in => {
                 'from' => {'acmcu' => 'acmr'},
index 9d2da15..22b5214 100644 (file)
@@ -1,6 +1,6 @@
 #!perl
 use strict; use warnings;
-use Test::More tests => 9;
+use Test::More tests => 11;
 use OpenILS::WWW::EGCatLoader;
 
 use_ok('OpenILS::WWW::EGCatLoader');
@@ -54,6 +54,40 @@ my $instructor_query_expected = {"-and" => [
 is_query_parsed_correctly( \@instructor_query, $instructor_query_expected,
                            'Create a valid instructor search json query');
 
+my @instructor_query_with_spaces = {'qtype' => 'instructor','contains' => 'contains','bool' => 'and','value' => 'professor gwendolyn davenport'};
+my $instructor_query_with_spaces_expected = {"-and" => [
+                                                {"owning_lib"=> $orgs},
+                                                {"-not"=>{"+acmc"=>"is_archived"}},
+                                                {"id"=>{"in"=>{
+                                                    "where"=>[
+                                                        {"+acmr"=>"is_public"},
+                                                        {"usr"=>{"in"=>
+                                                            {"select"=>{"au"=>["id"]},
+                                                            "where"=>{"name_kw_tsvector"=>
+                                                            {"@@"=>{"value"=>["to_tsquery","professor & gwendolyn & davenport"]}}},
+                                                        "from"=>"au"}}}],                                        
+                                                    "select"=>{"acmcu"=>["course"]},
+                                                    "from"=>{"acmcu" => "acmr"}}}}]};
+is_query_parsed_correctly( \@instructor_query_with_spaces, $instructor_query_with_spaces_expected,
+                           'Add & to the ts_query input in json query');
+
+my @instructor_query_leading_space = {'qtype' => 'instructor','contains' => 'contains','bool' => 'and','value' => ' gwendolyn'};
+my $instructor_query_leading_space_expected = {"-and" => [
+                                                {"owning_lib"=> $orgs},
+                                                {"-not"=>{"+acmc"=>"is_archived"}},
+                                                {"id"=>{"in"=>{
+                                                    "where"=>[
+                                                        {"+acmr"=>"is_public"},
+                                                        {"usr"=>{"in"=>
+                                                            {"select"=>{"au"=>["id"]},
+                                                            "where"=>{"name_kw_tsvector"=>
+                                                            {"@@"=>{"value"=>["to_tsquery","gwendolyn"]}}},
+                                                        "from"=>"au"}}}],                                        
+                                                    "select"=>{"acmcu"=>["course"]},
+                                                    "from"=>{"acmcu" => "acmr"}}}}]};
+is_query_parsed_correctly( \@instructor_query_leading_space, $instructor_query_leading_space_expected,
+                           'Removes preceding space before converting to tsquery in json_query');
+
 my @instructor_prefix_wildcard_query = {'qtype' => 'instructor','contains' => 'contains','bool' => 'and','value' => 'd*'};
 my $instructor_prefix_wildcard_query_expected = {"-and" => [
                                                     {"owning_lib"=> $orgs},