Skip to content

Commit

Permalink
test: abstract schema delete test uses correct constraints (#58)
Browse files Browse the repository at this point in the history
* fix: abstract schema delete test uses correct constraints

* remove autogenerated code
  • Loading branch information
cylkdev authored Oct 13, 2024
1 parent daaefe5 commit b73678b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
35 changes: 27 additions & 8 deletions test/ecto_shorts/actions_abstract_schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ defmodule EctoShorts.ActionsAbstractSchemaTest do
}
alias EctoShorts.Support.Schemas.{
FileInfo,
UserAvatar
UserAvatar,
UserAvatarNoConstraint
}

test "raises if repo not configured" do
Expand Down Expand Up @@ -107,31 +108,49 @@ defmodule EctoShorts.ActionsAbstractSchemaTest do
assert deleted_schema_data.id === schema_data.id
end

test "returns error when delete fails due to a constraint delete" do
test "returns error when constraint error occurs" do
assert {:ok, user_avatar} = Actions.create(UserAvatar, %{})

assert {:ok, _file_info} =
Actions.create({"file_info_user_avatars", FileInfo}, %{assoc_id: user_avatar.id})

assert {:error, error} = Actions.delete(user_avatar)

assert %ErrorMessage{
code: :internal_server_error,
details: %{changeset: changeset} = details,
message: "Error deleting EctoShorts.Support.Schemas.UserAvatar"
} = error

assert %{changeset: changeset, schema_data: user_avatar} === details

assert {:file_info, ["is still associated with this entry"]} in errors_on(changeset)
end

test "returns error when constraint error occurs and constraint is added with :changeset option" do
assert {:ok, user_avatar} = Actions.create(UserAvatarNoConstraint, %{})

assert {:ok, _file_info} =
Actions.create({"file_info_user_avatars", FileInfo}, %{assoc_id: user_avatar.id})

assert {:error, error} =
Actions.delete(user_avatar, changeset: fn changeset ->
Ecto.Changeset.foreign_key_constraint(
Ecto.Changeset.no_assoc_constraint(
changeset,
:assoc_id,
name: "file_info_user_avatars_assoc_id_fkey",
message: "Cannot delete, record is being referenced."
:file_info,
name: "file_info_user_avatars_assoc_id_fkey"
)
end)

assert %ErrorMessage{
code: :internal_server_error,
details: %{changeset: changeset} = details,
message: "Error deleting EctoShorts.Support.Schemas.UserAvatar"
message: "Error deleting EctoShorts.Support.Schemas.UserAvatarNoConstraint"
} = error

assert %{changeset: changeset, schema_data: user_avatar} === details

assert {:assoc_id, ["Cannot delete, record is being referenced."]} in errors_on(changeset)
assert {:file_info, ["is still associated with this entry"]} in errors_on(changeset)
end
end

Expand Down
1 change: 1 addition & 0 deletions test/support/schemas/file_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule EctoShorts.Support.Schemas.FileInfo do
|> cast(attrs, @available_fields)
|> unique_constraint(:unique_identifier)
|> validate_length(:unique_identifier, min: 3)
|> foreign_key_constraint(:assoc_id)
end

# This callback function is invoked by `EctoShorts.CommonFilters.convert_params_to_filter`
Expand Down
6 changes: 5 additions & 1 deletion test/support/schemas/user_avatar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule EctoShorts.Support.Schemas.UserAvatar do

belongs_to :user, EctoShorts.Support.Schemas.User

has_one :file_info, {"file_info_user_avatars", EctoShorts.Support.Schemas.FileInfo}

timestamps()
end

Expand All @@ -18,6 +20,8 @@ defmodule EctoShorts.Support.Schemas.UserAvatar do
]

def changeset(model_or_changeset, attrs \\ %{}) do
cast(model_or_changeset, attrs, @available_fields)
model_or_changeset
|> cast(attrs, @available_fields)
|> no_assoc_constraint(:file_info, name: "file_info_user_avatars_assoc_id_fkey")
end
end
25 changes: 25 additions & 0 deletions test/support/schemas/user_avatar_no_constraint.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule EctoShorts.Support.Schemas.UserAvatarNoConstraint do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset

schema "user_avatars" do
field :name, :string
field :description, :string

belongs_to :user, EctoShorts.Support.Schemas.User

has_one :file_info, {"file_info_user_avatars", EctoShorts.Support.Schemas.FileInfo}

timestamps()
end

@available_fields [
:name,
:description
]

def changeset(model_or_changeset, attrs \\ %{}) do
cast(model_or_changeset, attrs, @available_fields)
end
end

0 comments on commit b73678b

Please sign in to comment.