-
Notifications
You must be signed in to change notification settings - Fork 2
/
javascript_test.go
70 lines (66 loc) · 1.55 KB
/
javascript_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
package myjson
import (
"context"
"testing"
_ "github.com/autom8ter/myjson/kv/badger"
"github.com/stretchr/testify/assert"
)
func TestJavascript(t *testing.T) {
t.Run("bool expression", func(t *testing.T) {
ctx := context.Background()
db, err := Open(ctx, "badger", nil)
assert.NoError(t, err)
defer db.Close(ctx)
vm, err := getJavascriptVM(ctx, db, map[string]any{})
assert.NoError(t, err)
assert.NotNil(t, vm)
doc := NewDocument()
assert.NoError(t, doc.Set("age", 10))
assert.NoError(t, vm.Set("doc", doc))
v, err := vm.RunString(`doc.Get("age") > 5`)
assert.NoError(t, err)
res := v.Export().(bool)
assert.True(t, res, v.String())
})
t.Run("fetch", func(t *testing.T) {
ctx := context.Background()
db, err := Open(ctx, "badger", nil)
assert.NoError(t, err)
defer db.Close(ctx)
vm, err := getJavascriptVM(ctx, db, map[string]any{})
assert.NoError(t, err)
assert.NotNil(t, vm)
var fetch = `
fetch({
method: "GET",
url: "https://google.com",
query: {
q: "hello world"
}
})
`
val, err := vm.RunString(fetch)
assert.NoError(t, err)
assert.NotNil(t, val)
})
t.Run("fetch error", func(t *testing.T) {
ctx := context.Background()
db, err := Open(ctx, "badger", nil)
assert.NoError(t, err)
defer db.Close(ctx)
vm, err := getJavascriptVM(ctx, db, map[string]any{})
assert.NoError(t, err)
assert.NotNil(t, vm)
var fetch = `
fetch({
method: "GET",
url: "google.com",
query: {
q: "hello world"
}
})
`
_, err = vm.RunString(fetch)
assert.Error(t, err)
})
}