|
| 1 | +// SourceV2Tests — targeted tests for the source-v2 (ADR-0007) port: source.rdb |
| 2 | +// registration + accessors + bad-value rejection, one-primary multi-source |
| 3 | +// validation, relationship referential actions, and ERR_RESERVED_ATTR |
| 4 | +// enforcement in the canonical parser. |
| 5 | +// |
| 6 | +// Mirrors the Java Stage 2 Unit 1 test layout. The corresponding fixtures live |
| 7 | +// in fixtures/conformance/source-* and error-* (used by ConformanceTests too); |
| 8 | +// these tests give each behavior a named entry point with a clear failure |
| 9 | +// message and a small inline JSON variant where round-trip + accessor checks |
| 10 | +// are convenient. |
| 11 | + |
| 12 | +using MetaObjects; |
| 13 | +using MetaObjects.Loader; |
| 14 | +using MetaObjects.Meta; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace MetaObjects.Conformance.Tests; |
| 18 | + |
| 19 | +public class SourceV2Tests |
| 20 | +{ |
| 21 | + private static LoadResult LoadInline(string json) => |
| 22 | + new MetaDataLoader().Load([new InMemorySource(json, id: "inline.json")]); |
| 23 | + |
| 24 | + // ------------------------------------------------------------------------- |
| 25 | + // source.rdb registration + accessors |
| 26 | + // ------------------------------------------------------------------------- |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void Source_rdb_is_registered_with_table_kind_role_schema_attrs() |
| 30 | + { |
| 31 | + var reg = Provider.ComposeRegistry(new[] { CoreTypes.CoreTypesProvider }); |
| 32 | + Assert.True(reg.Has("source", "rdb")); |
| 33 | + |
| 34 | + var def = reg.Find("source", "rdb")!; |
| 35 | + var attrNames = def.Attributes.Select(a => a.Name).ToHashSet(); |
| 36 | + Assert.Contains("table", attrNames); |
| 37 | + Assert.Contains("kind", attrNames); |
| 38 | + Assert.Contains("role", attrNames); |
| 39 | + Assert.Contains("schema", attrNames); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void Source_rdb_kind_enum_rejects_unknown_value() |
| 44 | + { |
| 45 | + const string json = """ |
| 46 | + { "metadata.root": { "package": "acme", "children": [ |
| 47 | + { "object.entity": { "name": "Product", "children": [ |
| 48 | + { "source.rdb": { "@table": "products", "@kind": "nosuchkind" } }, |
| 49 | + { "field.long": { "name": "id" } }, |
| 50 | + { "identity.primary": { "@fields": "id" } } |
| 51 | + ]}} |
| 52 | + ]}} |
| 53 | + """; |
| 54 | + var result = LoadInline(json); |
| 55 | + Assert.Contains(result.Errors, e => e.Code == ErrorCode.ERR_BAD_ATTR_VALUE); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void Source_rdb_role_enum_rejects_unknown_value() |
| 60 | + { |
| 61 | + const string json = """ |
| 62 | + { "metadata.root": { "package": "acme", "children": [ |
| 63 | + { "object.entity": { "name": "Product", "children": [ |
| 64 | + { "source.rdb": { "@table": "products", "@role": "shadow" } }, |
| 65 | + { "field.long": { "name": "id" } }, |
| 66 | + { "identity.primary": { "@fields": "id" } } |
| 67 | + ]}} |
| 68 | + ]}} |
| 69 | + """; |
| 70 | + var result = LoadInline(json); |
| 71 | + Assert.Contains(result.Errors, e => e.Code == ErrorCode.ERR_BAD_ATTR_VALUE); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void MetaSource_accessors_default_when_attrs_absent() |
| 76 | + { |
| 77 | + const string json = """ |
| 78 | + { "metadata.root": { "package": "acme", "children": [ |
| 79 | + { "object.entity": { "name": "Product", "children": [ |
| 80 | + { "source.rdb": { "@table": "products" } }, |
| 81 | + { "field.long": { "name": "id" } }, |
| 82 | + { "identity.primary": { "@fields": "id" } } |
| 83 | + ]}} |
| 84 | + ]}} |
| 85 | + """; |
| 86 | + var result = LoadInline(json); |
| 87 | + Assert.Empty(result.Errors); |
| 88 | + |
| 89 | + var obj = (MetaObject)result.Root.Children().First(c => c.Type == "object"); |
| 90 | + var src = obj.OwnSources().Single(); |
| 91 | + Assert.Equal("products", src.TableName); |
| 92 | + Assert.Equal("table", src.EffectiveKind); |
| 93 | + Assert.Equal("primary", src.Role); |
| 94 | + Assert.True(src.IsWritable()); |
| 95 | + Assert.False(src.IsReadOnly()); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void MetaSource_read_only_kind_makes_source_read_only() |
| 100 | + { |
| 101 | + const string json = """ |
| 102 | + { "metadata.root": { "package": "acme", "children": [ |
| 103 | + { "object.entity": { "name": "ProductView", "children": [ |
| 104 | + { "source.rdb": { "@table": "v_products", "@kind": "view" } }, |
| 105 | + { "field.long": { "name": "id" } }, |
| 106 | + { "identity.primary": { "@fields": "id" } } |
| 107 | + ]}} |
| 108 | + ]}} |
| 109 | + """; |
| 110 | + var result = LoadInline(json); |
| 111 | + Assert.Empty(result.Errors); |
| 112 | + |
| 113 | + var obj = (MetaObject)result.Root.Children().First(c => c.Type == "object"); |
| 114 | + var src = obj.OwnSources().Single(); |
| 115 | + Assert.Equal("view", src.EffectiveKind); |
| 116 | + Assert.True(src.IsReadOnly()); |
| 117 | + Assert.False(src.IsWritable()); |
| 118 | + } |
| 119 | + |
| 120 | + // ------------------------------------------------------------------------- |
| 121 | + // One-primary multi-source validation |
| 122 | + // ------------------------------------------------------------------------- |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void Zero_sources_is_OK() |
| 126 | + { |
| 127 | + const string json = """ |
| 128 | + { "metadata.root": { "package": "acme", "children": [ |
| 129 | + { "object.value": { "name": "Address", "children": [ |
| 130 | + { "field.string": { "name": "city" } } |
| 131 | + ]}} |
| 132 | + ]}} |
| 133 | + """; |
| 134 | + Assert.Empty(LoadInline(json).Errors); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public void Single_primary_source_is_OK() |
| 139 | + { |
| 140 | + const string json = """ |
| 141 | + { "metadata.root": { "package": "acme", "children": [ |
| 142 | + { "object.entity": { "name": "Product", "children": [ |
| 143 | + { "source.rdb": { "@table": "products" } }, |
| 144 | + { "field.long": { "name": "id" } }, |
| 145 | + { "identity.primary": { "@fields": "id" } } |
| 146 | + ]}} |
| 147 | + ]}} |
| 148 | + """; |
| 149 | + Assert.Empty(LoadInline(json).Errors); |
| 150 | + } |
| 151 | + |
| 152 | + [Fact] |
| 153 | + public void No_primary_among_sources_is_ERR_SOURCE_NO_PRIMARY() |
| 154 | + { |
| 155 | + const string json = """ |
| 156 | + { "metadata.root": { "package": "acme", "children": [ |
| 157 | + { "object.entity": { "name": "Product", "children": [ |
| 158 | + { "source.rdb": { "@role": "replica", "@table": "products_r" } }, |
| 159 | + { "field.long": { "name": "id" } }, |
| 160 | + { "identity.primary": { "@fields": "id" } } |
| 161 | + ]}} |
| 162 | + ]}} |
| 163 | + """; |
| 164 | + var result = LoadInline(json); |
| 165 | + Assert.Contains(result.Errors, e => e.Code == ErrorCode.ERR_SOURCE_NO_PRIMARY); |
| 166 | + } |
| 167 | + |
| 168 | + [Fact] |
| 169 | + public void Multiple_primary_sources_is_ERR_SOURCE_MULTIPLE_PRIMARY() |
| 170 | + { |
| 171 | + const string json = """ |
| 172 | + { "metadata.root": { "package": "acme", "children": [ |
| 173 | + { "object.entity": { "name": "Product", "children": [ |
| 174 | + { "source.rdb": { "@table": "products_a" } }, |
| 175 | + { "source.rdb": { "@table": "products_b" } }, |
| 176 | + { "field.long": { "name": "id" } }, |
| 177 | + { "identity.primary": { "@fields": "id" } } |
| 178 | + ]}} |
| 179 | + ]}} |
| 180 | + """; |
| 181 | + var result = LoadInline(json); |
| 182 | + Assert.Contains(result.Errors, e => e.Code == ErrorCode.ERR_SOURCE_MULTIPLE_PRIMARY); |
| 183 | + } |
| 184 | + |
| 185 | + // ------------------------------------------------------------------------- |
| 186 | + // Relationship @onDelete / @onUpdate referential actions |
| 187 | + // ------------------------------------------------------------------------- |
| 188 | + |
| 189 | + [Fact] |
| 190 | + public void Relationship_explicit_onDelete_round_trips_and_resolves() |
| 191 | + { |
| 192 | + const string json = """ |
| 193 | + { "metadata.root": { "package": "acme", "children": [ |
| 194 | + { "object.entity": { "name": "Program", "children": [ |
| 195 | + { "source.rdb": { "@table": "programs" } }, |
| 196 | + { "field.long": { "name": "id" } }, |
| 197 | + { "identity.primary": { "@fields": "id" } } |
| 198 | + ]}}, |
| 199 | + { "object.entity": { "name": "Week", "children": [ |
| 200 | + { "source.rdb": { "@table": "weeks" } }, |
| 201 | + { "field.long": { "name": "id" } }, |
| 202 | + { "field.long": { "name": "programId" } }, |
| 203 | + { "relationship.composition": { "name": "program", |
| 204 | + "@objectRef": "Program", "@cardinality": "one", |
| 205 | + "@onDelete": "cascade", "@onUpdate": "cascade" } }, |
| 206 | + { "identity.primary": { "@fields": "id" } }, |
| 207 | + { "identity.reference": { "name": "programRef", |
| 208 | + "@fields": "programId", "@references": "Program" } } |
| 209 | + ]}} |
| 210 | + ]}} |
| 211 | + """; |
| 212 | + var result = LoadInline(json); |
| 213 | + Assert.Empty(result.Errors); |
| 214 | + |
| 215 | + var week = (MetaObject)result.Root.Children().First(o => o.Name == "Week"); |
| 216 | + var rel = week.Relationships().Single(); |
| 217 | + Assert.Equal("cascade", rel.EffectiveOnDelete); |
| 218 | + Assert.Equal("cascade", rel.EffectiveOnUpdate); |
| 219 | + } |
| 220 | + |
| 221 | + [Fact] |
| 222 | + public void Relationship_subtype_default_resolves_when_attr_absent() |
| 223 | + { |
| 224 | + const string json = """ |
| 225 | + { "metadata.root": { "package": "acme", "children": [ |
| 226 | + { "object.entity": { "name": "Program", "children": [ |
| 227 | + { "source.rdb": { "@table": "programs" } }, |
| 228 | + { "field.long": { "name": "id" } }, |
| 229 | + { "identity.primary": { "@fields": "id" } } |
| 230 | + ]}}, |
| 231 | + { "object.entity": { "name": "Week", "children": [ |
| 232 | + { "source.rdb": { "@table": "weeks" } }, |
| 233 | + { "field.long": { "name": "id" } }, |
| 234 | + { "field.long": { "name": "programId" } }, |
| 235 | + { "relationship.composition": { "name": "program", |
| 236 | + "@objectRef": "Program", "@cardinality": "one" } }, |
| 237 | + { "identity.primary": { "@fields": "id" } }, |
| 238 | + { "identity.reference": { "name": "programRef", |
| 239 | + "@fields": "programId", "@references": "Program" } } |
| 240 | + ]}} |
| 241 | + ]}} |
| 242 | + """; |
| 243 | + var result = LoadInline(json); |
| 244 | + Assert.Empty(result.Errors); |
| 245 | + |
| 246 | + var week = (MetaObject)result.Root.Children().First(o => o.Name == "Week"); |
| 247 | + var rel = week.Relationships().Single(); |
| 248 | + // composition default → cascade; update default → cascade. |
| 249 | + Assert.Equal("cascade", rel.EffectiveOnDelete); |
| 250 | + Assert.Equal("cascade", rel.EffectiveOnUpdate); |
| 251 | + } |
| 252 | + |
| 253 | + [Fact] |
| 254 | + public void Relationship_bad_onDelete_value_is_ERR_BAD_ATTR_VALUE() |
| 255 | + { |
| 256 | + const string json = """ |
| 257 | + { "metadata.root": { "package": "acme", "children": [ |
| 258 | + { "object.entity": { "name": "Program", "children": [ |
| 259 | + { "source.rdb": { "@table": "programs" } }, |
| 260 | + { "field.long": { "name": "id" } }, |
| 261 | + { "identity.primary": { "@fields": "id" } } |
| 262 | + ]}}, |
| 263 | + { "object.entity": { "name": "Week", "children": [ |
| 264 | + { "source.rdb": { "@table": "weeks" } }, |
| 265 | + { "field.long": { "name": "id" } }, |
| 266 | + { "field.long": { "name": "programId" } }, |
| 267 | + { "relationship.composition": { "name": "program", |
| 268 | + "@objectRef": "Program", "@cardinality": "one", |
| 269 | + "@onDelete": "destroyAll" } }, |
| 270 | + { "identity.primary": { "@fields": "id" } }, |
| 271 | + { "identity.reference": { "name": "programRef", |
| 272 | + "@fields": "programId", "@references": "Program" } } |
| 273 | + ]}} |
| 274 | + ]}} |
| 275 | + """; |
| 276 | + var result = LoadInline(json); |
| 277 | + Assert.Contains(result.Errors, e => e.Code == ErrorCode.ERR_BAD_ATTR_VALUE); |
| 278 | + } |
| 279 | + |
| 280 | + // ------------------------------------------------------------------------- |
| 281 | + // ERR_RESERVED_ATTR enforcement in the canonical parser |
| 282 | + // ------------------------------------------------------------------------- |
| 283 | + |
| 284 | + [Fact] |
| 285 | + public void At_prefixed_isArray_is_ERR_RESERVED_ATTR() |
| 286 | + { |
| 287 | + const string json = """ |
| 288 | + { "metadata.root": { "package": "acme", "children": [ |
| 289 | + { "object.entity": { "name": "Owner", "children": [ |
| 290 | + { "field.long": { "name": "id" } }, |
| 291 | + { "field.object": { "name": "items", "@isArray": true, "@objectRef": "Item" } }, |
| 292 | + { "identity.primary": { "name": "pk", "@fields": "id" } } |
| 293 | + ]}}, |
| 294 | + { "object.entity": { "name": "Item", "children": [ |
| 295 | + { "field.long": { "name": "id" } }, |
| 296 | + { "identity.primary": { "name": "pk", "@fields": "id" } } |
| 297 | + ]}} |
| 298 | + ]}} |
| 299 | + """; |
| 300 | + var result = LoadInline(json); |
| 301 | + Assert.Contains(result.Errors, e => e.Code == ErrorCode.ERR_RESERVED_ATTR); |
| 302 | + } |
| 303 | + |
| 304 | + [Fact] |
| 305 | + public void At_prefixed_name_is_ERR_RESERVED_ATTR() |
| 306 | + { |
| 307 | + const string json = """ |
| 308 | + { "metadata.root": { "package": "acme", "children": [ |
| 309 | + { "object.entity": { "name": "Owner", "children": [ |
| 310 | + { "field.long": { "@name": "id" } }, |
| 311 | + { "identity.primary": { "name": "pk", "@fields": "id" } } |
| 312 | + ]}} |
| 313 | + ]}} |
| 314 | + """; |
| 315 | + var result = LoadInline(json); |
| 316 | + Assert.Contains(result.Errors, e => e.Code == ErrorCode.ERR_RESERVED_ATTR); |
| 317 | + } |
| 318 | + |
| 319 | + [Fact] |
| 320 | + public void Bare_isArray_structural_key_is_OK() |
| 321 | + { |
| 322 | + const string json = """ |
| 323 | + { "metadata.root": { "package": "acme", "children": [ |
| 324 | + { "object.entity": { "name": "Owner", "children": [ |
| 325 | + { "field.long": { "name": "id" } }, |
| 326 | + { "field.object": { "name": "items", "isArray": true, "@objectRef": "Item" } }, |
| 327 | + { "identity.primary": { "name": "pk", "@fields": "id" } } |
| 328 | + ]}}, |
| 329 | + { "object.entity": { "name": "Item", "children": [ |
| 330 | + { "field.long": { "name": "id" } }, |
| 331 | + { "identity.primary": { "name": "pk", "@fields": "id" } } |
| 332 | + ]}} |
| 333 | + ]}} |
| 334 | + """; |
| 335 | + var result = LoadInline(json); |
| 336 | + Assert.Empty(result.Errors); |
| 337 | + } |
| 338 | +} |
0 commit comments