-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner_test.go
419 lines (369 loc) · 11.9 KB
/
runner_test.go
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
package yabre
import (
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type Product struct {
Ref int `json:"ref"`
Rank int `json:"rank"`
OrderItemRef int `json:"orderItemRef"`
OrderType string `json:"orderType"`
ProductType string `json:"productType"`
Amount int `json:"amount"`
Concentration int `json:"concentration"`
Solvent string `json:"solvent"`
State string `json:"state"`
}
type OrderItem struct {
Ref int `json:"ref"`
Rank int `json:"rank"`
ProductType string `json:"productType"`
Amount int `json:"amount"`
Concentration int `json:"concentration"`
Solvent string `json:"solvent"`
OrderType string `json:"orderType"`
State string `json:"state"`
}
type Container struct {
Amount int `json:"amount"`
}
type RecipeContext struct {
Container Container `json:"container"`
OrderItems []OrderItem `json:"orderItems"`
Products []Product `json:"products"`
}
func TestRunnerGoFunctions(t *testing.T) {
type TestContext struct{}
var debugMessage string
context := TestContext{}
add := func(a, b float64) (interface{}, error) {
return a + b, nil
}
yamlData, err := loadYaml("test/go_rules.yaml")
assert.NoError(t, err)
runner, err := NewRulesRunnerFromYaml(yamlData, &context,
WithDebugCallback[TestContext](
func(data ...interface{}) {
if len(data) > 0 {
debugMessage = fmt.Sprintf("%v", data[0])
}
}),
WithGoFunction[TestContext]("add", add))
assert.NoError(t, err)
_, err = runner.RunRules(&context, nil)
assert.NoError(t, err)
assert.Equal(t, "Go function result: 5.2", debugMessage)
}
func TestRunnerGoFunctionsVariadicArgs(t *testing.T) {
type TestContext struct{}
var debugMessage string
context := TestContext{}
add := func(args ...interface{}) (interface{}, error) {
a := args[0].(float64) // 2.2
b := args[1].(int64) // 3
return a + float64(b), nil
}
yamlData, err := loadYaml("test/go_rules.yaml")
assert.NoError(t, err)
runner, err := NewRulesRunnerFromYaml(yamlData, &context,
WithDebugCallback[TestContext](
func(data ...interface{}) {
if len(data) > 0 {
debugMessage = fmt.Sprintf("%v", data[0])
}
}),
WithGoFunction[TestContext]("add", add))
assert.NoError(t, err)
_, err = runner.RunRules(&context, nil)
assert.NoError(t, err)
assert.Equal(t, "Go function result: 5.2", debugMessage)
}
func TestRunnerUpdateContext(t *testing.T) {
type TestContext struct{ Value string }
context := TestContext{Value: "Initial"}
yamlData, err := loadYaml("test/update_context.yaml")
assert.NoError(t, err)
runner, err := NewRulesRunnerFromYaml(yamlData, &context)
assert.NoError(t, err)
updatedContext, err := runner.RunRules(&context, nil)
assert.NoError(t, err)
assert.Equal(t, "Updated", updatedContext.Value)
}
func TestRunnerAliquoting(t *testing.T) {
// Create a sample context
context := RecipeContext{
Container: Container{Amount: 2100},
OrderItems: []OrderItem{
{Ref: 1, Rank: 1, ProductType: "powder", Amount: 300, Concentration: 10, Solvent: "water", OrderType: "primary", State: "pending"},
{Ref: 2, Rank: 2, ProductType: "solution", Amount: 500, Concentration: 10, Solvent: "water", OrderType: "primary", State: "pending"},
{Ref: 3, Rank: 3, ProductType: "solution", Amount: 300, Concentration: 10, Solvent: "water", OrderType: "early", State: "pending"},
},
Products: []Product{},
}
var debugData interface{}
decisions := []string{}
yamlData, err := loadYaml("test/aliquoting_rules.yaml")
assert.NoError(t, err)
runner, err := NewRulesRunnerFromYaml(
yamlData,
&context,
WithDebugCallback[RecipeContext](
func(data ...interface{}) {
if len(data) > 0 {
debugData = data[0]
}
}),
WithDecisionCallback[RecipeContext](func(msg string, args ...interface{}) {
msg = fmt.Sprintf(msg, args...)
//fmt.Print(msg)
decisions = append(decisions, strings.Trim(strings.TrimLeft(msg, "\t"), " "))
}))
assert.NoError(t, err)
// Run the rules
updatedContext, err := runner.RunRules(&context, nil)
assert.NoError(t, err)
assert.NotNil(t, updatedContext)
// check debug string
debugString, ok := debugData.(string)
assert.True(t, ok)
assert.Equal(t, "create_products check", debugString)
// Check the updated context
assert.Equal(t, 5, len(updatedContext.Products))
assert.Equal(t, 300, updatedContext.Products[0].Amount)
assert.Equal(t, "fail", updatedContext.Products[0].State)
assert.Equal(t, 500, updatedContext.Products[1].Amount)
assert.Equal(t, "pending", updatedContext.Products[1].State)
assert.Equal(t, 300, updatedContext.Products[2].Amount)
assert.Equal(t, "pending", updatedContext.Products[2].State)
assert.Equal(t, 900, updatedContext.Products[3].Amount)
assert.Equal(t, "pending", updatedContext.Products[3].State)
assert.Equal(t, 400, updatedContext.Products[4].Amount)
assert.Equal(t, "pending", updatedContext.Products[4].State)
//p, _ := json.MarshalIndent(updatedContext.Products, "", " ")
//fmt.Printf("Products: %v\n", string(p))
expectedDecisions := []string{
"Evaluating condition: [create_products] Create products for order items in state \"pending\"",
"Condition [create_products] evaluated to [true]",
"Running action: [create_products_true]",
"Moving to next condition:[check_powder_protocols]",
"Evaluating condition: [check_powder_protocols] Check if there are any powder protocols among the products.",
"Condition [check_powder_protocols] evaluated to [true]",
"Running action: [check_powder_protocols_true] Fail all powder products and their corresponding order items.",
"Moving to next condition:[check_mixed_solvents]",
"Evaluating condition: [check_mixed_solvents] Check if there are mixed solvents or concentrations among the solution order items.",
"Condition [check_mixed_solvents] evaluated to [false]",
"Moving to next condition:[check_overflow]",
"Evaluating condition: [check_overflow] Check if the total required amount exceeds the container amount.",
"Condition [check_overflow] evaluated to [false]",
"Moving to next condition:[check_amount_less_than_required]",
"Evaluating condition: [check_amount_less_than_required] Check if the actual amount is less than the required amount.",
"Condition [check_amount_less_than_required] evaluated to [false]",
"Moving to next condition:[check_amount_more_than_required]",
"Evaluating condition: [check_amount_more_than_required] Check if the actual amount is more than the required amount.",
"Condition [check_amount_more_than_required] evaluated to [true]",
"Moving to next condition:[check_remainder_less_than_50]",
"Evaluating condition: [check_remainder_less_than_50] Check if the remainder is less than 50 μl.",
"Condition [check_remainder_less_than_50] evaluated to [false]",
"Moving to next condition:[check_remainder_between_50_and_950]",
"Evaluating condition: [check_remainder_between_50_and_950] Check if the remainder is between 50 μl and 950 μl.",
"Condition [check_remainder_between_50_and_950] evaluated to [false]",
"Moving to next condition:[check_remainder_between_950_and_1800]",
"Evaluating condition: [check_remainder_between_950_and_1800] Check if the remainder is between 950 μl and 1800 μl.",
"Condition [check_remainder_between_950_and_1800] evaluated to [true]",
"Running action: [check_remainder_between_950_and_1800_true] Create two spare tubes, one with 900 μl and another with the remaining amount.",
"Terminating",
}
assert.Equal(t, expectedDecisions, decisions)
}
func TestLoanApproval(t *testing.T) {
// Load the YAML rules
yamlData, err := loadYaml("test/loan_approval.yaml")
assert.NoError(t, err)
// Test case for the happy path
t.Run("HappyPath", func(t *testing.T) {
context := struct {
Applicants []Applicant
LoanAmount int
Decision string
Reason string
}{
Applicants: []Applicant{
{Type: "primary", Age: 25, Income: 5000, Debt: 1000, CreditScore: 750},
{Type: "co-applicant", Age: 30, Income: 4000, Debt: 500, CreditScore: 700},
},
LoanAmount: 40000,
}
runner, err := NewRulesRunnerFromYaml(yamlData, &context)
assert.NoError(t, err)
_, err = runner.RunRules(&context, nil)
assert.NoError(t, err)
assert.Equal(t, "approved", context.Decision)
assert.Empty(t, context.Reason)
})
// Test cases for failed decisions
testCases := []struct {
name string
context LoanContext
expected struct {
decision string
reason string
}
}{
{
name: "MissingPrimaryApplicant",
context: LoanContext{
Applicants: []Applicant{
{Type: "co-applicant", Age: 30, Income: 4000, Debt: 500, CreditScore: 700},
},
},
expected: struct {
decision string
reason string
}{
decision: "rejected",
reason: "No primary applicant",
},
},
{
name: "UnderagePrimaryApplicant",
context: LoanContext{
Applicants: []Applicant{
{Type: "primary", Age: 17, Income: 5000, Debt: 1000, CreditScore: 750},
},
},
expected: struct {
decision string
reason string
}{
decision: "rejected",
reason: "Primary applicant is underage",
},
},
{
name: "InsufficientIncome",
context: LoanContext{
Applicants: []Applicant{
{Type: "primary", Age: 25, Income: 500, Debt: 1000, CreditScore: 750},
},
},
expected: struct {
decision string
reason string
}{
decision: "rejected",
reason: "Insufficient income",
},
},
{
name: "LowCreditScore",
context: LoanContext{
Applicants: []Applicant{
{Type: "primary", Age: 25, Income: 5000, Debt: 1000, CreditScore: 550},
},
},
expected: struct {
decision string
reason string
}{
decision: "rejected",
reason: "Low credit score",
},
},
{
name: "UnderageCoApplicant",
context: LoanContext{
Applicants: []Applicant{
{Type: "primary", Age: 25, Income: 5000, Debt: 1000, CreditScore: 750},
{Type: "co-applicant", Age: 17, Income: 4000, Debt: 500, CreditScore: 700},
},
},
expected: struct {
decision string
reason string
}{
decision: "rejected",
reason: "Co-applicant is underage",
},
},
{
name: "LowCoApplicantCreditScore",
context: LoanContext{
Applicants: []Applicant{
{Type: "primary", Age: 25, Income: 5000, Debt: 1000, CreditScore: 750},
{Type: "co-applicant", Age: 30, Income: 4000, Debt: 500, CreditScore: 550},
},
},
expected: struct {
decision string
reason string
}{
decision: "rejected",
reason: "Co-applicant has low credit score",
},
},
{
name: "HighDebtToIncomeRatio",
context: LoanContext{
Applicants: []Applicant{
{Type: "primary", Age: 25, Income: 5000, Debt: 4000, CreditScore: 750},
},
},
expected: struct {
decision string
reason string
}{
decision: "rejected",
reason: "High debt-to-income ratio",
},
},
{
name: "ExcessiveLoanAmount",
context: LoanContext{
Applicants: []Applicant{
{Type: "primary", Age: 25, Income: 5000, Debt: 1000, CreditScore: 750},
},
LoanAmount: 100000,
},
expected: struct {
decision string
reason string
}{
decision: "rejected",
reason: "Excessive loan amount",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
runner, err := NewRulesRunnerFromYaml(yamlData, &tc.context)
assert.NoError(t, err)
_, err = runner.RunRules(&tc.context, nil)
assert.NoError(t, err)
assert.Equal(t, tc.expected.decision, runner.Context.Decision)
assert.Equal(t, tc.expected.reason, runner.Context.Reason)
})
}
}
type LoanContext struct {
Applicants []Applicant
LoanAmount int
Decision string
Reason string
}
type Applicant struct {
Type string
Age int
Income int
Debt int
CreditScore int
}
func loadYaml(fileName string) ([]byte, error) {
yamlFile, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("error reading YAML file: %v", err)
}
return yamlFile, nil
}