-
Notifications
You must be signed in to change notification settings - Fork 52
Implement values #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Implement values #174
Changes from 4 commits
ff6b2e1
bbd1dbb
cafc720
40733b8
2b01d45
e851684
2054a79
3183b70
b86ecbb
99cec9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
defmodule Ecto.Integration.ValuesTest do | ||
use Ecto.Integration.Case, async: true | ||
|
||
import Ecto.Query, only: [from: 2, with_cte: 3] | ||
|
||
alias Ecto.Integration.Comment | ||
alias Ecto.Integration.Post | ||
alias Ecto.Integration.TestRepo | ||
|
||
test "join to values works" do | ||
TestRepo.insert!(%Post{id: 1}) | ||
TestRepo.insert!(%Comment{post_id: 1, text: "short"}) | ||
TestRepo.insert!(%Comment{post_id: 1, text: "much longer text"}) | ||
|
||
params = [%{id: 1, post_id: 1, n: 0}, %{id: 2, post_id: 1, n: 10}] | ||
types = %{id: :integer, post_id: :integer, n: :integer} | ||
|
||
results = | ||
from(p in Post, | ||
right_join: params in values(params, types), | ||
on: params.post_id == p.id, | ||
left_join: c in Comment, | ||
on: c.post_id == p.id and fragment("LENGTH(?)", c.text) > params.n, | ||
group_by: params.id, | ||
select: {params.id, count(c.id)} | ||
) | ||
|> TestRepo.all() | ||
|
||
assert [{1, 2}, {2, 1}] = results | ||
end | ||
|
||
test "values can be used together with CTE" do | ||
TestRepo.insert!(%Post{id: 1, visits: 42}) | ||
TestRepo.insert!(%Comment{post_id: 1, text: "short"}) | ||
TestRepo.insert!(%Comment{post_id: 1, text: "much longer text"}) | ||
|
||
params = [%{id: 1, post_id: 1, n: 0}, %{id: 2, post_id: 1, n: 10}] | ||
types = %{id: :integer, post_id: :integer, n: :integer} | ||
|
||
cte_query = from(p in Post, select: %{id: p.id, visits: coalesce(p.visits, 0)}) | ||
|
||
q = Post |> with_cte("xxx", as: ^cte_query) | ||
|
||
results = | ||
from(p in q, | ||
right_join: params in values(params, types), | ||
on: params.post_id == p.id, | ||
left_join: c in Comment, | ||
on: c.post_id == p.id and fragment("LENGTH(?)", c.text) > params.n, | ||
left_join: cte in "xxx", | ||
on: cte.id == p.id, | ||
group_by: params.id, | ||
select: {params.id, count(c.id), cte.visits} | ||
) | ||
|> TestRepo.all() | ||
|
||
assert [{1, 2, 42}, {2, 1, 42}] = results | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,11 +135,11 @@ | |
end | ||
end | ||
|
||
test "join with values is not supported" do | ||
assert_raise Ecto.QueryError, fn -> | ||
rows = [%{x: 1, y: 1}, %{x: 2, y: 2}] | ||
types = %{x: :integer, y: :integer} | ||
test "join with values" do | ||
Check failure on line 138 in test/ecto/adapters/sqlite3/connection/join_test.exs
|
||
rows = [%{x: 1, y: 1}, %{x: 2, y: 2}] | ||
types = %{x: :integer, y: :integer} | ||
|
||
query = | ||
Schema | ||
|> join( | ||
:inner, | ||
|
@@ -149,8 +149,11 @@ | |
) | ||
|> select([p, q], {p.id, q.x}) | ||
|> plan() | ||
|> all() | ||
end | ||
|
||
assert ~s{SELECT s0."id", v1."x" FROM "schema" AS s0 } <> | ||
~s{INNER JOIN (WITH xxx(y, x) AS (VALUES ($1,$2),($3,$4)) SELECT * FROM xxx) AS v1 } <> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tests fails with OTP 25, as the order of x, y is swapped in I'm not sure how to solve this, any idea? |
||
~s{ON (v1."x" = s0."x") AND (v1."y" = s0."y")} == | ||
all(query) | ||
end | ||
|
||
test "join with fragment" do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can shove this into the
test/
directory under theconnection/
directory asvalues_test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inside
test/ecto/adapters/sqlite3/connection
?If I do that I get
** (Exqlite.Error) no such table: posts
. So there is some setup missing there.Also, I didn't see any other similar tests in that directory, tests that actually operate on a Repo. Only in
integration_test/