can_search_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'CanSearch', searchindex: true, type: :model do
  4. let(:roles) { create_list(:role, 100) }
  5. before do
  6. roles
  7. searchindex_model_reload([Role])
  8. end
  9. def search(params)
  10. Role.search({ current_user: User.find(1), full: false, with_total_count: true }.merge(params))
  11. end
  12. it 'does search by query', :aggregate_failures do
  13. expected_result = 10
  14. params = { query: 'TestRole*', limit: 10, with_total_count: false, full: true }
  15. expect(search(params).count).to eq(expected_result)
  16. allow(SearchIndexBackend).to receive(:enabled?).and_return(false)
  17. expect(search(params).count).to eq(expected_result)
  18. end
  19. it 'does search by query not full', :aggregate_failures do
  20. expected_result = 10
  21. params = { query: 'TestRole*', limit: 10, with_total_count: false }
  22. expect(search(params).count).to eq(expected_result)
  23. allow(SearchIndexBackend).to receive(:enabled?).and_return(false)
  24. expect(search(params).count).to eq(expected_result)
  25. end
  26. it 'does search by query with total count', :aggregate_failures do
  27. expected_result = 100
  28. params = { query: 'TestRole*', limit: 10 }
  29. expect(search(params)[:object_ids].count).to eq(10)
  30. expect(search(params)[:total_count]).to eq(expected_result)
  31. allow(SearchIndexBackend).to receive(:enabled?).and_return(false)
  32. expect(search(params)[:object_ids].count).to eq(10)
  33. expect(search(params)[:total_count]).to eq(expected_result)
  34. end
  35. it 'does search by query only total count', :aggregate_failures do
  36. expected_result = 100
  37. params = { query: 'TestRole*', limit: 10, only_total_count: true }
  38. expect(search(params)[:total_count]).to eq(expected_result)
  39. allow(SearchIndexBackend).to receive(:enabled?).and_return(false)
  40. expect(search(params)[:total_count]).to eq(expected_result)
  41. end
  42. it 'does search by query and ids', :aggregate_failures do
  43. expected_result = roles[0..3].map(&:id)
  44. params = { query: 'TestRole*', limit: 10, ids: expected_result }
  45. expect(search(params)[:object_ids].sort.map(&:to_i)).to eq(expected_result)
  46. expect(search(params)[:total_count]).to eq(4)
  47. allow(SearchIndexBackend).to receive(:enabled?).and_return(false)
  48. expect(search(params)[:object_ids].sort.map(&:to_i)).to eq(expected_result)
  49. expect(search(params)[:total_count]).to eq(4)
  50. end
  51. it 'does search by query and ids and sorted', :aggregate_failures do
  52. expected_result = roles[0..3].map(&:id).reverse
  53. params = { query: 'TestRole*', limit: 10, ids: expected_result, sort_by: 'id', order_by: 'desc' }
  54. expect(search(params)[:object_ids].map(&:to_i)).to eq(expected_result)
  55. expect(search(params)[:total_count]).to eq(4)
  56. allow(SearchIndexBackend).to receive(:enabled?).and_return(false)
  57. expect(search(params)[:object_ids].map(&:to_i)).to eq(expected_result)
  58. expect(search(params)[:total_count]).to eq(4)
  59. end
  60. it 'does search by query and ids and sorted and offset', :aggregate_failures do
  61. ids = roles[0..3].map(&:id)
  62. params = { query: 'TestRole*', limit: 1, ids: ids, sort_by: 'id', order_by: 'asc' }
  63. expect(search(params)[:object_ids].map(&:to_i)).to eq([ids[0]])
  64. expect(search(params)[:total_count]).to eq(4)
  65. expect(search(params.merge(offset: 1))[:object_ids].map(&:to_i)).to eq([ids[1]])
  66. expect(search(params.merge(offset: 1))[:total_count]).to eq(4)
  67. expect(search(params.merge(offset: 2))[:object_ids].map(&:to_i)).to eq([ids[2]])
  68. expect(search(params.merge(offset: 2))[:total_count]).to eq(4)
  69. allow(SearchIndexBackend).to receive(:enabled?).and_return(false)
  70. expect(search(params)[:object_ids].map(&:to_i)).to eq([ids[0]])
  71. expect(search(params)[:total_count]).to eq(4)
  72. expect(search(params.merge(offset: 1))[:object_ids].map(&:to_i)).to eq([ids[1]])
  73. expect(search(params.merge(offset: 1))[:total_count]).to eq(4)
  74. expect(search(params.merge(offset: 2))[:object_ids].map(&:to_i)).to eq([ids[2]])
  75. expect(search(params.merge(offset: 2))[:total_count]).to eq(4)
  76. end
  77. it 'does search by query and condition and sorted and offset', :aggregate_failures do
  78. ids = roles[0..3].map(&:id)
  79. params = { query: 'TestRole*', limit: 1, condition: { 'role.id' => { 'operator' => 'is', 'value' => ids.map(&:to_s) } }, sort_by: 'id', order_by: 'asc' }
  80. expect(search(params)[:object_ids].map(&:to_i)).to eq([ids[0]])
  81. expect(search(params)[:total_count]).to eq(4)
  82. expect(search(params.merge(offset: 1))[:object_ids].map(&:to_i)).to eq([ids[1]])
  83. expect(search(params.merge(offset: 1))[:total_count]).to eq(4)
  84. expect(search(params.merge(offset: 2))[:object_ids].map(&:to_i)).to eq([ids[2]])
  85. expect(search(params.merge(offset: 2))[:total_count]).to eq(4)
  86. allow(SearchIndexBackend).to receive(:enabled?).and_return(false)
  87. expect(search(params)[:object_ids].map(&:to_i)).to eq([ids[0]])
  88. expect(search(params)[:total_count]).to eq(4)
  89. expect(search(params.merge(offset: 1))[:object_ids].map(&:to_i)).to eq([ids[1]])
  90. expect(search(params.merge(offset: 1))[:total_count]).to eq(4)
  91. expect(search(params.merge(offset: 2))[:object_ids].map(&:to_i)).to eq([ids[2]])
  92. expect(search(params.merge(offset: 2))[:total_count]).to eq(4)
  93. end
  94. end