tag_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. require 'rails_helper'
  2. RSpec.describe Tag, type: :request do
  3. describe 'request handling' do
  4. let(:agent) { create(:agent) }
  5. context 'tag search' do
  6. before do
  7. authenticated_as(agent)
  8. end
  9. let!(:tags) do
  10. [
  11. Tag::Item.lookup_by_name_and_create('foobar'),
  12. Tag::Item.lookup_by_name_and_create('xxxxxxxxx_DUMMY_VALUE1'),
  13. Tag::Item.lookup_by_name_and_create('121212121_DUMMY_VALUE2'),
  14. Tag::Item.lookup_by_name_and_create('oxoxoxoxo_DUMMY_VALUE3'),
  15. ]
  16. end
  17. let(:foobar_tag) { tags.first }
  18. shared_examples 'foobar tag found using' do |search_term:|
  19. it "found 1 tag using search term '#{search_term}'" do
  20. get '/api/v1/tag_search', params: { term: search_term }
  21. expect(response).to have_http_status(:ok)
  22. expect(json_response).to contain_exactly( 'id' => foobar_tag.id, 'value' => foobar_tag.name )
  23. end
  24. end
  25. shared_examples 'no tag found using' do |search_term:|
  26. it "found 0 tags using search term '#{search_term}'" do
  27. get '/api/v1/tag_search', params: { term: search_term }
  28. expect(response).to have_http_status(:ok)
  29. expect(json_response).to contain_exactly()
  30. end
  31. end
  32. shared_examples 'all tags found using' do |search_term:|
  33. it "found all tags using search term '#{search_term}'" do
  34. get '/api/v1/tag_search', params: { term: search_term }
  35. expect(response).to have_http_status(:ok)
  36. expect(json_response.size).to eq(Tag::Item.count)
  37. end
  38. end
  39. describe 'using prefix search' do
  40. include_examples 'foobar tag found using', search_term: 'foobar'
  41. include_examples 'foobar tag found using', search_term: 'foo'
  42. include_examples 'foobar tag found using', search_term: 'f'
  43. end
  44. describe 'using substring search (added via Enhancement #2569 - Enhance tag search to use fulltext search)' do
  45. include_examples 'foobar tag found using', search_term: 'bar'
  46. include_examples 'foobar tag found using', search_term: 'ar'
  47. include_examples 'foobar tag found using', search_term: 'oo'
  48. end
  49. describe 'using wildcard search' do
  50. include_examples 'all tags found using', search_term: ' '
  51. include_examples 'all tags found using', search_term: ' '
  52. end
  53. describe 'using invalid search terms' do
  54. include_examples 'no tag found using', search_term: 'WRONG_VALUE'
  55. include_examples 'no tag found using', search_term: '-'
  56. include_examples 'no tag found using', search_term: 'fooar'
  57. include_examples 'no tag found using', search_term: '1foobar'
  58. include_examples 'no tag found using', search_term: 'foobar2'
  59. end
  60. end
  61. end
  62. end