Skip to content

Commit

Permalink
Init fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikspang committed Oct 14, 2024
1 parent f52d4ac commit d9ebc85
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ gemspec

gem 'rails', ENV.fetch('RAILS_VERSION')
gem 'pg'
gem 'sqlite3'
gem 'sqlite3', '~> 1.3'
2 changes: 1 addition & 1 deletion active_record_union.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.files.grep(%r{^bin/})
spec.require_paths = ["lib"]

spec.add_dependency "activerecord", ">= 4.0"
spec.add_dependency "activerecord", ">= 7.0"

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
Expand Down
57 changes: 18 additions & 39 deletions spec/union_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,9 @@
end

def bind_values_from_relation(relation)
if ActiveRecord.gem_version >= Gem::Version.new('5.2.0.beta2')
relation.arel_table.class.engine.connection.visitor.accept(
relation.arel.ast, Arel::Collectors::Bind.new
).value.map(&:value)
elsif ActiveRecord::VERSION::MAJOR >= 5
relation.bound_attributes.map { |a| a.value_for_database }
else
(relation.arel.bind_values + relation.bind_values).map { |_column, value| value }
end
relation.arel_table.class.engine.connection.visitor.accept(
relation.arel.ast, Arel::Collectors::Bind.new
).value.map(&:value)
end

it "binds values properly" do
Expand All @@ -72,16 +66,16 @@ def bind_values_from_relation(relation)
bind_values = bind_values_from_relation union
expect(bind_values).to eq([true, 11])


expect(union.to_sql.squish).to eq(
"SELECT \"users\".* FROM ( SELECT \"users\".* FROM \"users\" INNER JOIN \"posts\" ON \"posts\".\"user_id\" = \"users\".\"id\" AND \"posts\".\"draft\" = 't' UNION SELECT \"users\".* FROM \"users\" WHERE \"users\".\"id\" = 11 ) \"users\""
"SELECT \"users\".* FROM ( SELECT \"users\".* FROM \"users\" INNER JOIN \"posts\" ON \"posts\".\"draft\" = 1 AND \"posts\".\"user_id\" = \"users\".\"id\" UNION SELECT \"users\".* FROM \"users\" WHERE \"users\".\"id\" = 11 ) \"users\""
)
expect{union.to_a}.to_not raise_error
end

it "doesn't repeat default scopes" do
expect(Time).to receive(:now) { Time.utc(2014, 7, 24, 0, 0, 0) }
sql_now = "2014-07-24 00:00:00#{".000000" if ActiveRecord::VERSION::MAJOR < 5}"

sql_now = "2014-07-24 00:00:00"

class PublishedPost < ActiveRecord::Base
self.table_name = "posts"
Expand All @@ -105,31 +99,21 @@ class PublishedPost < ActiveRecord::Base

context "in SQLite" do
it "lets ORDER BY in query subselects throw a syntax error" do
if ([ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] <=> [5, 2]) >= 0
expect(union.to_sql.squish).to eq(
"SELECT \"posts\".* FROM ( SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC UNION SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '2014-07-19 00:00:00') ORDER BY \"posts\".\"created_at\" ASC ) \"posts\" ORDER BY \"created_at\" ASC"
)
else
expect(union.to_sql.squish).to eq(
"SELECT \"posts\".* FROM ( SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC UNION SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '#{SQL_TIME}') ORDER BY \"posts\".\"created_at\" ASC ) \"posts\" ORDER BY \"posts\".\"created_at\" ASC"
)
end
expect(union.to_sql.squish).to eq(
"SELECT \"posts\".* FROM ( SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC UNION SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '2014-07-19 00:00:00') ORDER BY \"posts\".\"created_at\" ASC ) \"posts\" ORDER BY \"created_at\" ASC"
)

expect{union.to_a}.to raise_error(ActiveRecord::StatementInvalid)
end
end

context "in Postgres" do
it "wraps query subselects in parentheses to allow ORDER BY clauses" do
Databases.with_postgres do
if ([ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] <=> [5, 2]) >= 0
expect(union.to_sql.squish).to eq(
"SELECT \"posts\".* FROM ( (SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC) UNION (SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '2014-07-19 00:00:00') ORDER BY \"posts\".\"created_at\" ASC) ) \"posts\" ORDER BY \"created_at\" ASC"
)
else
expect(union.to_sql.squish).to eq(
"SELECT \"posts\".* FROM ( (SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC) UNION (SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '#{SQL_TIME}') ORDER BY \"posts\".\"created_at\" ASC) ) \"posts\" ORDER BY \"posts\".\"created_at\" ASC"
)
end
expect(union.to_sql.squish).to eq(
"SELECT \"posts\".* FROM ( (SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC) UNION (SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '2014-07-19 00:00:00') ORDER BY \"posts\".\"created_at\" ASC) ) \"posts\" ORDER BY \"created_at\" ASC"
)

expect{union.to_a}.to_not raise_error
end
end
Expand All @@ -138,15 +122,10 @@ class PublishedPost < ActiveRecord::Base
context "in MySQL" do
it "wraps query subselects in parentheses to allow ORDER BY clauses" do
Databases.with_mysql do
if ([ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] <=> [5, 2]) >= 0
expect(union.to_sql.squish).to eq(
"SELECT `posts`.* FROM ( (SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` = 1 ORDER BY `posts`.`created_at` ASC) UNION (SELECT `posts`.* FROM `posts` WHERE (created_at > '2014-07-19 00:00:00') ORDER BY `posts`.`created_at` ASC) ) `posts` ORDER BY `created_at` ASC"
)
else
expect(union.to_sql.squish).to eq(
"SELECT `posts`.* FROM ( (SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` = 1 ORDER BY `posts`.`created_at` ASC) UNION (SELECT `posts`.* FROM `posts` WHERE (created_at > '#{SQL_TIME}') ORDER BY `posts`.`created_at` ASC) ) `posts` ORDER BY `posts`.`created_at` ASC"
)
end
expect(union.to_sql.squish).to eq(
"SELECT `posts`.* FROM ( (SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` = 1 ORDER BY `posts`.`created_at` ASC) UNION (SELECT `posts`.* FROM `posts` WHERE (created_at > '2014-07-19 00:00:00') ORDER BY `posts`.`created_at` ASC) ) `posts` ORDER BY `created_at` ASC"
)

expect{union.to_a}.to_not raise_error
end
end
Expand Down

0 comments on commit d9ebc85

Please sign in to comment.