-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
171 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../test_helper" | ||
|
||
# has_one requires doing sub-queries with SELECT IN / applying limits to | ||
# handle conditions / counts properly. However, when there is no conditions and counts, | ||
# the deepest layer can avoid those sub-queries, which is faster. | ||
|
||
describe "wa_exists(has_one)" do | ||
# MySQL doesn't support has_one | ||
next if Test::SelectedDBHelper == Test::MySQL | ||
|
||
it "with a single has_one and no condition, doesn't do a sub-query" do | ||
sql = S0.where_assoc_exists(:o1).to_sql | ||
assert sql.include?('SELECT 1 FROM "s1s"') | ||
assert !sql.include?('SELECT 1 FROM (SELECT "s1s".*') | ||
end | ||
|
||
it "with a single has_one and a block, does a sub-query" do | ||
sql = S0.where_assoc_exists(:o1) {}.to_sql | ||
assert !sql.include?('SELECT 1 FROM "s1s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s1s".*') | ||
end | ||
|
||
it "with a single has_one and a condition, does a sub-query" do | ||
sql = S0.where_assoc_exists(:o1, id: 1).to_sql | ||
assert !sql.include?('SELECT 1 FROM "s1s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s1s".*') | ||
end | ||
|
||
|
||
it "with a two has_one and no condition, does a sub-query only once" do | ||
sql = S0.where_assoc_exists([:o1, :o2]).to_sql | ||
assert !sql.include?('SELECT 1 FROM "s1s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s1s".*') | ||
|
||
assert sql.include?('SELECT 1 FROM "s2s"') | ||
assert !sql.include?('SELECT 1 FROM (SELECT "s2s".*') | ||
end | ||
|
||
it "with a two has_one and a block, does two sub-queries" do | ||
sql = S0.where_assoc_exists([:o1, :o2]) {}.to_sql | ||
assert !sql.include?('SELECT 1 FROM "s1s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s1s".*') | ||
|
||
assert !sql.include?('SELECT 1 FROM "s2s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s2s".*') | ||
end | ||
|
||
it "with a two has_one and a condition, does two sub-queries" do | ||
sql = S0.where_assoc_exists([:o1, :o2], id: 1).to_sql | ||
assert !sql.include?('SELECT 1 FROM "s1s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s1s".*') | ||
|
||
assert !sql.include?('SELECT 1 FROM "s2s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s2s".*') | ||
end | ||
|
||
|
||
it "with a a has_one through has_one and no condition, does a sub-query only once" do | ||
sql = S0.where_assoc_exists(:o2o1).to_sql | ||
assert !sql.include?('SELECT 1 FROM "s1s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s1s".*') | ||
|
||
assert sql.include?('SELECT 1 FROM "s2s"') | ||
assert !sql.include?('SELECT 1 FROM (SELECT "s2s".*') | ||
end | ||
|
||
it "with a a has_one through has_one and a block, does two sub-queries" do | ||
sql = S0.where_assoc_exists(:o2o1) {}.to_sql | ||
assert !sql.include?('SELECT 1 FROM "s1s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s1s".*') | ||
|
||
assert !sql.include?('SELECT 1 FROM "s2s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s2s".*') | ||
end | ||
|
||
it "with a a has_one through has_one and a condition, does two sub-queries" do | ||
sql = S0.where_assoc_exists(:o2o1, id: 1).to_sql | ||
assert !sql.include?('SELECT 1 FROM "s1s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s1s".*') | ||
|
||
assert !sql.include?('SELECT 1 FROM "s2s"') | ||
assert sql.include?('SELECT 1 FROM (SELECT "s2s".*') | ||
end | ||
end | ||
|
||
describe "wa_exists(schema has_one)" do | ||
next if Test::SelectedDBHelper == Test::SQLite3 | ||
|
||
it "with a single schema has_one and no condition, doesn't do a sub-query" do | ||
sql = S0.where_assoc_exists(:schema_o1).to_sql | ||
puts sql | ||
assert sql.include?('SELECT 1 FROM "bar_schema"."s1s"') | ||
assert !sql.include?('SELECT 1 FROM (SELECT "bar_schema"."s1s".*') | ||
assert !sql.include?(' IN (SELECT "bar_schema"."schema_s1s"') | ||
end | ||
|
||
it "with a single schema has_one and a condition, does a ...IN..." do | ||
sql = S0.where_assoc_exists(:schema_o1, id: 1).to_sql | ||
assert !sql.include?('SELECT 1 FROM "bar_schema"."s1s"') | ||
assert !sql.include?('SELECT 1 FROM (SELECT "bar_schema"."s1s".*') | ||
assert sql.include?(' IN (SELECT "bar_schema"."schema_s1s"') | ||
end | ||
|
||
|
||
it "with two single schema has_one and no condition, does a single sub-query with ...IN..." do | ||
sql = S0.where_assoc_exists([:schema_o1, :schema_o2]).to_sql | ||
puts sql | ||
assert !sql.include?('SELECT 1 FROM "bar_schema"."s1s"') | ||
assert !sql.include?('SELECT 1 FROM (SELECT "bar_schema"."s1s".*') | ||
assert sql.include?(' IN (SELECT "bar_schema"."schema_s1s"') | ||
|
||
assert sql.include?('SELECT 1 FROM "bar_schema"."s2s"') | ||
assert !sql.include?('SELECT 1 FROM (SELECT "bar_schema"."s2s".*') | ||
assert !sql.include?(' IN (SELECT "bar_schema"."schema_s2s"') | ||
end | ||
|
||
it "with two single schema has_one and a condition, does two ...IN..." do | ||
sql = S0.where_assoc_exists([:schema_o1, :schema_o2], id: 1).to_sql | ||
assert !sql.include?('SELECT 1 FROM "bar_schema"."s1s"') | ||
assert !sql.include?('SELECT 1 FROM (SELECT "bar_schema"."s1s".*') | ||
assert sql.include?(' IN (SELECT "bar_schema"."schema_s1s"') | ||
|
||
assert !sql.include?('SELECT 1 FROM "bar_schema"."s2s"') | ||
assert !sql.include?('SELECT 1 FROM (SELECT "bar_schema"."s2s".*') | ||
assert sql.include?(' IN (SELECT "bar_schema"."schema_s2s"') | ||
end | ||
end | ||
|
||
describe "wa_count(has_one)" do | ||
# MySQL doesn't support has_one | ||
next if Test::SelectedDBHelper == Test::MySQL | ||
|
||
it "with a single has_one, does a sub-query (because the count needs it)" do | ||
sql = S0.where_assoc_count(1, :<, :o1).to_sql | ||
assert sql.include?('SELECT COUNT(*) FROM (SELECT "s1s".*') | ||
end | ||
|
||
it "with a two has_one and a condition, does two sub-queries (because the count needs it)" do | ||
sql = S0.where_assoc_count(1, :<, [:o1, :o2]).to_sql | ||
assert sql.include?('FROM (SELECT "s1s".*') | ||
assert sql.include?('SELECT COUNT(*) FROM (SELECT "s2s".*') | ||
end | ||
|
||
it "with a a has_one through has_one and a condition, does two sub-queries (because the count needs it)" do | ||
sql = S0.where_assoc_count(1, :<, :o2o1).to_sql | ||
assert sql.include?('FROM (SELECT "s1s".*') | ||
assert sql.include?('SELECT COUNT(*) FROM (SELECT "s2s".*') | ||
end | ||
end |