|
| 1 | +# SPDX-FileCopyrightText: 2019 ash contributors <https://github.com/ash-project/ash/graphs/contributors> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +defmodule Ash.Test.NotLoadedTest do |
| 6 | + @moduledoc false |
| 7 | + use ExUnit.Case, async: true |
| 8 | + |
| 9 | + defmodule Domain do |
| 10 | + @moduledoc false |
| 11 | + use Ash.Domain |
| 12 | + |
| 13 | + resources do |
| 14 | + allow_unregistered? true |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + defmodule Post do |
| 19 | + @moduledoc false |
| 20 | + use Ash.Resource, |
| 21 | + domain: Domain, |
| 22 | + data_layer: Ash.DataLayer.Ets |
| 23 | + |
| 24 | + ets do |
| 25 | + private?(true) |
| 26 | + end |
| 27 | + |
| 28 | + actions do |
| 29 | + default_accept :* |
| 30 | + defaults [:read, :destroy, create: :*, update: :*] |
| 31 | + end |
| 32 | + |
| 33 | + attributes do |
| 34 | + uuid_primary_key :id |
| 35 | + attribute :title, :string, public?: true |
| 36 | + attribute :body, :string, public?: true |
| 37 | + end |
| 38 | + |
| 39 | + relationships do |
| 40 | + belongs_to :author, __MODULE__, public?: true |
| 41 | + end |
| 42 | + |
| 43 | + calculations do |
| 44 | + calculate :title_upcase, :string, expr(string_upcase(title)) do |
| 45 | + public? true |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + aggregates do |
| 50 | + count :count_of_related, :author, public?: true |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + describe "struct" do |
| 55 | + test "resource field defaults to nil" do |
| 56 | + not_loaded = %Ash.NotLoaded{type: :relationship, field: :author} |
| 57 | + assert not_loaded.resource == nil |
| 58 | + end |
| 59 | + |
| 60 | + test "resource field can be set" do |
| 61 | + not_loaded = %Ash.NotLoaded{type: :relationship, field: :author, resource: Post} |
| 62 | + assert not_loaded.resource == Post |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + describe "resource in struct defaults" do |
| 67 | + test "relationship fields have resource set in struct default" do |
| 68 | + post = struct(Post) |
| 69 | + assert %Ash.NotLoaded{resource: Post, type: :relationship} = post.author |
| 70 | + end |
| 71 | + |
| 72 | + test "aggregate fields have resource set in struct default" do |
| 73 | + post = struct(Post) |
| 74 | + assert %Ash.NotLoaded{resource: Post, type: :aggregate} = post.count_of_related |
| 75 | + end |
| 76 | + |
| 77 | + test "calculation fields have resource set in struct default" do |
| 78 | + post = struct(Post) |
| 79 | + assert %Ash.NotLoaded{resource: Post, type: :calculation} = post.title_upcase |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + describe "inspect" do |
| 84 | + test "shows resource when inspected standalone" do |
| 85 | + not_loaded = %Ash.NotLoaded{type: :relationship, field: :author, resource: Post} |
| 86 | + result = inspect(not_loaded) |
| 87 | + assert result =~ "resource:" |
| 88 | + assert result =~ "Post" |
| 89 | + end |
| 90 | + |
| 91 | + test "omits resource when resource is nil" do |
| 92 | + not_loaded = %Ash.NotLoaded{type: :relationship, field: :author} |
| 93 | + result = inspect(not_loaded) |
| 94 | + refute result =~ "resource:" |
| 95 | + end |
| 96 | + |
| 97 | + test "hides resource inside a record inspect" do |
| 98 | + post = struct(Post) |
| 99 | + result = inspect(post) |
| 100 | + assert result =~ "NotLoaded" |
| 101 | + refute result =~ "resource:" |
| 102 | + end |
| 103 | + |
| 104 | + test "hides resource when in_resource? custom option is set" do |
| 105 | + not_loaded = %Ash.NotLoaded{type: :relationship, field: :author, resource: Post} |
| 106 | + opts = %Inspect.Opts{custom_options: [in_resource?: true]} |
| 107 | + |
| 108 | + result = |
| 109 | + Inspect.Algebra.format(Inspect.inspect(not_loaded, opts), 80) |
| 110 | + |> IO.iodata_to_binary() |
| 111 | + |
| 112 | + refute result =~ "resource:" |
| 113 | + end |
| 114 | + |
| 115 | + test "shows resource when in_resource? is not set" do |
| 116 | + not_loaded = %Ash.NotLoaded{type: :relationship, field: :author, resource: Post} |
| 117 | + opts = %Inspect.Opts{} |
| 118 | + |
| 119 | + result = |
| 120 | + Inspect.Algebra.format(Inspect.inspect(not_loaded, opts), 80) |
| 121 | + |> IO.iodata_to_binary() |
| 122 | + |
| 123 | + assert result =~ "resource:" |
| 124 | + assert result =~ "Post" |
| 125 | + end |
| 126 | + end |
| 127 | +end |
0 commit comments