Skip to content

Commit b795cf1

Browse files
committed
test(Aggregate Uniq Count): added a test to demonstrate the aggregate issue
1 parent 4be9d1c commit b795cf1

7 files changed

Lines changed: 250 additions & 1 deletion

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"attributes": [
3+
{
4+
"allow_nil?": false,
5+
"default": "nil",
6+
"generated?": false,
7+
"precision": null,
8+
"primary_key?": true,
9+
"references": {
10+
"deferrable": false,
11+
"destination_attribute": "id",
12+
"destination_attribute_default": null,
13+
"destination_attribute_generated": null,
14+
"index?": false,
15+
"match_type": null,
16+
"match_with": null,
17+
"multitenancy": {
18+
"attribute": null,
19+
"global": null,
20+
"strategy": null
21+
},
22+
"name": "comment_like_author_fkey",
23+
"on_delete": "delete",
24+
"on_update": "update",
25+
"primary_key?": true,
26+
"schema": "public",
27+
"table": "authors"
28+
},
29+
"scale": null,
30+
"size": null,
31+
"source": "author_id",
32+
"type": "uuid"
33+
},
34+
{
35+
"allow_nil?": false,
36+
"default": "nil",
37+
"generated?": false,
38+
"precision": null,
39+
"primary_key?": true,
40+
"references": {
41+
"deferrable": false,
42+
"destination_attribute": "id",
43+
"destination_attribute_default": null,
44+
"destination_attribute_generated": null,
45+
"index?": false,
46+
"match_type": null,
47+
"match_with": null,
48+
"multitenancy": {
49+
"attribute": null,
50+
"global": null,
51+
"strategy": null
52+
},
53+
"name": "comment_like_comment_fkey",
54+
"on_delete": "delete",
55+
"on_update": "update",
56+
"primary_key?": true,
57+
"schema": "public",
58+
"table": "comments"
59+
},
60+
"scale": null,
61+
"size": null,
62+
"source": "comment_id",
63+
"type": "uuid"
64+
}
65+
],
66+
"base_filter": null,
67+
"check_constraints": [],
68+
"create_table_options": null,
69+
"custom_indexes": [],
70+
"custom_statements": [],
71+
"has_create_action": true,
72+
"hash": "84EF2268196E984A63CA6547634850899334D1B15F2AE7B1A9C04A17566ABAB4",
73+
"identities": [],
74+
"multitenancy": {
75+
"attribute": null,
76+
"global": null,
77+
"strategy": null
78+
},
79+
"repo": "Elixir.AshPostgres.TestRepo",
80+
"schema": null,
81+
"table": "comment_likes"
82+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule AshPostgres.TestRepo.Migrations.MigrateResources70 do
2+
@moduledoc """
3+
Updates resources based on their most recent snapshots.
4+
5+
This file was autogenerated with `mix ash_postgres.generate_migrations`
6+
"""
7+
8+
use Ecto.Migration
9+
10+
def up do
11+
create table(:comment_likes, primary_key: false) do
12+
add(
13+
:author_id,
14+
references(:authors,
15+
column: :id,
16+
name: "comment_like_author_fkey",
17+
type: :uuid,
18+
prefix: "public",
19+
on_delete: :delete_all,
20+
on_update: :update_all
21+
), primary_key: true, null: false)
22+
23+
add(
24+
:comment_id,
25+
references(:comments,
26+
column: :id,
27+
name: "comment_like_comment_fkey",
28+
type: :uuid,
29+
prefix: "public",
30+
on_delete: :delete_all,
31+
on_update: :update_all
32+
), primary_key: true, null: false)
33+
end
34+
end
35+
36+
def down do
37+
drop(constraint(:comment_likes, "comment_like_author_fkey"))
38+
39+
drop(constraint(:comment_likes, "comment_like_comment_fkey"))
40+
41+
drop(table(:comment_likes))
42+
end
43+
end

test/aggregate_test.exs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
defmodule AshSql.AggregateTest do
66
use AshPostgres.RepoCase, async: false
77
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}
99

1010
require Ash.Query
1111
require Ash.Sort
@@ -469,6 +469,59 @@ defmodule AshSql.AggregateTest do
469469
|> Ash.Query.load(:count_of_comments_called_match)
470470
|> Ash.read_one!()
471471
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
472525
end
473526

474527
describe "exists" do

test/support/domain.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ defmodule AshPostgres.Test.Domain do
1515

1616
resource(AshPostgres.Test.Comedian)
1717
resource(AshPostgres.Test.Comment)
18+
resource(AshPostgres.Test.CommentLike)
1819
resource(AshPostgres.Test.CommentLink)
1920
resource(AshPostgres.Test.IntegerPost)
2021
resource(AshPostgres.Test.Rating)

test/support/resources/comment.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ defmodule AshPostgres.Test.Comment do
100100
public?(true)
101101
end
102102

103+
has_many(:better_likes, AshPostgres.Test.CommentLike) do
104+
public?(true)
105+
end
106+
103107
many_to_many(:linked_comments, AshPostgres.Test.Comment) do
104108
public?(true)
105109
through(AshPostgres.Test.CommentLink)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs/contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule AshPostgres.Test.CommentLike do
6+
@moduledoc false
7+
use Ash.Resource,
8+
domain: AshPostgres.Test.Domain,
9+
data_layer: AshPostgres.DataLayer,
10+
authorizers: [
11+
Ash.Policy.Authorizer
12+
]
13+
14+
policies do
15+
bypass action_type(:read) do
16+
# Check that the comment is in the same org (via post) as actor
17+
authorize_if(relates_to_actor_via([:post, :organization, :users]))
18+
end
19+
end
20+
21+
postgres do
22+
table "comment_likes"
23+
repo(AshPostgres.TestRepo)
24+
25+
references do
26+
reference(:author,
27+
on_delete: :delete,
28+
on_update: :update,
29+
name: "comment_like_author_fkey"
30+
)
31+
32+
reference(:comment,
33+
on_delete: :delete,
34+
on_update: :update,
35+
name: "comment_like_comment_fkey"
36+
)
37+
end
38+
end
39+
40+
actions do
41+
default_accept(:*)
42+
defaults([:create, :read, :update, :destroy])
43+
end
44+
45+
relationships do
46+
belongs_to(:author, AshPostgres.Test.Author) do
47+
allow_nil?(false)
48+
public?(true)
49+
primary_key?(true)
50+
end
51+
52+
belongs_to(:comment, AshPostgres.Test.Comment) do
53+
allow_nil?(false)
54+
public?(true)
55+
primary_key?(true)
56+
end
57+
end
58+
end

test/support/resources/organization.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ defmodule AshPostgres.Test.Organization do
3434
count :no_cast_open_posts_count, :posts do
3535
filter(expr(status_enum_no_cast != :closed))
3636
end
37+
38+
count :org_likes, [:posts, :comments, :better_likes] do
39+
uniq?(true)
40+
end
41+
42+
count :unique_org_likers, [:posts, :comments, :better_likes, :author] do
43+
uniq?(true)
44+
end
3745
end
3846

3947
actions do

0 commit comments

Comments
 (0)