Skip to content

Commit 94029e2

Browse files
committed
test: add tests for field?: false calculations
1 parent a8641ea commit 94029e2

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ defmodule AshPostgres.MixProject do
187187
defp deps do
188188
[
189189
{:ash, ash_version("~> 3.19")},
190-
{:spark, "~> 2.3 and >= 2.3.4"},
190+
{:spark, ">= 2.3.4"},
191191
{:ash_sql, ash_sql_version("~> 0.4 and >= 0.4.3")},
192192
{:igniter, "~> 0.6 and >= 0.6.29", optional: true},
193193
{:ecto_sql, "~> 3.13"},

mix.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
%{
2-
"ash": {:hex, :ash, "3.20.0", "1837fd4af980a67cf723e48612fe48d86d410c5ec86a3bef2c0a7ba5815fe371", [:mix], [{:crux, ">= 0.1.2 and < 1.0.0-0", [hex: :crux, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, ">= 0.6.29 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 1.0", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.3.14 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.3", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "29300a6dbbe38ea33941b2bb479e9826acf8aa46590c94ebfca22102a54d569e"},
3-
"ash_sql": {:hex, :ash_sql, "0.5.0", "06cf976f2cca3c16542b9c3103220ed1358903c6a83bd0ee541432d371a87a9e", [:mix], [{:ash, "~> 3.7", [hex: :ash, repo: "hexpm", optional: false]}, {:ecto, ">= 3.13.4 and < 4.0.0-0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "b78865699cd706db7d8e07f366a5bd61ea06dde19f649870ff895d293ecc42a1"},
2+
"ash": {:hex, :ash, "3.21.0", "d06e61a32116e09efd1c5258f299ee302dbe84b601fbb593868898a7063fb2bc", [:mix], [{:crux, ">= 0.1.2 and < 1.0.0-0", [hex: :crux, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, ">= 0.6.29 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 1.0", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.6.0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.3", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3b713cb02bde604f54a26558ee859d03be20e7736258f0c42a64020ddc7c1bb0"},
3+
"ash_sql": {:hex, :ash_sql, "0.5.1", "f4fcaa29308bdf417fe85373e57fb6d868b7db1e7ccc2fae5cc7f8f92016d622", [:mix], [{:ash, "~> 3.7", [hex: :ash, repo: "hexpm", optional: false]}, {:ecto, ">= 3.13.4 and < 4.0.0-0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "33febc8a84443683c3685a2d65f674391d8f21c073555d7c22d6c484f72faa65"},
44
"benchee": {:hex, :benchee, "1.5.0", "4d812c31d54b0ec0167e91278e7de3f596324a78a096fd3d0bea68bb0c513b10", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.1", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "5b075393aea81b8ae74eadd1c28b1d87e8a63696c649d8293db7c4df3eb67535"},
55
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
66
"credo": {:hex, :credo, "1.7.17", "f92b6aa5b26301eaa5a35e4d48ebf5aa1e7094ac00ae38f87086c562caf8a22f", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1eb5645c835f0b6c9b5410f94b5a185057bcf6d62a9c2b476da971cde8749645"},
@@ -47,7 +47,7 @@
4747
"simple_sat": {:hex, :simple_sat, "0.1.4", "39baf72cdca14f93c0b6ce2b6418b72bbb67da98fa9ca4384e2f79bbc299899d", [:mix], [], "hexpm", "3569b68e346a5fd7154b8d14173ff8bcc829f2eb7b088c30c3f42a383443930b"},
4848
"sobelow": {:hex, :sobelow, "0.14.1", "2f81e8632f15574cba2402bcddff5497b413c01e6f094bc0ab94e83c2f74db81", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8fac9a2bd90fdc4b15d6fca6e1608efb7f7c600fa75800813b794ee9364c87f2"},
4949
"sourceror": {:hex, :sourceror, "1.12.0", "da354c5f35aad3cc1132f5d5b0d8437d865e2661c263260480bab51b5eedb437", [:mix], [], "hexpm", "755703683bd014ebcd5de9acc24b68fb874a660a568d1d63f8f98cd8a6ef9cd0"},
50-
"spark": {:hex, :spark, "2.5.0", "82c39d2800fbc6628b8cfa8ffc2027317a0578a00ac5e6a4bb34baf245de34b1", [:mix], [{:igniter, ">= 0.3.64 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: true]}, {:sourceror, "~> 1.2", [hex: :sourceror, repo: "hexpm", optional: true]}], "hexpm", "61247c5ce248bca41678ef7206677c7d8fa5235d431c16c12705b2de0d4cfed1"},
50+
"spark": {:hex, :spark, "2.6.0", "3d4d0e5d65c0ca2b03d0ec3d069bf3a6bc0d23f2d57e6ef6a0a0b448ea8c6f2b", [:mix], [{:igniter, ">= 0.3.64 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: true]}, {:sourceror, "~> 1.2", [hex: :sourceror, repo: "hexpm", optional: true]}], "hexpm", "a9c4c48235dc6d3fe1021ab363da740a0a2a8d704ee7b1875590924e6950d6b2"},
5151
"spitfire": {:hex, :spitfire, "0.3.10", "19aea9914132456515e8f7d592f63ab9f3130876b0252e834d2390bdd8becb24", [:mix], [], "hexpm", "6a6a5f77eb4165249c76199cd2d01fb595bac9207aed3de551918ac1c2bc9267"},
5252
"splode": {:hex, :splode, "0.3.0", "ff8effecc509a51245df2f864ec78d849248647c37a75886033e3b1a53ca9470", [:mix], [], "hexpm", "73cfd0892d7316d6f2c93e6e8784bd6e137b2aa38443de52fd0a25171d106d81"},
5353
"statistex": {:hex, :statistex, "1.1.0", "7fec1eb2f580a0d2c1a05ed27396a084ab064a40cfc84246dbfb0c72a5c761e5", [:mix], [], "hexpm", "f5950ea26ad43246ba2cce54324ac394a4e7408fdcf98b8e230f503a0cba9cf5"},

test/calculation_test.exs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,4 +1724,64 @@ defmodule AshPostgres.CalculationTest do
17241724
Ash.load!(post, [:has_matching_author_by_unrelated_exists])
17251725
end
17261726
end
1727+
1728+
describe "field?: false calculations" do
1729+
test "can be loaded and appear in the calculations map" do
1730+
author =
1731+
Author
1732+
|> Ash.Changeset.for_create(:create, %{first_name: "zach", last_name: "daniel"})
1733+
|> Ash.create!()
1734+
1735+
author = Ash.load!(author, [:non_field_full_name])
1736+
assert author.calculations[:non_field_full_name] == "zach daniel"
1737+
end
1738+
1739+
test "can be loaded via Ash.Query.load" do
1740+
Author
1741+
|> Ash.Changeset.for_create(:create, %{first_name: "zach", last_name: "daniel"})
1742+
|> Ash.create!()
1743+
1744+
author =
1745+
Author
1746+
|> Ash.Query.load(:non_field_full_name)
1747+
|> Ash.read_one!()
1748+
1749+
assert author.calculations[:non_field_full_name] == "zach daniel"
1750+
end
1751+
1752+
test "can be referenced in other expressions" do
1753+
Author
1754+
|> Ash.Changeset.for_create(:create, %{first_name: "zach", last_name: "daniel"})
1755+
|> Ash.create!()
1756+
1757+
author =
1758+
Author
1759+
|> Ash.Query.load(:non_field_refers_to_field_calc)
1760+
|> Ash.read_one!()
1761+
1762+
assert author.calculations[:non_field_refers_to_field_calc] ==
1763+
"zach daniel (zach daniel)"
1764+
end
1765+
1766+
test "can be used in filters" do
1767+
Author
1768+
|> Ash.Changeset.for_create(:create, %{first_name: "zach", last_name: "daniel"})
1769+
|> Ash.create!()
1770+
1771+
Author
1772+
|> Ash.Changeset.for_create(:create, %{first_name: "other", last_name: "person"})
1773+
|> Ash.create!()
1774+
1775+
assert [author] =
1776+
Author
1777+
|> Ash.Query.filter(non_field_full_name == "zach daniel")
1778+
|> Ash.read!()
1779+
1780+
assert author.first_name == "zach"
1781+
end
1782+
1783+
test "do not add a key to the resource struct" do
1784+
refute Map.has_key?(%Author{}, :non_field_full_name)
1785+
end
1786+
end
17271787
end

test/support/resources/author.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ defmodule AshPostgres.Test.Author do
217217

218218
calculate(:profile_description_calc, :string, expr(profile.description), allow_nil?: true)
219219

220+
calculate :non_field_full_name, :string, expr(first_name <> " " <> last_name) do
221+
field?(false)
222+
end
223+
224+
calculate :non_field_refers_to_field_calc, :string, expr(full_name <> " (" <> non_field_full_name <> ")") do
225+
field?(false)
226+
end
227+
220228
calculate(:true_if_actor_in_context, :boolean, TrueIfActorInContext)
221229

222230
calculate(

0 commit comments

Comments
 (0)