From ee0c2a0f452b8cf8b5f379ad993f7ed7546ab064 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Mon, 16 Sep 2024 17:04:15 -0400 Subject: [PATCH] test: add test for behavior fixed in ash --- test/calculation_test.exs | 39 ++++++++++++++++++++++++++++++++++ test/support/resources/post.ex | 1 + 2 files changed, 40 insertions(+) diff --git a/test/calculation_test.exs b/test/calculation_test.exs index 0301868d..8c0f2678 100644 --- a/test/calculation_test.exs +++ b/test/calculation_test.exs @@ -5,6 +5,45 @@ defmodule AshPostgres.CalculationTest do require Ash.Query import Ash.Expr + test "a calculation that references a first optimizable aggregate can be sorted on" do + author1 = + Author + |> Ash.Changeset.for_create(:create, %{ + first_name: "abc" + }) + |> Ash.create!() + + post = + Post + |> Ash.Changeset.for_create(:create, %{title: "match", author_id: author1.id}) + |> Ash.create!() + + author2 = + Author + |> Ash.Changeset.for_create(:create, %{ + first_name: "def" + }) + |> Ash.create!() + + post2 = + Post + |> Ash.Changeset.for_create(:create, %{title: "match", author_id: author2.id}) + |> Ash.create!() + + post1_id = post.id + post2_id = post2.id + + assert [%{id: ^post2_id}, %{id: ^post1_id}] = + Post + |> Ash.Query.sort(author_first_name_ref_agg_calc: :desc) + |> Ash.read!() + + assert [%{id: ^post1_id}, %{id: ^post2_id}] = + Post + |> Ash.Query.sort(author_first_name_ref_agg_calc: :asc) + |> Ash.read!() + end + test "an expression calculation can be filtered on" do post = Post diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index 7f1385a9..d6b8e745 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -692,6 +692,7 @@ defmodule AshPostgres.Test.Post do ) calculate(:author_first_name_calc, :string, expr(author.first_name)) + calculate(:author_first_name_ref_agg_calc, :string, expr(author_first_name)) calculate(:author_profile_description_from_agg, :string, expr(author_profile_description)) end