Skip to content

Commit 27cb9aa

Browse files
committed
test(catalog): add unit tests for builder pure helper functions
Cover getMicroflowObjectType, getMicroflowActionType, getDataTypeName, countMicroflowActivities, calculateMcCabeComplexity, countNanoflowActivities, calculateNanoflowComplexity, extractLayoutRef, extractPageWidgets, extractWidgetsRecursive, extractSnippetWidgets, getBsonArrayElements, toBsonArray, extractString, extractBsonID, decodeBase64GUID, extractBinaryID, formatGUID, bytesToHex, entityAccessFromMemberRights, countWorkflowActivityTypes, countMenuItems, extractAttrName.
1 parent ea6603f commit 27cb9aa

6 files changed

Lines changed: 1129 additions & 0 deletions
Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package catalog
4+
5+
import (
6+
"testing"
7+
8+
"github.com/mendixlabs/mxcli/sdk/microflows"
9+
)
10+
11+
func TestGetMicroflowObjectType(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
obj microflows.MicroflowObject
15+
want string
16+
}{
17+
{"ActionActivity", &microflows.ActionActivity{}, "ActionActivity"},
18+
{"StartEvent", &microflows.StartEvent{}, "StartEvent"},
19+
{"EndEvent", &microflows.EndEvent{}, "EndEvent"},
20+
{"ExclusiveSplit", &microflows.ExclusiveSplit{}, "ExclusiveSplit"},
21+
{"InheritanceSplit", &microflows.InheritanceSplit{}, "InheritanceSplit"},
22+
{"ExclusiveMerge", &microflows.ExclusiveMerge{}, "ExclusiveMerge"},
23+
{"LoopedActivity", &microflows.LoopedActivity{}, "LoopedActivity"},
24+
{"Annotation", &microflows.Annotation{}, "Annotation"},
25+
{"BreakEvent", &microflows.BreakEvent{}, "BreakEvent"},
26+
{"ContinueEvent", &microflows.ContinueEvent{}, "ContinueEvent"},
27+
{"ErrorEvent", &microflows.ErrorEvent{}, "ErrorEvent"},
28+
}
29+
30+
for _, tt := range tests {
31+
t.Run(tt.name, func(t *testing.T) {
32+
if got := getMicroflowObjectType(tt.obj); got != tt.want {
33+
t.Errorf("getMicroflowObjectType() = %q, want %q", got, tt.want)
34+
}
35+
})
36+
}
37+
}
38+
39+
func TestGetMicroflowActionType(t *testing.T) {
40+
tests := []struct {
41+
name string
42+
action microflows.MicroflowAction
43+
want string
44+
}{
45+
{"CreateObjectAction", &microflows.CreateObjectAction{}, "CreateObjectAction"},
46+
{"ChangeObjectAction", &microflows.ChangeObjectAction{}, "ChangeObjectAction"},
47+
{"RetrieveAction", &microflows.RetrieveAction{}, "RetrieveAction"},
48+
{"MicroflowCallAction", &microflows.MicroflowCallAction{}, "MicroflowCallAction"},
49+
{"JavaActionCallAction", &microflows.JavaActionCallAction{}, "JavaActionCallAction"},
50+
{"ShowMessageAction", &microflows.ShowMessageAction{}, "ShowMessageAction"},
51+
{"LogMessageAction", &microflows.LogMessageAction{}, "LogMessageAction"},
52+
{"ValidationFeedbackAction", &microflows.ValidationFeedbackAction{}, "ValidationFeedbackAction"},
53+
{"ChangeVariableAction", &microflows.ChangeVariableAction{}, "ChangeVariableAction"},
54+
{"CreateVariableAction", &microflows.CreateVariableAction{}, "CreateVariableAction"},
55+
{"AggregateListAction", &microflows.AggregateListAction{}, "AggregateListAction"},
56+
{"ListOperationAction", &microflows.ListOperationAction{}, "ListOperationAction"},
57+
{"CastAction", &microflows.CastAction{}, "CastAction"},
58+
{"DownloadFileAction", &microflows.DownloadFileAction{}, "DownloadFileAction"},
59+
{"ClosePageAction", &microflows.ClosePageAction{}, "ClosePageAction"},
60+
{"ShowPageAction", &microflows.ShowPageAction{}, "ShowPageAction"},
61+
{"CallExternalAction", &microflows.CallExternalAction{}, "CallExternalAction"},
62+
}
63+
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
if got := getMicroflowActionType(tt.action); got != tt.want {
67+
t.Errorf("getMicroflowActionType() = %q, want %q", got, tt.want)
68+
}
69+
})
70+
}
71+
}
72+
73+
func TestGetDataTypeName(t *testing.T) {
74+
tests := []struct {
75+
name string
76+
dt microflows.DataType
77+
want string
78+
}{
79+
{"nil", nil, ""},
80+
{"Boolean", &microflows.BooleanType{}, "Boolean"},
81+
{"Integer", &microflows.IntegerType{}, "Integer"},
82+
{"Long", &microflows.LongType{}, "Long"},
83+
{"Decimal", &microflows.DecimalType{}, "Decimal"},
84+
{"String", &microflows.StringType{}, "String"},
85+
{"DateTime", &microflows.DateTimeType{}, "DateTime"},
86+
{"Date", &microflows.DateType{}, "Date"},
87+
{"Void", &microflows.VoidType{}, "Void"},
88+
{"Object with entity", &microflows.ObjectType{EntityQualifiedName: "Module.Entity"}, "Object:Module.Entity"},
89+
{"List with entity", &microflows.ListType{EntityQualifiedName: "Module.Entity"}, "List:Module.Entity"},
90+
{"Enumeration", &microflows.EnumerationType{EnumerationQualifiedName: "Module.Color"}, "Enumeration:Module.Color"},
91+
}
92+
93+
for _, tt := range tests {
94+
t.Run(tt.name, func(t *testing.T) {
95+
if got := getDataTypeName(tt.dt); got != tt.want {
96+
t.Errorf("getDataTypeName() = %q, want %q", got, tt.want)
97+
}
98+
})
99+
}
100+
}
101+
102+
func TestCountMicroflowActivities(t *testing.T) {
103+
tests := []struct {
104+
name string
105+
mf *microflows.Microflow
106+
want int
107+
}{
108+
{
109+
name: "nil object collection",
110+
mf: &microflows.Microflow{},
111+
want: 0,
112+
},
113+
{
114+
name: "empty objects",
115+
mf: &microflows.Microflow{
116+
ObjectCollection: &microflows.MicroflowObjectCollection{},
117+
},
118+
want: 0,
119+
},
120+
{
121+
name: "excludes start/end/merge",
122+
mf: &microflows.Microflow{
123+
ObjectCollection: &microflows.MicroflowObjectCollection{
124+
Objects: []microflows.MicroflowObject{
125+
&microflows.StartEvent{},
126+
&microflows.ActionActivity{},
127+
&microflows.ExclusiveSplit{},
128+
&microflows.EndEvent{},
129+
&microflows.ExclusiveMerge{},
130+
},
131+
},
132+
},
133+
want: 2, // ActionActivity + ExclusiveSplit
134+
},
135+
{
136+
name: "counts loops and annotations",
137+
mf: &microflows.Microflow{
138+
ObjectCollection: &microflows.MicroflowObjectCollection{
139+
Objects: []microflows.MicroflowObject{
140+
&microflows.LoopedActivity{},
141+
&microflows.Annotation{},
142+
&microflows.ErrorEvent{},
143+
},
144+
},
145+
},
146+
want: 3,
147+
},
148+
}
149+
150+
for _, tt := range tests {
151+
t.Run(tt.name, func(t *testing.T) {
152+
if got := countMicroflowActivities(tt.mf); got != tt.want {
153+
t.Errorf("countMicroflowActivities() = %d, want %d", got, tt.want)
154+
}
155+
})
156+
}
157+
}
158+
159+
func TestCalculateMcCabeComplexity(t *testing.T) {
160+
tests := []struct {
161+
name string
162+
mf *microflows.Microflow
163+
want int
164+
}{
165+
{
166+
name: "nil object collection — base complexity",
167+
mf: &microflows.Microflow{},
168+
want: 1,
169+
},
170+
{
171+
name: "no decision points",
172+
mf: &microflows.Microflow{
173+
ObjectCollection: &microflows.MicroflowObjectCollection{
174+
Objects: []microflows.MicroflowObject{
175+
&microflows.StartEvent{},
176+
&microflows.ActionActivity{},
177+
&microflows.EndEvent{},
178+
},
179+
},
180+
},
181+
want: 1,
182+
},
183+
{
184+
name: "exclusive split adds 1",
185+
mf: &microflows.Microflow{
186+
ObjectCollection: &microflows.MicroflowObjectCollection{
187+
Objects: []microflows.MicroflowObject{
188+
&microflows.ExclusiveSplit{},
189+
},
190+
},
191+
},
192+
want: 2,
193+
},
194+
{
195+
name: "inheritance split adds 1",
196+
mf: &microflows.Microflow{
197+
ObjectCollection: &microflows.MicroflowObjectCollection{
198+
Objects: []microflows.MicroflowObject{
199+
&microflows.InheritanceSplit{},
200+
},
201+
},
202+
},
203+
want: 2,
204+
},
205+
{
206+
name: "loop adds 1 plus nested decisions",
207+
mf: &microflows.Microflow{
208+
ObjectCollection: &microflows.MicroflowObjectCollection{
209+
Objects: []microflows.MicroflowObject{
210+
&microflows.LoopedActivity{
211+
ObjectCollection: &microflows.MicroflowObjectCollection{
212+
Objects: []microflows.MicroflowObject{
213+
&microflows.ExclusiveSplit{},
214+
},
215+
},
216+
},
217+
},
218+
},
219+
},
220+
want: 3, // 1 base + 1 loop + 1 nested split
221+
},
222+
{
223+
name: "error event adds 1",
224+
mf: &microflows.Microflow{
225+
ObjectCollection: &microflows.MicroflowObjectCollection{
226+
Objects: []microflows.MicroflowObject{
227+
&microflows.ErrorEvent{},
228+
},
229+
},
230+
},
231+
want: 2,
232+
},
233+
{
234+
name: "complex flow",
235+
mf: &microflows.Microflow{
236+
ObjectCollection: &microflows.MicroflowObjectCollection{
237+
Objects: []microflows.MicroflowObject{
238+
&microflows.ExclusiveSplit{},
239+
&microflows.ExclusiveSplit{},
240+
&microflows.InheritanceSplit{},
241+
&microflows.LoopedActivity{},
242+
&microflows.ErrorEvent{},
243+
},
244+
},
245+
},
246+
want: 6, // 1 + 2 splits + 1 inheritance + 1 loop + 1 error
247+
},
248+
}
249+
250+
for _, tt := range tests {
251+
t.Run(tt.name, func(t *testing.T) {
252+
if got := calculateMcCabeComplexity(tt.mf); got != tt.want {
253+
t.Errorf("calculateMcCabeComplexity() = %d, want %d", got, tt.want)
254+
}
255+
})
256+
}
257+
}
258+
259+
func TestCountNanoflowActivities(t *testing.T) {
260+
tests := []struct {
261+
name string
262+
nf *microflows.Nanoflow
263+
want int
264+
}{
265+
{
266+
name: "nil object collection",
267+
nf: &microflows.Nanoflow{},
268+
want: 0,
269+
},
270+
{
271+
name: "excludes structural elements",
272+
nf: &microflows.Nanoflow{
273+
ObjectCollection: &microflows.MicroflowObjectCollection{
274+
Objects: []microflows.MicroflowObject{
275+
&microflows.StartEvent{},
276+
&microflows.ActionActivity{},
277+
&microflows.EndEvent{},
278+
&microflows.ExclusiveMerge{},
279+
},
280+
},
281+
},
282+
want: 1, // only ActionActivity
283+
},
284+
}
285+
286+
for _, tt := range tests {
287+
t.Run(tt.name, func(t *testing.T) {
288+
if got := countNanoflowActivities(tt.nf); got != tt.want {
289+
t.Errorf("countNanoflowActivities() = %d, want %d", got, tt.want)
290+
}
291+
})
292+
}
293+
}
294+
295+
func TestCalculateNanoflowComplexity(t *testing.T) {
296+
nf := &microflows.Nanoflow{
297+
ObjectCollection: &microflows.MicroflowObjectCollection{
298+
Objects: []microflows.MicroflowObject{
299+
&microflows.ExclusiveSplit{},
300+
&microflows.LoopedActivity{
301+
ObjectCollection: &microflows.MicroflowObjectCollection{
302+
Objects: []microflows.MicroflowObject{
303+
&microflows.InheritanceSplit{},
304+
},
305+
},
306+
},
307+
},
308+
},
309+
}
310+
311+
got := calculateNanoflowComplexity(nf)
312+
want := 4 // 1 base + 1 split + 1 loop + 1 nested inheritance
313+
if got != want {
314+
t.Errorf("calculateNanoflowComplexity() = %d, want %d", got, want)
315+
}
316+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package catalog
4+
5+
import "testing"
6+
7+
func TestExtractAttrName(t *testing.T) {
8+
tests := []struct {
9+
input string
10+
want string
11+
}{
12+
{"Module.Entity.Attribute", "Attribute"},
13+
{"Module.Entity.Attribute.Sub", "Sub"},
14+
{"Module.Entity", ""},
15+
{"Single", ""},
16+
{"", ""},
17+
}
18+
19+
for _, tt := range tests {
20+
t.Run(tt.input, func(t *testing.T) {
21+
if got := extractAttrName(tt.input); got != tt.want {
22+
t.Errorf("extractAttrName(%q) = %q, want %q", tt.input, got, tt.want)
23+
}
24+
})
25+
}
26+
}

0 commit comments

Comments
 (0)