2023-03-10 12:33:30 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe SearchQueryTransformer do
|
2023-09-04 15:20:35 +00:00
|
|
|
subject { described_class.new.apply(parser, current_account: account) }
|
2023-03-10 12:33:30 +00:00
|
|
|
|
2023-09-04 15:20:35 +00:00
|
|
|
let(:account) { Fabricate(:account) }
|
2023-09-01 07:43:12 +00:00
|
|
|
let(:parser) { SearchQueryParser.new.parse(query) }
|
2023-03-10 12:33:30 +00:00
|
|
|
|
2023-09-01 07:43:12 +00:00
|
|
|
context 'with "hello world"' do
|
|
|
|
let(:query) { 'hello world' }
|
|
|
|
|
|
|
|
it 'transforms clauses' do
|
2023-09-04 15:20:35 +00:00
|
|
|
expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello world)
|
|
|
|
expect(subject.send(:must_not_clauses)).to be_empty
|
|
|
|
expect(subject.send(:filter_clauses)).to be_empty
|
2023-09-01 07:43:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with "hello -world"' do
|
|
|
|
let(:query) { 'hello -world' }
|
|
|
|
|
|
|
|
it 'transforms clauses' do
|
2023-09-04 15:20:35 +00:00
|
|
|
expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello)
|
|
|
|
expect(subject.send(:must_not_clauses).map(&:term)).to match_array %w(world)
|
|
|
|
expect(subject.send(:filter_clauses)).to be_empty
|
2023-09-01 07:43:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with "hello is:reply"' do
|
|
|
|
let(:query) { 'hello is:reply' }
|
|
|
|
|
|
|
|
it 'transforms clauses' do
|
2023-09-04 15:20:35 +00:00
|
|
|
expect(subject.send(:must_clauses).map(&:term)).to match_array %w(hello)
|
|
|
|
expect(subject.send(:must_not_clauses)).to be_empty
|
|
|
|
expect(subject.send(:filter_clauses).map(&:term)).to match_array %w(reply)
|
2023-09-01 07:43:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with "foo: bar"' do
|
|
|
|
let(:query) { 'foo: bar' }
|
|
|
|
|
|
|
|
it 'transforms clauses' do
|
2023-09-04 15:20:35 +00:00
|
|
|
expect(subject.send(:must_clauses).map(&:term)).to match_array %w(foo bar)
|
|
|
|
expect(subject.send(:must_not_clauses)).to be_empty
|
|
|
|
expect(subject.send(:filter_clauses)).to be_empty
|
2023-09-01 07:43:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with "foo:bar"' do
|
|
|
|
let(:query) { 'foo:bar' }
|
|
|
|
|
|
|
|
it 'transforms clauses' do
|
2023-09-04 15:20:35 +00:00
|
|
|
expect(subject.send(:must_clauses).map(&:term)).to contain_exactly('foo bar')
|
|
|
|
expect(subject.send(:must_not_clauses)).to be_empty
|
|
|
|
expect(subject.send(:filter_clauses)).to be_empty
|
2023-03-10 12:33:30 +00:00
|
|
|
end
|
|
|
|
end
|
2023-09-08 12:25:00 +00:00
|
|
|
|
|
|
|
context 'with \'"hello world"\'' do
|
|
|
|
let(:query) { '"hello world"' }
|
|
|
|
|
|
|
|
it 'transforms clauses' do
|
|
|
|
expect(subject.send(:must_clauses).map(&:phrase)).to contain_exactly('hello world')
|
|
|
|
expect(subject.send(:must_not_clauses)).to be_empty
|
|
|
|
expect(subject.send(:filter_clauses)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with \'before:"2022-01-01 23:00"\'' do
|
|
|
|
let(:query) { 'before:"2022-01-01 23:00"' }
|
|
|
|
|
|
|
|
it 'transforms clauses' do
|
|
|
|
expect(subject.send(:must_clauses)).to be_empty
|
|
|
|
expect(subject.send(:must_not_clauses)).to be_empty
|
|
|
|
expect(subject.send(:filter_clauses).map(&:term)).to contain_exactly(lt: '2022-01-01 23:00', time_zone: 'UTC')
|
|
|
|
end
|
|
|
|
end
|
2023-03-10 12:33:30 +00:00
|
|
|
end
|