|
| 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_AllowsAssociationPathSortAttribute(t *testing.T) { |
| 16 | + moduleID := model.ID("sample-module") |
| 17 | + parentID := model.ID("parent-entity") |
| 18 | + childID := model.ID("child-entity") |
| 19 | + fb := &flowBuilder{ |
| 20 | + varTypes: map[string]string{}, |
| 21 | + backend: &mock.MockBackend{ |
| 22 | + GetModuleByNameFunc: func(name string) (*model.Module, error) { |
| 23 | + if name != "SampleApps" { |
| 24 | + return nil, nil |
| 25 | + } |
| 26 | + return &model.Module{BaseElement: model.BaseElement{ID: moduleID}, Name: name}, nil |
| 27 | + }, |
| 28 | + GetDomainModelFunc: func(id model.ID) (*domainmodel.DomainModel, error) { |
| 29 | + if id != moduleID { |
| 30 | + return nil, nil |
| 31 | + } |
| 32 | + return &domainmodel.DomainModel{ |
| 33 | + ContainerID: moduleID, |
| 34 | + Entities: []*domainmodel.Entity{ |
| 35 | + {BaseElement: model.BaseElement{ID: parentID}, Name: "DeploymentTarget"}, |
| 36 | + {BaseElement: model.BaseElement{ID: childID}, Name: "ApplicationView"}, |
| 37 | + }, |
| 38 | + Associations: []*domainmodel.Association{ |
| 39 | + { |
| 40 | + Name: "DeploymentTarget_ApplicationView", |
| 41 | + ParentID: parentID, |
| 42 | + ChildID: childID, |
| 43 | + Type: domainmodel.AssociationTypeReference, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, nil |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + fb.addRetrieveAction(&ast.RetrieveStmt{ |
| 52 | + Variable: "DeploymentTargetList", |
| 53 | + Source: ast.QualifiedName{ |
| 54 | + Module: "SampleApps", |
| 55 | + Name: "DeploymentTarget", |
| 56 | + }, |
| 57 | + SortColumns: []ast.SortColumnDef{ |
| 58 | + {Attribute: "SampleApps.ApplicationView.CreatedAt", Order: "DESC"}, |
| 59 | + {Attribute: "Name", Order: "ASC"}, |
| 60 | + }, |
| 61 | + }) |
| 62 | + |
| 63 | + if len(fb.errors) > 0 { |
| 64 | + t.Fatalf("unexpected builder errors: %v", fb.errors) |
| 65 | + } |
| 66 | + if len(fb.objects) != 1 { |
| 67 | + t.Fatalf("got %d objects, want 1", len(fb.objects)) |
| 68 | + } |
| 69 | + |
| 70 | + activity, ok := fb.objects[0].(*microflows.ActionActivity) |
| 71 | + if !ok { |
| 72 | + t.Fatalf("got object %T, want *microflows.ActionActivity", fb.objects[0]) |
| 73 | + } |
| 74 | + action, ok := activity.Action.(*microflows.RetrieveAction) |
| 75 | + if !ok { |
| 76 | + t.Fatalf("got action %T, want *microflows.RetrieveAction", activity.Action) |
| 77 | + } |
| 78 | + source, ok := action.Source.(*microflows.DatabaseRetrieveSource) |
| 79 | + if !ok { |
| 80 | + t.Fatalf("got source %T, want *microflows.DatabaseRetrieveSource", action.Source) |
| 81 | + } |
| 82 | + if len(source.Sorting) != 2 { |
| 83 | + t.Fatalf("got %d sort items, want 2", len(source.Sorting)) |
| 84 | + } |
| 85 | + if got := source.Sorting[0].AttributeQualifiedName; got != "SampleApps.ApplicationView.CreatedAt" { |
| 86 | + t.Fatalf("first sort attribute = %q", got) |
| 87 | + } |
| 88 | + if got := source.Sorting[0].EntityRefSteps; len(got) != 1 || got[0].Association != "SampleApps.DeploymentTarget_ApplicationView" || got[0].DestinationEntity != "SampleApps.ApplicationView" { |
| 89 | + t.Fatalf("first sort entity ref steps = %#v", got) |
| 90 | + } |
| 91 | + if got := source.Sorting[0].Direction; got != microflows.SortDirectionDescending { |
| 92 | + t.Fatalf("first sort direction = %q, want %q", got, microflows.SortDirectionDescending) |
| 93 | + } |
| 94 | + if got := source.Sorting[1].AttributeQualifiedName; got != "SampleApps.DeploymentTarget.Name" { |
| 95 | + t.Fatalf("second sort attribute = %q", got) |
| 96 | + } |
| 97 | + if got := source.Sorting[1].EntityRefSteps; len(got) != 0 { |
| 98 | + t.Fatalf("second sort entity ref steps = %#v, want none", got) |
| 99 | + } |
| 100 | +} |
0 commit comments