test: add test for string enum (#1344)

This commit is contained in:
Barnabas Jovanovics 2024-07-25 13:24:50 +02:00 committed by GitHub
parent d199f701ac
commit 8a1c47c131
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 52 additions and 30 deletions

21
test/support/enum.ex Normal file
View file

@ -0,0 +1,21 @@
defmodule Status do
use Ash.Type.Enum, values: [:open, :Closed, :NeverHappened, :Always_Was]
def match("never_happened"), do: {:ok, :NeverHappened}
def match(value), do: super(value)
end
defmodule DescriptiveEnum do
use Ash.Type.Enum,
values: [
{:foo, "Clearly a foo"},
{:bar, "Obviously a bar"},
{:baz, "Undoubtedly a baz"},
:a_thing_with_no_description,
{:another_thing_with_no_description, nil}
]
end
defmodule StringEnum do
use Ash.Type.Enum, values: ["foo", "bar", "baz"]
end

View file

@ -5,25 +5,6 @@ defmodule Ash.Test.Type.EnumTest do
require Ash.Query require Ash.Query
alias Ash.Test.Domain, as: Domain alias Ash.Test.Domain, as: Domain
alias Ash.Type.DurationName
defmodule Status do
use Ash.Type.Enum, values: [:open, :Closed, :NeverHappened, :Always_Was]
def match("never_happened"), do: {:ok, :NeverHappened}
def match(value), do: super(value)
end
defmodule DescriptiveEnum do
use Ash.Type.Enum,
values: [
{:foo, "Clearly a foo"},
{:bar, "Obviously a bar"},
{:baz, "Undoubtedly a baz"},
:a_thing_with_no_description,
{:another_thing_with_no_description, nil}
]
end
defmodule Post do defmodule Post do
@moduledoc false @moduledoc false
@ -105,16 +86,36 @@ defmodule Ash.Test.Type.EnumTest do
assert DescriptiveEnum.description(:another_thing_with_no_description) == nil assert DescriptiveEnum.description(:another_thing_with_no_description) == nil
end end
test "types are correctly generated" do describe "types are correctly generated" do
# Testing with DurationName instead of Status since modules defined in test "simple atoms" do
# .exs files are not written to disc and their types therefore can't be assert {:ok,
# loaded. [
assert {:ok, type:
[ {:t, {:type, 0, :union, [{:atom, 0, :Always_Was}, _, _, {:atom, 0, :open}]}, []}
type: ]} = Code.Typespec.fetch_types(Status)
{:t, end
{:type, 0, :union,
[{:atom, 0, :microsecond}, _, _, _, _, _, _, _, {:atom, 0, :year}]}, []} test "descriptive atoms" do
]} = Code.Typespec.fetch_types(DurationName) assert {:ok,
[
type:
{:t,
{:type, 0, :union,
[
{:atom, 0, :another_thing_with_no_description},
_,
_,
_,
{:atom, 0, :foo}
]}, []}
]} = Code.Typespec.fetch_types(DescriptiveEnum)
end
test "strings" do
assert {:ok,
[
type: {:t, {:type, 0, :union, [{:string, 0, "foo"}, _, {:string, 0, "baz"}]}, []}
]} = Code.Typespec.fetch_types(StringEnum)
end
end end
end end