Skip to content

Commit 6b9eec0

Browse files
committed
test: expand mock tests for modules, entities, associations, enumerations, constants, microflows
1 parent 4d462ac commit 6b9eec0

6 files changed

Lines changed: 114 additions & 5 deletions

mdl/executor/cmd_associations_mock_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package executor
44

55
import (
6+
"fmt"
67
"testing"
78

89
"github.com/mendixlabs/mxcli/mdl/backend/mock"
@@ -75,3 +76,40 @@ func TestShowAssociations_Mock_FilterByModule(t *testing.T) {
7576
assertContainsStr(t, out, "HR.Employee_Dept")
7677
assertContainsStr(t, out, "(1 associations)")
7778
}
79+
80+
// NOTE: listAssociations and describeAssociation have no Connected() guard.
81+
// They call backend directly — error propagation is the only failure mode.
82+
83+
func TestShowAssociations_BackendError(t *testing.T) {
84+
mb := &mock.MockBackend{
85+
IsConnectedFunc: func() bool { return true },
86+
ListModulesFunc: func() ([]*model.Module, error) { return nil, fmt.Errorf("connection lost") },
87+
}
88+
ctx, _ := newMockCtx(t, withBackend(mb))
89+
assertError(t, listAssociations(ctx, ""))
90+
}
91+
92+
func TestShowAssociations_JSON(t *testing.T) {
93+
mod := mkModule("App")
94+
ent1 := mkEntity(mod.ID, "A")
95+
ent2 := mkEntity(mod.ID, "B")
96+
assoc := mkAssociation(mod.ID, "A_B", ent1.ID, ent2.ID)
97+
98+
dm := &domainmodel.DomainModel{
99+
BaseElement: model.BaseElement{ID: nextID("dm")},
100+
ContainerID: mod.ID,
101+
Entities: []*domainmodel.Entity{ent1, ent2},
102+
Associations: []*domainmodel.Association{assoc},
103+
}
104+
105+
mb := &mock.MockBackend{
106+
IsConnectedFunc: func() bool { return true },
107+
ListModulesFunc: func() ([]*model.Module, error) { return []*model.Module{mod}, nil },
108+
ListDomainModelsFunc: func() ([]*domainmodel.DomainModel, error) { return []*domainmodel.DomainModel{dm}, nil },
109+
}
110+
111+
ctx, buf := newMockCtx(t, withBackend(mb), withFormat(FormatJSON))
112+
assertNoError(t, listAssociations(ctx, ""))
113+
assertValidJSON(t, buf.String())
114+
assertContainsStr(t, buf.String(), "A_B")
115+
}

mdl/executor/cmd_constants_mock_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,6 @@ func TestDescribeConstant_Mock_NotFound(t *testing.T) {
104104
err := describeConstant(ctx, ast.QualifiedName{Module: "MyModule", Name: "Missing"})
105105
assertError(t, err)
106106
}
107+
108+
// Backend error: cmd_error_mock_test.go (TestShowConstants_Mock_BackendError)
109+
// JSON: cmd_json_mock_test.go (TestShowConstants_Mock_JSON)

mdl/executor/cmd_entities_mock_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package executor
44

55
import (
6+
"fmt"
67
"testing"
78

89
"github.com/mendixlabs/mxcli/mdl/backend/mock"
@@ -56,3 +57,40 @@ func TestShowEntities_Mock_FilterByModule(t *testing.T) {
5657
assertContainsStr(t, out, "HR.Employee")
5758
assertContainsStr(t, out, "(1 entities)")
5859
}
60+
61+
// NOTE: listEntities has no Connected() guard — it calls backend directly.
62+
63+
func TestShowEntities_BackendError_Modules(t *testing.T) {
64+
mb := &mock.MockBackend{
65+
IsConnectedFunc: func() bool { return true },
66+
ListModulesFunc: func() ([]*model.Module, error) { return nil, fmt.Errorf("not connected") },
67+
}
68+
ctx, _ := newMockCtx(t, withBackend(mb))
69+
assertError(t, listEntities(ctx, ""))
70+
}
71+
72+
func TestShowEntities_BackendError(t *testing.T) {
73+
mb := &mock.MockBackend{
74+
IsConnectedFunc: func() bool { return true },
75+
ListDomainModelsFunc: func() ([]*domainmodel.DomainModel, error) { return nil, fmt.Errorf("backend down") },
76+
}
77+
ctx, _ := newMockCtx(t, withBackend(mb))
78+
assertError(t, listEntities(ctx, ""))
79+
}
80+
81+
func TestShowEntities_JSON(t *testing.T) {
82+
mod := mkModule("App")
83+
ent := mkEntity(mod.ID, "Item")
84+
dm := mkDomainModel(mod.ID, ent)
85+
86+
mb := &mock.MockBackend{
87+
IsConnectedFunc: func() bool { return true },
88+
ListModulesFunc: func() ([]*model.Module, error) { return []*model.Module{mod}, nil },
89+
ListDomainModelsFunc: func() ([]*domainmodel.DomainModel, error) { return []*domainmodel.DomainModel{dm}, nil },
90+
}
91+
92+
ctx, buf := newMockCtx(t, withBackend(mb), withFormat(FormatJSON))
93+
assertNoError(t, listEntities(ctx, ""))
94+
assertValidJSON(t, buf.String())
95+
assertContainsStr(t, buf.String(), "Item")
96+
}

mdl/executor/cmd_enumerations_mock_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,6 @@ func TestDescribeEnumeration_Mock_NotFound(t *testing.T) {
9797
err := describeEnumeration(ctx, ast.QualifiedName{Module: "MyModule", Name: "Missing"})
9898
assertError(t, err)
9999
}
100+
101+
// Backend error: cmd_error_mock_test.go (TestShowEnumerations_Mock_BackendError)
102+
// JSON: cmd_json_mock_test.go (TestShowEnumerations_Mock_JSON)

mdl/executor/cmd_microflows_mock_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,6 @@ func TestDescribeMicroflow_Mock_NotFound(t *testing.T) {
111111
err := describeMicroflow(ctx, ast.QualifiedName{Module: "MyModule", Name: "Missing"})
112112
assertError(t, err)
113113
}
114+
115+
// Backend error: cmd_error_mock_test.go (TestShowMicroflows_Mock_BackendError, TestShowNanoflows_Mock_BackendError)
116+
// JSON: cmd_json_mock_test.go (TestShowMicroflows_Mock_JSON, TestShowNanoflows_Mock_JSON)

mdl/executor/cmd_modules_mock_test.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package executor
44

55
import (
6+
"fmt"
67
"testing"
78

89
"github.com/mendixlabs/mxcli/mdl/backend/mock"
@@ -15,16 +16,12 @@ func TestShowModules_Mock(t *testing.T) {
1516
mod1 := mkModule("MyModule")
1617
mod2 := mkModule("System")
1718

18-
// listModules uses ListUnits to count documents per module.
19-
// Provide a unit belonging to mod1 so the count is non-zero.
2019
unitID := nextID("unit")
2120
units := []*types.UnitInfo{{ID: unitID, ContainerID: mod1.ID}}
2221

23-
// Need a hierarchy for getHierarchy — provide modules + units + folders
2422
h := mkHierarchy(mod1, mod2)
2523
withContainer(h, unitID, mod1.ID)
2624

27-
// Provide one domain model for mod1 with one entity
2825
ent := mkEntity(mod1.ID, "Customer")
2926
dm := mkDomainModel(mod1.ID, ent)
3027

@@ -33,7 +30,6 @@ func TestShowModules_Mock(t *testing.T) {
3330
ListModulesFunc: func() ([]*model.Module, error) { return []*model.Module{mod1, mod2}, nil },
3431
ListUnitsFunc: func() ([]*types.UnitInfo, error) { return units, nil },
3532
ListDomainModelsFunc: func() ([]*domainmodel.DomainModel, error) { return []*domainmodel.DomainModel{dm}, nil },
36-
// All other list functions return nil (zero counts) via MockBackend defaults.
3733
}
3834

3935
ctx, buf := newMockCtx(t, withBackend(mb), withHierarchy(h))
@@ -44,3 +40,31 @@ func TestShowModules_Mock(t *testing.T) {
4440
assertContainsStr(t, out, "System")
4541
assertContainsStr(t, out, "(2 modules)")
4642
}
43+
44+
// Not-connected covered in cmd_notconnected_mock_test.go
45+
46+
func TestShowModules_BackendError(t *testing.T) {
47+
mb := &mock.MockBackend{
48+
IsConnectedFunc: func() bool { return true },
49+
ListModulesFunc: func() ([]*model.Module, error) { return nil, fmt.Errorf("connection lost") },
50+
}
51+
ctx, _ := newMockCtx(t, withBackend(mb), withHierarchy(mkHierarchy()))
52+
assertError(t, listModules(ctx))
53+
}
54+
55+
func TestShowModules_JSON(t *testing.T) {
56+
mod := mkModule("App")
57+
h := mkHierarchy(mod)
58+
59+
mb := &mock.MockBackend{
60+
IsConnectedFunc: func() bool { return true },
61+
ListModulesFunc: func() ([]*model.Module, error) { return []*model.Module{mod}, nil },
62+
ListUnitsFunc: func() ([]*types.UnitInfo, error) { return nil, nil },
63+
ListDomainModelsFunc: func() ([]*domainmodel.DomainModel, error) { return nil, nil },
64+
}
65+
66+
ctx, buf := newMockCtx(t, withBackend(mb), withHierarchy(h), withFormat(FormatJSON))
67+
assertNoError(t, listModules(ctx))
68+
assertValidJSON(t, buf.String())
69+
assertContainsStr(t, buf.String(), "App")
70+
}

0 commit comments

Comments
 (0)