chore: test enum types

This commit is contained in:
Zach Daniel 2022-02-09 12:13:11 -05:00
parent cd9701a6ea
commit a6577d5175
6 changed files with 189 additions and 0 deletions

View file

@ -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"
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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