# frozen_string_literal: true require 'spec_helper' describe 'Queries with Range criteria' do let(:now_utc) { Time.utc(2020, 1, 1, 16, 0, 0, 0) } let(:now_in_zone) { now_utc.in_time_zone('Asia/Tokyo') } let(:today) { Date.new(2020, 1, 1) } let(:labels) do [ Label.new(age: 12), Label.new(age: 16) ] end let!(:band1) { Band.create!(likes: 0, rating: 0.9, founded: today, updated_at: now_utc) } let!(:band2) { Band.create!(likes: 1, rating: 1.0, founded: today + 1.day, updated_at: now_utc + 1.days) } let!(:band3) { Band.create!(likes: 2, rating: 2.9, founded: today + 2.days, updated_at: now_utc + 2.days) } let!(:band4) { Band.create!(likes: 3, rating: 3.0, founded: today + 3.days, updated_at: now_utc + 3.days) } let!(:band5) { Band.create!(likes: 4, rating: 3.1, founded: today + 4.days, updated_at: now_utc + 4.days, labels: labels) } context 'Range criteria vs Integer field' do it 'returns objects within the range' do expect(Band.where(likes: 1..3).to_a).to eq [band2, band3, band4] expect(Band.where(likes: 1...3).to_a).to eq [band2, band3] end context 'endless range' do ruby_version_gte '2.6' it 'returns all objects above the value' do expect(Band.where(likes: eval('1..')).to_a).to eq [band2, band3, band4, band5] end end context 'beginless range' do ruby_version_gte '2.7' it 'returns all objects under the value' do expect(Band.where(likes: eval('..3')).to_a).to eq [band1, band2, band3, band4] expect(Band.where(likes: eval('...3')).to_a).to eq [band1, band2, band3] end end end context 'Range criteria vs Float field' do it 'returns objects within the range' do expect(Band.where(rating: 1..3).to_a).to eq [band2, band3, band4] expect(Band.where(rating: 1...3).to_a).to eq [band2, band3] end context 'endless range' do ruby_version_gte '2.6' it 'returns all objects above the value' do expect(Band.where(rating: eval('1..')).to_a).to eq [band2, band3, band4, band5] end end context 'beginless range' do ruby_version_gte '2.7' it 'returns all objects under the value' do expect(Band.where(rating: eval('..3')).to_a).to eq [band1, band2, band3, band4] expect(Band.where(rating: eval('...3')).to_a).to eq [band1, band2, band3] end end end context 'Range criteria vs Integer field' do it 'returns objects within the range' do expect(Band.where(likes: 0.95..3.05).to_a).to eq [band2, band3, band4] expect(Band.where(likes: 0.95...3.0).to_a).to eq [band2, band3] end context 'endless range' do ruby_version_gte '2.6' it 'returns all objects above the value' do expect(Band.where(likes: eval('0.95..')).to_a).to eq [band2, band3, band4, band5] end end context 'beginless range' do ruby_version_gte '2.7' it 'returns all objects under the value' do expect(Band.where(likes: eval('..3.05')).to_a).to eq [band1, band2, band3, band4] expect(Band.where(likes: eval('...3.0')).to_a).to eq [band1, band2, band3] end end end context 'Range criteria vs Float field' do it 'returns objects within the range' do expect(Band.where(rating: 0.95..3.05).to_a).to eq [band2, band3, band4] expect(Band.where(rating: 0.95...3.0).to_a).to eq [band2, band3] end context 'endless range' do ruby_version_gte '2.6' it 'returns all objects above the value' do expect(Band.where(rating: eval('0.95..')).to_a).to eq [band2, band3, band4, band5] end end context 'beginless range' do ruby_version_gte '2.7' it 'returns all objects under the value' do expect(Band.where(rating: eval('..3.05')).to_a).to eq [band1, band2, band3, band4] expect(Band.where(rating: eval('...3.0')).to_a).to eq [band1, band2, band3] end end end context 'Range