Skip to content

Instantly share code, notes, and snippets.

@thefloweringash
Last active June 8, 2023 07:34
Show Gist options
  • Save thefloweringash/3f4deabebb7920fbae3898d175743dc2 to your computer and use it in GitHub Desktop.
Save thefloweringash/3f4deabebb7920fbae3898d175743dc2 to your computer and use it in GitHub Desktop.
Rails group/count key type bug test case
services:
postgres:
image: postgres
healthcheck:
test: ["CMD", "pg_isready"]
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
test:
image: rails-group-by-test
build: ./.
depends_on:
postgres:
condition: service_healthy
environment:
DATABASE_URL: postgres://postgres@postgres/postgres
volumes:
- bundler:/usr/local/bundle
volumes:
bundler:
FROM ruby:3.2
COPY group-by-test.rb /
CMD "ruby" "/group-by-test.rb"
# frozen_string_literal: true
#
# Requires a postgres database, for example
#
# createdb group_by_test
# DATABASE_URL=postgres://localhost/group-by-test ruby ./group-by-test.rb
#
# Also supports checking other Rails versions via the RAILS_VERSION
# environment variable. For example:
#
# createdb group_by_test
# RAILS_VERSION=7.0.4.3 DATABASE_URL=postgres://localhost/group-by-test ruby ./group-by-test.rb
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"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
if (rails_version = ENV['RAILS_VERSION'])
gem "rails", rails_version
else
gem "rails", github: "rails/rails", branch: "main"
end
gem "pg"
gem 'timeout', '=0.3.2' # workaround for bundler/inline
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection
# ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :bookings, force: true
end
class Booking < ActiveRecord::Base
end
class BugTest < Minitest::Test
def setup
Booking.create!
end
def teardown
Booking.delete_all
end
def test_group_by
grouped_count =
Booking
.group("tsrange(timestamp '2020-01-01', timestamp '2020-01-02', '[)') ")
.count(:id)
assert_equal Range, grouped_count.keys.first.class # passes
end
def test_group_by_join
grouped_count =
Booking
.joins("JOIN (SELECT tsrange(timestamp '2020-01-01', timestamp '2020-01-02', '[)') AS range) ranges ON TRUE")
.group('ranges.range')
.count(:id)
assert_equal Range, grouped_count.keys.first.class # fails
end
end
@thefloweringash
Copy link
Author

Rails 7.0.4.3

❯ RAILS_VERSION=7.0.4.3 DATABASE_URL=postgres://localhost/group-by-test ruby ./group-by-test.rb
[...]
Using rails 7.0.4.3
[...]
# Running:

..

Finished in 0.009883s, 202.3589 runs/s, 202.3589 assertions/s.
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

Rails 7.0.5

❯ RAILS_VERSION=7.0.5 DATABASE_URL=postgres://localhost/group-by-test ruby ./group-by-test.rb
[...]
Using rails 7.0.5
[...]
# Running:

.F

Failure:
BugTest#test_group_by_join [./group-by-test.rb:76]:
Expected: Range
  Actual: String


rails test ./group-by-test.rb:69



Finished in 0.010084s, 198.3436 runs/s, 198.3436 assertions/s.
2 runs, 2 assertions, 1 failures, 0 errors, 0 skips

Rails master

❯ DATABASE_URL=postgres://localhost/group-by-test ruby ./group-by-test.rb
[...]
Using rails 7.1.0.alpha from https://github.com/rails/rails.git (at main@55c3066)
[...]
# Running:

.F

Failure:
BugTest#test_group_by_join [./group-by-test.rb:76]:
Expected: Range
  Actual: String


bin/rails test ./group-by-test.rb:69



Finished in 0.013512s, 148.0131 runs/s, 148.0131 assertions/s.
2 runs, 2 assertions, 1 failures, 0 errors, 0 skips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment