spec/arel/array_spec.rb in postgres_ext-0.2.2 vs spec/arel/array_spec.rb in postgres_ext-0.3.0

- old
+ new

@@ -1,57 +1,77 @@ require 'spec_helper' describe 'Array Column Predicates' do - let!(:adapter) { ActiveRecord::Base.connection } + describe 'Array Overlap' do + it 'converts Arel overlap statment' do + arel_table = Person.arel_table - before do - adapter.create_table :arel_arrays, :force => true do |t| - t.string :tags, :array => true - t.integer :tag_ids, :array => true + arel_table.where(arel_table[:tags].overlap(['tag','tag 2'])).to_sql.should match /&& '\{"tag","tag 2"\}'/ end - class ArelArray < ActiveRecord::Base - attr_accessible :tags + it 'converts Arel overlap statment' do + arel_table = Person.arel_table + + arel_table.where(arel_table[:tag_ids].overlap([1,2])).to_sql.should match /&& '\{1,2\}'/ end - end - after do - adapter.drop_table :arel_arrays - Object.send(:remove_const, :ArelArray) + it 'works with count (and other predicates)' do + arel_table = Person.arel_table + + Person.where(arel_table[:tag_ids].overlap([1,2])).count.should eq 0 + end + + it 'returns matched records' do + one = Person.create!(:tags => ['one']) + two = Person.create!(:tags => ['two']) + arel_table = Person.arel_table + + query = arel_table.where(arel_table[:tags].overlap(['one'])).project(Arel.sql('*')) + Person.find_by_sql(query.to_sql).should include(one) + + query = arel_table.where(arel_table[:tags].overlap(['two'])).project(Arel.sql('*')) + Person.find_by_sql(query.to_sql).should include(two) + + query = arel_table.where(arel_table[:tags].overlap(['two','one'])).project(Arel.sql('*')) + Person.find_by_sql(query.to_sql).should include(two) + Person.find_by_sql(query.to_sql).should include(one) + end end - describe 'Array Overlap' do - it 'converts Arel array_overlap statment' do - arel_table = ArelArray.arel_table + describe 'Array Contains' do + it 'converts Arel contains statement and escapes strings' do + arel_table = Person.arel_table - arel_table.where(arel_table[:tags].array_overlap(['tag','tag 2'])).to_sql.should match /&& '\{"tag","tag 2"\}'/ + arel_table.where(arel_table[:tags].contains(['tag','tag 2'])).to_sql.should match /@> '\{"tag","tag 2"\}'/ end - it 'converts Arel array_overlap statment' do - arel_table = ArelArray.arel_table + it 'converts Arel contains statement with numbers' do + arel_table = Person.arel_table - arel_table.where(arel_table[:tag_ids].array_overlap([1,2])).to_sql.should match /&& '\{1,2\}'/ + arel_table.where(arel_table[:tag_ids].contains([1,2])).to_sql.should match /@> '\{1,2\}'/ end it 'works with count (and other predicates)' do - arel_table = ArelArray.arel_table + arel_table = Person.arel_table - ArelArray.where(arel_table[:tag_ids].array_overlap([1,2])).count.should eq 0 + Person.where(arel_table[:tag_ids].contains([1,2])).count.should eq 0 end it 'returns matched records' do - one = ArelArray.create!(:tags => ['one']) - two = ArelArray.create!(:tags => ['two']) - arel_table = ArelArray.arel_table + one = Person.create!(:tags => ['one', 'two', 'three']) + two = Person.create!(:tags => ['one', 'three']) + arel_table = Person.arel_table - query = arel_table.where(arel_table[:tags].array_overlap(['one'])).project(Arel.sql('*')) - ArelArray.find_by_sql(query.to_sql).should include(one) + query = arel_table.where(arel_table[:tags].contains(['one', 'two'])).project(Arel.sql('*')) + Person.find_by_sql(query.to_sql).should include one + Person.find_by_sql(query.to_sql).should_not include two - query = arel_table.where(arel_table[:tags].array_overlap(['two'])).project(Arel.sql('*')) - ArelArray.find_by_sql(query.to_sql).should include(two) + query = arel_table.where(arel_table[:tags].contains(['one', 'three'])).project(Arel.sql('*')) + Person.find_by_sql(query.to_sql).should include one + Person.find_by_sql(query.to_sql).should include two - query = arel_table.where(arel_table[:tags].array_overlap(['two','one'])).project(Arel.sql('*')) - ArelArray.find_by_sql(query.to_sql).should include(two) - ArelArray.find_by_sql(query.to_sql).should include(one) + query = arel_table.where(arel_table[:tags].contains(['two'])).project(Arel.sql('*')) + Person.find_by_sql(query.to_sql).should include one + Person.find_by_sql(query.to_sql).should_not include two end end end