search_query_transformer_spec.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. 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. context 'with "hello world"' do
  8. let(:query) { 'hello world' }
  9. it 'transforms clauses' do
  10. expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello world)
  11. expect(subject.send(:must_not_clauses)).to be_empty
  12. expect(subject.send(:filter_clauses)).to be_empty
  13. end
  14. end
  15. context 'with "hello -world"' do
  16. let(:query) { 'hello -world' }
  17. it 'transforms clauses' do
  18. expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello)
  19. expect(subject.send(:must_not_clauses).map(&:term)).to match_array %w(world)
  20. expect(subject.send(:filter_clauses)).to be_empty
  21. end
  22. end
  23. context 'with "hello is:reply"' do
  24. let(:query) { 'hello is:reply' }
  25. it 'transforms clauses' do
  26. expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello)
  27. expect(subject.send(:must_not_clauses)).to be_empty
  28. expect(subject.send(:filter_clauses).map(&:term)).to match_array %w(reply)
  29. end
  30. end
  31. context 'with "foo: bar"' do
  32. let(:query) { 'foo: bar' }
  33. it 'transforms clauses' do
  34. expect(subject.send(:must_clauses).map(&:term)).to match_array %w(foo bar)
  35. expect(subject.send(:must_not_clauses)).to be_empty
  36. expect(subject.send(:filter_clauses)).to be_empty
  37. end
  38. end
  39. context 'with "foo:bar"' do
  40. let(:query) { 'foo:bar' }
  41. it 'transforms clauses' do
  42. expect(subject.send(:must_clauses).map(&:term)).to contain_exactly('foo bar')
  43. expect(subject.send(:must_not_clauses)).to be_empty
  44. expect(subject.send(:filter_clauses)).to be_empty
  45. end
  46. end
  47. context 'with \'"hello world"\'' do
  48. let(:query) { '"hello world"' }
  49. it 'transforms clauses' do
  50. expect(subject.send(:must_clauses).map(&:phrase)).to contain_exactly('hello world')
  51. expect(subject.send(:must_not_clauses)).to be_empty
  52. expect(subject.send(:filter_clauses)).to be_empty
  53. end
  54. end
  55. context 'with \'before:"2022-01-01 23:00"\'' do
  56. let(:query) { 'before:"2022-01-01 23:00"' }
  57. it 'transforms clauses' do
  58. expect(subject.send(:must_clauses)).to be_empty
  59. expect(subject.send(:must_not_clauses)).to be_empty
  60. expect(subject.send(:filter_clauses).map(&:term)).to contain_exactly(lt: '2022-01-01 23:00', time_zone: 'UTC')
  61. end
  62. end
  63. end