forked from mendixlabs/mxcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbugfix_test.go
More file actions
595 lines (534 loc) · 16.9 KB
/
bugfix_test.go
File metadata and controls
595 lines (534 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// SPDX-License-Identifier: Apache-2.0
// Tests for bug fixes discovered during BST Monitoring app session (2026-03-13).
package executor
import (
"fmt"
"strings"
"testing"
"github.com/mendixlabs/mxcli/mdl/ast"
"github.com/mendixlabs/mxcli/mdl/visitor"
)
// TestValidateDuplicateVariableDeclareRetrieve verifies that DECLARE followed by
// RETRIEVE for the same variable is caught as a duplicate (CE0111).
// Bug #3: mxcli check passed but mx check reported CE0111.
func TestValidateDuplicateVariableDeclareRetrieve(t *testing.T) {
input := `create microflow Test.MF_DuplicateVar ()
begin
declare $Count Integer = 0;
retrieve $Count from Test.TestItem;
return $Count;
end;`
errors := validateMicroflowFromMDL(t, input)
found := false
for _, e := range errors {
if strings.Contains(e, "duplicate") && strings.Contains(e, "Count") {
found = true
break
}
}
if !found {
t.Errorf("Expected duplicate variable error for $Count, got errors: %v", errors)
}
}
// TestValidateDuplicateVariableDeclareOnly verifies that two DECLARE statements
// for the same variable are caught as duplicate.
func TestValidateDuplicateVariableDeclareOnly(t *testing.T) {
input := `create microflow Test.MF_DoubleDeclare ()
begin
declare $X Integer = 0;
declare $X String = 'hello';
end;`
errors := validateMicroflowFromMDL(t, input)
found := false
for _, e := range errors {
if strings.Contains(e, "duplicate") && strings.Contains(e, "X") {
found = true
break
}
}
if !found {
t.Errorf("Expected duplicate variable error for $X, got errors: %v", errors)
}
}
// TestValidateNoDuplicateWhenRetrieveOnly verifies that a single RETRIEVE
// (without prior DECLARE) does not trigger a false positive.
func TestValidateNoDuplicateWhenRetrieveOnly(t *testing.T) {
input := `create microflow Test.MF_RetrieveOnly ()
begin
retrieve $Items from Test.SomeEntity;
end;`
errors := validateMicroflowFromMDL(t, input)
for _, e := range errors {
if strings.Contains(e, "duplicate") {
t.Errorf("Unexpected duplicate variable error: %s", e)
}
}
}
// TestValidateDuplicateVariableDeclareCreate verifies that DECLARE followed by
// CREATE for the same variable is caught as a duplicate (CE0111).
func TestValidateDuplicateVariableDeclareCreate(t *testing.T) {
input := `create microflow Test.MF_DeclareCreate ()
begin
declare $NewTodo Test.Todo;
$NewTodo = create Test.Todo();
end;`
errors := validateMicroflowFromMDL(t, input)
found := false
for _, e := range errors {
if strings.Contains(e, "duplicate") && strings.Contains(e, "NewTodo") {
found = true
break
}
}
if !found {
t.Errorf("Expected duplicate variable error for $NewTodo, got errors: %v", errors)
}
}
// TestValidateEntityReservedAttributeName verifies that persistent entity attributes
// using reserved system names (CreatedDate, ChangedDate, Owner, ChangedBy) are caught.
func TestValidateEntityReservedAttributeName(t *testing.T) {
input := `create persistent entity Test.MyEntity (
Name : String(200),
CreatedDate : DateTime,
Status : String(50)
);`
prog, errs := visitor.Build(input)
if len(errs) > 0 {
t.Fatalf("Parse error: %v", errs[0])
}
stmt, ok := prog.Statements[0].(*ast.CreateEntityStmt)
if !ok {
t.Fatalf("Expected CreateEntityStmt, got %T", prog.Statements[0])
}
violations := ValidateEntity(stmt)
found := false
for _, v := range violations {
if strings.Contains(v.Message, "CreatedDate") && strings.Contains(v.Message, "system attribute") {
found = true
break
}
}
if !found {
t.Errorf("Expected reserved attribute error for CreatedDate, got: %v", violations)
}
}
// TestValidateEntityNonPersistentAllowed verifies that non-persistent entities
// can use system attribute names without error.
func TestValidateEntityNonPersistentAllowed(t *testing.T) {
input := `create non-persistent entity Test.MyNPE (
CreatedDate : DateTime,
Owner : String(200)
);`
prog, errs := visitor.Build(input)
if len(errs) > 0 {
t.Fatalf("Parse error: %v", errs[0])
}
stmt, ok := prog.Statements[0].(*ast.CreateEntityStmt)
if !ok {
t.Fatalf("Expected CreateEntityStmt, got %T", prog.Statements[0])
}
violations := ValidateEntity(stmt)
if len(violations) > 0 {
t.Errorf("Non-persistent entity should allow system attribute names, got: %v", violations)
}
}
// TestValidateEntityNormalAttributesPass verifies that normal attribute names
// don't trigger false positives.
func TestValidateEntityNormalAttributesPass(t *testing.T) {
input := `create persistent entity Test.MyEntity (
Name : String(200),
Description : String(2000),
Amount : Decimal,
IsActive : Boolean
);`
prog, errs := visitor.Build(input)
if len(errs) > 0 {
t.Fatalf("Parse error: %v", errs[0])
}
stmt, ok := prog.Statements[0].(*ast.CreateEntityStmt)
if !ok {
t.Fatalf("Expected CreateEntityStmt, got %T", prog.Statements[0])
}
violations := ValidateEntity(stmt)
if len(violations) > 0 {
t.Errorf("Normal attributes should not trigger errors, got: %v", violations)
}
}
// TestReturnsNothingAcceptsBarReturn verifies that RETURNS Nothing treats
// RETURN; (no value) as valid — "Nothing" means void.
func TestReturnsNothingAcceptsBarReturn(t *testing.T) {
input := `create microflow Test.MF_ReturnsNothing ()
returns Nothing
begin
log info 'hello';
return;
end;`
prog, errs := visitor.Build(input)
if len(errs) > 0 {
t.Fatalf("Parse error: %v", errs[0])
}
stmt := prog.Statements[0].(*ast.CreateMicroflowStmt)
// The return type should be TypeVoid
if stmt.ReturnType != nil && stmt.ReturnType.Type.Kind != ast.TypeVoid {
t.Errorf("Expected TypeVoid for returns Nothing, got %v", stmt.ReturnType.Type.Kind)
}
// Validation should NOT produce errors about RETURN requiring a value
warnings := ValidateMicroflowBody(stmt)
for _, w := range warnings {
if strings.Contains(w, "return requires a value") {
t.Errorf("returns Nothing should not reject bare return;, got: %s", w)
}
}
}
// TestEnumDefaultNotDoubleQualified verifies that enum DEFAULT values are stored
// without the enum prefix (just the value name), preventing double-qualification.
func TestEnumDefaultNotDoubleQualified(t *testing.T) {
input := `create persistent entity Test.Item (
Status : Enumeration(Test.ItemStatus) default Test.ItemStatus.Active
);`
prog, errs := visitor.Build(input)
if len(errs) > 0 {
t.Fatalf("Parse error: %v", errs[0])
}
stmt := prog.Statements[0].(*ast.CreateEntityStmt)
if len(stmt.Attributes) == 0 {
t.Fatal("Expected at least 1 attribute")
}
attr := stmt.Attributes[0]
if !attr.HasDefault {
t.Fatal("Expected attribute to have a default value")
}
// The default value from the parser is the full text "Test.ItemStatus.Active"
defaultStr := fmt.Sprintf("%v", attr.DefaultValue)
// When stored, it should be stripped to just "Active" (the executor does this)
// Here we verify the parser at least captures the full text correctly
if !strings.Contains(defaultStr, "Active") {
t.Errorf("Default value should contain 'Active', got: %s", defaultStr)
}
}
// TestExpressionToXPath_TokenQuoting verifies that [%CurrentDateTime%] tokens
// are unquoted in XPath context (Mendix special placeholders, not string literals).
// Quoting them causes CE0161 (XPath parse error) in Studio Pro.
func TestExpressionToXPath_TokenQuoting(t *testing.T) {
tests := []struct {
name string
expr ast.Expression
wantExpr string // expressionToString output
wantXP string // expressionToXPath output
}{
{
name: "Token_CurrentDateTime",
expr: &ast.TokenExpr{Token: "CurrentDateTime"},
wantExpr: "[%CurrentDateTime%]",
wantXP: "'[%CurrentDateTime%]'",
},
{
name: "Token_CurrentUser",
expr: &ast.TokenExpr{Token: "CurrentUser"},
wantExpr: "[%CurrentUser%]",
wantXP: "'[%CurrentUser%]'",
},
{
name: "BinaryExpr_with_token",
expr: &ast.BinaryExpr{
Left: &ast.IdentifierExpr{Name: "DueDate"},
Operator: "<",
Right: &ast.TokenExpr{Token: "CurrentDateTime"},
},
wantExpr: "DueDate < [%CurrentDateTime%]",
wantXP: "DueDate < '[%CurrentDateTime%]'",
},
{
name: "Variable_unchanged",
expr: &ast.VariableExpr{Name: "MyVar"},
wantExpr: "$MyVar",
wantXP: "$MyVar",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotExpr := expressionToString(tt.expr)
if gotExpr != tt.wantExpr {
t.Errorf("expressionToString() = %q, want %q", gotExpr, tt.wantExpr)
}
gotXP := expressionToXPath(tt.expr)
if gotXP != tt.wantXP {
t.Errorf("expressionToXPath() = %q, want %q", gotXP, tt.wantXP)
}
})
}
}
// TestExpressionToXPath_XPathPathExpr verifies that XPathPathExpr (bare association paths,
// nested predicates) serialize correctly via expressionToXPath.
func TestExpressionToXPath_XPathPathExpr(t *testing.T) {
tests := []struct {
name string
expr ast.Expression
wantXP string
}{
{
name: "bare_association_path",
expr: &ast.XPathPathExpr{
Steps: []ast.XPathStep{
{Expr: &ast.QualifiedNameExpr{QualifiedName: ast.QualifiedName{Module: "Module", Name: "Assoc"}}},
{Expr: &ast.QualifiedNameExpr{QualifiedName: ast.QualifiedName{Module: "Module", Name: "Entity"}}},
{Expr: &ast.IdentifierExpr{Name: "Attr"}},
},
},
wantXP: "Module.Assoc/Module.Entity/Attr",
},
{
name: "path_with_nested_predicate",
expr: &ast.XPathPathExpr{
Steps: []ast.XPathStep{
{Expr: &ast.QualifiedNameExpr{QualifiedName: ast.QualifiedName{Module: "Sys", Name: "roles"}}},
{
Expr: &ast.QualifiedNameExpr{QualifiedName: ast.QualifiedName{Module: "Sys", Name: "UserRole"}},
Predicate: &ast.BinaryExpr{
Left: &ast.IdentifierExpr{Name: "Active"},
Operator: "=",
Right: &ast.LiteralExpr{Value: true, Kind: ast.LiteralBoolean},
},
},
},
},
wantXP: "Sys.roles/Sys.UserRole[Active = true]",
},
{
name: "path_with_reversed",
expr: &ast.XPathPathExpr{
Steps: []ast.XPathStep{
{
Expr: &ast.QualifiedNameExpr{QualifiedName: ast.QualifiedName{Module: "System", Name: "roles"}},
Predicate: &ast.FunctionCallExpr{Name: "reversed"},
},
{Expr: &ast.QualifiedNameExpr{QualifiedName: ast.QualifiedName{Module: "System", Name: "UserRole"}}},
},
},
wantXP: "System.roles[reversed()]/System.UserRole",
},
{
name: "comparison_with_path_and_token",
expr: &ast.BinaryExpr{
Left: &ast.XPathPathExpr{
Steps: []ast.XPathStep{
{Expr: &ast.QualifiedNameExpr{QualifiedName: ast.QualifiedName{Module: "System", Name: "owner"}}},
},
},
Operator: "=",
Right: &ast.TokenExpr{Token: "CurrentUser"},
},
wantXP: "System.owner = '[%CurrentUser%]'",
},
{
name: "not_with_path",
expr: &ast.UnaryExpr{
Operator: "not",
Operand: &ast.XPathPathExpr{
Steps: []ast.XPathStep{
{Expr: &ast.QualifiedNameExpr{QualifiedName: ast.QualifiedName{Module: "Module", Name: "Assoc"}}},
{Expr: &ast.QualifiedNameExpr{QualifiedName: ast.QualifiedName{Module: "Module", Name: "Entity"}}},
},
},
},
wantXP: "not(Module.Assoc/Module.Entity)",
},
{
name: "function_with_path_args",
expr: &ast.FunctionCallExpr{
Name: "contains",
Arguments: []ast.Expression{
&ast.IdentifierExpr{Name: "Name"},
&ast.VariableExpr{Name: "SearchStr"},
},
},
wantXP: "contains(Name, $SearchStr)",
},
{
name: "empty_literal",
expr: &ast.LiteralExpr{Value: nil, Kind: ast.LiteralEmpty},
wantXP: "empty",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotXP := expressionToXPath(tt.expr)
if gotXP != tt.wantXP {
t.Errorf("expressionToXPath() = %q, want %q", gotXP, tt.wantXP)
}
})
}
}
// validateMicroflowFromMDL parses a CREATE MICROFLOW statement and runs
// ValidateMicroflowBody, returning any validation errors.
func validateMicroflowFromMDL(t *testing.T, input string) []string {
t.Helper()
prog, errs := visitor.Build(input)
if len(errs) > 0 {
t.Fatalf("Parse error: %v", errs[0])
}
if len(prog.Statements) == 0 {
t.Fatal("No statements parsed")
}
stmt, ok := prog.Statements[0].(*ast.CreateMicroflowStmt)
if !ok {
t.Fatalf("Expected CreateMicroflowStmt, got %T", prog.Statements[0])
}
return ValidateMicroflowBody(stmt)
}
// TestAssociationNavParsing verifies that $Var/Module.Assoc/Attr parses as
// AttributePathExpr (not nested BinaryExpr with "/" operator).
// Issue #120: extra spaces around path separators.
func TestAssociationNavParsing(t *testing.T) {
input := `create microflow Test.MF_Nav()
returns String as $Result
begin
declare $CustName String = $Order/Test.Order_Customer/Name;
return $CustName;
end;`
prog, errs := visitor.Build(input)
if len(errs) > 0 {
t.Fatalf("Parse error: %v", errs[0])
}
stmt := prog.Statements[0].(*ast.CreateMicroflowStmt)
declStmt := stmt.Body[0].(*ast.DeclareStmt)
// The expression should be an AttributePathExpr, not a BinaryExpr
pathExpr, ok := declStmt.InitialValue.(*ast.AttributePathExpr)
if !ok {
t.Fatalf("Expected AttributePathExpr, got %T", declStmt.InitialValue)
}
if pathExpr.Variable != "Order" {
t.Errorf("Variable = %q, want %q", pathExpr.Variable, "Order")
}
if len(pathExpr.Path) != 2 {
t.Fatalf("Path length = %d, want 2", len(pathExpr.Path))
}
if pathExpr.Path[0] != "Test.Order_Customer" {
t.Errorf("Path[0] = %q, want %q", pathExpr.Path[0], "Test.Order_Customer")
}
if pathExpr.Path[1] != "Name" {
t.Errorf("Path[1] = %q, want %q", pathExpr.Path[1], "Name")
}
// Serialized form should have no extra spaces
got := expressionToString(pathExpr)
want := "$Order/Test.Order_Customer/Name"
if got != want {
t.Errorf("expressionToString() = %q, want %q", got, want)
}
}
// TestResolveAssociationPaths verifies that resolveAssociationPaths inserts
// the target entity after an association segment.
// Issue #120: missing target entity qualifier.
func TestResolveAssociationPaths(t *testing.T) {
tests := []struct {
name string
path []string
want []string
}{
{
name: "simple_attribute",
path: []string{"Name"},
want: []string{"Name"},
},
{
name: "assoc_then_attr",
path: []string{"Test.Order_Customer", "Name"},
want: []string{"Test.Order_Customer", "Test.Customer", "Name"},
},
{
name: "already_has_target_entity",
path: []string{"Test.Order_Customer", "Test.Customer", "Name"},
want: []string{"Test.Order_Customer", "Test.Customer", "Name"},
},
{
name: "assoc_at_end",
path: []string{"Test.Order_Customer"},
want: []string{"Test.Order_Customer"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fb := &flowBuilder{
backend: nil, // nil backend → no resolution, path unchanged
}
got := fb.resolvePathSegments(tt.path)
// With nil reader, all paths should be unchanged
if len(got) != len(tt.path) {
t.Errorf("resolvePathSegments() length = %d, want %d", len(got), len(tt.path))
}
})
}
}
func TestResolveAssociationPathsUnwrapsEmptySourceExpr(t *testing.T) {
fb := &flowBuilder{}
resolved := fb.resolveAssociationPaths(&ast.SourceExpr{
Expression: &ast.VariableExpr{Name: "CurrentObject"},
})
if _, ok := resolved.(*ast.SourceExpr); ok {
t.Fatalf("empty SourceExpr should unwrap to resolved inner expression, got %T", resolved)
}
if got := expressionToString(resolved); got != "$CurrentObject" {
t.Fatalf("resolved expression = %q, want $CurrentObject", got)
}
}
func TestResolveAssociationPathsKeepsNonEmptySourceExprVerbatim(t *testing.T) {
source := "$CurrentObject/Module.Assoc/Name\n"
fb := &flowBuilder{}
resolved := fb.resolveAssociationPaths(&ast.SourceExpr{
Expression: &ast.AttributePathExpr{
Variable: "CurrentObject",
Path: []string{"Module.Assoc", "Name"},
},
Source: source,
})
sourceExpr, ok := resolved.(*ast.SourceExpr)
if !ok {
t.Fatalf("non-empty SourceExpr should remain SourceExpr, got %T", resolved)
}
if sourceExpr.Source != source {
t.Fatalf("source = %q, want %q", sourceExpr.Source, source)
}
}
// TestExprToStringNoSpaces verifies that association navigation expressions
// produce no extra spaces around separators after parsing.
// Issue #120: generated $Order / Module.Assoc / Name instead of $Order/Module.Assoc/Name
func TestExprToStringNoSpaces(t *testing.T) {
tests := []struct {
name string
expr ast.Expression
want string
}{
{
name: "simple_path",
expr: &ast.AttributePathExpr{
Variable: "Order",
Path: []string{"OrderNumber"},
},
want: "$Order/OrderNumber",
},
{
name: "assoc_path",
expr: &ast.AttributePathExpr{
Variable: "Order",
Path: []string{"Test.Order_Customer", "Name"},
},
want: "$Order/Test.Order_Customer/Name",
},
{
name: "multi_segment_path",
expr: &ast.AttributePathExpr{
Variable: "Invoice",
Path: []string{"Billing.Invoice_Order", "Billing.Order_Customer", "Name"},
},
want: "$Invoice/Billing.Invoice_Order/Billing.Order_Customer/Name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := expressionToString(tt.expr)
if got != tt.want {
t.Errorf("expressionToString() = %q, want %q", got, tt.want)
}
})
}
}