-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext_test.go
More file actions
62 lines (55 loc) · 1.45 KB
/
context_test.go
File metadata and controls
62 lines (55 loc) · 1.45 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
package probe
import (
"testing"
)
func TestResponseTimeExpressions(t *testing.T) {
// Create a step context with the new RT structure
ctx := StepContext{
RT: ResponseTime{
Duration: "250ms",
Sec: 0.25,
},
Vars: map[string]any{
"threshold": 0.3,
},
}
expr := &Expr{}
t.Run("access rt.duration", func(t *testing.T) {
result, err := expr.Eval(`rt.duration`, ctx)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result != "250ms" {
t.Errorf("Expected rt.duration to be '250ms', got %v", result)
}
})
t.Run("access rt.sec", func(t *testing.T) {
result, err := expr.Eval(`rt.sec`, ctx)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result != 0.25 {
t.Errorf("Expected rt.sec to be 0.25, got %v", result)
}
})
t.Run("compare rt.sec with threshold", func(t *testing.T) {
result, err := expr.Eval(`rt.sec < vars.threshold`, ctx)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if result != true {
t.Errorf("Expected rt.sec < threshold to be true, got %v", result)
}
})
t.Run("use rt in template", func(t *testing.T) {
template := "Response took {{ rt.duration }} ({{ rt.sec }}s)"
result, err := expr.EvalTemplate(template, ctx)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
expected := "Response took 250ms (0.25s)"
if result != expected {
t.Errorf("Expected template result to be '%s', got '%s'", expected, result)
}
})
}