Created
July 14, 2016 08:33
-
-
Save yuthura/ac8e738af75db22918972e60248d9a5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile true do | |
source 'https://rubygems.org' | |
gem 'rails', '4.2.7' | |
# gem 'sqlite3' | |
gem "mysql2" | |
end | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# This issue does not appear to happen on sqlite3, but I'm unsure whether this is the (mysql) Rails adapter failing, or the actual driver failing | |
# Toggle this and the mysql gem and driver to see for yourself | |
# ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
# Change username/password accordingly and create database manually | |
ActiveRecord::Base.establish_connection(adapter: 'mysql2', host: 'localhost', username: 'root', password: '', database: 'gist_test') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :baskets, force: true do |t| | |
t.integer :required_amount_of_apples | |
end | |
create_table :apples, force: true do |t| | |
t.integer :basket_id | |
t.integer :amount_of_stems | |
end | |
end | |
class Basket < ActiveRecord::Base | |
has_many :apples | |
end | |
class Apple < ActiveRecord::Base | |
belongs_to :basket | |
end | |
class BugTest < ActiveSupport::TestCase | |
# This test fails with: | |
# ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'baskets.required_amount_of_apples' in 'having clause' | |
def test_failing_magic | |
assert_nothing_raised do | |
Basket.includes(:apples) | |
.group(:id) | |
.having("baskets.required_amount_of_apples != COUNT(DISTINCT apples.amount_of_stems)") | |
.limit(1) | |
.pluck("baskets.required_amount_of_apples, baskets.id") | |
end | |
end | |
# This test passes. | |
def test_passing_magic | |
assert_nothing_raised do | |
Basket.includes(:apples) | |
.group(:id) | |
.having("baskets.required_amount_of_apples != COUNT(DISTINCT apples.amount_of_stems)") | |
.pluck("baskets.required_amount_of_apples, baskets.id") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment