2024-01-01 02:40:30 +13:00
|
|
|
defmodule AshPostgres.BulkDestroyTest do
|
|
|
|
use AshPostgres.RepoCase, async: false
|
2024-03-28 09:52:28 +13:00
|
|
|
alias AshPostgres.Test.Post
|
2024-01-01 02:40:30 +13:00
|
|
|
|
|
|
|
require Ash.Expr
|
|
|
|
require Ash.Query
|
|
|
|
|
|
|
|
test "bulk destroys can run with nothing in the table" do
|
2024-05-17 08:48:38 +12:00
|
|
|
Ash.bulk_destroy!(Post, :destroy, %{})
|
2024-01-01 02:40:30 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "bulk destroys destroy everything pertaining to the query" do
|
2024-03-28 09:52:28 +13:00
|
|
|
Ash.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create)
|
2024-01-01 02:40:30 +13:00
|
|
|
|
2024-05-17 08:48:38 +12:00
|
|
|
Ash.bulk_destroy!(Post, :destroy, %{})
|
2024-01-01 02:40:30 +13:00
|
|
|
|
2024-03-28 09:52:28 +13:00
|
|
|
assert Ash.read!(Post) == []
|
2024-01-01 02:40:30 +13:00
|
|
|
end
|
|
|
|
|
2024-02-02 07:09:06 +13:00
|
|
|
test "bulk destroys only apply to things that the query produces" do
|
2024-03-28 09:52:28 +13:00
|
|
|
Ash.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create)
|
2024-01-01 02:40:30 +13:00
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.Query.filter(title == "fred")
|
2024-05-07 03:26:41 +12:00
|
|
|
|> Ash.bulk_destroy!(:destroy, %{})
|
2024-01-01 02:40:30 +13:00
|
|
|
|
|
|
|
# 😢 sad
|
2024-05-07 03:26:41 +12:00
|
|
|
assert ["george"] = Ash.read!(Post) |> Enum.map(& &1.title)
|
2024-01-01 02:40:30 +13:00
|
|
|
end
|
2024-02-02 07:09:06 +13:00
|
|
|
|
2024-05-13 08:28:48 +12:00
|
|
|
test "bulk destroys honor changeset filters" do
|
|
|
|
Ash.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create)
|
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.bulk_destroy!(:destroy_only_freds, %{})
|
|
|
|
|
|
|
|
# 😢 sad
|
|
|
|
assert ["george"] = Ash.read!(Post) |> Enum.map(& &1.title)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "bulk destroys honor changeset filters when streaming" do
|
|
|
|
Ash.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create)
|
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.bulk_destroy!(:destroy_only_freds, %{}, strategy: :stream)
|
|
|
|
|
|
|
|
# 😢 sad
|
|
|
|
assert ["george"] = Ash.read!(Post) |> Enum.map(& &1.title)
|
|
|
|
end
|
|
|
|
|
2024-02-20 07:29:41 +13:00
|
|
|
test "the query can join to related tables when necessary" do
|
2024-03-28 09:52:28 +13:00
|
|
|
Ash.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create)
|
2024-02-20 07:29:41 +13:00
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.Query.filter(author.first_name == "fred" or title == "fred")
|
2024-05-07 03:26:41 +12:00
|
|
|
|> Ash.Query.select([:title])
|
2024-05-17 08:48:38 +12:00
|
|
|
|> Ash.bulk_destroy!(:destroy, %{}, return_records?: true)
|
2024-02-20 07:29:41 +13:00
|
|
|
|
2024-03-28 09:52:28 +13:00
|
|
|
assert [%{title: "george"}] = Ash.read!(Post)
|
2024-02-20 07:29:41 +13:00
|
|
|
end
|
|
|
|
|
2024-02-02 07:09:06 +13:00
|
|
|
test "bulk destroys can be done even on stream inputs" do
|
2024-03-28 09:52:28 +13:00
|
|
|
Ash.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create)
|
2024-02-02 07:09:06 +13:00
|
|
|
|
|
|
|
Post
|
2024-03-28 09:52:28 +13:00
|
|
|
|> Ash.read!()
|
|
|
|
|> Ash.bulk_destroy!(:destroy, %{}, strategy: :stream, return_errors?: true)
|
2024-02-02 07:09:06 +13:00
|
|
|
|
2024-03-28 09:52:28 +13:00
|
|
|
assert [] = Ash.read!(Post)
|
2024-02-02 07:09:06 +13:00
|
|
|
end
|
2024-01-01 02:40:30 +13:00
|
|
|
end
|