|
| 1 | +# SPDX-License-Identifier: MPL-2.0 |
| 2 | + |
| 3 | +defmodule VeriSim.SchemaRegistryTest do |
| 4 | + @moduledoc """ |
| 5 | + Tests for VeriSim.SchemaRegistry — the named GenServer that owns the |
| 6 | + type system (built-ins + user types) and runs constraint validation. |
| 7 | +
|
| 8 | + Each test uses unique type IRIs to avoid collisions with built-in types |
| 9 | + and with parallel tests. The registry is started by the Application |
| 10 | + supervisor. |
| 11 | + """ |
| 12 | + |
| 13 | + use ExUnit.Case, async: false |
| 14 | + |
| 15 | + alias VeriSim.SchemaRegistry |
| 16 | + |
| 17 | + setup do |
| 18 | + case GenServer.whereis(SchemaRegistry) do |
| 19 | + nil -> |
| 20 | + {:ok, _pid} = SchemaRegistry.start_link([]) |
| 21 | + :ok |
| 22 | + |
| 23 | + _pid -> |
| 24 | + :ok |
| 25 | + end |
| 26 | + |
| 27 | + :ok |
| 28 | + end |
| 29 | + |
| 30 | + describe "built-in types" do |
| 31 | + test "verisim:Entity is registered at startup" do |
| 32 | + assert is_map(SchemaRegistry.get_type("verisim:Entity")) |
| 33 | + end |
| 34 | + |
| 35 | + test "verisim:Document is registered with a title_required constraint" do |
| 36 | + doc = SchemaRegistry.get_type("verisim:Document") |
| 37 | + assert is_map(doc) |
| 38 | + assert doc.supertypes == ["verisim:Entity"] |
| 39 | + assert Enum.any?(doc.constraints, fn c -> c.name == "title_required" end) |
| 40 | + end |
| 41 | + |
| 42 | + test "list_types includes the 4 built-ins" do |
| 43 | + types = SchemaRegistry.list_types() |
| 44 | + assert "verisim:Entity" in types |
| 45 | + assert "verisim:Document" in types |
| 46 | + assert "verisim:Node" in types |
| 47 | + assert "verisim:TimeSeries" in types |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + describe "register_type/1" do |
| 52 | + test "successfully registers a new type" do |
| 53 | + iri = "test:Type#{System.unique_integer([:positive])}" |
| 54 | + |
| 55 | + assert :ok = |
| 56 | + SchemaRegistry.register_type(%{ |
| 57 | + iri: iri, |
| 58 | + label: "Test", |
| 59 | + supertypes: [], |
| 60 | + constraints: [] |
| 61 | + }) |
| 62 | + |
| 63 | + assert is_map(SchemaRegistry.get_type(iri)) |
| 64 | + end |
| 65 | + |
| 66 | + test "rejects duplicate registration" do |
| 67 | + iri = "test:Dup#{System.unique_integer([:positive])}" |
| 68 | + type = %{iri: iri, label: "Dup", supertypes: [], constraints: []} |
| 69 | + |
| 70 | + assert :ok = SchemaRegistry.register_type(type) |
| 71 | + assert {:error, :already_exists} = SchemaRegistry.register_type(type) |
| 72 | + end |
| 73 | + |
| 74 | + test "registered type appears in list_types" do |
| 75 | + iri = "test:Listed#{System.unique_integer([:positive])}" |
| 76 | + |
| 77 | + SchemaRegistry.register_type(%{ |
| 78 | + iri: iri, |
| 79 | + label: "Listed", |
| 80 | + supertypes: [], |
| 81 | + constraints: [] |
| 82 | + }) |
| 83 | + |
| 84 | + assert iri in SchemaRegistry.list_types() |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe "get_type/1" do |
| 89 | + test "unknown IRI returns nil" do |
| 90 | + assert SchemaRegistry.get_type("never:Exists") == nil |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + describe "validate/1 — required constraint" do |
| 95 | + setup do |
| 96 | + iri = "test:Required#{System.unique_integer([:positive])}" |
| 97 | + |
| 98 | + SchemaRegistry.register_type(%{ |
| 99 | + iri: iri, |
| 100 | + label: "Required", |
| 101 | + supertypes: [], |
| 102 | + constraints: [ |
| 103 | + %{name: "name_req", kind: {:required, "name"}, message: "name is required"} |
| 104 | + ] |
| 105 | + }) |
| 106 | + |
| 107 | + {:ok, type: iri} |
| 108 | + end |
| 109 | + |
| 110 | + test "entity missing required property is rejected", %{type: iri} do |
| 111 | + assert {:error, ["name is required"]} = |
| 112 | + SchemaRegistry.validate(%{types: [iri], properties: %{}}) |
| 113 | + end |
| 114 | + |
| 115 | + test "entity with required property is accepted", %{type: iri} do |
| 116 | + assert :ok = SchemaRegistry.validate(%{types: [iri], properties: %{"name" => "Alice"}}) |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + describe "validate/1 — pattern constraint" do |
| 121 | + setup do |
| 122 | + iri = "test:Pattern#{System.unique_integer([:positive])}" |
| 123 | + |
| 124 | + SchemaRegistry.register_type(%{ |
| 125 | + iri: iri, |
| 126 | + label: "Pattern", |
| 127 | + supertypes: [], |
| 128 | + constraints: [ |
| 129 | + %{ |
| 130 | + name: "email_format", |
| 131 | + kind: {:pattern, "email", "^[^@]+@[^@]+$"}, |
| 132 | + message: "email must be valid" |
| 133 | + } |
| 134 | + ] |
| 135 | + }) |
| 136 | + |
| 137 | + {:ok, type: iri} |
| 138 | + end |
| 139 | + |
| 140 | + test "missing property is allowed (pattern only checks when present)", %{type: iri} do |
| 141 | + assert :ok = SchemaRegistry.validate(%{types: [iri], properties: %{}}) |
| 142 | + end |
| 143 | + |
| 144 | + test "matching value is accepted", %{type: iri} do |
| 145 | + assert :ok = |
| 146 | + SchemaRegistry.validate(%{ |
| 147 | + types: [iri], |
| 148 | + properties: %{"email" => "a@b.com"} |
| 149 | + }) |
| 150 | + end |
| 151 | + |
| 152 | + test "non-matching value is rejected", %{type: iri} do |
| 153 | + assert {:error, ["email must be valid"]} = |
| 154 | + SchemaRegistry.validate(%{ |
| 155 | + types: [iri], |
| 156 | + properties: %{"email" => "not-an-email"} |
| 157 | + }) |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + describe "validate/1 — range constraint" do |
| 162 | + setup do |
| 163 | + iri = "test:Range#{System.unique_integer([:positive])}" |
| 164 | + |
| 165 | + SchemaRegistry.register_type(%{ |
| 166 | + iri: iri, |
| 167 | + label: "Range", |
| 168 | + supertypes: [], |
| 169 | + constraints: [ |
| 170 | + %{ |
| 171 | + name: "age_range", |
| 172 | + kind: {:range, "age", 0, 150}, |
| 173 | + message: "age must be 0-150" |
| 174 | + } |
| 175 | + ] |
| 176 | + }) |
| 177 | + |
| 178 | + {:ok, type: iri} |
| 179 | + end |
| 180 | + |
| 181 | + test "value inside range is accepted", %{type: iri} do |
| 182 | + assert :ok = SchemaRegistry.validate(%{types: [iri], properties: %{"age" => 42}}) |
| 183 | + end |
| 184 | + |
| 185 | + test "value below minimum is rejected", %{type: iri} do |
| 186 | + assert {:error, ["age must be 0-150"]} = |
| 187 | + SchemaRegistry.validate(%{types: [iri], properties: %{"age" => -1}}) |
| 188 | + end |
| 189 | + |
| 190 | + test "value above maximum is rejected", %{type: iri} do |
| 191 | + assert {:error, ["age must be 0-150"]} = |
| 192 | + SchemaRegistry.validate(%{types: [iri], properties: %{"age" => 200}}) |
| 193 | + end |
| 194 | + |
| 195 | + test "non-numeric value is silently ignored", %{type: iri} do |
| 196 | + assert :ok = SchemaRegistry.validate(%{types: [iri], properties: %{"age" => "old"}}) |
| 197 | + end |
| 198 | + end |
| 199 | + |
| 200 | + describe "validate/1 — unknown type" do |
| 201 | + test "entity tagged with an unregistered type passes validation" do |
| 202 | + assert :ok = SchemaRegistry.validate(%{types: ["bogus:Type"], properties: %{}}) |
| 203 | + end |
| 204 | + |
| 205 | + test "entity with no types passes validation" do |
| 206 | + assert :ok = SchemaRegistry.validate(%{properties: %{}}) |
| 207 | + end |
| 208 | + end |
| 209 | + |
| 210 | + describe "type_hierarchy/1" do |
| 211 | + test "built-in verisim:Document hierarchy includes verisim:Entity" do |
| 212 | + hierarchy = SchemaRegistry.type_hierarchy("verisim:Document") |
| 213 | + assert "verisim:Entity" in hierarchy or hierarchy == ["verisim:Entity"] |
| 214 | + end |
| 215 | + |
| 216 | + test "unknown type returns an empty or singleton hierarchy" do |
| 217 | + hierarchy = SchemaRegistry.type_hierarchy("never:Heard:Of") |
| 218 | + assert is_list(hierarchy) |
| 219 | + end |
| 220 | + |
| 221 | + test "custom type with chained supertypes resolves" do |
| 222 | + iri = "test:Chain#{System.unique_integer([:positive])}" |
| 223 | + |
| 224 | + SchemaRegistry.register_type(%{ |
| 225 | + iri: iri, |
| 226 | + label: "Chain", |
| 227 | + supertypes: ["verisim:Document"], |
| 228 | + constraints: [] |
| 229 | + }) |
| 230 | + |
| 231 | + hierarchy = SchemaRegistry.type_hierarchy(iri) |
| 232 | + assert "verisim:Document" in hierarchy |
| 233 | + end |
| 234 | + end |
| 235 | +end |
0 commit comments