|
5 | 5 | defmodule AshSql.AggregateTest do |
6 | 6 | use AshPostgres.RepoCase, async: false |
7 | 7 | import ExUnit.CaptureIO |
8 | | - alias AshPostgres.Test.{Author, Chat, Comment, Organization, Post, Rating, User} |
| 8 | + alias AshPostgres.Test.{Author, Chat, Comment, CommentLike, Organization, Post, Rating, User} |
9 | 9 |
|
10 | 10 | require Ash.Query |
11 | 11 | require Ash.Sort |
@@ -469,6 +469,59 @@ defmodule AshSql.AggregateTest do |
469 | 469 | |> Ash.Query.load(:count_of_comments_called_match) |
470 | 470 | |> Ash.read_one!() |
471 | 471 | end |
| 472 | + |
| 473 | + test "organization like aggregates count likes and dedupe unique likers" do |
| 474 | + org = |
| 475 | + Organization |
| 476 | + |> Ash.Changeset.for_create(:create, %{name: "The Org"}) |
| 477 | + |> Ash.create!() |
| 478 | + |
| 479 | + post = |
| 480 | + Post |
| 481 | + |> Ash.Changeset.for_create(:create, %{title: "title"}) |
| 482 | + |> Ash.Changeset.manage_relationship(:organization, org, type: :append_and_remove) |
| 483 | + |> Ash.create!() |
| 484 | + |
| 485 | + comment_1 = |
| 486 | + Comment |
| 487 | + |> Ash.Changeset.for_create(:create, %{title: "comment 1"}) |
| 488 | + |> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove) |
| 489 | + |> Ash.create!() |
| 490 | + |
| 491 | + comment_2 = |
| 492 | + Comment |
| 493 | + |> Ash.Changeset.for_create(:create, %{title: "comment 2"}) |
| 494 | + |> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove) |
| 495 | + |> Ash.create!() |
| 496 | + |
| 497 | + author_1 = |
| 498 | + Author |
| 499 | + |> Ash.Changeset.for_create(:create, %{first_name: "A", last_name: "One"}) |
| 500 | + |> Ash.create!() |
| 501 | + |
| 502 | + author_2 = |
| 503 | + Author |
| 504 | + |> Ash.Changeset.for_create(:create, %{first_name: "B", last_name: "Two"}) |
| 505 | + |> Ash.create!() |
| 506 | + |
| 507 | + [{comment_1, author_1}, {comment_2, author_1}, {comment_1, author_2}] |
| 508 | + |> Enum.each(fn {comment, author} -> |
| 509 | + CommentLike |
| 510 | + |> Ash.Changeset.for_create(:create, %{}) |
| 511 | + |> Ash.Changeset.manage_relationship(:comment, comment, type: :append_and_remove) |
| 512 | + |> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove) |
| 513 | + |> Ash.create!() |
| 514 | + end) |
| 515 | + |
| 516 | + loaded_org = |
| 517 | + Organization |
| 518 | + |> Ash.Query.filter(id == ^org.id) |
| 519 | + |> Ash.Query.load([:org_likes, :unique_org_likers]) |
| 520 | + |> Ash.read_one!() |
| 521 | + |
| 522 | + assert loaded_org.org_likes == 3 |
| 523 | + assert loaded_org.unique_org_likers == 2 |
| 524 | + end |
472 | 525 | end |
473 | 526 |
|
474 | 527 | describe "exists" do |
|
0 commit comments