|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package mpr |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mendixlabs/mxcli/sdk/domainmodel" |
| 9 | +) |
| 10 | + |
| 11 | +// ============================================================================= |
| 12 | +// Issue #50: CrossAssociation must NOT include ParentConnection/ChildConnection |
| 13 | +// ============================================================================= |
| 14 | + |
| 15 | +// TestSerializeCrossAssociation_NoConnectionFields verifies that |
| 16 | +// serializeCrossAssociation does NOT emit ParentConnection or ChildConnection. |
| 17 | +// These properties only exist on DomainModels$Association, not on |
| 18 | +// DomainModels$CrossAssociation. Writing them causes Studio Pro to crash with |
| 19 | +// System.InvalidOperationException: Sequence contains no matching element. |
| 20 | +func TestSerializeCrossAssociation_NoConnectionFields(t *testing.T) { |
| 21 | + ca := &domainmodel.CrossModuleAssociation{ |
| 22 | + Name: "Child_Parent", |
| 23 | + ParentID: "parent-entity-id", |
| 24 | + ChildRef: "OtherModule.Parent", |
| 25 | + Type: domainmodel.AssociationTypeReference, |
| 26 | + Owner: domainmodel.AssociationOwnerDefault, |
| 27 | + } |
| 28 | + ca.ID = "test-cross-assoc-id" |
| 29 | + |
| 30 | + result := serializeCrossAssociation(ca) |
| 31 | + |
| 32 | + // Must NOT contain these fields |
| 33 | + for key := range result { |
| 34 | + if key == "ParentConnection" { |
| 35 | + t.Error("serializeCrossAssociation must NOT include ParentConnection (only valid for Association)") |
| 36 | + } |
| 37 | + if key == "ChildConnection" { |
| 38 | + t.Error("serializeCrossAssociation must NOT include ChildConnection (only valid for Association)") |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Must contain all expected fields (exhaustive structural contract) |
| 43 | + expectedKeys := []string{"$ID", "$Type", "Name", "Child", "ParentPointer", "Type", "Owner", |
| 44 | + "Documentation", "ExportLevel", "GUID", "StorageFormat", "Source", "DeleteBehavior"} |
| 45 | + for _, key := range expectedKeys { |
| 46 | + if _, ok := result[key]; !ok { |
| 47 | + t.Errorf("serializeCrossAssociation missing expected field %q", key) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // $Type must be CrossAssociation |
| 52 | + if got := result["$Type"]; got != "DomainModels$CrossAssociation" { |
| 53 | + t.Errorf("$Type = %q, want %q", got, "DomainModels$CrossAssociation") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// TestSerializeAssociation_HasConnectionFields verifies that the regular |
| 58 | +// serializeAssociation DOES include ParentConnection and ChildConnection |
| 59 | +// (to ensure we didn't accidentally remove them from the wrong function). |
| 60 | +func TestSerializeAssociation_HasConnectionFields(t *testing.T) { |
| 61 | + a := &domainmodel.Association{ |
| 62 | + Name: "Child_Parent", |
| 63 | + ParentID: "parent-entity-id", |
| 64 | + ChildID: "child-entity-id", |
| 65 | + Type: domainmodel.AssociationTypeReference, |
| 66 | + Owner: domainmodel.AssociationOwnerDefault, |
| 67 | + } |
| 68 | + a.ID = "test-assoc-id" |
| 69 | + |
| 70 | + result := serializeAssociation(a) |
| 71 | + |
| 72 | + hasParentConn := false |
| 73 | + hasChildConn := false |
| 74 | + for key := range result { |
| 75 | + if key == "ParentConnection" { |
| 76 | + hasParentConn = true |
| 77 | + } |
| 78 | + if key == "ChildConnection" { |
| 79 | + hasChildConn = true |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if !hasParentConn { |
| 84 | + t.Error("serializeAssociation must include ParentConnection") |
| 85 | + } |
| 86 | + if !hasChildConn { |
| 87 | + t.Error("serializeAssociation must include ChildConnection") |
| 88 | + } |
| 89 | +} |
0 commit comments