|
231 | 231 | expect(query.autocomplete.first).to eq(otu2) |
232 | 232 | end |
233 | 233 |
|
| 234 | + # Regression: `terms` (Queries::Query#terms) always returns a 2-element |
| 235 | + # array of wildcarded copies of the *whole* query_string, never the |
| 236 | + # query_string split on whitespace. `autocomplete_taxon_name_hybrid` was |
| 237 | + # (mis)written assuming `terms.first`/`terms.second` were the genus and |
| 238 | + # otu.name (e.g. "Tapinoma CASC_2231") words, so `terms.length == 2` was |
| 239 | + # vacuously true for any query, and it trigram-matched the *entire* |
| 240 | + # phrase against both `taxon_names.cached` and `otus.name` instead of |
| 241 | + # matching each word against its own column. |
| 242 | + context '#autocomplete_taxon_name_hybrid' do |
| 243 | + let!(:tapinoma) { Protonym.create!(name: 'Tapinoma', rank_class: Ranks.lookup(:iczn, 'genus'), parent: root) } |
| 244 | + let!(:target) { Otu.create!(taxon_name: tapinoma, name: 'CASC_2231', project_id: project_id) } |
| 245 | + |
| 246 | + specify 'matches genus term to taxon_names.cached and otu term to otus.name, independently' do |
| 247 | + # This otu.name redundantly restates the genus (as happens in real |
| 248 | + # data, e.g. "Scaphoideus menoni_E26" under genus "Scaphoideus"). |
| 249 | + # Under the old whole-phrase bug this shared genus text was enough |
| 250 | + # to pass both trigram conditions even though the otu-specific part |
| 251 | + # ("sp2_Z9") has nothing to do with the query's otu term |
| 252 | + # ("CASC_2231"). |
| 253 | + decoy = Otu.create!(taxon_name: tapinoma, name: 'Tapinoma sp2_Z9', project_id: project_id) |
| 254 | + |
| 255 | + q = Queries::Otu::Autocomplete.new('Tapinoma CASC_2231', project_id: project_id) |
| 256 | + r = q.autocomplete_taxon_name_hybrid.to_a |
| 257 | + |
| 258 | + expect(r).to include(target) |
| 259 | + expect(r).not_to include(decoy) |
| 260 | + end |
234 | 261 |
|
| 262 | + specify 'does not run for single-word queries' do |
| 263 | + q = Queries::Otu::Autocomplete.new('Tapinoma', project_id: project_id) |
| 264 | + expect(q.autocomplete_taxon_name_hybrid).to be_nil |
| 265 | + end |
235 | 266 |
|
| 267 | + specify 'does not run for queries with more than two words' do |
| 268 | + q = Queries::Otu::Autocomplete.new('Tapinoma CASC 2231', project_id: project_id) |
| 269 | + expect(q.autocomplete_taxon_name_hybrid).to be_nil |
| 270 | + end |
| 271 | + end |
236 | 272 |
|
237 | 273 | end |
238 | 274 |
|
|
0 commit comments