Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Doctor06/7c61453231f99f06a4c0d5903a4c7d38 to your computer and use it in GitHub Desktop.
Save Doctor06/7c61453231f99f06a4c0d5903a4c7d38 to your computer and use it in GitHub Desktop.
Test to see if we get wrong query with negative predicate
# test-ransack-scope-and-column-same-name.rb
# This is a stand-alone test case.
# Run it in your console with: `ruby test-ransack-scope-and-column-same-name.rb`
# If you change the gem dependencies, run it with:
# `rm gemfile* && ruby test-ransack-scope-and-column-same-name.rb`
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
# Rails master
gem 'rails', '5.2.4.1'
# Rails last release
# gem 'rails'
gem 'sqlite3'
gem 'ransack', github: 'activerecord-hackery/ransack'
gem 'polyamorous', github: 'activerecord-hackery/polyamorous'
GEMFILE
system 'bundle install'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'ransack'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
# Display versions.
message = "Running test case with Ruby #{RUBY_VERSION}, Active Record #{
::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION} and #{
::ActiveRecord::Base.connection.adapter_name}"
line = '=' * message.length
puts line, message, line
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.boolean :active, null: false, default: true
end
create_table :projects, force: true do |t|
t.belongs_to :user
end
create_table :tags, force: true do |t|
t.string :name
end
create_join_table :projects, :tags
end
class User < ActiveRecord::Base
has_many :projects
end
class Project < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :tags
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :projects
end
class BugTest < Minitest::Test
def test_project_tags_query
sql = Project.ransack(tags_id_not_in: [1,2]).result.to_sql
puts sql
assert_equal(
"SELECT \"projects\".* FROM \"projects\" WHERE \"projects\".\"id\" NOT IN (SELECT \"projects_tags\".\"project_id\" FROM \"projects_tags\" INNER JOIN \"tags\" ON \"tags\".\"id\" = \"projects_tags\".\"tag_id\" WHERE \"projects_tags\".\"project_id\" = \"projects\".\"id\" AND NOT (\"tags\".\"id\" NOT IN (1, 2)))", sql
)
end
def test_activated_scope_equals_false
sql
puts sql
assert_equal(
"SELECT \"users\".* FROM \"users\" LEFT OUTER JOIN \"projects\" ON \"projects\".\"user_id\" = \"users\".\"id\" WHERE \"projects\".\"id\" NOT IN (SELECT \"projects_tags\".\"project_id\" FROM \"projects_tags\" INNER JOIN \"tags\" ON \"tags\".\"id\" = \"projects_tags\".\"tag_id\" WHERE \"projects_tags\".\"project_id\" = \"projects\".\"id\" AND NOT (\"tags\".\"id\" NOT IN (1, 2)))", sql
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment