-
Notifications
You must be signed in to change notification settings - Fork 35
/
context_test.go
108 lines (92 loc) · 2.53 KB
/
context_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
package endly_test
import (
"github.com/stretchr/testify/assert"
"github.com/viant/endly"
"github.com/viant/endly/model/location"
"github.com/viant/toolbox"
"strings"
"sync/atomic"
"testing"
)
func TestNewDefaultState(t *testing.T) {
state := endly.NewDefaultState(nil)
for _, expr := range []string{"$rand", "${env.HOME}", "$time", "$ts", "$tmpDir", "$uuid.get", "$uuid.next", "$timestamp.now", "$timestamp.tomorrow", "$timestamp.yesterday"} {
var expanded = state.ExpandAsText(expr)
assert.False(t, strings.Contains(expanded, expr))
assert.True(t, len(expr) > 0)
}
var expanded = state.ExpandAsText("${tmpDir.subdir}")
assert.Contains(t, expanded, "/subdir")
}
func TestContext_Expand_Resource(t *testing.T) {
manager := endly.New()
context := manager.NewContext(toolbox.NewContext())
_, err := context.ExpandResource(nil)
assert.NotNil(t, err)
_, err = context.ExpandResource(&location.Resource{})
assert.NotNil(t, err)
{
state := context.State()
state.Put("host", "127.0.0.1")
expanded, err := context.ExpandResource(location.NewResource("scp://${host}/as"))
if assert.Nil(t, err) {
assert.EqualValues(t, "scp://127.0.0.1/as", expanded.URL)
}
}
{
_, err := context.ExpandResource(location.NewResource("path"))
assert.Nil(t, err)
}
}
func TestContext_NewRequest(t *testing.T) {
manager := endly.New()
context := manager.NewContext(toolbox.NewContext())
{
request, err := context.NewRequest("nop", "nop", map[string]interface{}{})
if assert.Nil(t, err) {
assert.NotNil(t, request)
}
}
{
_, err := context.NewRequest("invalid", "nop", nil)
assert.NotNil(t, err)
}
{
_, err := context.NewRequest("nop", "abc", nil)
assert.NotNil(t, err)
}
}
func TestContext_AsRequest(t *testing.T) {
manager := endly.New()
context := manager.NewContext(toolbox.NewContext())
{
request, err := context.AsRequest("nop", "nop", map[string]interface{}{})
if assert.Nil(t, err) {
assert.NotNil(t, request)
}
}
{
_, err := context.AsRequest("zzz", "nop", map[string]interface{}{})
assert.NotNil(t, err)
}
}
func TestContext_Clone(t *testing.T) {
manager := endly.New()
{
context := manager.NewContext(toolbox.NewContext())
cloned := context.Clone()
assert.NotNil(t, cloned)
}
{
var deferredRun int32 = 0
context := manager.NewContext(toolbox.NewContext())
context.Deffer(func() {
atomic.AddInt32(&deferredRun, 1)
})
cloned := context.Clone()
assert.False(t, cloned.IsClosed())
context.Close()
assert.True(t, cloned.IsClosed())
assert.EqualValues(t, int32(2), deferredRun)
}
}