Skip to content

Commit

Permalink
filter rails version and return different results in rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Dingying0410 committed Mar 31, 2021
1 parent ebba851 commit 98868a7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions redis-memo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec', '~> 3.2'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'rails'
end
15 changes: 15 additions & 0 deletions spec/memoize_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ def expect_to_eq_with_or_without_redis
let!(:relation2_with_only_not) { Site.where.not(a: 2, b: 2) }
let!(:relation3_with_only_not) { Site.where.not(a: 2).where.not(b: 1) }
let!(:relation_with_not_and_other) { Site.where.not(a: 2).where(b: 1) }
# where.not(a: ..., b:...) relation is treated differently in Rails 6.0, Rails 6.1 and before Rails 6
# Details: https://bigbinary.com/blog/rails-6-deprecates-where-not-working-as-nor-and-will-change-to-nand-in-rails-6-1
let!(:special_relation_with_not) { Site.where.not(a: 2).where(b: 1) }


it 'does not memoize queries with only NOT' do
expect_not_to_use_redis do
Expand All @@ -464,6 +468,17 @@ def expect_to_eq_with_or_without_redis
end
end

it 'does not memoize but adapts to different Rails versions flexibly' do
if Rails.version >= '6.1'
# Site.where.not(a: 2).where(b: 1) will be treated as NAND in Rails 6.1
expect(special_relation_with_not.to_a).to eq([record])
else
# Site.where.not(a: 2).where(b: 1) will be treated as NOR in and before Rails 6.0
# but will give a deprecation warning in Rails 6.0
expect(special_relation_with_not.to_a).to eq([])
end
end

it 'memoizes queries with both NOT and other bound conditions' do
expect_to_use_redis do
expect(relation_with_not_and_other.to_a).to eq([record])
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'database_cleaner/active_record'
require 'simplecov'
require 'rails'

SimpleCov.start

Expand Down

0 comments on commit 98868a7

Please sign in to comment.