|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package executor |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mendixlabs/mxcli/mdl/ast" |
| 9 | + "github.com/mendixlabs/mxcli/mdl/backend/mock" |
| 10 | + "github.com/mendixlabs/mxcli/model" |
| 11 | + "github.com/mendixlabs/mxcli/sdk/domainmodel" |
| 12 | + "github.com/mendixlabs/mxcli/sdk/microflows" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAddRetrieveAction_ReverseReferenceOwnerBothUsesAssociationSource(t *testing.T) { |
| 16 | + fb := newRetrieveAssociationFlowBuilder(domainmodel.AssociationOwnerBoth) |
| 17 | + fb.varTypes["Child"] = "Sample.Child" |
| 18 | + |
| 19 | + fb.addRetrieveAction(&ast.RetrieveStmt{ |
| 20 | + Variable: "Parent", |
| 21 | + StartVariable: "Child", |
| 22 | + Source: ast.QualifiedName{Module: "Sample", Name: "Parent_Child"}, |
| 23 | + }) |
| 24 | + |
| 25 | + action := onlyRetrieveAction(t, fb) |
| 26 | + source, ok := action.Source.(*microflows.AssociationRetrieveSource) |
| 27 | + if !ok { |
| 28 | + t.Fatalf("owner-both reverse retrieve source = %T, want AssociationRetrieveSource", action.Source) |
| 29 | + } |
| 30 | + if source.StartVariable != "Child" || source.AssociationQualifiedName != "Sample.Parent_Child" { |
| 31 | + t.Fatalf("association source = %#v", source) |
| 32 | + } |
| 33 | + if got := fb.varTypes["Parent"]; got != "Sample.Parent" { |
| 34 | + t.Fatalf("result var type = %q, want Sample.Parent", got) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestAddRetrieveAction_ReverseReferenceDefaultOwnerUsesDatabaseSource(t *testing.T) { |
| 39 | + fb := newRetrieveAssociationFlowBuilder(domainmodel.AssociationOwnerDefault) |
| 40 | + fb.varTypes["Child"] = "Sample.Child" |
| 41 | + |
| 42 | + fb.addRetrieveAction(&ast.RetrieveStmt{ |
| 43 | + Variable: "Parents", |
| 44 | + StartVariable: "Child", |
| 45 | + Source: ast.QualifiedName{Module: "Sample", Name: "Parent_Child"}, |
| 46 | + }) |
| 47 | + |
| 48 | + action := onlyRetrieveAction(t, fb) |
| 49 | + source, ok := action.Source.(*microflows.DatabaseRetrieveSource) |
| 50 | + if !ok { |
| 51 | + t.Fatalf("default-owner reverse retrieve source = %T, want DatabaseRetrieveSource", action.Source) |
| 52 | + } |
| 53 | + if source.EntityQualifiedName != "Sample.Parent" || source.XPathConstraint != "[Sample.Parent_Child = $Child]" { |
| 54 | + t.Fatalf("database source = %#v", source) |
| 55 | + } |
| 56 | + if got := fb.varTypes["Parents"]; got != "List of Sample.Parent" { |
| 57 | + t.Fatalf("result var type = %q, want List of Sample.Parent", got) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestAddRetrieveAction_ReverseReferenceNonPersistableParentUsesAssociationSource(t *testing.T) { |
| 62 | + fb := newRetrieveAssociationFlowBuilderWithPersistability(domainmodel.AssociationOwnerDefault, false, true) |
| 63 | + fb.varTypes["Child"] = "Sample.Child" |
| 64 | + |
| 65 | + fb.addRetrieveAction(&ast.RetrieveStmt{ |
| 66 | + Variable: "Parents", |
| 67 | + StartVariable: "Child", |
| 68 | + Source: ast.QualifiedName{Module: "Sample", Name: "Parent_Child"}, |
| 69 | + }) |
| 70 | + |
| 71 | + action := onlyRetrieveAction(t, fb) |
| 72 | + source, ok := action.Source.(*microflows.AssociationRetrieveSource) |
| 73 | + if !ok { |
| 74 | + t.Fatalf("non-persistable reverse retrieve source = %T, want AssociationRetrieveSource", action.Source) |
| 75 | + } |
| 76 | + if source.StartVariable != "Child" || source.AssociationQualifiedName != "Sample.Parent_Child" { |
| 77 | + t.Fatalf("association source = %#v", source) |
| 78 | + } |
| 79 | + if got := fb.varTypes["Parents"]; got != "Sample.Parent" { |
| 80 | + t.Fatalf("result var type = %q, want Sample.Parent", got) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestAddRetrieveAction_ReferenceSetRegistersOtherEntityListType(t *testing.T) { |
| 85 | + fb := newRetrieveAssociationFlowBuilderWithType(domainmodel.AssociationTypeReferenceSet, domainmodel.AssociationOwnerBoth, true, true) |
| 86 | + fb.varTypes["Parent"] = "Sample.Parent" |
| 87 | + |
| 88 | + fb.addRetrieveAction(&ast.RetrieveStmt{ |
| 89 | + Variable: "Children", |
| 90 | + StartVariable: "Parent", |
| 91 | + Source: ast.QualifiedName{Module: "Sample", Name: "Parent_Child"}, |
| 92 | + }) |
| 93 | + |
| 94 | + action := onlyRetrieveAction(t, fb) |
| 95 | + source, ok := action.Source.(*microflows.AssociationRetrieveSource) |
| 96 | + if !ok { |
| 97 | + t.Fatalf("reference-set retrieve source = %T, want AssociationRetrieveSource", action.Source) |
| 98 | + } |
| 99 | + if source.StartVariable != "Parent" || source.AssociationQualifiedName != "Sample.Parent_Child" { |
| 100 | + t.Fatalf("association source = %#v", source) |
| 101 | + } |
| 102 | + if got := fb.varTypes["Children"]; got != "List of Sample.Child" { |
| 103 | + t.Fatalf("result var type = %q, want List of Sample.Child", got) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func newRetrieveAssociationFlowBuilder(owner domainmodel.AssociationOwner) *flowBuilder { |
| 108 | + return newRetrieveAssociationFlowBuilderWithPersistability(owner, true, true) |
| 109 | +} |
| 110 | + |
| 111 | +func newRetrieveAssociationFlowBuilderWithPersistability(owner domainmodel.AssociationOwner, parentPersistable, childPersistable bool) *flowBuilder { |
| 112 | + return newRetrieveAssociationFlowBuilderWithType(domainmodel.AssociationTypeReference, owner, parentPersistable, childPersistable) |
| 113 | +} |
| 114 | + |
| 115 | +func newRetrieveAssociationFlowBuilderWithType(associationType domainmodel.AssociationType, owner domainmodel.AssociationOwner, parentPersistable, childPersistable bool) *flowBuilder { |
| 116 | + moduleID := model.ID("sample-module") |
| 117 | + parentID := model.ID("parent-entity") |
| 118 | + childID := model.ID("child-entity") |
| 119 | + return &flowBuilder{ |
| 120 | + varTypes: map[string]string{}, |
| 121 | + backend: &mock.MockBackend{ |
| 122 | + GetModuleByNameFunc: func(name string) (*model.Module, error) { |
| 123 | + if name != "Sample" { |
| 124 | + return nil, nil |
| 125 | + } |
| 126 | + return &model.Module{BaseElement: model.BaseElement{ID: moduleID}, Name: name}, nil |
| 127 | + }, |
| 128 | + GetDomainModelFunc: func(id model.ID) (*domainmodel.DomainModel, error) { |
| 129 | + if id != moduleID { |
| 130 | + return nil, nil |
| 131 | + } |
| 132 | + return &domainmodel.DomainModel{ |
| 133 | + ContainerID: moduleID, |
| 134 | + Entities: []*domainmodel.Entity{ |
| 135 | + {BaseElement: model.BaseElement{ID: parentID}, Name: "Parent", Persistable: parentPersistable}, |
| 136 | + {BaseElement: model.BaseElement{ID: childID}, Name: "Child", Persistable: childPersistable}, |
| 137 | + }, |
| 138 | + Associations: []*domainmodel.Association{ |
| 139 | + { |
| 140 | + Name: "Parent_Child", |
| 141 | + ParentID: parentID, |
| 142 | + ChildID: childID, |
| 143 | + Type: associationType, |
| 144 | + Owner: owner, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, nil |
| 148 | + }, |
| 149 | + }, |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func onlyRetrieveAction(t *testing.T, fb *flowBuilder) *microflows.RetrieveAction { |
| 154 | + t.Helper() |
| 155 | + if len(fb.objects) != 1 { |
| 156 | + t.Fatalf("got %d objects, want 1", len(fb.objects)) |
| 157 | + } |
| 158 | + activity, ok := fb.objects[0].(*microflows.ActionActivity) |
| 159 | + if !ok { |
| 160 | + t.Fatalf("got object %T, want *microflows.ActionActivity", fb.objects[0]) |
| 161 | + } |
| 162 | + action, ok := activity.Action.(*microflows.RetrieveAction) |
| 163 | + if !ok { |
| 164 | + t.Fatalf("got action %T, want *microflows.RetrieveAction", activity.Action) |
| 165 | + } |
| 166 | + return action |
| 167 | +} |
0 commit comments