|
| 1 | +defmodule Ecto.Integration.ValuesTest do |
| 2 | + use Ecto.Integration.Case, async: true |
| 3 | + |
| 4 | + import Ecto.Query, only: [from: 2, with_cte: 3] |
| 5 | + |
| 6 | + alias Ecto.Integration.Comment |
| 7 | + alias Ecto.Integration.Post |
| 8 | + alias Ecto.Integration.TestRepo |
| 9 | + |
| 10 | + test "values works with datetime" do |
| 11 | + TestRepo.insert!(%Post{inserted_at: ~N[2000-01-01 00:01:00]}) |
| 12 | + TestRepo.insert!(%Post{inserted_at: ~N[2000-01-01 00:02:00]}) |
| 13 | + TestRepo.insert!(%Post{inserted_at: ~N[2000-01-01 00:03:00]}) |
| 14 | + |
| 15 | + params = [ |
| 16 | + %{id: 1, date: ~N[2000-01-01 00:00:00]}, |
| 17 | + %{id: 2, date: ~N[2000-01-01 00:01:00]}, |
| 18 | + %{id: 3, date: ~N[2000-01-01 00:02:00]}, |
| 19 | + %{id: 4, date: ~N[2000-01-01 00:03:00]} |
| 20 | + ] |
| 21 | + |
| 22 | + types = %{id: :integer, date: :naive_datetime} |
| 23 | + |
| 24 | + results = |
| 25 | + from(params in values(params, types), |
| 26 | + left_join: p in Post, |
| 27 | + on: p.inserted_at <= params.date, |
| 28 | + group_by: params.id, |
| 29 | + select: %{id: params.id, count: count(p.id)}, |
| 30 | + order_by: count(p.id) |
| 31 | + ) |
| 32 | + |> TestRepo.all() |
| 33 | + |
| 34 | + assert results == [ |
| 35 | + %{count: 0, id: 1}, |
| 36 | + %{count: 1, id: 2}, |
| 37 | + %{count: 2, id: 3}, |
| 38 | + %{count: 3, id: 4} |
| 39 | + ] |
| 40 | + end |
| 41 | + |
| 42 | + test "join to values works" do |
| 43 | + TestRepo.insert!(%Post{id: 1}) |
| 44 | + TestRepo.insert!(%Comment{post_id: 1, text: "short"}) |
| 45 | + TestRepo.insert!(%Comment{post_id: 1, text: "much longer text"}) |
| 46 | + |
| 47 | + params = [%{id: 1, post_id: 1, n: 0}, %{id: 2, post_id: 1, n: 10}] |
| 48 | + types = %{id: :integer, post_id: :integer, n: :integer} |
| 49 | + |
| 50 | + results = |
| 51 | + from(p in Post, |
| 52 | + right_join: params in values(params, types), |
| 53 | + on: params.post_id == p.id, |
| 54 | + left_join: c in Comment, |
| 55 | + on: c.post_id == p.id and fragment("LENGTH(?)", c.text) > params.n, |
| 56 | + group_by: params.id, |
| 57 | + select: {params.id, count(c.id)} |
| 58 | + ) |
| 59 | + |> TestRepo.all() |
| 60 | + |
| 61 | + assert [{1, 2}, {2, 1}] = results |
| 62 | + end |
| 63 | + |
| 64 | + test "values can be used together with CTE" do |
| 65 | + TestRepo.insert!(%Post{id: 1, visits: 42}) |
| 66 | + TestRepo.insert!(%Comment{post_id: 1, text: "short"}) |
| 67 | + TestRepo.insert!(%Comment{post_id: 1, text: "much longer text"}) |
| 68 | + |
| 69 | + params = [%{id: 1, post_id: 1, n: 0}, %{id: 2, post_id: 1, n: 10}] |
| 70 | + types = %{id: :integer, post_id: :integer, n: :integer} |
| 71 | + |
| 72 | + cte_query = from(p in Post, select: %{id: p.id, visits: coalesce(p.visits, 0)}) |
| 73 | + |
| 74 | + q = Post |> with_cte("xxx", as: ^cte_query) |
| 75 | + |
| 76 | + results = |
| 77 | + from(p in q, |
| 78 | + right_join: params in values(params, types), |
| 79 | + on: params.post_id == p.id, |
| 80 | + left_join: c in Comment, |
| 81 | + on: c.post_id == p.id and fragment("LENGTH(?)", c.text) > params.n, |
| 82 | + left_join: cte in "xxx", |
| 83 | + on: cte.id == p.id, |
| 84 | + group_by: params.id, |
| 85 | + select: {params.id, count(c.id), cte.visits} |
| 86 | + ) |
| 87 | + |> TestRepo.all() |
| 88 | + |
| 89 | + assert [{1, 2, 42}, {2, 1, 42}] = results |
| 90 | + end |
| 91 | +end |
0 commit comments