2022-07-07 06:44:18 +12:00
|
|
|
defmodule AshPostgres.Test.UpsertTest do
|
|
|
|
use AshPostgres.RepoCase, async: false
|
|
|
|
alias AshPostgres.Test.{Api, Post}
|
|
|
|
|
|
|
|
require Ash.Query
|
|
|
|
|
|
|
|
test "upserting results in the same created_at timestamp, but a new updated_at timestamp" do
|
|
|
|
id = Ash.UUID.generate()
|
|
|
|
|
|
|
|
new_post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{
|
|
|
|
id: id,
|
|
|
|
title: "title2"
|
|
|
|
})
|
|
|
|
|> Api.create!(upsert?: true)
|
|
|
|
|
|
|
|
assert new_post.id == id
|
|
|
|
assert new_post.created_at == new_post.updated_at
|
|
|
|
|
|
|
|
updated_post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{
|
|
|
|
id: id,
|
|
|
|
title: "title2"
|
|
|
|
})
|
|
|
|
|> Api.create!(upsert?: true)
|
|
|
|
|
|
|
|
assert updated_post.id == id
|
|
|
|
assert updated_post.created_at == new_post.created_at
|
|
|
|
assert updated_post.created_at != updated_post.updated_at
|
|
|
|
end
|
2022-12-08 14:32:38 +13:00
|
|
|
|
|
|
|
test "upserting a field with a default sets to the new value" do
|
|
|
|
id = Ash.UUID.generate()
|
|
|
|
|
|
|
|
new_post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{
|
|
|
|
id: id,
|
|
|
|
title: "title2"
|
|
|
|
})
|
|
|
|
|> Api.create!(upsert?: true)
|
|
|
|
|
|
|
|
assert new_post.id == id
|
|
|
|
assert new_post.created_at == new_post.updated_at
|
|
|
|
|
|
|
|
updated_post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{
|
|
|
|
id: id,
|
|
|
|
title: "title2",
|
|
|
|
decimal: Decimal.new(5)
|
|
|
|
})
|
|
|
|
|> Api.create!(upsert?: true)
|
|
|
|
|
|
|
|
assert updated_post.id == id
|
|
|
|
assert Decimal.equal?(updated_post.decimal, Decimal.new(5))
|
|
|
|
end
|
2022-07-07 06:44:18 +12:00
|
|
|
end
|