search_query_transformer_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe SearchQueryTransformer do
  4. subject { described_class.new.apply(parser, current_account: account) }
  5. let(:account) { Fabricate(:account) }
  6. let(:parser) { SearchQueryParser.new.parse(query) }
  7. shared_examples 'date operator' do |operator|
  8. let(:statement_operations) { [] }
  9. [
  10. ['2022-01-01', '2022-01-01'],
  11. ['"2022-01-01"', '2022-01-01'],
  12. ['12345678', '12345678'],
  13. ['"12345678"', '12345678'],
  14. ['"2024-10-31T23:47:20Z"', '2024-10-31T23:47:20Z'],
  15. ].each do |value, parsed|
  16. context "with #{operator}:#{value}" do
  17. let(:query) { "#{operator}:#{value}" }
  18. it 'transforms clauses' do
  19. ops = statement_operations.index_with { |_op| parsed }
  20. expect(subject.send(:must_clauses)).to be_empty
  21. expect(subject.send(:must_not_clauses)).to be_empty
  22. expect(subject.send(:filter_clauses).map(&:term)).to contain_exactly(**ops, time_zone: 'UTC')
  23. end
  24. end
  25. end
  26. context "with #{operator}:\"abc\"" do
  27. let(:query) { "#{operator}:\"abc\"" }
  28. it 'raises an exception' do
  29. expect { subject }.to raise_error(Date::Error)
  30. end
  31. end
  32. end
  33. context 'with "hello world"' do
  34. let(:query) { 'hello world' }
  35. it 'transforms clauses' do
  36. expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello world)
  37. expect(subject.send(:must_not_clauses)).to be_empty
  38. expect(subject.send(:filter_clauses)).to be_empty
  39. end
  40. end
  41. context 'with "hello -world"' do
  42. let(:query) { 'hello -world' }
  43. it 'transforms clauses' do
  44. expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello)
  45. expect(subject.send(:must_not_clauses).map(&:term)).to match_array %w(world)
  46. expect(subject.send(:filter_clauses)).to be_empty
  47. end
  48. end
  49. context 'with "hello is:reply"' do
  50. let(:query) { 'hello is:reply' }
  51. it 'transforms clauses' do
  52. expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello)
  53. expect(subject.send(:must_not_clauses)).to be_empty
  54. expect(subject.send(:filter_clauses).map(&:term)).to match_array %w(reply)
  55. end
  56. end
  57. context 'with "foo: bar"' do
  58. let(:query) { 'foo: bar' }
  59. it 'transforms clauses' do
  60. expect(subject.send(:must_clauses).map(&:term)).to match_array %w(foo bar)
  61. expect(subject.send(:must_not_clauses)).to be_empty
  62. expect(subject.send(:filter_clauses)).to be_empty
  63. end
  64. end
  65. context 'with "foo:bar"' do
  66. let(:query) { 'foo:bar' }
  67. it 'transforms clauses' do
  68. expect(subject.send(:must_clauses).map(&:term)).to contain_exactly('foo bar')
  69. expect(subject.send(:must_not_clauses)).to be_empty
  70. expect(subject.send(:filter_clauses)).to be_empty
  71. end
  72. end
  73. context 'with \'"hello world"\'' do
  74. let(:query) { '"hello world"' }
  75. it 'transforms clauses' do
  76. expect(subject.send(:must_clauses).map(&:phrase)).to contain_exactly('hello world')
  77. expect(subject.send(:must_not_clauses)).to be_empty
  78. expect(subject.send(:filter_clauses)).to be_empty
  79. end
  80. end
  81. context 'with \'is:"foo bar"\'' do
  82. let(:query) { 'is:"foo bar"' }
  83. it 'transforms clauses' do
  84. expect(subject.send(:must_clauses)).to be_empty
  85. expect(subject.send(:must_not_clauses)).to be_empty
  86. expect(subject.send(:filter_clauses).map(&:term)).to contain_exactly('foo bar')
  87. end
  88. end
  89. context 'with date operators' do
  90. context 'with "before"' do
  91. it_behaves_like 'date operator', 'before' do
  92. let(:statement_operations) { [:lt] }
  93. end
  94. end
  95. context 'with "after"' do
  96. it_behaves_like 'date operator', 'after' do
  97. let(:statement_operations) { [:gt] }
  98. end
  99. end
  100. context 'with "during"' do
  101. it_behaves_like 'date operator', 'during' do
  102. let(:statement_operations) { [:gte, :lte] }
  103. end
  104. end
  105. end
  106. end