From 7dce5315e19fe8de54e41de1911b5b68188c48df Mon Sep 17 00:00:00 2001 From: Billy Zhou Date: Wed, 3 Jul 2019 17:50:47 +0200 Subject: [PATCH] Added Enhancement #2569 - Enhance tag search to use fulltext search --- app/controllers/tags_controller.rb | 2 +- spec/requests/tag_spec.rb | 75 ++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 spec/requests/tag_spec.rb diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 9f636458a..66810265f 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -5,7 +5,7 @@ class TagsController < ApplicationController # GET /api/v1/tag_search?term=abc def search - list = Tag::Item.where('name_downcase LIKE ?', "#{params[:term].strip.downcase}%").order(name: :asc).limit(params[:limit] || 10) + list = Tag::Item.where('name_downcase LIKE ?', "%#{params[:term].strip.downcase}%").order(name: :asc).limit(params[:limit] || 10) results = [] list.each do |item| result = { diff --git a/spec/requests/tag_spec.rb b/spec/requests/tag_spec.rb new file mode 100644 index 000000000..21855facd --- /dev/null +++ b/spec/requests/tag_spec.rb @@ -0,0 +1,75 @@ +require 'rails_helper' + +RSpec.describe Tag, type: :request do + + describe 'request handling' do + + let(:agent) { create(:agent_user) } + + context 'tag search' do + before do + authenticated_as(agent) + end + + let!(:tags) do + [ + Tag::Item.lookup_by_name_and_create('foobar'), + Tag::Item.lookup_by_name_and_create('xxxxxxxxx_DUMMY_VALUE1'), + Tag::Item.lookup_by_name_and_create('121212121_DUMMY_VALUE2'), + Tag::Item.lookup_by_name_and_create('oxoxoxoxo_DUMMY_VALUE3'), + ] + end + + let(:foobar_tag) { tags.first } + + shared_examples 'foobar tag found using' do |search_term:| + it "found 1 tag using search term '#{search_term}'" do + get '/api/v1/tag_search', params: { term: search_term } + expect(response).to have_http_status(:ok) + expect(json_response).to contain_exactly( 'id' => foobar_tag.id, 'value' => foobar_tag.name ) + end + end + + shared_examples 'no tag found using' do |search_term:| + it "found 0 tags using search term '#{search_term}'" do + get '/api/v1/tag_search', params: { term: search_term } + expect(response).to have_http_status(:ok) + expect(json_response).to contain_exactly() + end + end + + shared_examples 'all tags found using' do |search_term:| + it "found all tags using search term '#{search_term}'" do + get '/api/v1/tag_search', params: { term: search_term } + expect(response).to have_http_status(:ok) + expect(json_response.size).to eq(Tag::Item.count) + end + end + + describe 'using prefix search' do + include_examples 'foobar tag found using', search_term: 'foobar' + include_examples 'foobar tag found using', search_term: 'foo' + include_examples 'foobar tag found using', search_term: 'f' + end + + describe 'using substring search (added via Enhancement #2569 - Enhance tag search to use fulltext search)' do + include_examples 'foobar tag found using', search_term: 'bar' + include_examples 'foobar tag found using', search_term: 'ar' + include_examples 'foobar tag found using', search_term: 'oo' + end + + describe 'using wildcard search' do + include_examples 'all tags found using', search_term: ' ' + include_examples 'all tags found using', search_term: ' ' + end + + describe 'using invalid search terms' do + include_examples 'no tag found using', search_term: 'WRONG_VALUE' + include_examples 'no tag found using', search_term: '-' + include_examples 'no tag found using', search_term: 'fooar' + include_examples 'no tag found using', search_term: '1foobar' + include_examples 'no tag found using', search_term: 'foobar2' + end + end + end +end