|
| 1 | +defmodule AshPostgres.ManyToManyExprTest do |
| 2 | + use AshPostgres.RepoCase, async: false |
| 3 | + |
| 4 | + alias AshPostgres.Test.Author |
| 5 | + alias AshPostgres.Test.CoAuthorPost |
| 6 | + alias AshPostgres.Test.Post |
| 7 | + |
| 8 | + require Ash.Query |
| 9 | + |
| 10 | + setup ctx do |
| 11 | + main_author = |
| 12 | + if ctx[:main_author?], |
| 13 | + do: create_author(), |
| 14 | + else: nil |
| 15 | + |
| 16 | + co_authors = |
| 17 | + if ctx[:co_authors], |
| 18 | + do: 1..ctx[:co_authors] |
| 19 | + |> Stream.map(& |
| 20 | + Author |
| 21 | + |> Ash.Changeset.for_create(:create, %{first_name: "John #{&1}", last_name: "Doe"}) |
| 22 | + |> Ash.create!() |
| 23 | + ) |
| 24 | + |> Enum.into([]), |
| 25 | + else: [] |
| 26 | + |
| 27 | + %{ |
| 28 | + main_author: main_author, |
| 29 | + co_authors: co_authors, |
| 30 | + } |
| 31 | + end |
| 32 | + |
| 33 | + def create_author(params \\ %{first_name: "John", last_name: "Doe"}) do |
| 34 | + Author |
| 35 | + |> Ash.Changeset.for_create(:create, params) |
| 36 | + |> Ash.create!() |
| 37 | + end |
| 38 | + |
| 39 | + def create_post(author) do |
| 40 | + Post |
| 41 | + |> Ash.Changeset.for_create(:create, %{title: "Post by #{author.first_name}"}) |
| 42 | + |> Ash.create!() |
| 43 | + end |
| 44 | + |
| 45 | + def create_co_author_post(author, post, role) do |
| 46 | + CoAuthorPost |
| 47 | + |> Ash.Changeset.for_create(:create, %{author_id: author.id, post_id: post.id, role: role}) |
| 48 | + |> Ash.create!() |
| 49 | + end |
| 50 | + |
| 51 | + def get_author!(author_id) do |
| 52 | + Author |
| 53 | + |> Ash.Query.new() |
| 54 | + |> Ash.Query.filter(id == ^author_id) |
| 55 | + |> Ash.Query.load([:all_co_authored_posts, :cancelled_co_authored_posts, :editor_of, :writer_of]) |
| 56 | + |> Ash.read_one!() |
| 57 | + end |
| 58 | + |
| 59 | + def get_co_author_post!(a_id, p_id) do |
| 60 | + CoAuthorPost |
| 61 | + |> Ash.Query.new() |
| 62 | + |> Ash.Query.filter(author_id == ^a_id and post_id == ^p_id) |
| 63 | + |> Ash.read_one!() |
| 64 | + end |
| 65 | + |
| 66 | + def get_post!(post_id) do |
| 67 | + Post.get_by_id!(post_id, load: [:co_author_posts, :co_authors_unfiltered, :co_authors]) |
| 68 | + end |
| 69 | + |
| 70 | + def cancel(author, post) do |
| 71 | + get_co_author_post!(author.id, post.id) |
| 72 | + |> CoAuthorPost.cancel() |
| 73 | + end |
| 74 | + |
| 75 | + def uncancel(author, post) do |
| 76 | + get_co_author_post!(author.id, post.id) |
| 77 | + |> CoAuthorPost.uncancel() |
| 78 | + end |
| 79 | + |
| 80 | + describe "manual join-resource insertion" do |
| 81 | + @tag main_author?: true |
| 82 | + @tag co_authors: 3 |
| 83 | + test "filter on many_to_many relationship using parent works as expected - basic", |
| 84 | + %{ |
| 85 | + main_author: main_author, |
| 86 | + co_authors: co_authors, |
| 87 | + } do |
| 88 | + post = create_post(main_author) |
| 89 | + |
| 90 | + [first_ca, second_ca, third_ca] = co_authors |
| 91 | + |
| 92 | + # Add first co-author |
| 93 | + create_co_author_post(first_ca, post, :editor) |
| 94 | + |
| 95 | + first_ca = get_author!(first_ca.id) |
| 96 | + post = get_post!(post.id) |
| 97 | + |
| 98 | + assert Enum.count(post.co_authors) == 1 |
| 99 | + assert Enum.count(first_ca.all_co_authored_posts) == 1 |
| 100 | + assert Enum.count(first_ca.editor_of) == 1 |
| 101 | + assert Enum.count(first_ca.writer_of) == 0 |
| 102 | + assert Enum.count(first_ca.cancelled_co_authored_posts) == 0 |
| 103 | + |
| 104 | + # Add second co-author |
| 105 | + create_co_author_post(second_ca, post, :writer) |
| 106 | + |
| 107 | + second_ca = get_author!(second_ca.id) |
| 108 | + post = get_post!(post.id) |
| 109 | + |
| 110 | + assert Enum.count(post.co_authors) == 2 |
| 111 | + assert Enum.count(second_ca.all_co_authored_posts) == 1 |
| 112 | + assert Enum.count(second_ca.editor_of) == 0 |
| 113 | + assert Enum.count(second_ca.writer_of) == 1 |
| 114 | + assert Enum.count(second_ca.cancelled_co_authored_posts) == 0 |
| 115 | + |
| 116 | + # Add third co-author |
| 117 | + create_co_author_post(third_ca, post, :proof_reader) |
| 118 | + |
| 119 | + third_ca = get_author!(third_ca.id) |
| 120 | + post = get_post!(post.id) |
| 121 | + |
| 122 | + assert Enum.count(post.co_authors) == 3 |
| 123 | + assert Enum.count(third_ca.all_co_authored_posts) == 1 |
| 124 | + assert Enum.count(third_ca.editor_of) == 0 |
| 125 | + assert Enum.count(third_ca.writer_of) == 0 |
| 126 | + assert Enum.count(third_ca.cancelled_co_authored_posts) == 0 |
| 127 | + end |
| 128 | + |
| 129 | + @tag main_author?: true |
| 130 | + @tag co_authors: 4 |
| 131 | + test "filter on many_to_many relationship using parent works as expected - cancelled", |
| 132 | + %{ |
| 133 | + main_author: main_author, |
| 134 | + co_authors: co_authors, |
| 135 | + } do |
| 136 | + first_post = create_post(main_author) |
| 137 | + second_post = create_post(main_author) |
| 138 | + |
| 139 | + [first_ca, second_ca, third_ca, fourth_ca] = co_authors |
| 140 | + |
| 141 | + # Add first co-author |
| 142 | + create_co_author_post(first_ca, first_post, :editor) |
| 143 | + create_co_author_post(first_ca, second_post, :writer) |
| 144 | + |
| 145 | + first_ca = get_author!(first_ca.id) |
| 146 | + first_post = get_post!(first_post.id) |
| 147 | + |
| 148 | + assert Enum.count(first_post.co_authors) == 1 |
| 149 | + assert Enum.count(first_post.co_authors_unfiltered) == 1 |
| 150 | + |
| 151 | + assert Enum.count(first_ca.all_co_authored_posts) == 2 |
| 152 | + assert Enum.count(first_ca.editor_of) == 1 |
| 153 | + assert Enum.count(first_ca.writer_of) == 1 |
| 154 | + assert Enum.count(first_ca.cancelled_co_authored_posts) == 0 |
| 155 | + |
| 156 | + # Add second co-author |
| 157 | + create_co_author_post(second_ca, first_post, :proof_reader) |
| 158 | + create_co_author_post(second_ca, second_post, :writer) |
| 159 | + |
| 160 | + second_ca = get_author!(second_ca.id) |
| 161 | + first_post = get_post!(first_post.id) |
| 162 | + second_post = get_post!(second_post.id) |
| 163 | + |
| 164 | + assert Enum.count(second_post.co_authors) == 2 |
| 165 | + assert Enum.count(second_post.co_authors_unfiltered) == 2 |
| 166 | + |
| 167 | + assert Enum.count(second_ca.all_co_authored_posts) == 2 |
| 168 | + assert Enum.count(second_ca.editor_of) == 0 |
| 169 | + assert Enum.count(second_ca.writer_of) == 1 |
| 170 | + assert Enum.count(second_ca.cancelled_co_authored_posts) == 0 |
| 171 | + |
| 172 | + # Add third co-author |
| 173 | + create_co_author_post(third_ca, first_post, :proof_reader) |
| 174 | + create_co_author_post(third_ca, second_post, :proof_reader) |
| 175 | + cancel(third_ca, second_post) |
| 176 | + |
| 177 | + third_ca = get_author!(third_ca.id) |
| 178 | + first_post = get_post!(first_post.id) |
| 179 | + second_post = get_post!(second_post.id) |
| 180 | + |
| 181 | + assert Enum.count(first_post.co_authors) == 3 |
| 182 | + assert Enum.count(first_post.co_authors_unfiltered) == 3 |
| 183 | + assert Enum.count(second_post.co_authors) == 2 |
| 184 | + assert Enum.count(second_post.co_authors_unfiltered) == 3 |
| 185 | + |
| 186 | + assert Enum.count(third_ca.all_co_authored_posts) == 2 |
| 187 | + assert Enum.count(third_ca.editor_of) == 0 |
| 188 | + assert Enum.count(third_ca.writer_of) == 0 |
| 189 | + assert Enum.count(third_ca.cancelled_co_authored_posts) == 1 |
| 190 | + |
| 191 | + # Add fourth co-author |
| 192 | + create_co_author_post(fourth_ca, first_post, :proof_reader) |
| 193 | + create_co_author_post(fourth_ca, second_post, :editor) |
| 194 | + cancel(fourth_ca, first_post) |
| 195 | + cancel(fourth_ca, second_post) |
| 196 | + |
| 197 | + fourth_ca = get_author!(fourth_ca.id) |
| 198 | + first_post = get_post!(first_post.id) |
| 199 | + second_post = get_post!(second_post.id) |
| 200 | + |
| 201 | + assert Enum.count(first_post.co_authors) == 3 |
| 202 | + assert Enum.count(first_post.co_authors_unfiltered) == 4 |
| 203 | + assert Enum.count(second_post.co_authors) == 2 |
| 204 | + assert Enum.count(second_post.co_authors_unfiltered) == 4 |
| 205 | + |
| 206 | + assert Enum.count(fourth_ca.all_co_authored_posts) == 2 |
| 207 | + assert Enum.count(fourth_ca.editor_of) == 1 |
| 208 | + assert Enum.count(fourth_ca.writer_of) == 0 |
| 209 | + assert Enum.count(fourth_ca.cancelled_co_authored_posts) == 2 |
| 210 | + end |
| 211 | + end |
| 212 | +end |
0 commit comments