spec/pgrel/hstore_spec.rb in pgrel-0.2.0 vs spec/pgrel/hstore_spec.rb in pgrel-0.3.0

- old
+ new

@@ -1,9 +1,9 @@ require 'spec_helper' describe Hstore do - before do + before(:all) do @connection = ActiveRecord::Base.connection @connection.transaction do @connection.create_table('hstores') do |t| t.hstore 'tags', default: {}, null: false @@ -12,11 +12,13 @@ end Hstore.reset_column_information end - after do + after { Hstore.delete_all } + + after(:all) do @connection.drop_table 'hstores', if_exists: true end let!(:setup) do Hstore.create!(name: 'a', tags: { a: 1, b: 2, f: true, d: 'a', g: 'b' }) @@ -97,27 +99,34 @@ records = Hstore.where.store(:tags).keys([:a, :c]) expect(records.size).to eq 1 expect(records.first.name).to eq 'e' end - it '#value' do - records = Hstore.where.store(:tags).value(1, false, [1, 2, { a: 1 }]) - expect(records.size).to eq 3 + describe '#overlap_values' do + let(:records) { Hstore.where.store(:tags).overlap_values(1, false, [1, 2, { a: 1 }]) } + + it 'returns records with overlapping values' do + expect(records.size).to eq 3 + end + + it 'calls avals function only once' do + expect(records.to_sql.scan(/avals/).count).to eq 1 + end end - it '#values' do - records = Hstore.where.store(:tags).values(1) + it '#contains_values' do + records = Hstore.where.store(:tags).contains_values(1) expect(records.size).to eq 1 expect(records.first.name).to eq 'a' - records = Hstore.where.store(:tags).values('2', 'b') + records = Hstore.where.store(:tags).contains_values('2', 'b') expect(records.size).to eq 2 - records = Hstore.where.store(:tags).values(true) + records = Hstore.where.store(:tags).contains_values(true) expect(records.size).to eq 2 - records = Hstore.where.store(:tags).values([1, 2, { a: 1 }], { a: 1, b: [1], f: false }) + records = Hstore.where.store(:tags).contains_values([1, 2, { a: 1 }], { a: 1, b: [1], f: false }) expect(records.size).to eq 1 end it '#any' do records = Hstore.where.store(:tags).any('b', 'f') @@ -201,8 +210,28 @@ subject.delete_pairs(f: true, a: 1) expect(Hstore.where.store(store, f: true)).to_not exist expect(Hstore.where.store(store, a: 1)).to_not exist expect(Hstore.where.store(store, f: false)).to exist expect(Hstore.where.store(store, a: 2)).to exist + end + end + + context "joins" do + before do + User.create!(name: "x", hstore: Hstore.find_by!(name: "a")) + User.create!(name: "y", hstore: Hstore.find_by!(name: "b")) + User.create!(name: "z", hstore: Hstore.find_by!(name: "c")) + end + + it "works" do + users = User.joins(:hstore).merge(Hstore.where.store(:tags).key(:a)) + expect(users.size).to eq 2 + expect(users.map(&:name)).to match_array(["x", "y"]) + end + + it "works with #contains_values" do + users = User.joins(:hstore).merge(Hstore.where.store(:tags).contains_values(1)) + expect(users.size).to eq 1 + expect(users.map(&:name)).to match_array(["x"]) end end end