-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstate_test.go
127 lines (119 loc) · 3.24 KB
/
state_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
package pymlstate
import (
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/sensorbee/py.v0/pystate"
"gopkg.in/sensorbee/sensorbee.v0/core"
"gopkg.in/sensorbee/sensorbee.v0/data"
"testing"
)
func TestPyMLStateFitAndPredict(t *testing.T) {
cc := &core.ContextConfig{}
ctx := core.NewContext(cc)
Convey("Given a context set pymlstate for fit/predict test", t, func() {
baseParams := &pystate.BaseParams{
ModulePath: "./",
ModuleName: "_test_pymlstate",
ClassName: "TestClass",
}
mlParams := &MLParams{
BatchSize: 10,
}
s, err := New(baseParams, mlParams, data.Map{})
So(err, ShouldBeNil)
Reset(func() {
s.Terminate(ctx)
})
err = ctx.SharedStates.Add("pystate_test", "py", s)
So(err, ShouldBeNil)
Convey("When call fit", func() {
bu := []data.Value{
data.String("a"), data.String("b"),
}
ac, err := Fit(ctx, "pystate_test", bu)
So(err, ShouldBeNil)
Convey("Then fit function should be called", func() {
So(ac, ShouldEqual, "fit called")
Convey("And when call predict", func() {
ac2, err := Predict(ctx, "pystate_test", data.String("c"))
So(err, ShouldBeNil)
Convey("Then predict function should be called", func() {
So(ac2, ShouldEqual, "predict called")
})
})
})
})
})
}
func TestPyMLStateWrite(t *testing.T) {
cc := &core.ContextConfig{}
ctx := core.NewContext(cc)
Convey("Given a context set pymlstate for write test", t, func() {
baseParams := &pystate.BaseParams{
ModulePath: "./",
ModuleName: "_test_pymlstate",
ClassName: "TestClass",
}
mlParams := &MLParams{
BatchSize: 3,
}
s, err := New(baseParams, mlParams, data.Map{})
So(err, ShouldBeNil)
Reset(func() {
s.Terminate(ctx)
})
err = ctx.SharedStates.Add("pystate_test", "py", s)
So(err, ShouldBeNil)
Convey("When write a data", func() {
dt := data.String("1")
tu := &core.Tuple{
Data: data.Map{
"data": dt,
},
}
err := s.Write(ctx, tu)
So(err, ShouldBeNil)
Convey("Then fit function should not be called", func() {
ac, err := s.base.Call("confirm_to_call_fit")
So(err, ShouldBeNil)
So(ac, ShouldEqual, 0)
So(len(s.bucket), ShouldEqual, 1)
Convey("And when write data until bucket size", func() {
tu2 := tu.Copy()
err := s.Write(ctx, tu2)
So(err, ShouldBeNil)
tu3 := tu.Copy()
err = s.Write(ctx, tu3)
So(err, ShouldBeNil)
Convey("Then fit function should be called and bucket is flushed", func() {
ac2, err := s.base.Call("confirm_to_call_fit")
So(err, ShouldBeNil)
So(ac2, ShouldEqual, 1)
So(len(s.bucket), ShouldEqual, 0)
})
})
})
})
})
}
func TestPyMLStateFlush(t *testing.T) {
Convey("Given a context set dummy state", t, func() {
bu := []data.Value{data.String("a"), data.String("b")}
cc := &core.ContextConfig{}
ctx := core.NewContext(cc)
s := &State{
bucket: bu,
}
stateName := "test_state_for_flush"
err := ctx.SharedStates.Add(stateName, stateName, s)
So(err, ShouldBeNil)
So(len(s.bucket), ShouldEqual, 2)
Convey("When call flush", func() {
ac, err := Flush(ctx, stateName)
So(ac, ShouldBeNil)
So(err, ShouldBeNil)
Convey("Then state bucket should be empty", func() {
So(len(s.bucket), ShouldEqual, 0)
})
})
})
}