Last active
June 8, 2023 07:34
-
-
Save thefloweringash/3f4deabebb7920fbae3898d175743dc2 to your computer and use it in GitHub Desktop.
Rails group/count key type bug test case
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
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: |
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
FROM ruby:3.2 | |
COPY group-by-test.rb / | |
CMD "ruby" "/group-by-test.rb" |
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rails 7.0.4.3
Rails 7.0.5
Rails master