Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 1 | use Mojo::Base -strict; |
| 2 | use Test::Mojo; |
| 3 | use Test::More; |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 4 | use Mojo::File qw/path/; |
| 5 | |
| 6 | |
| 7 | ##################### |
| 8 | # Start Fake server # |
| 9 | ##################### |
| 10 | my $mount_point = '/api/'; |
| 11 | $ENV{KALAMAR_API} = $mount_point; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 12 | |
| 13 | my $t = Test::Mojo->new('Kalamar'); |
| 14 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 15 | # Mount fake backend |
| 16 | # Get the fixture path |
Akron | 73f3608 | 2018-10-25 15:34:59 +0200 | [diff] [blame] | 17 | my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server'); |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 18 | my $fake_backend = $t->app->plugin( |
| 19 | Mount => { |
| 20 | $mount_point => |
Akron | 73f3608 | 2018-10-25 15:34:59 +0200 | [diff] [blame] | 21 | $fixtures_path->child('mock.pl') |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 22 | } |
| 23 | ); |
| 24 | # Configure fake backend |
| 25 | $fake_backend->pattern->defaults->{app}->log($t->app->log); |
| 26 | |
Akron | d1ff8d8 | 2018-11-08 13:16:55 +0100 | [diff] [blame] | 27 | if (0) { |
| 28 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 29 | # Query passed |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 30 | $t->get_ok('/?q=baum') |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 31 | ->status_is(200) |
| 32 | ->text_is('#error','') |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 33 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 34 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 35 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 36 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
Akron | dffa939 | 2018-10-12 16:26:09 +0200 | [diff] [blame] | 37 | |
| 38 | # Total results |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 39 | ->text_is('#total-results', 51) |
Akron | dffa939 | 2018-10-12 16:26:09 +0200 | [diff] [blame] | 40 | |
| 41 | # Total pages |
| 42 | ->element_count_is('#pagination a', 5) |
| 43 | |
| 44 | # api_response |
| 45 | ->content_like(qr/\"authorized\":null/) |
| 46 | ->content_like(qr/\"pubDate\",\"subTitle\",\"author\"/) |
| 47 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 48 | # No cutOff |
| 49 | ->content_unlike(qr!\"cutOff":true!) |
| 50 | |
Akron | dffa939 | 2018-10-12 16:26:09 +0200 | [diff] [blame] | 51 | ->element_exists('li[data-text-sigle=GOE/AGI/00000]') |
| 52 | ->element_exists('li:nth-of-type(1) div.flop') |
| 53 | ->element_exists('li[data-text-sigle=GOE/AGI/00001]') |
| 54 | ->element_exists('li:nth-of-type(2) div.flip') |
| 55 | |
| 56 | # Match1 |
| 57 | ->element_exists('li:nth-of-type(1)' . |
| 58 | '[data-match-id="p2030-2031"]' . |
| 59 | '[data-text-sigle="GOE/AGI/00000"]' . |
| 60 | '[id="GOE/AGI/00000#p2030-2031"]' . |
| 61 | '[data-available-info^="base/s=spans"]' . |
| 62 | '[data-info^="{"]') |
| 63 | ->text_is('li:nth-of-type(1) div.meta', 'GOE/AGI/00000') |
| 64 | ->element_exists('li:nth-of-type(1) div.match-main div.match-wrap div.snippet') |
| 65 | ->element_exists('li:nth-of-type(1) div.snippet.startMore.endMore') |
| 66 | ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!) |
| 67 | ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!) |
| 68 | ->text_is('li:nth-of-type(1) div.snippet span.match mark','Baum') |
| 69 | ->text_like('li:nth-of-type(1) div.snippet span.context-right',qr!es war!) |
| 70 | ->text_is('li:nth-of-type(1) p.ref strong', 'Italienische Reise') |
| 71 | ->text_like('li:nth-of-type(1) p.ref', qr!by Goethe, Johann Wolfgang!) |
| 72 | ->text_is('li:nth-of-type(1) p.ref time[datetime=1982]', 1982) |
| 73 | ->text_is('li:nth-of-type(1) p.ref span.sigle', '[GOE/AGI/00000]') |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 74 | ->header_isnt('X-Kalamar-Cache', 'true') |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 75 | ; |
| 76 | |
Akron | 7093b81 | 2018-10-19 17:28:21 +0200 | [diff] [blame] | 77 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 78 | $t->get_ok('/?q=[orth=das') |
Akron | 7093b81 | 2018-10-19 17:28:21 +0200 | [diff] [blame] | 79 | ->status_is(400) |
| 80 | ->text_is('div.notify-error:nth-of-type(1)', '302: Parantheses/brackets unbalanced.') |
| 81 | ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!) |
| 82 | ; |
| 83 | |
Akron | 73f3608 | 2018-10-25 15:34:59 +0200 | [diff] [blame] | 84 | # Check for query error with ql (from remote.t) |
| 85 | $t->get_ok('/?q=[orth=das&ql=poliqarp') |
| 86 | ->element_exists('.notify-error') |
| 87 | ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.') |
| 88 | ->content_like(qr!KorAP\.koralQuery =!) |
| 89 | ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the search.') |
| 90 | ; |
| 91 | |
| 92 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 93 | # Query with partial cache (for total results) |
| 94 | $t->get_ok('/?q=baum') |
| 95 | ->status_is(200) |
| 96 | ->text_is('#error','') |
| 97 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 98 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
| 99 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
| 100 | ->header_isnt('X-Kalamar-Cache', 'true') |
| 101 | ->content_like(qr!\"cutOff":true!) |
| 102 | ->text_is('#total-results', 51) |
| 103 | ; |
| 104 | |
Akron | 385249d | 2018-10-29 12:26:29 +0100 | [diff] [blame] | 105 | # Query without partial cache (unfortunately) (but no total results) |
| 106 | $t->get_ok('/?q=baum&cutoff=true') |
| 107 | ->status_is(200) |
| 108 | ->text_is('#error','') |
| 109 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 110 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
| 111 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
| 112 | ->header_isnt('X-Kalamar-Cache', 'true') |
| 113 | ->content_like(qr!\"cutOff":true!) |
| 114 | ->element_exists_not('#total-results') |
| 115 | ; |
| 116 | |
| 117 | # Query with partial cache (but no total results) |
| 118 | $t->get_ok('/?q=baum&cutoff=true') |
| 119 | ->status_is(200) |
| 120 | ->text_is('#error','') |
| 121 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 122 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
| 123 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
| 124 | ->header_is('X-Kalamar-Cache', 'true') |
| 125 | ->content_like(qr!\"cutOff":true!) |
| 126 | ->element_exists_not('#total-results') |
| 127 | ; |
| 128 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 129 | # Query with full cache |
| 130 | $t->get_ok('/?q=baum') |
| 131 | ->status_is(200) |
| 132 | ->text_is('#error','') |
| 133 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 134 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
| 135 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
| 136 | ->header_is('X-Kalamar-Cache', 'true') |
| 137 | ->content_like(qr!\"cutOff":true!) |
| 138 | ->text_is('#total-results', 51) |
| 139 | ; |
| 140 | |
| 141 | |
| 142 | # Query with page information |
| 143 | $t->get_ok('/?q=der&p=1&count=2') |
| 144 | ->status_is(200) |
| 145 | ->text_is('#error','') |
| 146 | ->text_is('title', 'KorAP: Find »der« with Poliqarp') |
| 147 | |
| 148 | # Total results |
| 149 | ->text_is('#total-results', '14,581') |
| 150 | |
| 151 | # Total pages |
| 152 | ->element_count_is('#pagination a', 7) |
| 153 | ->text_is('#pagination a:nth-of-type(6) span', 7291) |
| 154 | ->content_like(qr!\"count":2!) |
| 155 | ->content_like(qr!\"startIndex":0!) |
| 156 | ->content_like(qr!\"itemsPerPage":2!) |
| 157 | |
| 158 | # No caching |
| 159 | ->header_isnt('X-Kalamar-Cache', 'true') |
| 160 | |
| 161 | # Not searched for "der" before |
| 162 | ->content_unlike(qr!\"cutOff":true!) |
| 163 | ; |
| 164 | |
| 165 | # Query with page information - next page |
| 166 | $t->get_ok('/?q=der&p=2&count=2') |
| 167 | ->status_is(200) |
| 168 | ->text_is('#error','') |
| 169 | ->text_is('title', 'KorAP: Find »der« with Poliqarp') |
| 170 | |
| 171 | # Total results |
| 172 | ->text_is('#total-results', '14,581') |
| 173 | |
| 174 | # Total pages |
| 175 | ->element_count_is('#pagination a', 7) |
| 176 | ->text_is('#pagination a:nth-of-type(6) span', 7291) |
| 177 | ->content_like(qr!\"count":2!) |
| 178 | ->content_like(qr!\"itemsPerPage":2!) |
| 179 | ->content_like(qr!\"startIndex":2!) |
| 180 | |
| 181 | # No caching |
| 182 | ->header_isnt('X-Kalamar-Cache', 'true') |
| 183 | ->content_like(qr!\"cutOff":true!) |
| 184 | ; |
| 185 | |
Akron | c4be819 | 2018-10-25 16:07:53 +0200 | [diff] [blame] | 186 | # Query with failing parameters |
| 187 | $t->get_ok('/?q=fantastisch&ql=Fabelsprache') |
| 188 | ->status_is(400) |
| 189 | ->text_is('noscript div.notify-error', 'Parameter "ql" invalid') |
| 190 | ->element_count_is('noscript div.notify-error', 1) |
| 191 | ; |
| 192 | $t->get_ok('/?q=fantastisch&cutoff=no') |
| 193 | ->status_is(400) |
| 194 | ->text_is('noscript div.notify-error', 'Parameter "cutoff" invalid') |
| 195 | ->element_count_is('noscript div.notify-error', 1) |
| 196 | ; |
| 197 | $t->get_ok('/?q=fantastisch&p=hui&o=hui&count=-8') |
| 198 | ->status_is(400) |
| 199 | ->text_like('noscript div.notify-error', qr!Parameter ".+?" invalid!) |
| 200 | ->element_count_is('noscript div.notify-error', 3) |
| 201 | ; |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 202 | |
Akron | d1ff8d8 | 2018-11-08 13:16:55 +0100 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | # Query too long |
| 206 | my $long_query = 'b' x 2000; |
| 207 | $t->get_ok('/?q=' . $long_query) |
| 208 | ->status_is(400) |
| 209 | ->text_is('#error','') |
| 210 | ->text_like('noscript div.notify-error', qr!Parameter ".+?" invalid!) |
| 211 | ; |
| 212 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 213 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 214 | done_testing; |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 215 | __END__ |