diff --git a/priv/resource_snapshots/test_repo/posts/20220209165617.json b/priv/resource_snapshots/test_repo/posts/20220209165617.json new file mode 100644 index 0000000..a7fa7c8 --- /dev/null +++ b/priv/resource_snapshots/test_repo/posts/20220209165617.json @@ -0,0 +1,136 @@ +{ + "attributes": [ + { + "allow_nil?": false, + "default": "fragment(\"uuid_generate_v4()\")", + "generated?": false, + "name": "id", + "primary_key?": true, + "references": null, + "size": null, + "type": "uuid" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "name": "title", + "primary_key?": false, + "references": null, + "size": null, + "type": "text" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "name": "score", + "primary_key?": false, + "references": null, + "size": null, + "type": "bigint" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "name": "public", + "primary_key?": false, + "references": null, + "size": null, + "type": "boolean" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "name": "category", + "primary_key?": false, + "references": null, + "size": null, + "type": "citext" + }, + { + "allow_nil?": true, + "default": "\"sponsored\"", + "generated?": false, + "name": "type", + "primary_key?": false, + "references": null, + "size": null, + "type": "text" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "name": "price", + "primary_key?": false, + "references": null, + "size": null, + "type": "bigint" + }, + { + "allow_nil?": true, + "default": "0", + "generated?": false, + "name": "decimal", + "primary_key?": false, + "references": null, + "size": null, + "type": "decimal" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "name": "status", + "primary_key?": false, + "references": null, + "size": null, + "type": "text" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "name": "status_enum", + "primary_key?": false, + "references": null, + "size": null, + "type": "status" + }, + { + "allow_nil?": false, + "default": "fragment(\"now()\")", + "generated?": false, + "name": "created_at", + "primary_key?": false, + "references": null, + "size": null, + "type": "utc_datetime_usec" + } + ], + "base_filter": "type = 'sponsored'", + "check_constraints": [ + { + "attribute": [ + "price" + ], + "base_filter": "type = 'sponsored'", + "check": "price > 0", + "name": "price_must_be_positive" + } + ], + "custom_indexes": [], + "has_create_action": true, + "hash": "6640FEEABB05DCF7AAFF6D81D0F6A068332DD4729177F530A19A732510450E33", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.AshPostgres.TestRepo", + "table": "posts" +} \ No newline at end of file diff --git a/priv/test_repo/migrations/20220209165137_add_status_enum.exs b/priv/test_repo/migrations/20220209165137_add_status_enum.exs new file mode 100644 index 0000000..edec726 --- /dev/null +++ b/priv/test_repo/migrations/20220209165137_add_status_enum.exs @@ -0,0 +1,12 @@ +defmodule AshPostgres.TestRepo.Migrations.AddStatusEnum do + use Ecto.Migration + + def change do + execute(""" + CREATE TYPE status AS ENUM ('open', 'closed'); + """, """ + DROP TYPE status; + """ + ) + end +end diff --git a/priv/test_repo/migrations/20220209165617_migrate_resources3.exs b/priv/test_repo/migrations/20220209165617_migrate_resources3.exs new file mode 100644 index 0000000..59af29c --- /dev/null +++ b/priv/test_repo/migrations/20220209165617_migrate_resources3.exs @@ -0,0 +1,21 @@ +defmodule AshPostgres.TestRepo.Migrations.MigrateResources3 do + @moduledoc """ + Updates resources based on their most recent snapshots. + + This file was autogenerated with `mix ash_postgres.generate_migrations` + """ + + use Ecto.Migration + + def up do + alter table(:posts) do + add :status_enum, :status + end + end + + def down do + alter table(:posts) do + remove :status_enum + end + end +end \ No newline at end of file diff --git a/test/filter_test.exs b/test/filter_test.exs index 0f5de67..fab7900 100644 --- a/test/filter_test.exs +++ b/test/filter_test.exs @@ -408,6 +408,19 @@ defmodule AshPostgres.FilterTest do end end + describe "filtering on enum types" do + test "it allows simple filtering" do + Post + |> Ash.Changeset.new(status_enum: "open") + |> Api.create!() + + assert %{status_enum: :open} = + Post + |> Ash.Query.filter(status_enum == ^"open") + |> Api.read_one!() + end + end + describe "atom filters" do test "it works on matches" do Post diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index ea7b4c2..f98ad26 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -52,6 +52,7 @@ defmodule AshPostgres.Test.Post do attribute(:price, :integer) attribute(:decimal, :decimal, default: Decimal.new(0)) attribute(:status, AshPostgres.Test.Types.Status) + attribute(:status_enum, AshPostgres.Test.Types.StatusEnum) create_timestamp(:created_at) end diff --git a/test/support/types/status_enum.ex b/test/support/types/status_enum.ex new file mode 100644 index 0000000..dfd46a2 --- /dev/null +++ b/test/support/types/status_enum.ex @@ -0,0 +1,6 @@ +defmodule AshPostgres.Test.Types.StatusEnum do + @moduledoc false + use Ash.Type.Enum, values: [:open, :closed] + + def storage_type, do: :status +end