blob: d2055e11e3953f893ecb8f517d018eecf679f758 [file] [log] [blame]
Nils Diewald034ea702015-01-16 19:41:52 +00001use Mojo::Base -strict;
2use lib '../lib', 'lib';
Nils Diewalda748b0e2015-05-19 22:54:06 +00003use Test::More skip_all => 'No remote tests';
Nils Diewald034ea702015-01-16 19:41:52 +00004use Test::Mojo;
5use Mojo::URL;
6use Benchmark qw/:hireswallclock/;
7
Nils Diewald2fe12e12015-03-06 16:47:06 +00008my $t = Test::Mojo->new('Kalamar');
Nils Diewald034ea702015-01-16 19:41:52 +00009
10$t->app->routes->get('/searchtest')->to(
11 cb => sub {
12 my $c = shift;
13 $c->render(inline => <<'TEMPLATE');
14%= search query => param('q'), start_page => param('p'), no_cache => 1, begin
15<h1><%= search->query %></h1>
16<p id="api"><%= search->api %></p>
17<p id="cutoff"><%= search->cutoff %></p>
18<p id="ql"><%= search->query_language %></p>
19<p id="no_cache"><%= search->no_cache %></p>
20<p id="start_page"><%= search->start_page %></p>
21<p id="total_results"><%= search->total_results %></p>
22<p id="api_request"><%= search->api_request %></p>
23%= search_results begin
24 <li><%= $_->{ID} %></li>
25% end
26% end
27TEMPLATE
28 }
29);
30
31my $exttemplate = <<'EXTTEMPLATE';
32<h1><%= search->query %></h1>
33<p id="api"><%= search->api %></p>
34<p id="cutoff"><%= search->cutoff %></p>
35<p id="ql"><%= search->query_language %></p>
36<p id="no_cache"><%= search->no_cache %></p>
37<p id="start_page"><%= search->start_page %></p>
38<p id="total_results"><%= search->total_results %></p>
39<p id="api_request"><%= search->api_request %></p>
40%= search_results begin
41 <li><%= $_->{ID} %></li>
42% end
43EXTTEMPLATE
44
45
46$t->app->routes->get('/searchasync')->to(
47 cb => sub {
48 my $c = shift;
49 $c->search(
50 query => $c->param('q'),
51 start_page => $c->param('p'),
52 no_cache => 1,
53 cb => sub {
54 return $c->render(inline => $exttemplate);
55 }
56 );
57 }
58);
59
60my $tracetemplate = <<'TRACETEMPLATE';
61<h1><%= search->query %></h1>
62<p id="api"><%= search->api %></p>
63<p id="cutoff"><%= search->cutoff %></p>
64<p id="ql"><%= search->query_language %></p>
65<p id="api_request"><%= search->api_request %></p>
66<p id="query-jsonld"><%= dumper search->query_jsonld %></p>
67TRACETEMPLATE
68
69$t->app->routes->get('/traceasync')->to(
70 cb => sub {
71 my $c = shift;
72 $c->search->trace(
73 query => $c->param('q'),
74 sub {
75 return $c->render(inline => $tracetemplate);
76 }
77 );
78 }
79);
80
81my $matchtemplate = <<'MATCHTEMPLATE';
82<p id="api"><%= search->api %></p>
83<p id="api_request"><%= search->api_request %></p>
Nils Diewalda748b0e2015-05-19 22:54:06 +000084<p id="search-result"><%= search->results->first->{docID} . '.' . search->results->first->{textID} %></p>
Nils Diewald034ea702015-01-16 19:41:52 +000085MATCHTEMPLATE
86
87
88$t->app->routes->get('/matchinfo')->to(
89 cb => sub {
90 my $c = shift;
91 $c->search->match(
92 corpus_id => $c->param('corpus_id'),
93 doc_id => $c->param('doc_id'),
Nils Diewalda748b0e2015-05-19 22:54:06 +000094 text_id => $c->param('text_id'),
Nils Diewald034ea702015-01-16 19:41:52 +000095 match_id => $c->param('match_id'),
96 foundry => '*',
97 sub {
98 return $c->render(inline => $matchtemplate);
99 }
100 );
101 }
102);
103
104my $colltemplate = <<'COLLTEMPLATE';
105<p id="api"><%= search->api %></p>
106<p id="api_request"><%= search->api_request %></p>
107<p id="search-resource"><%= stash('search.resource')->[0]->{name} %></p>
108COLLTEMPLATE
109
110$t->app->routes->get('/collectioninfo')->to(
111 cb => sub {
112 my $c = shift;
113 $c->search->resource(
114 type => 'collection',
115 sub {
116 return $c->render(inline => $colltemplate);
117 }
118 );
119 }
120);
121
122
123$t->app->routes->get('/collectionandsearch-parallel')->to(
124 cb => sub {
125 my $c = shift;
126 $c->delay(
127 sub {
128 my $delay = shift;
129 $c->search->resource(
130 type => 'collection',
131 $delay->begin
132 );
133
134 $c->search(
135 query => $c->param('q'),
136 start_page => $c->param('p'),
137 no_cache => 1,
138 cb => $delay->begin
139 );
140 },
141 sub {
142 return $c->render(
143 inline => $exttemplate .
144 q!<p id="search-resource"><%= stash('search.resource')->[0]->{name} %></p>!
145 );
146 }
147 )
148 }
149);
150
151my $query = 'startswith(<s>,[mate/m=gender:masc]{3,5})';
152
153# Search everything in parallel!
154$t->get_ok(Mojo::URL->new('/collectionandsearch-parallel')->query({q => $query}))
155 ->status_is(200)
156 ->text_is('.notify-error', '')
157 ->text_is('h1', $query)
158 ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/')
159 ->text_is('#ql', 'poliqarp')
160 ->text_is('#search-resource', 'Wikipedia');
161
162$t->get_ok('/collectioninfo')
163 ->status_is(200)
164 ->text_is('#search-resource', 'Wikipedia');
165
Nils Diewald034ea702015-01-16 19:41:52 +0000166# http://10.0.10.14:6666/corpus/WPD/WWW.04738/p265-266
167$t->get_ok(Mojo::URL->new('/matchinfo')->query({
168 corpus_id => 'WPD',
Nils Diewalda748b0e2015-05-19 22:54:06 +0000169 doc_id => 'WWW',
170 text_id => '04738',
Nils Diewald034ea702015-01-16 19:41:52 +0000171 match_id => 'p265-266'
172}))
173 ->status_is(200)
174 ->text_is('#search-result', 'WWW.04738');
175
176$t->get_ok(Mojo::URL->new('/traceasync')->query({q => $query}))
177 ->status_is(200)
178 ->text_is('.notify-error', '')
179 ->text_is('h1', $query)
180 ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/')
181 ->text_is('#ql', 'poliqarp')
182 ->text_like('#query-jsonld', qr!korap:boundary!);
183
184my $t0 = Benchmark->new;
185
186$t->get_ok(Mojo::URL->new('/searchasync')->query({q => $query}))
187 ->status_is(200)
188 ->text_is('.notify-error', '')
189 ->text_is('h1', $query)
190 ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/')
191 ->text_is('#cutoff', '')
192 ->text_is('#ql', 'poliqarp')
193 ->text_is('#no_cache', 1)
194 ->text_is('#start_page', 1)
195 ->text_is('#total_results', 54215)
196# ->text_is('li', 'p265-266')
197 ;
198
199my $t1 = Benchmark->new;
200
201$t->get_ok(Mojo::URL->new('/searchtest')->query({q => $query}))
202 ->status_is(200)
203 ->text_is('.notify-error', '')
204 ->text_is('h1', $query)
205 ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/')
206 ->text_is('#cutoff', '')
207 ->text_is('#ql', 'poliqarp')
208 ->text_is('#no_cache', 1)
209 ->text_is('#start_page', 1)
210 ->text_is('#total_results', 54215)
211# ->text_is('li', 'p265-266')
212 ;
213
214diag 'sync ' . timestr(timediff(Benchmark->new, $t1));
215diag 'async ' . timestr(timediff($t1, $t0));
216
217# Check time_exceeded!
218
219done_testing;
220__END__