|
| 1 | +# SPDX-FileCopyrightText: 2025 diffo contributors <https://github.com/diffo-dev/diffo/graphs.contributors> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +defmodule Diffo.Provider.Extension.FieldViaAliasedRelationshipTest do |
| 6 | + @moduledoc false |
| 7 | + use ExUnit.Case, async: true |
| 8 | + @moduletag :domain_extended |
| 9 | + |
| 10 | + alias Diffo.Test.Parties |
| 11 | + alias Diffo.Test.Servo |
| 12 | + |
| 13 | + setup do |
| 14 | + AshNeo4j.Sandbox.checkout() |
| 15 | + on_exit(&AshNeo4j.Sandbox.rollback/0) |
| 16 | + end |
| 17 | + |
| 18 | + describe "FieldViaAliasedRelationship — aliased" do |
| 19 | + test "returns field from target instance reached via alias" do |
| 20 | + {:ok, shelf} = Parties.build_shelf_with_installer() |
| 21 | + {:ok, card} = Servo.build_card(%{name: "target-card"}) |
| 22 | + |
| 23 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 24 | + type: :assignedTo, |
| 25 | + alias: :link, |
| 26 | + source_id: shelf.id, |
| 27 | + target_id: card.id |
| 28 | + }) |
| 29 | + |
| 30 | + shelf = Ash.load!(shelf, [:linked_target_name], domain: Servo) |
| 31 | + |
| 32 | + assert shelf.linked_target_name == ["target-card"] |
| 33 | + end |
| 34 | + |
| 35 | + test "returns empty list when no matching relationship exists" do |
| 36 | + {:ok, shelf} = Parties.build_shelf_with_installer() |
| 37 | + |
| 38 | + shelf = Ash.load!(shelf, [:linked_target_name], domain: Servo) |
| 39 | + |
| 40 | + assert shelf.linked_target_name == [] |
| 41 | + end |
| 42 | + |
| 43 | + test "alias filters to only the matching target" do |
| 44 | + {:ok, shelf} = Parties.build_shelf_with_installer() |
| 45 | + {:ok, card_a} = Servo.build_card(%{name: "target-a"}) |
| 46 | + {:ok, card_b} = Servo.build_card(%{name: "target-b"}) |
| 47 | + |
| 48 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 49 | + type: :assignedTo, |
| 50 | + alias: :link, |
| 51 | + source_id: shelf.id, |
| 52 | + target_id: card_a.id |
| 53 | + }) |
| 54 | + |
| 55 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 56 | + type: :assignedTo, |
| 57 | + alias: :other, |
| 58 | + source_id: shelf.id, |
| 59 | + target_id: card_b.id |
| 60 | + }) |
| 61 | + |
| 62 | + shelf = Ash.load!(shelf, [:linked_target_name], domain: Servo) |
| 63 | + |
| 64 | + assert shelf.linked_target_name == ["target-a"] |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe "FieldViaAliasedRelationship — unaliased (all targets)" do |
| 69 | + test "returns fields from all related target instances regardless of alias" do |
| 70 | + {:ok, shelf} = Parties.build_shelf_with_installer() |
| 71 | + {:ok, card_a} = Servo.build_card(%{name: "target-a"}) |
| 72 | + {:ok, card_b} = Servo.build_card(%{name: "target-b"}) |
| 73 | + |
| 74 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 75 | + type: :assignedTo, |
| 76 | + alias: :link, |
| 77 | + source_id: shelf.id, |
| 78 | + target_id: card_a.id |
| 79 | + }) |
| 80 | + |
| 81 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 82 | + type: :assignedTo, |
| 83 | + alias: :other, |
| 84 | + source_id: shelf.id, |
| 85 | + target_id: card_b.id |
| 86 | + }) |
| 87 | + |
| 88 | + shelf = Ash.load!(shelf, [:all_linked_names], domain: Servo) |
| 89 | + |
| 90 | + assert Enum.sort(shelf.all_linked_names) == ["target-a", "target-b"] |
| 91 | + end |
| 92 | + |
| 93 | + test "returns empty list when no relationships exist" do |
| 94 | + {:ok, shelf} = Parties.build_shelf_with_installer() |
| 95 | + |
| 96 | + shelf = Ash.load!(shelf, [:all_linked_names], domain: Servo) |
| 97 | + |
| 98 | + assert shelf.all_linked_names == [] |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments