From dd09f6fc58d0c5f45a5fd920722e63f7a493e220 Mon Sep 17 00:00:00 2001 From: Rebecca Le Date: Wed, 4 Mar 2026 13:25:04 +0800 Subject: [PATCH] Add test for filtering on doubly-nested embedded resource fields Adds `AshPostgres.Test.BioAddress` as a nested embedded resource within `AshPostgres.Test.Bio`, and a regression test that filters `Author` records by `bio[:address][:city]`. This exercises the `get_path` code path for doubly-nested embedded resources, which previously raised a `CaseClauseError` in `AshSql.Expr.split_at_paths/4`. --- test/embeddable_resource_test.exs | 22 ++++++++++++++++++++++ test/support/resources/bio.ex | 2 ++ test/support/resources/bio_address.ex | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 test/support/resources/bio_address.ex diff --git a/test/embeddable_resource_test.exs b/test/embeddable_resource_test.exs index b987f90d..ead20aa8 100644 --- a/test/embeddable_resource_test.exs +++ b/test/embeddable_resource_test.exs @@ -23,6 +23,28 @@ defmodule AshPostgres.EmbeddableResourceTest do Ash.load!(post, :calc_returning_json) end + test "can filter on a doubly-nested embedded resource field" do + Author + |> Ash.Changeset.for_create(:create, %{ + bio: %{address: %{city: "Sydney", country: "AU"}} + }) + |> Ash.create!() + + Author + |> Ash.Changeset.for_create(:create, %{ + bio: %{address: %{city: "Melbourne", country: "AU"}} + }) + |> Ash.create!() + + results = + Author + |> Ash.Query.filter(bio[:address][:city] == "Sydney") + |> Ash.read!() + + assert length(results) == 1 + assert hd(results).bio.address.city == "Sydney" + end + test "embeds with list attributes set to nil are loaded as nil" do author = Author diff --git a/test/support/resources/bio.ex b/test/support/resources/bio.ex index 0dce7e04..8d03b31e 100644 --- a/test/support/resources/bio.ex +++ b/test/support/resources/bio.ex @@ -22,5 +22,7 @@ defmodule AshPostgres.Test.Bio do allow_nil?(true) default(nil) end + + attribute(:address, AshPostgres.Test.BioAddress, public?: true) end end diff --git a/test/support/resources/bio_address.ex b/test/support/resources/bio_address.ex new file mode 100644 index 00000000..30ce5425 --- /dev/null +++ b/test/support/resources/bio_address.ex @@ -0,0 +1,18 @@ +# SPDX-FileCopyrightText: 2019 ash_postgres contributors +# +# SPDX-License-Identifier: MIT + +defmodule AshPostgres.Test.BioAddress do + @moduledoc false + use Ash.Resource, data_layer: :embedded + + actions do + default_accept(:*) + defaults([:create, :read, :update, :destroy]) + end + + attributes do + attribute(:city, :string, public?: true) + attribute(:country, :string, public?: true) + end +end