2021-06-04 17:48:35 +12:00
|
|
|
defmodule AshPostgres.CalculationTest do
|
|
|
|
use AshPostgres.RepoCase, async: false
|
2022-12-18 01:57:25 +13:00
|
|
|
alias AshPostgres.Test.{Api, Author, Comment, Post, User, Account}
|
2021-06-04 17:48:35 +12:00
|
|
|
|
|
|
|
require Ash.Query
|
|
|
|
|
|
|
|
test "an expression calculation can be filtered on" do
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{title: "match"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{title: "title2"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post3 =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{title: "title3"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "_"})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "_"})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "_"})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post
|
|
|
|
|> Ash.Changeset.new()
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:linked_posts, [post2, post3], type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.update!()
|
|
|
|
|
|
|
|
post2
|
|
|
|
|> Ash.Changeset.new()
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:linked_posts, [post3], type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.update!()
|
|
|
|
|
|
|
|
assert [%{c_times_p: 6, title: "match"}] =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.load(:c_times_p)
|
|
|
|
|> Api.read!()
|
|
|
|
|> Enum.filter(&(&1.c_times_p == 6))
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%{c_times_p: %Ash.NotLoaded{}, title: "match"}
|
|
|
|
] =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.filter(c_times_p == 6)
|
|
|
|
|> Api.read!()
|
|
|
|
end
|
|
|
|
|
2022-02-08 10:48:36 +13:00
|
|
|
test "calculations can refer to embedded attributes" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: %{title: "Mr.", bio: "Bones"}})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert %{title: "Mr."} =
|
|
|
|
Author
|
|
|
|
|> Ash.Query.filter(id == ^author.id)
|
|
|
|
|> Ash.Query.load(:title)
|
|
|
|
|> Api.read_one!()
|
|
|
|
end
|
|
|
|
|
2022-07-21 06:19:06 +12:00
|
|
|
test "calculations can use the || operator" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: %{title: "Mr.", bio: "Bones"}})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert %{first_name_or_bob: "bob"} =
|
|
|
|
Author
|
|
|
|
|> Ash.Query.filter(id == ^author.id)
|
|
|
|
|> Ash.Query.load(:first_name_or_bob)
|
|
|
|
|> Api.read_one!()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "calculations can use the && operator" do
|
|
|
|
author =
|
|
|
|
Author
|
2022-10-25 17:17:42 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{
|
|
|
|
first_name: "fred",
|
|
|
|
bio: %{title: "Mr.", bio: "Bones"}
|
|
|
|
})
|
2022-07-21 06:19:06 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert %{first_name_and_bob: "bob"} =
|
|
|
|
Author
|
|
|
|
|> Ash.Query.filter(id == ^author.id)
|
|
|
|
|> Ash.Query.load(:first_name_and_bob)
|
|
|
|
|> Api.read_one!()
|
|
|
|
end
|
|
|
|
|
2021-06-04 17:48:35 +12:00
|
|
|
test "calculations can be used in related filters" do
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{title: "match"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{title: "title2"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post3 =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{title: "title3"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "match"})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "match"})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "match"})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "no_match"})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:post, post2, type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post
|
|
|
|
|> Ash.Changeset.new()
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:linked_posts, [post2, post3], type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.update!()
|
|
|
|
|
|
|
|
post2
|
|
|
|
|> Ash.Changeset.new()
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:linked_posts, [post3], type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.update!()
|
|
|
|
|
|
|
|
posts_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.load(:c_times_p)
|
|
|
|
|
|
|
|
assert %{post: %{c_times_p: 6}} =
|
|
|
|
Comment
|
|
|
|
|> Ash.Query.load(post: posts_query)
|
|
|
|
|> Api.read!()
|
|
|
|
|> Enum.filter(&(&1.post.c_times_p == 6))
|
|
|
|
|> Enum.at(0)
|
|
|
|
|
|
|
|
query =
|
|
|
|
Comment
|
|
|
|
|> Ash.Query.filter(post.c_times_p == 6)
|
|
|
|
|> Ash.Query.load(post: posts_query)
|
|
|
|
|> Ash.Query.limit(1)
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%{post: %{c_times_p: 6, title: "match"}}
|
|
|
|
] = Api.read!(query)
|
2022-12-18 01:57:25 +13:00
|
|
|
|
|
|
|
post |> Api.load!(:c_times_p)
|
2021-06-04 17:48:35 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
test "concat calculation can be filtered on" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.new(%{first_name: "is", last_name: "match"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.new(%{first_name: "not", last_name: "match"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
author_id = author.id
|
|
|
|
|
|
|
|
assert %{id: ^author_id} =
|
|
|
|
Author
|
2022-02-01 11:50:26 +13:00
|
|
|
|> Ash.Query.load(:full_name)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Ash.Query.filter(full_name == "is match")
|
|
|
|
|> Api.read_one!()
|
|
|
|
end
|
|
|
|
|
2021-11-14 07:55:49 +13:00
|
|
|
test "calculations that refer to aggregates in comparison expressions can be filtered on" do
|
|
|
|
Post
|
|
|
|
|> Ash.Query.load(:has_future_comment)
|
|
|
|
|> Api.read!()
|
|
|
|
end
|
|
|
|
|
2021-06-04 17:48:35 +12:00
|
|
|
test "conditional calculations can be filtered on" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.new(%{first_name: "tom"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.new(%{first_name: "tom", last_name: "holland"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
author_id = author.id
|
|
|
|
|
|
|
|
assert %{id: ^author_id} =
|
|
|
|
Author
|
2022-02-01 11:50:26 +13:00
|
|
|
|> Ash.Query.load([:conditional_full_name, :full_name])
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Ash.Query.filter(conditional_full_name == "(none)")
|
|
|
|
|> Api.read_one!()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "parameterized calculations can be filtered on" do
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.new(%{first_name: "tom", last_name: "holland"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert %{param_full_name: "tom holland"} =
|
|
|
|
Author
|
|
|
|
|> Ash.Query.load(:param_full_name)
|
|
|
|
|> Api.read_one!()
|
|
|
|
|
|
|
|
assert %{param_full_name: "tom~holland"} =
|
|
|
|
Author
|
|
|
|
|> Ash.Query.load(param_full_name: [separator: "~"])
|
|
|
|
|> Api.read_one!()
|
|
|
|
|
|
|
|
assert %{} =
|
|
|
|
Author
|
|
|
|
|> Ash.Query.filter(param_full_name(separator: "~") == "tom~holland")
|
|
|
|
|> Api.read_one!()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "parameterized related calculations can be filtered on" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.new(%{first_name: "tom", last_name: "holland"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "match"})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|
2021-06-04 17:48:35 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert %{title: "match"} =
|
|
|
|
Comment
|
|
|
|
|> Ash.Query.filter(author.param_full_name(separator: "~") == "tom~holland")
|
|
|
|
|> Api.read_one!()
|
|
|
|
|
|
|
|
assert %{title: "match"} =
|
|
|
|
Comment
|
|
|
|
|> Ash.Query.filter(
|
|
|
|
author.param_full_name(separator: "~") == "tom~holland" and
|
|
|
|
author.param_full_name(separator: " ") == "tom holland"
|
|
|
|
)
|
|
|
|
|> Api.read_one!()
|
|
|
|
end
|
2021-06-06 10:13:20 +12:00
|
|
|
|
2021-09-14 04:58:23 +12:00
|
|
|
test "parameterized calculations can be sorted on" do
|
2021-06-06 10:13:20 +12:00
|
|
|
Author
|
|
|
|
|> Ash.Changeset.new(%{first_name: "tom", last_name: "holland"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.new(%{first_name: "abc", last_name: "def"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert [%{first_name: "abc"}, %{first_name: "tom"}] =
|
|
|
|
Author
|
|
|
|
|> Ash.Query.sort(param_full_name: [separator: "~"])
|
|
|
|
|> Api.read!()
|
|
|
|
end
|
2021-11-14 07:56:14 +13:00
|
|
|
|
2022-05-21 05:22:32 +12:00
|
|
|
test "calculations using if and literal boolean results can run" do
|
|
|
|
Post
|
|
|
|
|> Ash.Query.load(:was_created_in_the_last_month)
|
2022-05-21 05:42:20 +12:00
|
|
|
|> Ash.Query.filter(was_created_in_the_last_month == true)
|
2022-05-21 05:22:32 +12:00
|
|
|
|> Api.read!()
|
|
|
|
end
|
|
|
|
|
2022-02-01 08:36:46 +13:00
|
|
|
test "nested conditional calculations can be loaded" do
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.new(%{last_name: "holland"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.new(%{first_name: "tom"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert [%{nested_conditional: "No First Name"}, %{nested_conditional: "No Last Name"}] =
|
|
|
|
Author
|
|
|
|
|> Ash.Query.load(:nested_conditional)
|
|
|
|
|> Ash.Query.sort(:nested_conditional)
|
|
|
|
|> Api.read!()
|
|
|
|
end
|
|
|
|
|
2021-11-14 07:56:14 +13:00
|
|
|
test "calculations load nullable timestamp aggregates compared to a fragment" do
|
2021-11-14 08:48:14 +13:00
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{title: "aaa", score: 0})
|
|
|
|
|> Api.create!()
|
2021-11-14 07:56:14 +13:00
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{title: "aaa", score: 1})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.new(%{title: "bbb", score: 0})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "aaa", likes: 1, arbitrary_timestamp: DateTime.now!("Etc/UTC")})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|
2021-11-14 07:56:14 +13:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "bbb", likes: 1})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|
2021-11-14 07:56:14 +13:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Comment
|
|
|
|
|> Ash.Changeset.new(%{title: "aaa", likes: 2})
|
2022-09-22 05:36:18 +12:00
|
|
|
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|
2021-11-14 07:56:14 +13:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.Query.load([:has_future_arbitrary_timestamp])
|
|
|
|
|> Api.read!()
|
|
|
|
end
|
2022-12-18 01:57:25 +13:00
|
|
|
|
|
|
|
test "loading a calculation loads its dependent loads" do
|
|
|
|
user =
|
|
|
|
User
|
|
|
|
|> Ash.Changeset.for_create(:create, %{is_active: true})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
account =
|
|
|
|
Account
|
|
|
|
|> Ash.Changeset.for_create(:create, %{is_active: true})
|
|
|
|
|> Ash.Changeset.manage_relationship(:user, user, type: :append_and_remove)
|
|
|
|
|> Api.create!()
|
|
|
|
# When the commented out line below is used instead, it works. However the
|
|
|
|
# :user_is_active aggregate is already specified as needing to be loaded
|
|
|
|
# in the Account resource :active calculation, so we should not have to
|
|
|
|
# pass it in again.
|
|
|
|
#
|
|
|
|
# |> Api.load!([:user_is_active, :active])
|
|
|
|
|> Api.load!([:active])
|
|
|
|
|> IO.inspect(limit: :infinity, pretty: true)
|
|
|
|
|
|
|
|
assert account.active
|
|
|
|
end
|
2021-06-04 17:48:35 +12:00
|
|
|
end
|