2022-03-16 10:41:25 +13:00
defmodule Ash.Test.Resource.Relationships.HasOneTest do
2020-06-02 17:47:25 +12:00
@moduledoc false
2019-12-06 20:00:26 +13:00
use ExUnit.Case , async : true
2020-11-03 09:33:14 +13:00
alias Ash.Resource.Relationships.HasOne
2019-12-06 20:00:26 +13:00
defmacrop defposts ( do : body ) do
quote do
defmodule Post do
2020-06-02 17:47:25 +12:00
@moduledoc false
2021-01-13 09:05:56 +13:00
use Ash.Resource , data_layer : Ash.DataLayer.Ets
2019-12-06 20:00:26 +13:00
2020-06-01 17:14:23 +12:00
attributes do
2021-01-13 09:40:55 +13:00
uuid_primary_key :id
2020-06-01 17:14:23 +12:00
end
2019-12-06 20:00:26 +13:00
unquote ( body )
end
end
end
describe " representation " do
test " it creates a relationship " do
defposts do
relationships do
2022-08-16 06:00:02 +12:00
has_one :foo , Foo , destination_attribute : :post_id
has_one :bar , Bar , destination_attribute : :post_id , private? : true
2019-12-06 20:00:26 +13:00
end
end
assert [
2020-11-03 09:33:14 +13:00
% HasOne {
2019-12-06 20:00:26 +13:00
cardinality : :one ,
2020-11-03 09:33:14 +13:00
destination : Foo ,
2022-08-16 06:00:02 +12:00
destination_attribute : :post_id ,
2020-11-03 09:33:14 +13:00
name : :foo ,
2022-08-16 06:00:02 +12:00
source_attribute : :id ,
2020-11-03 09:33:14 +13:00
type : :has_one ,
private? : false
} ,
% HasOne {
cardinality : :one ,
destination : Bar ,
2022-08-16 06:00:02 +12:00
destination_attribute : :post_id ,
2020-11-03 09:33:14 +13:00
name : :bar ,
2022-08-16 06:00:02 +12:00
source_attribute : :id ,
2020-11-03 09:33:14 +13:00
type : :has_one ,
private? : true
2019-12-06 20:00:26 +13:00
}
2021-02-23 14:29:31 +13:00
] = Ash.Resource.Info . relationships ( Post )
2020-11-03 09:33:14 +13:00
2021-02-23 14:29:31 +13:00
assert [ % HasOne { name : :foo } ] = Ash.Resource.Info . public_relationships ( Post )
2020-11-03 09:33:14 +13:00
2021-02-23 14:29:31 +13:00
assert % HasOne { name : :foo } = Ash.Resource.Info . public_relationship ( Post , :foo )
2020-11-03 09:33:14 +13:00
2021-02-23 14:29:31 +13:00
assert nil == Ash.Resource.Info . relationship ( Post , :definitely_legit_relationship )
2020-11-03 09:33:14 +13:00
2021-02-23 14:29:31 +13:00
assert nil == Ash.Resource.Info . public_relationship ( Post , :bar )
2019-12-06 20:00:26 +13:00
end
end
describe " validations " do
2022-08-16 06:00:02 +12:00
test " fails if destination_attribute is not an atom " do
2019-12-06 20:00:26 +13:00
assert_raise (
2022-08-15 06:06:58 +12:00
Spark.Error.DslError ,
2022-08-16 06:00:02 +12:00
" [Ash.Test.Resource.Relationships.HasOneTest.Post] \n relationships -> has_one -> foobar: \n expected :destination_attribute to be an atom, got: \" foo \" " ,
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
2022-08-16 06:00:02 +12:00
has_one :foobar , FooBar , destination_attribute : " foo "
2019-12-06 20:00:26 +13:00
end
end
end
)
end
2022-08-16 06:00:02 +12:00
test " fails if source_attribute is not an atom " do
2019-12-06 20:00:26 +13:00
assert_raise (
2022-08-15 06:06:58 +12:00
Spark.Error.DslError ,
2022-08-16 06:00:02 +12:00
" [Ash.Test.Resource.Relationships.HasOneTest.Post] \n relationships -> has_one -> foobar: \n expected :source_attribute to be an atom, got: \" foo \" " ,
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
2022-08-16 06:00:02 +12:00
has_one :foobar , FooBar , source_attribute : " foo " , destination_attribute : :post_id
2019-12-06 20:00:26 +13:00
end
end
end
)
end
test " fails if the destination is not an atom " do
assert_raise (
2022-08-15 06:06:58 +12:00
Spark.Error.DslError ,
2022-03-16 10:41:25 +13:00
" [Ash.Test.Resource.Relationships.HasOneTest.Post] \n relationships -> has_one -> foobar: \n expected :destination to be an atom, got: \" foobar \" " ,
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
has_one :foobar , " foobar "
end
end
end
)
end
test " fails if the relationship name is not an atom " do
assert_raise (
2022-08-15 06:06:58 +12:00
Spark.Error.DslError ,
2022-03-16 10:41:25 +13:00
" [Ash.Test.Resource.Relationships.HasOneTest.Post] \n relationships -> has_one -> foobar: \n expected :name to be an atom, got: \" foobar \" " ,
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
has_one " foobar " , Foobar
end
end
end
)
end
2020-11-03 09:33:14 +13:00
test " fails if private? is not an boolean " do
assert_raise (
2022-08-15 06:06:58 +12:00
Spark.Error.DslError ,
2022-03-16 10:41:25 +13:00
" [Ash.Test.Resource.Relationships.HasOneTest.Post] \n relationships -> has_one -> foobar: \n expected :private? to be a boolean, got: \" foo \" " ,
2020-11-03 09:33:14 +13:00
fn ->
defposts do
relationships do
2022-08-16 06:00:02 +12:00
has_one :foobar , FooBar , private? : " foo " , destination_attribute : :post_id
2020-11-03 09:33:14 +13:00
end
end
end
)
end
2019-12-06 20:00:26 +13:00
end
end