|
| 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.DefinedSimpleRelationshipTest do |
| 6 | + @moduledoc false |
| 7 | + use ExUnit.Case, async: true |
| 8 | + |
| 9 | + alias Diffo.Type.NameValuePrimitive |
| 10 | + alias Diffo.Type.Primitive |
| 11 | + |
| 12 | + setup do |
| 13 | + AshNeo4j.Sandbox.checkout() |
| 14 | + on_exit(&AshNeo4j.Sandbox.rollback/0) |
| 15 | + end |
| 16 | + |
| 17 | + defp build_instances do |
| 18 | + spec_a = Diffo.Provider.create_specification!(%{name: "accessEvc"}) |
| 19 | + spec_b = Diffo.Provider.create_specification!(%{name: "aggregationEvc"}) |
| 20 | + source = Diffo.Provider.create_instance!(%{specified_by: spec_a.id, name: "access1"}) |
| 21 | + target = Diffo.Provider.create_instance!(%{specified_by: spec_b.id, name: "agg1"}) |
| 22 | + {source, target} |
| 23 | + end |
| 24 | + |
| 25 | + describe "DefinedSimpleRelationship create" do |
| 26 | + test "creates a relationship with no characteristic" do |
| 27 | + {source, target} = build_instances() |
| 28 | + |
| 29 | + rel = |
| 30 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 31 | + type: :assignedTo, |
| 32 | + source_id: source.id, |
| 33 | + target_id: target.id |
| 34 | + }) |
| 35 | + |
| 36 | + assert rel.type == :assignedTo |
| 37 | + assert rel.source_id == source.id |
| 38 | + assert rel.target_id == target.id |
| 39 | + assert rel.characteristic == nil |
| 40 | + end |
| 41 | + |
| 42 | + test "creates a relationship with an integer characteristic" do |
| 43 | + {source, target} = build_instances() |
| 44 | + |
| 45 | + rel = |
| 46 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 47 | + type: :assignedTo, |
| 48 | + source_id: source.id, |
| 49 | + target_id: target.id, |
| 50 | + characteristic: %NameValuePrimitive{ |
| 51 | + name: :slot, |
| 52 | + value: Primitive.wrap("integer", 7) |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + assert rel.characteristic.name == :slot |
| 57 | + assert Diffo.Unwrap.unwrap(rel.characteristic.value) == 7 |
| 58 | + end |
| 59 | + |
| 60 | + test "creates a relationship with a string characteristic" do |
| 61 | + {source, target} = build_instances() |
| 62 | + |
| 63 | + rel = |
| 64 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 65 | + type: :definedBy, |
| 66 | + source_id: source.id, |
| 67 | + target_id: target.id, |
| 68 | + characteristic: %NameValuePrimitive{ |
| 69 | + name: :bandwidth, |
| 70 | + value: Primitive.wrap("string", "1000Mbps") |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + assert rel.characteristic.name == :bandwidth |
| 75 | + assert Diffo.Unwrap.unwrap(rel.characteristic.value) == "1000Mbps" |
| 76 | + end |
| 77 | + |
| 78 | + test "characteristic is persisted and reloaded correctly" do |
| 79 | + {source, target} = build_instances() |
| 80 | + |
| 81 | + created = |
| 82 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 83 | + type: :assignedTo, |
| 84 | + source_id: source.id, |
| 85 | + target_id: target.id, |
| 86 | + characteristic: %NameValuePrimitive{ |
| 87 | + name: :slot, |
| 88 | + value: Primitive.wrap("integer", 42) |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + reloaded = Diffo.Provider.get_defined_simple_relationship_by_id!(created.id) |
| 93 | + |
| 94 | + assert reloaded.characteristic.name == :slot |
| 95 | + assert Diffo.Unwrap.unwrap(reloaded.characteristic.value) == 42 |
| 96 | + end |
| 97 | + |
| 98 | + test "target_href and target_type are populated" do |
| 99 | + {source, target} = build_instances() |
| 100 | + |
| 101 | + rel = |
| 102 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 103 | + type: :assignedTo, |
| 104 | + source_id: source.id, |
| 105 | + target_id: target.id |
| 106 | + }) |
| 107 | + |
| 108 | + assert rel.target_type == :service |
| 109 | + assert is_binary(rel.target_href) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + describe "DefinedSimpleRelationship read" do |
| 114 | + test "get by id returns the relationship" do |
| 115 | + {source, target} = build_instances() |
| 116 | + |
| 117 | + created = |
| 118 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 119 | + type: :assignedTo, |
| 120 | + source_id: source.id, |
| 121 | + target_id: target.id |
| 122 | + }) |
| 123 | + |
| 124 | + fetched = Diffo.Provider.get_defined_simple_relationship_by_id!(created.id) |
| 125 | + assert fetched.id == created.id |
| 126 | + assert fetched.type == :assignedTo |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + describe "DefinedSimpleRelationship destroy" do |
| 131 | + test "destroys the relationship" do |
| 132 | + {source, target} = build_instances() |
| 133 | + |
| 134 | + rel = |
| 135 | + Diffo.Provider.create_defined_simple_relationship!(%{ |
| 136 | + type: :assignedTo, |
| 137 | + source_id: source.id, |
| 138 | + target_id: target.id |
| 139 | + }) |
| 140 | + |
| 141 | + Diffo.Provider.delete_defined_simple_relationship!(rel) |
| 142 | + |
| 143 | + assert_raise Ash.Error.Invalid, fn -> |
| 144 | + Diffo.Provider.get_defined_simple_relationship_by_id!(rel.id) |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + describe "NameValuePrimitive TypedStruct" do |
| 150 | + test "new!/1 constructs with a Primitive value" do |
| 151 | + char = NameValuePrimitive.new!(name: :slot, value: Primitive.wrap("integer", 7)) |
| 152 | + assert char.name == :slot |
| 153 | + assert Diffo.Unwrap.unwrap(char.value) == 7 |
| 154 | + end |
| 155 | + |
| 156 | + test "new!/1 raises when name is nil" do |
| 157 | + assert_raise Ash.Error.Invalid, fn -> |
| 158 | + NameValuePrimitive.new!(name: nil, value: Primitive.wrap("string", "x")) |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + test "Jason encoding produces name then unwrapped value" do |
| 163 | + char = NameValuePrimitive.new!(name: :slot, value: Primitive.wrap("integer", 7)) |
| 164 | + json = Jason.encode!(char) |
| 165 | + assert json == ~s({"name":"slot","value":7}) |
| 166 | + end |
| 167 | + end |
| 168 | +end |
0 commit comments