2023-11-28 11:47:54 +13:00
|
|
|
defmodule AshPostgres.Test.CompositeTypeTest do
|
|
|
|
use AshPostgres.RepoCase
|
2024-03-28 09:52:28 +13:00
|
|
|
alias AshPostgres.Test.Post
|
2023-11-28 11:47:54 +13:00
|
|
|
require Ash.Query
|
|
|
|
|
|
|
|
test "can be cast and stored" do
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "locked", composite_point: %{x: 1, y: 2}})
|
2024-03-28 09:52:28 +13:00
|
|
|
|> Ash.create!()
|
2023-11-28 11:47:54 +13:00
|
|
|
|
|
|
|
assert post.composite_point.x == 1
|
|
|
|
assert post.composite_point.y == 2
|
|
|
|
end
|
|
|
|
|
|
|
|
test "can be referenced in expressions" do
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "locked", composite_point: %{x: 1, y: 2}})
|
2024-03-28 09:52:28 +13:00
|
|
|
|> Ash.create!()
|
2023-11-28 11:47:54 +13:00
|
|
|
|
|
|
|
post_id = post.id
|
|
|
|
|
2024-03-28 09:52:28 +13:00
|
|
|
assert %{id: ^post_id} = Post |> Ash.Query.filter(composite_point[:x] == 1) |> Ash.read_one!()
|
|
|
|
refute Post |> Ash.Query.filter(composite_point[:x] == 2) |> Ash.read_one!()
|
2023-11-28 11:47:54 +13:00
|
|
|
end
|
2023-11-30 03:57:10 +13:00
|
|
|
|
|
|
|
test "composite types can be constructed" do
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "locked", composite_point: %{x: 1, y: 2}})
|
2024-03-28 09:52:28 +13:00
|
|
|
|> Ash.create!()
|
2023-11-30 03:57:10 +13:00
|
|
|
|
|
|
|
assert %{composite_origin: %{x: 0, y: 0}} =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.load(:composite_origin)
|
2024-03-28 09:52:28 +13:00
|
|
|
|> Ash.read_one!()
|
2023-11-30 03:57:10 +13:00
|
|
|
end
|
2023-11-28 11:47:54 +13:00
|
|
|
end
|