Skip to content

Commit 82aeb45

Browse files
Bernard Dugganjosevalim
authored andcommitted
Tests for subquery select parameter count
1 parent d46b9bd commit 82aeb45

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

test/ecto/adapters/postgres_test.exs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,40 @@ defmodule Ecto.Adapters.PostgresTest do
565565
"SELECT s0.\"id\", s2.\"id\" FROM \"schema\" AS s0 INNER JOIN \"schema2\" AS s1 ON TRUE INNER JOIN \"schema2\" AS s2 ON TRUE"
566566
end
567567

568+
describe "query interpolation parameters" do
569+
test "self join on subquery" do
570+
subquery = select(Schema, [r], %{x: r.x, y: r.y})
571+
query = subquery |> join(:inner, [c], p in subquery(subquery), true) |> normalize
572+
assert SQL.all(query) ==
573+
~s{SELECT s0."x", s0."y" FROM "schema" AS s0 INNER JOIN } <>
574+
~s{(SELECT s0."x" AS "x", s0."y" AS "y" FROM "schema" AS s0) } <>
575+
~s{AS s1 ON TRUE}
576+
end
577+
578+
test "self join on subquery with fragment" do
579+
subquery = select(Schema, [r], %{string: fragment("downcase(?)", ^"string")})
580+
query = subquery |> join(:inner, [c], p in subquery(subquery), true) |> normalize
581+
assert SQL.all(query) ==
582+
~s{SELECT downcase($1) FROM "schema" AS s0 INNER JOIN } <>
583+
~s{(SELECT downcase($2) AS "string" FROM "schema" AS s0) } <>
584+
~s{AS s1 ON TRUE}
585+
end
586+
587+
test "join on subquery with simple select" do
588+
subquery = select(Schema, [r], %{x: ^999, w: ^888})
589+
query = Schema
590+
|> select([r], %{y: ^666})
591+
|> join(:inner, [c], p in subquery(subquery), true)
592+
|> where([a, b], a.x == ^111)
593+
|> normalize
594+
595+
assert SQL.all(query) ==
596+
~s{SELECT $1 FROM "schema" AS s0 INNER JOIN } <>
597+
~s{(SELECT $2 AS "x", $3 AS "w" FROM "schema" AS s0) AS s1 ON TRUE } <>
598+
~s{WHERE (s0."x" = $4)}
599+
end
600+
end
601+
568602
## Associations
569603

570604
test "association join belongs_to" do

0 commit comments

Comments
 (0)