From 9ad74022276e7db993cf3209ed829909a9871608 Mon Sep 17 00:00:00 2001 From: James Harton Date: Tue, 23 Apr 2024 10:07:53 +1200 Subject: [PATCH] chore: Add failing test for tenanted aggregate bug. --- test/aggregate_test.exs | 26 +++++++++++++++++++++ test/support/multitenancy/resources/post.ex | 4 ++++ test/support/multitenancy/resources/user.ex | 4 ++++ 3 files changed, 34 insertions(+) diff --git a/test/aggregate_test.exs b/test/aggregate_test.exs index ce6df24..1dadc92 100644 --- a/test/aggregate_test.exs +++ b/test/aggregate_test.exs @@ -444,6 +444,32 @@ defmodule AshSql.AggregateTest do |> Ash.Query.load([:count_comment_titles, :count_uniq_comment_titles]) |> Ash.read_one!() end + + test "when related data that uses schema-based multitenancy, it returns the uniq" do + alias AshPostgres.MultitenancyTest.{Org, Post, User} + + org = + Org + |> Ash.Changeset.for_create(:create, %{name: "BTTF"}) + |> Ash.create!() + + user = + User + |> Ash.Changeset.for_create(:create, %{name: "Marty", org_id: org.id}) + |> Ash.create!() + + posts = + ["Back to 1955", "Forwards to 1985", "Forward to 2015", "Back again to 1985"] + |> Enum.map( + &(Post + |> Ash.Changeset.for_create(:create, %{name: &1}) + |> Ash.create!(tenant: "org_#{org.id}", load: [:last_word])) + ) + + user = Ash.load!(user, :years_visited, tenant: "org_#{org.id}") + + assert Enum.sort(user.years_visited) == ["1955", "1985", "2015"] + end end describe "first" do diff --git a/test/support/multitenancy/resources/post.ex b/test/support/multitenancy/resources/post.ex index 84452d8..35cc9ad 100644 --- a/test/support/multitenancy/resources/post.ex +++ b/test/support/multitenancy/resources/post.ex @@ -51,4 +51,8 @@ defmodule AshPostgres.MultitenancyTest.Post do has_one(:self, __MODULE__, destination_attribute: :id, source_attribute: :id, public?: true) end + + calculations do + calculate(:last_word, :string, expr(fragment("split_part(?, ' ', -1)", name))) + end end diff --git a/test/support/multitenancy/resources/user.ex b/test/support/multitenancy/resources/user.ex index 05ab967..1f872ff 100644 --- a/test/support/multitenancy/resources/user.ex +++ b/test/support/multitenancy/resources/user.ex @@ -36,5 +36,9 @@ defmodule AshPostgres.MultitenancyTest.User do end end + aggregates do + list(:years_visited, :posts, :last_word) + end + def parse_tenant("org_" <> id), do: id end