From ff7cd0a21d6a829e73a2e591c29cc6786cf41af9 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Tue, 11 Feb 2020 13:36:15 +0100 Subject: [PATCH] add tests for field mappings and joins --- spec/lib/pg_searchable_new_spec.rb | 70 ++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/spec/lib/pg_searchable_new_spec.rb b/spec/lib/pg_searchable_new_spec.rb index 9775582..1df925a 100644 --- a/spec/lib/pg_searchable_new_spec.rb +++ b/spec/lib/pg_searchable_new_spec.rb @@ -33,6 +33,12 @@ describe PgSearchable do end describe 'single model' do + it 'fails if column name is unknown' do + record1 = VectorModel.create name: 'hamo', value: '100' + + expect{(VectorModel.scope_search("device_id:#{record1.id}"))}.to raise_error(RuntimeError, "Unknown field 'device_id'") + end + it 'searches only default column if no column name is used' do record1 = VectorModel.create name: 'hamo', value: '-45' record2 = VectorModel.create name: 'meho', value: '120' @@ -97,5 +103,69 @@ describe PgSearchable do expect(VectorModel.scope_search('name:hamo AND (value:120 OR value:80)')).to be_empty end end + + describe 'field mappings and joins' do + it 'does not fail if column is unknown but mapping with that name exists' do + record1 = VectorModelWithMappings.create name: 'hamo', value: '-45' + + expect(VectorModelWithMappings.scope_search("device_id:#{record1.id}")).to contain_exactly(record1) + end + + it 'can search in referenced column' do + record = DynamicModelWithTagValues.create name: 'something', value: 'amazing' + Tag.create(taggable: record, value: 'red') + + expect(DynamicModelWithTagValues.scope_search('tag:red')).to contain_exactly(record) + end + + it 'can search in referenced column with multiple search terms' do + record1 = DynamicModelWithTagValues.create name: 'something', value: 'amazing' + record2 = DynamicModelWithTagValues.create name: 'new record', value: 'not so amazing' + Tag.create(taggable: record1, value: 'red') + Tag.create(taggable: record2, value: 'green') + + expect(DynamicModelWithTagValues.scope_search('tag:red tag:green')).to contain_exactly(record1, record2) + end + + it 'can search in referenced column and in model columns with multiple search terms' do + record1 = DynamicModelWithTagValues.create name: 'something', value: 'amazing' + record2 = DynamicModelWithTagValues.create name: 'new record', value: 'not so amazing' + Tag.create(taggable: record1, value: 'red') + Tag.create(taggable: record2, value: 'green') + + expect(DynamicModelWithTagValues.scope_search('tag:red value:"not"')).to contain_exactly(record1, record2) + end + + it 'can search in referenced column and in model columns with multiple search terms connected with logical operators' do + record1 = DynamicModelWithTagValues.create name: 'something', value: 'amazing' + record2 = DynamicModelWithTagValues.create name: 'new record', value: 'not so amazing' + record3 = DynamicModelWithTagValues.create name: 'last one', value: 'no value' + record4 = DynamicModelWithTagValues.create name: 'really last one', value: 'no value' + Tag.create(taggable: record1, value: 'red') + Tag.create(taggable: record1, value: 'green') + Tag.create(taggable: record2, value: 'black') + Tag.create(taggable: record3, value: '-12') + Tag.create(taggable: record4, value: '-') + + expect(DynamicModelWithTagValues.scope_search('tag:red or tag:black')).to contain_exactly(record1, record2) + expect(DynamicModelWithTagValues.scope_search('tag:red and tag:black')).to be_empty + expect(DynamicModelWithTagValues.scope_search('tag:red or tag:green')).to contain_exactly(record1) + expect(DynamicModelWithTagValues.scope_search('not tag:-12 and not value:amazing')).to contain_exactly(record4) + end + + it 'can search in referenced column and in model columns with multiple search terms connected with logical operators and with brackets' do + record1 = DynamicModelWithTagValues.create name: 'something', value: 'amazing' + record2 = DynamicModelWithTagValues.create name: 'new record', value: 'not so amazing' + record3 = DynamicModelWithTagValues.create name: 'last one', value: 'no value' + record4 = DynamicModelWithTagValues.create name: 'really last one', value: 'no value' + Tag.create(taggable: record1, value: 'red') + Tag.create(taggable: record1, value: 'green') + Tag.create(taggable: record2, value: 'black') + Tag.create(taggable: record3, value: '-12') + Tag.create(taggable: record4, value: '-') + + expect(DynamicModelWithTagValues.scope_search('(tag:- and not tag:12) or (value:"amazing" and not value:"not")')).to contain_exactly(record1, record4) + end + end end end