search_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::Translation::Search do
  4. describe '#execute' do
  5. let(:locale) { 'en-us' }
  6. let(:filter_locale) { locale }
  7. let(:query) { nil }
  8. let(:translation_search_service) { described_class.new(locale: filter_locale, query:) }
  9. context 'when query is nil', :aggregate_failures do
  10. it 'return default list' do
  11. expect(translation_search_service.execute[:items].count).to eq(150)
  12. expect(translation_search_service.execute[:total_count]).to be > 150
  13. end
  14. it 'includes already translated suggestions with translation item' do
  15. result = translation_search_service.execute
  16. priority_name = Ticket::Priority.first.name
  17. first_priority_translation = result[:items].select { |item| item[:source].eql?(priority_name) }
  18. expect(first_priority_translation.length).to be(1)
  19. expect(first_priority_translation[0][:id]).to eq(Translation.find_source(locale, priority_name).id)
  20. end
  21. end
  22. context 'with specific query' do
  23. let(:query) { SecureRandom.uuid }
  24. before do
  25. create(:translation, locale: locale, source: "Other #{query}", target: "Andere #{query}")
  26. create(:translation, locale: locale, source: "Example #{query}", target: "Ein #{query}", is_synchronized_from_codebase: true)
  27. end
  28. context 'with not matching locale' do
  29. let(:filter_locale) { 'de-de' }
  30. it 'returns no result' do
  31. expect(translation_search_service.execute[:items].count).to eq(0)
  32. end
  33. end
  34. context 'when case sensitive should match' do
  35. let(:query) { Translation.last.source.downcase }
  36. it 'returns also with case insensitive a result' do
  37. expect(translation_search_service.execute[:items].count).to eq(2)
  38. end
  39. end
  40. context 'with matching query' do
  41. shared_examples 'returns filtered result' do |factory_name, attribute, count|
  42. before do
  43. create(factory_name, attribute => "#{factory_name} #{query}")
  44. end
  45. it 'returns filtered result' do
  46. expect(translation_search_service.execute[:items].count).to eq(count)
  47. end
  48. end
  49. context 'with custom priority' do
  50. it_behaves_like 'returns filtered result', :ticket_priority, 'name', 2
  51. end
  52. context 'with custom state' do
  53. it_behaves_like 'returns filtered result', :ticket_state, 'name', 2
  54. end
  55. context 'with custom overview' do
  56. it_behaves_like 'returns filtered result', :overview, 'name', 2
  57. end
  58. context 'with custom macro' do
  59. it_behaves_like 'returns filtered result', :macro, 'name', 2
  60. end
  61. context 'with custom object attribute' do
  62. before do
  63. create(:object_manager_attribute_select, display: "Select #{query}", data_option_options: [{ name: query, value: query }])
  64. end
  65. it 'returns filtered result' do
  66. expect(translation_search_service.execute[:items].count).to eq(3)
  67. end
  68. context 'with not translatabale option' do
  69. before do
  70. create(:object_manager_attribute_select, display: "Select2 #{query}", data_option: {
  71. default: '',
  72. options: [{ name: "item #{query}", value: "item #{query}" }],
  73. translate: false
  74. })
  75. end
  76. it 'returns filtered result' do
  77. expect(translation_search_service.execute[:items].count).to eq(4)
  78. end
  79. end
  80. end
  81. context 'with multiple suggestion with the same name' do
  82. before do
  83. create(:ticket_priority, name: "high #{query}")
  84. create(:object_manager_attribute_select, display: "high #{query}")
  85. end
  86. it 'returns filtered result without duplicate entries' do
  87. expect(translation_search_service.execute[:items].count).to eq(2)
  88. end
  89. end
  90. end
  91. end
  92. end
  93. end