Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lcmen committed Feb 13, 2025
1 parent 69c0099 commit 0344284
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
64 changes: 64 additions & 0 deletions priv/resource_snapshots/test_repo/orgs/20250210191116.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"attributes": [
{
"allow_nil?": false,
"default": "fragment(\"gen_random_uuid()\")",
"generated?": false,
"primary_key?": true,
"references": null,
"size": null,
"source": "id",
"type": "uuid"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "name",
"type": "text"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"primary_key?": false,
"references": null,
"size": null,
"source": "department",
"type": "text"
}
],
"base_filter": null,
"check_constraints": [],
"custom_indexes": [],
"custom_statements": [],
"has_create_action": true,
"hash": "1D1BA9E1E272238D80C9861CAA67C4A85F675E3B052A15F4D5AC272551B820A7",
"identities": [
{
"all_tenants?": false,
"base_filter": null,
"index_name": "orgs_department_index",
"keys": [
{
"type": "string",
"value": "(LOWER(department))"
}
],
"name": "department",
"nils_distinct?": true,
"where": null
}
],
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"repo": "Elixir.AshPostgres.TestRepo",
"schema": null,
"table": "orgs"
}
25 changes: 25 additions & 0 deletions priv/test_repo/migrations/20250210191116_migrate_resources49.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule AshPostgres.TestRepo.Migrations.MigrateResources49 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(:orgs) do
add(:department, :text)
end

create(unique_index(:orgs, ["(LOWER(department))"], name: "orgs_department_index"))
end

def down do
drop_if_exists(unique_index(:orgs, ["(LOWER(department))"], name: "orgs_department_index"))

alter table(:orgs) do
remove(:department)
end
end
end
11 changes: 11 additions & 0 deletions test/support/resources/organization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defmodule AshPostgres.Test.Organization do
postgres do
table("orgs")
repo(AshPostgres.TestRepo)

calculations_to_sql(lower_department: "LOWER(department)")
end

policies do
Expand Down Expand Up @@ -39,6 +41,15 @@ defmodule AshPostgres.Test.Organization do
attributes do
uuid_primary_key(:id, writable?: true)
attribute(:name, :string, public?: true)
attribute(:department, :string, public?: true)
end

calculations do
calculate(:lower_department, :string, expr(fragment("LOWER(?)", department)))
end

identities do
identity(:department, [:lower_department], field_names: [:department_slug])
end

relationships do
Expand Down
1 change: 0 additions & 1 deletion test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ defmodule AshPostgres.Test.Post do
identities do
identity(:uniq_one_and_two, [:uniq_one, :uniq_two])
identity(:uniq_on_upper, [:upper_thing])
identity(:uniq_on_upper_title, [:upper_title], field_names: [:title])

identity(:uniq_if_contains_foo, [:uniq_if_contains_foo]) do
where expr(contains(uniq_if_contains_foo, "foo"))
Expand Down
14 changes: 14 additions & 0 deletions test/unique_identity_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule AshPostgres.Test.UniqueIdentityTest do
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.Post
alias AshPostgres.Test.Organization

require Ash.Query

Expand All @@ -19,6 +20,19 @@ defmodule AshPostgres.Test.UniqueIdentityTest do
end
end

test "unique constraint field names are property set" do
Organization
|> Ash.Changeset.for_create(:create, %{name: "Acme", department: "Sales"})
|> Ash.create!()

assert {:error, %Ash.Error.Invalid{errors: [invalid_attribute]}} =
Organization
|> Ash.Changeset.for_create(:create, %{name: "Acme", department: "SALES"})
|> Ash.create()

assert %Ash.Error.Changes.InvalidAttribute{field: :department_slug} = invalid_attribute
end

test "a unique constraint can be used to upsert when the resource has a base filter" do
post =
Post
Expand Down

0 comments on commit 0344284

Please sign in to comment.