ash/test/distinct/distinct_test.exs
Zach Daniel 5b2efe5e5c improvement: support distincting on calculations
improvement: support distinct in ets data layer for testing
2023-07-18 14:35:25 -04:00

64 lines
1.2 KiB
Elixir

defmodule Ash.Test.Sort.SortTest do
@moduledoc false
use ExUnit.Case, async: true
require Ash.Query
defmodule Post do
@moduledoc false
use Ash.Resource, data_layer: Ash.DataLayer.Ets
ets do
private?(true)
end
actions do
defaults [:read, :create, :update]
end
attributes do
uuid_primary_key :id
attribute :title, :string
end
calculations do
calculate :first_title_word, :string, expr(at(string_split(title, " ", trim?: true), 0))
end
end
defmodule Api do
@moduledoc false
use Ash.Api
resources do
allow_unregistered? true
end
end
setup do
Post
|> Ash.Changeset.for_create(:create, %{title: "fred armisen"})
|> Api.create!()
Post
|> Ash.Changeset.for_create(:create, %{title: "fred armisen"})
|> Api.create!()
Post
|> Ash.Changeset.for_create(:create, %{title: "fred weasley"})
|> Api.create!()
:ok
end
test "distinct by attribute works" do
assert [_, _] = Post |> Ash.Query.distinct(:title) |> Api.read!()
end
test "distinct by calculation works" do
assert [_] =
Post
|> Ash.Query.distinct(:first_title_word)
|> Api.read!()
end
end