|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe 'Queries with Mongoid::RawValue criteria' do |
| 6 | + before { Time.zone = 'UTC'} |
| 7 | + let(:now_utc) { Time.utc(2020, 1, 1, 16, 0, 0, 0) } |
| 8 | + let(:today) { Date.new(2020, 1, 1) } |
| 9 | + |
| 10 | + let(:labels) do |
| 11 | + [ Label.new(age: 12), Label.new(age: 16) ] |
| 12 | + end |
| 13 | + |
| 14 | + let!(:band1) { Band.create!(name: '1', likes: 0, rating: 0.9, decibels: 20..80, founded: today, updated_at: now_utc) } |
| 15 | + let!(:band2) { Band.create!(name: '2', likes: 1, rating: 1.0, decibels: 30..90, founded: today, updated_at: now_utc + 1.days) } |
| 16 | + let!(:band3) { Band.create!(name: '3', likes: 1, rating: 2.2, decibels: 40..100, founded: today + 1.days, updated_at: now_utc + 2.days) } |
| 17 | + let!(:band4) { Band.create!(name: '3', likes: 2, rating: 3.1, decibels: 50..120, founded: today + 1.days, updated_at: now_utc + 3.days) } |
| 18 | + let!(:band5) { Band.create!(name: '4', likes: 3, rating: 3.1, decibels: 60..150, founded: today + 2.days, updated_at: now_utc + 3.days, labels: labels) } |
| 19 | + |
| 20 | + context 'Mongoid::RawValue<String> criteria vs Integer field' do |
| 21 | + it 'does not match objects' do |
| 22 | + expect(Band.where(likes: Mongoid::RawValue('1')).to_a).to eq [] |
| 23 | + end |
| 24 | + |
| 25 | + it 'matches objects without raw value' do |
| 26 | + expect(Band.where(likes: '1').to_a).to eq [band2, band3] |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + context 'Mongoid::RawValue<String> criteria vs Float field' do |
| 31 | + it 'does not match objects' do |
| 32 | + expect(Band.where(rating: Mongoid::RawValue('3.1')).to_a).to eq [] |
| 33 | + end |
| 34 | + |
| 35 | + it 'matches objects without raw value' do |
| 36 | + expect(Band.where(rating: '3.1').to_a).to eq [band4, band5] |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + context 'Mongoid::RawValue<String> criteria vs String field' do |
| 41 | + it 'matches objects' do |
| 42 | + expect(Band.where(name: Mongoid::RawValue('3')).to_a).to eq [band3, band4] |
| 43 | + end |
| 44 | + |
| 45 | + it 'matches objects without raw value' do |
| 46 | + expect(Band.where(name: '3').to_a).to eq [band3, band4] |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context 'Mongoid::RawValue<String> criteria vs Range field' do |
| 51 | + it 'does not match objects' do |
| 52 | + expect(Band.where(decibels: Mongoid::RawValue('90')).to_a).to eq [] |
| 53 | + end |
| 54 | + |
| 55 | + it 'does not match objects without raw value' do |
| 56 | + expect(Band.where(name: '90').to_a).to eq [] |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + context 'Mongoid::RawValue<String> criteria vs Date field' do |
| 61 | + it 'does not match objects' do |
| 62 | + expect(Band.where(founded: Mongoid::RawValue('2020-01-02')).to_a).to eq [] |
| 63 | + end |
| 64 | + |
| 65 | + it 'matches objects without raw value' do |
| 66 | + expect(Band.where(founded: '2020-01-02').to_a).to eq [band3, band4] |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + context 'Mongoid::RawValue<String> criteria vs Time field' do |
| 71 | + it 'does not match objects' do |
| 72 | + expect(Band.where(updated_at: Mongoid::RawValue('2020-01-04 16:00:00 UTC')).to_a).to eq [] |
| 73 | + end |
| 74 | + |
| 75 | + # TODO: this isn't working for some reason |
| 76 | + xit 'matches objects without raw value' do |
| 77 | + expect(Band.where(updated_at: '2020-01-04 16:00:00 UTC').to_a).to eq [band4, band5] |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + context 'Mongoid::RawValue<Integer> criteria vs Integer field' do |
| 82 | + it 'does not match objects' do |
| 83 | + expect(Band.where(likes: Mongoid::RawValue(1)).to_a).to eq [band2, band3] |
| 84 | + end |
| 85 | + |
| 86 | + it 'matches objects without raw value' do |
| 87 | + expect(Band.where(likes: 1).to_a).to eq [band2, band3] |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + context 'Mongoid::RawValue<Integer> criteria vs Float field' do |
| 92 | + it 'does not match objects' do |
| 93 | + expect(Band.where(rating: Mongoid::RawValue(1)).to_a).to eq [band2] |
| 94 | + expect(Band.where(rating: Mongoid::RawValue(3)).to_a).to eq [] |
| 95 | + end |
| 96 | + |
| 97 | + it 'matches objects without raw value' do |
| 98 | + expect(Band.where(rating: 1).to_a).to eq [band2] |
| 99 | + expect(Band.where(rating: 3).to_a).to eq [] |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + context 'Mongoid::RawValue<Integer> criteria vs String field' do |
| 104 | + it 'matches objects' do |
| 105 | + expect(Band.where(name: Mongoid::RawValue(3)).to_a).to eq [] |
| 106 | + end |
| 107 | + |
| 108 | + it 'matches objects without raw value' do |
| 109 | + expect(Band.where(name: 3).to_a).to eq [band3, band4] |
| 110 | + end |
| 111 | + end |
| 112 | +end |
0 commit comments