"Fossies" - the Fresh Open Source Software Archive

Member "eprints3.4-3.4.4/tests/30_search.pl" (7 Aug 2022, 13864 Bytes) of package /linux/www/eprints3.4-3.4.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "30_search.pl": 3.4-3.4.3_vs_3.4-3.4.4.

    1 use strict;
    2 use utf8;
    3 use Test::More tests => 35;
    4 
    5 BEGIN { use_ok( "EPrints" ); }
    6 BEGIN { use_ok( "EPrints::Test" ); }
    7 
    8 my $session = EPrints::Test::get_test_session( 0 );
    9 ok(defined $session, 'opened an EPrints::Session object (noisy, no_check_db)');
   10 
   11 my $dataset = $session->dataset( "eprint" );
   12 
   13 my $searchexp = EPrints::Search->new(
   14     session => $session,
   15     dataset => $dataset,
   16     allow_blank => 1,
   17 );
   18 
   19 my $list = eval { $searchexp->perform_search };
   20 
   21 ok(defined($list) && $list->count > 0, "blank found matches");
   22 
   23 
   24 $searchexp = EPrints::Search->new(
   25     session => $session,
   26     dataset => $dataset,
   27 );
   28 
   29 $searchexp->add_field( $dataset->get_field( "eprintid" ), "1-" );
   30 
   31 $list = eval { $searchexp->perform_search };
   32 
   33 ok(defined($list) && $list->count > 1, "search range eprintid" );
   34 
   35 $searchexp = EPrints::Search->new(
   36     session => $session,
   37     dataset => $dataset,
   38     satisfy_all => 1 );
   39 
   40 $searchexp->add_field( $dataset->field( "title" ), "eagle", "IN" );
   41 $searchexp->add_field( $dataset->field( "creators_name" ), "Maury, W Parkes, F", "EQ" );
   42 
   43 ok(defined($list) && $list->count, "title IN + creators_name GREP\n".$searchexp->get_conditions->describe);
   44 
   45 $searchexp = EPrints::Search->new(
   46     session => $session,
   47     dataset => $dataset,
   48 );
   49 
   50 $searchexp->add_field( $dataset->get_field( "creators_name" ), "", "SET" );
   51 $searchexp->add_field( $dataset->get_field( "metadata_visibility" ), "show" );
   52 $searchexp->add_field( $dataset->get_field( "eprint_status" ), "archive" );
   53 
   54 $list = eval { $searchexp->perform_search };
   55 
   56 #print STDERR $searchexp->get_conditions->sql( dataset => $dataset, session => $session )."\n";
   57 
   58 ok(defined($list) && $list->count > 1, "SET match" );
   59 
   60 $searchexp = EPrints::Search->new(
   61     session => $session,
   62     dataset => $dataset,
   63     satisfy_all => 1
   64 );
   65 
   66 $searchexp->add_field( $dataset->get_field( "subjects" ), "GR" );
   67 $searchexp->add_field( $dataset->get_field( "divisions" ), "sch_mat" );
   68 
   69 $list = eval { $searchexp->perform_search };
   70 
   71 ok(defined($list) && $list->count > 0, "subjects and divisions: " . $searchexp->get_conditions->describe );
   72 
   73 $searchexp = EPrints::Search->new(
   74     session => $session,
   75     dataset => $dataset,
   76 );
   77 
   78 $searchexp->add_field( $dataset->get_field( "documents" ), "article", "IN" );
   79 
   80 $list = eval { $searchexp->perform_search };
   81 
   82 ok(defined($list) && $list->count > 0, "match testdata article full text" );
   83 
   84 
   85 my $sample_doc = EPrints::Test::get_test_document( $session );
   86 my $sample_eprint = $sample_doc->get_parent;
   87 
   88 $searchexp = EPrints::Search->new(
   89     session => $session,
   90     dataset => $dataset,
   91 );
   92 
   93 $searchexp->add_field( $dataset->get_field( "eprintid" ), $sample_doc->get_value( "eprintid" ) );
   94 $searchexp->add_field( $sample_doc->get_dataset->get_field( "format" ), $sample_doc->get_value( "format" ) );
   95 
   96 $list = eval { $searchexp->perform_search };
   97 
   98 my $is_ok = 0;
   99 if( defined $list )
  100 {
  101     my( $eprint ) = $list->get_records( 0, 1 );
  102     $is_ok = $list->count == 1 && $eprint->get_id == $sample_doc->get_value( "eprintid" );
  103 }
  104 
  105 ok($is_ok, "search for eprint id + doc format: " . $searchexp->get_conditions->describe);
  106 
  107 
  108 $searchexp = EPrints::Search->new(
  109     session => $session,
  110     dataset => $dataset,
  111 );
  112 
  113 $searchexp->add_field( $dataset->get_field( "creators_name" ), "Neumeier, M" );
  114 
  115 $list = eval { $searchexp->perform_search };
  116 
  117 ok(defined($list) && $list->count > 0, "search multiple name field".sql($searchexp));
  118 
  119 
  120 $searchexp = EPrints::Search->new(
  121     session => $session,
  122     dataset => $dataset,
  123     satisfy_all => 0,
  124 );
  125 
  126 $searchexp->add_field( $dataset->get_field( "relation_type" ), "NOMATCH" );
  127 $searchexp->add_field( $dataset->get_field( "editors_name" ), "NOMATCH, P" );
  128 $searchexp->add_field( $dataset->get_field( "title" ), "legend", "IN" );
  129 
  130 $list = eval { $searchexp->perform_search };
  131 
  132 ok(defined($list) && $list->count > 0, "satisfy-any, nomatch multiple");
  133 
  134 
  135 $searchexp = EPrints::Search->new(
  136     session => $session,
  137     dataset => $dataset,
  138     satisfy_all => 0,
  139     custom_order => "-date/title",
  140 );
  141 
  142 $searchexp->add_field( $dataset->get_field( "creators_name" ), "Smith, John" );
  143 $searchexp->add_field( $sample_doc->get_dataset->get_field( "format" ), "application/pdf" );
  144 $searchexp->add_field( $sample_doc->get_dataset->get_field( "relation_type" ), EPrints::Utils::make_relation("isVolatileVersionOf") );
  145 
  146 $list = eval { $searchexp->perform_search };
  147 
  148 ok(defined($list) && $list->count > 0, "satisfy/multi datasets/multiple");
  149 
  150 
  151 $searchexp = EPrints::Search->new(
  152     session => $session,
  153     dataset => $dataset,
  154 );
  155 
  156 $searchexp->add_field( $dataset->get_field( "subjects" ), "QH" );
  157 
  158 $list = eval { $searchexp->perform_search };
  159 
  160 ok(defined($list) && $list->count > 0, "subject hierarchy");
  161 
  162 
  163 $searchexp = EPrints::Search->new(
  164     session => $session,
  165     dataset => $dataset,
  166 );
  167 
  168 $searchexp->add_field( $dataset->get_field( "subjects" ), "QH" );
  169 
  170 my( $values, $counts ) = eval { $searchexp->perform_groupby( $dataset->get_field( "creators_name" ) ) };
  171 
  172 ok(defined($values) && scalar(@$values) > 0, "groupby");
  173 
  174 my $dataset_size = $dataset->count( $session );
  175 BAIL_OUT( "Can't test empty dataset" ) unless $dataset_size > 0;
  176 
  177 my $cond = EPrints::Search::Condition::False->new;
  178 
  179 my $matches = $cond->process(
  180     session => $session,
  181     dataset => $dataset,
  182     );
  183 
  184 ok(@$matches == 0, "FALSE condition returns empty");
  185 
  186 $cond = EPrints::Search::Condition::True->new;
  187 
  188 $matches = $cond->process(
  189     session => $session,
  190     dataset => $dataset,
  191     );
  192 
  193 ok(@$matches == $dataset_size, "TRUE condition returns everything");
  194 
  195 $cond = EPrints::Search::Condition::And->new(
  196     EPrints::Search::Condition::False->new,
  197     EPrints::Search::Condition::True->new );
  198 
  199 $matches = $cond->process(
  200     session => $session,
  201     dataset => $dataset,
  202     );
  203 
  204 ok(@$matches == 0, "TRUE AND FALSE is FALSE");
  205 
  206 $cond = EPrints::Search::Condition::Or->new(
  207     EPrints::Search::Condition::True->new,
  208     EPrints::Search::Condition::False->new );
  209 
  210 $matches = $cond->process(
  211     session => $session,
  212     dataset => $dataset,
  213     );
  214 
  215 ok(@$matches == $dataset_size, "TRUE OR FALSE is TRUE");
  216 
  217 my $hdataset = $session->dataset( "history" );
  218 
  219 my $db = $session->get_database;
  220 
  221 my $retry = 0;
  222 HISTORY:
  223 
  224 my $sql = "SELECT ".$db->quote_identifier( "userid" )." FROM ".$db->quote_identifier( $hdataset->get_sql_table_name )." WHERE ".$db->quote_identifier( "userid" )." IS NOT NULL";
  225 my $sth = $db->prepare_select( $sql, limit => 1 );
  226 $sth->execute;
  227 
  228 my( $userid ) = $sth->fetchrow_array;
  229 
  230 undef $sth;
  231 
  232 if( !$retry && !defined $userid )
  233 {
  234     my $eprint = $session->dataset( "eprint" )->search( limit => 1 )->item( 0 );
  235     BAIL_OUT("No eprints") if !defined $eprint;
  236     $eprint->save_revision( user => $session->user( 1 ), action => "unit_test" );
  237     $retry = 1;
  238     goto HISTORY;
  239 }
  240 
  241 BAIL_OUT("Need at least one history object") unless defined $userid;
  242 my $user = EPrints::DataObj::User->new( $session, $userid );
  243 
  244 $searchexp = EPrints::Search->new(
  245     session => $session,
  246     dataset => $hdataset,
  247     allow_blank => 1,
  248     filters => [{ meta_fields => [qw( userid.username )], value => $user->get_value( "username" ) }],
  249     );
  250 
  251 $list = $searchexp->perform_search;
  252 ok($list->count > 0, "history object by username subquery".$searchexp->get_conditions->describe."\n".$searchexp->get_conditions->sql( dataset => $hdataset, session => $session ));
  253 
  254 $list = eval { $dataset->search(
  255     filters => [{ meta_fields => [qw( documents.format )], value => "text" }],
  256     ) };
  257 ok($list->count > 0, "documents.format join path");
  258 
  259 my $udataset = $session->dataset( "user" );
  260 my $ssdataset = $session->dataset( "saved_search" );
  261 my @usertypes = $session->get_repository->get_types( "user" );
  262 
  263 $searchexp = EPrints::Search->new(
  264     session => $session,
  265     dataset => $ssdataset );
  266 
  267 $searchexp->add_field(
  268     $udataset->get_field( "frequency" ),
  269     "never" );
  270 
  271 eval { $searchexp->perform_search };
  272 ok( !$@, "userid->frequency on saved_search" );
  273 
  274 $cond = EPrints::Search::Condition::Regexp->new( $udataset, $udataset->get_field( "username" ), '^' . $user->get_value( "username" ) . '$' );
  275 $matches = $cond->process(
  276     session => $session,
  277     dataset => $udataset,
  278     );
  279 
  280 is(scalar(@$matches), 1, "regexp username matched itself" );
  281 
  282 $searchexp = EPrints::Search->new(
  283     session => $session,
  284     dataset => $dataset,
  285     satisfy_all => 0 );
  286 
  287 $searchexp->add_field( $dataset->get_field( "documents" ), "article", "IN" );
  288 $searchexp->add_field( $dataset->get_field( "title" ), "article", "IN" );
  289 $searchexp->add_field( $dataset->get_field( "relation_type" ), "article" );
  290 
  291 #print STDERR $searchexp->get_conditions->describe;
  292 
  293 $list = $searchexp->perform_search;
  294 
  295 ok($list->count > 0, "satisfy_all => 0");
  296 
  297 $searchexp = EPrints::Search->new(
  298     session => $session,
  299     dataset => $sample_doc->get_dataset,
  300     satisfy_all => 0 );
  301 
  302 $searchexp->add_field( $dataset->get_field( "type" ), "article" );
  303 
  304 $list = $searchexp->perform_search;
  305 
  306 ok($list->count > 0, "documents.eprint.type/satisfy_all => 0");
  307 
  308 $searchexp = EPrints::Search->new(
  309     session => $session,
  310     dataset => $session->dataset( "history" ),
  311     satisfy_all => 0 );
  312 
  313 $searchexp->add_field( $session->dataset( "user" )->get_field( "usertype" ), "admin" );
  314 
  315 $list = $searchexp->perform_search;
  316 
  317 ok($list->count > 0, "query history by user type");
  318 
  319 $searchexp = EPrints::Search->new(
  320     session => $session,
  321     dataset => $dataset,
  322     satisfy_all => 0 );
  323 
  324 $searchexp->add_field( $session->dataset( "user" )->get_field( "name" ), "Admin, A" );
  325 
  326 $list = $searchexp->perform_search;
  327 
  328 # name isn't set in test data set
  329 ok(1, "query eprint by user name");
  330 
  331 SKIP: {
  332 skip "No support for arbitrary dataset joins yet", 1..1;
  333 
  334 $searchexp = EPrints::Search->new(
  335     session => $session,
  336     dataset => $sample_doc->get_dataset,
  337     satisfy_all => 0 );
  338 
  339 my $file_dataset = $session->dataset( "file" );
  340 $searchexp->add_field( $file_dataset->get_field( "mime_type" ), "application/pdf" );
  341 
  342 $list = $searchexp->perform_search;
  343 
  344 ok($list->count > 0, "documents.file.mime_type/satisfy_all => 0");
  345 };
  346 
  347 $searchexp = EPrints::Search->new(
  348     session => $session,
  349     dataset => $sample_doc->dataset,
  350     satisfy_all => 0 );
  351 
  352 $searchexp->add_field( $sample_doc->dataset->field( "relation" ), "http%3A//eprints.org/relation/islightboxThumbnailVersionOf:/id/document/1", "EX" );
  353 
  354 #print STDERR $searchexp->get_conditions->sql( dataset => $sample_doc->dataset, session => $session );
  355 
  356 $list = $searchexp->perform_search;
  357 
  358 ok($list->count > 0, "compound type field query");
  359 
  360 SKIP: {
  361     skip "not implemented yet", 1;
  362 
  363     $searchexp = EPrints::Search->new(
  364         session => $session,
  365         dataset => $dataset,
  366         satisfy_all => 0 );
  367 
  368     $searchexp->add_field( $dataset->field( "contributors" ), {
  369         type => "http://www.loc.gov/loc.terms/relators/ACT",
  370         name => { family => "LĂ©ricolais", given => "I." },
  371     }, "EX" );
  372 
  373     $list = $searchexp->perform_search;
  374 
  375     ok($list->count > 1, "compound type with name query\n".$searchexp->get_conditions->describe."\n".$searchexp->get_conditions->sql( dataset => $dataset, session => $session ));
  376 };
  377 
  378 $searchexp = EPrints::Search->new(
  379     session => $session,
  380     dataset => $dataset,
  381     satisfy_all => 0 );
  382 
  383 $searchexp->add_field( $dataset->field( "title" ), "eagl*", "IN" );
  384 
  385 my $sf = $searchexp->get_searchfield( "title" );
  386 # any better way to check this?
  387 ok( $sf->get_conditions->describe =~ "index_start", "title=eagl* results in index_start" );
  388 
  389 $searchexp = EPrints::Search->new(
  390     session => $session,
  391     dataset => $dataset,
  392     satisfy_all => 0 );
  393 
  394 $searchexp->add_field( $dataset->field( "title" ), "waxing monkey", "IN" );
  395 $searchexp->add_field( $dataset->field( "date" ), "2000" );
  396 
  397 $list = $searchexp->perform_search;
  398 
  399 ok($list->count > 0, "title OR date: ".$searchexp->get_conditions->describe);
  400 
  401 $searchexp = EPrints::Search->new(
  402     session => $session,
  403     dataset => $dataset,
  404     satisfy_all => 0 );
  405 
  406 $searchexp->add_field( $dataset->field( "title" ), "banded geckos", "IN" );
  407 $searchexp->add_field( $dataset->field( "abstract" ), "demonstration data", "IN" );
  408 
  409 $list = $searchexp->perform_search;
  410 
  411 ok($list->count > 0, "title OR abstract: ".$searchexp->get_conditions->describe."\n".$searchexp->get_conditions->sql( dataset => $dataset, session => $session ));
  412 
  413 $searchexp = EPrints::Search->new(
  414     session => $session,
  415     dataset => $sample_doc->get_dataset,
  416 );
  417 
  418 $searchexp->add_field(
  419     $sample_doc->get_dataset->get_field( "relation_type" ),
  420     EPrints::Utils::make_relation("isVolatileVersionOf")." ".EPrints::Utils::make_relation("ispreviewThumbnailVersionOf"), "EQ", "ALL" );
  421 
  422 $list = eval { $searchexp->perform_search };
  423 
  424 ok(defined($list) && $list->count > 0, "search multiple field".&describe($searchexp).&sql($searchexp));
  425 
  426 SKIP:
  427 {
  428     skip "Enable Xapian", 1 if !defined $session->plugin( "Search::Xapian" );
  429 
  430     my $searchexp = $session->plugin( "Search::Xapian",
  431             dataset => $dataset,
  432             search_fields => [
  433                 { meta_fields => [qw( creators_name )], },
  434             ],
  435             q => "creators_name:LĂ©ricolais",
  436         );
  437     my $list = $searchexp->execute;
  438 
  439     ok($list->count > 0, "Xapian creators_name");
  440 }
  441 
  442 $session->terminate;
  443 
  444 sub describe
  445 {
  446     return "\n: ".$_[0]->get_conditions->describe;
  447 }
  448 
  449 sub sql
  450 {
  451     return "\n: ".$_[0]->get_conditions->sql(
  452         session => $_[0]->{session},
  453         dataset => $_[0]->{dataset},
  454     );
  455 }
  456 
  457 =head1 COPYRIGHT
  458 
  459 =for COPYRIGHT BEGIN
  460 
  461 Copyright 2022 University of Southampton.
  462 EPrints 3.4 is supplied by EPrints Services.
  463 
  464 http://www.eprints.org/eprints-3.4/
  465 
  466 =for COPYRIGHT END
  467 
  468 =for LICENSE BEGIN
  469 
  470 This file is part of EPrints 3.4 L<http://www.eprints.org/>.
  471 
  472 EPrints 3.4 and this file are released under the terms of the
  473 GNU Lesser General Public License version 3 as published by
  474 the Free Software Foundation unless otherwise stated.
  475 
  476 EPrints 3.4 is distributed in the hope that it will be useful,
  477 but WITHOUT ANY WARRANTY; without even the implied warranty of
  478 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  479 See the GNU Lesser General Public License for more details.
  480 
  481 You should have received a copy of the GNU Lesser General Public
  482 License along with EPrints 3.4.
  483 If not, see L<http://www.gnu.org/licenses/>.
  484 
  485 =for LICENSE END
  486