Skip to content

Commit

Permalink
test: add failing test for an aggregate of a calculation (#92)
Browse files Browse the repository at this point in the history
An error occurs at compile time:

== Compilation error in file test/support/schema.ex ==
** (MatchError) no match of right hand side value: {:error, "Must provide field type for max"}
    lib/resource/resource.ex:2278: AshGraphql.Resource.filterable?/2
    (elixir 1.15.4) lib/enum.ex:4265: Enum.filter_list/2
    (elixir 1.15.4) lib/enum.ex:4266: Enum.filter_list/2
    lib/resource/resource.ex:2229: AshGraphql.Resource.aggregate_filter_fields/2
    lib/resource/resource.ex:2195: AshGraphql.Resource.resource_filter_fields/2
    lib/resource/resource.ex:1159: AshGraphql.Resource.args/5
    lib/resource/resource.ex:425: anonymous fn/6 in AshGraphql.Resource.queries/5
    (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
  • Loading branch information
ahey authored Aug 30, 2023
1 parent 39e33ca commit 0c869aa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/read_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,44 @@ defmodule AshGraphql.ReadTest do
assert %{data: %{"getCompositePrimaryKey" => %{"id" => ^id}}} = result
end

test "aggregate of a calculation" do
post =
AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create,
text: "foo",
published: true,
score: 9.8
)
|> AshGraphql.Test.Api.create!()

post =
AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create, text: "foo", published: true)
|> AshGraphql.Test.Api.create!()

AshGraphql.Test.Comment
|> Ash.Changeset.for_create(:create, %{text: "stuff"})
|> Ash.Changeset.force_change_attribute(:post_id, post.id)
|> AshGraphql.Test.Api.create!()

resp =
"""
query Post($id: ID!) {
getPost(id: $id) {
latestCommentAt
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"id" => post.id
}
)

assert {:ok, result} = resp
assert result[:data]["getPost"]["latestCommentAt"]
end

test "a read with custom types works" do
AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create,
Expand Down
10 changes: 10 additions & 0 deletions test/support/resources/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ defmodule AshGraphql.Test.Comment do
writable?(false)
default(:comment)
end

create_timestamp(:created_at)
end

calculations do
calculate(
:timestamp,
:utc_datetime_usec,
expr(created_at)
)
end

relationships do
Expand Down
1 change: 1 addition & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ defmodule AshGraphql.Test.Post do

aggregates do
count(:comment_count, :comments)
max(:latest_comment_at, [:comments], :timestamp)
end

relationships do
Expand Down

0 comments on commit 0c869aa

Please sign in to comment.