Skip to content

Commit 01c4064

Browse files
committed
fix test to support both python3 and python2
1 parent 33dee84 commit 01c4064

6 files changed

+44
-24
lines changed

_test_go2py.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ def go2py_tostr(arg):
55
return str(arg)
66

77

8+
def go2py_toutf8(arg):
9+
return arg.decode('utf-8')
10+
11+
812
def go2py_mapinmap(arg):
913
ret = arg['string']
1014
ret += '_' + arg['map']['instr']

_test_new_instance.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ def get_instance2(a, b=5, **c):
4747
ins = PythonTest3()
4848
ins.a = a
4949
ins.b = b
50-
ins.c = c
50+
ins.c1 = c['v1']
5151
return ins
5252

5353
def confirm(self):
54-
return str(self.a) + '_' + str(self.b) + '_' + str(self.c)
54+
return str(self.a) + '_' + str(self.b) + '_' + str(self.c1)
5555

5656

5757
class ChildClass(PythonTest3):
@@ -64,7 +64,10 @@ class PythonTestForKwd(object):
6464
def __init__(self, a, b=5, **c):
6565
self.a = a
6666
self.b = b
67-
self.c = c
67+
self.c = c['c'] if 'c' in c else ''
68+
self.d = c['d'] if 'd' in c else ''
69+
self.e = c['e'] if 'e' in c else ''
6870

6971
def confirm_init(self):
70-
return str(self.a) + '_' + str(self.b) + '_' + str(self.c)
72+
return str(self.a) + '_' + str(self.b) + '_' + str(self.c) + '_' + \
73+
str(self.d) + '_' + str(self.e)

func_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package py
22

33
import (
4+
"testing"
5+
46
. "github.com/smartystreets/goconvey/convey"
57
"gopkg.in/sensorbee/py.v0/mainthread"
68
"gopkg.in/sensorbee/sensorbee.v0/data"
7-
"testing"
89
)
910

1011
func TestGetPyFuncError(t *testing.T) {
@@ -105,7 +106,8 @@ func TestPyFunc(t *testing.T) {
105106
Convey("it should return an error.", func() {
106107
So(err, ShouldNotBeNil)
107108
So(ret2.p, ShouldBeNil)
108-
So(err.Error(), ShouldContainSubstring, "__call__() missing 1 required positional argument")
109+
So(err.Error(), ShouldContainSubstring, "TypeError:")
110+
So(err.Error(), ShouldContainSubstring, "argument")
109111
})
110112
})
111113

go2py_converter_test.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package py
22

33
import (
44
"fmt"
5+
"testing"
6+
"time"
7+
58
. "github.com/smartystreets/goconvey/convey"
69
"gopkg.in/sensorbee/py.v0/mainthread"
710
"gopkg.in/sensorbee/sensorbee.v0/data"
8-
"testing"
9-
"time"
1011
)
1112

1213
func TestConvertGo2PyObject(t *testing.T) {
@@ -17,17 +18,15 @@ func TestConvertGo2PyObject(t *testing.T) {
1718
So(err, ShouldBeNil)
1819
So(mdl, ShouldNotBeNil)
1920

20-
type argAndExpected struct {
21-
arg data.Value
22-
expected string
23-
}
24-
2521
Convey("When set an object", func() {
22+
type argAndExpected struct {
23+
arg data.Value
24+
expected string
25+
}
2626
values := map[string]argAndExpected{
2727
"string": argAndExpected{data.String("test"), "test"},
2828
"int": argAndExpected{data.Int(9), "9"},
2929
"float": argAndExpected{data.Float(0.9), "0.9"},
30-
"byte": argAndExpected{data.Blob([]byte("ABC")), "b'ABC'"},
3130
"true": argAndExpected{data.True, "True"},
3231
"false": argAndExpected{data.False, "False"},
3332
"null": argAndExpected{data.Null{}, "None"},
@@ -41,9 +40,11 @@ func TestConvertGo2PyObject(t *testing.T) {
4140
So(actual, ShouldEqual, v.expected)
4241
})
4342
}
43+
})
4444

45-
Convey("Then function should return string value: time", func() {
46-
now := time.Now().UTC()
45+
Convey("When set a time value", func() {
46+
now := time.Now().UTC()
47+
Convey("Then function should return time as string type", func() {
4748
actual, err := mdl.Call("go2py_tostr", data.Timestamp(now))
4849
So(err, ShouldBeNil)
4950
retStr, err := data.AsString(actual)
@@ -54,6 +55,15 @@ func TestConvertGo2PyObject(t *testing.T) {
5455
})
5556
})
5657

58+
Convey("When set a byte array", func() {
59+
b := data.Blob([]byte("ABC"))
60+
Convey("Then function should return string", func() {
61+
actual, err := mdl.Call("go2py_toutf8", b)
62+
So(err, ShouldBeNil)
63+
So(actual, ShouldEqual, "ABC")
64+
})
65+
})
66+
5767
Convey("When set map in map and map in array", func() {
5868
arg := data.Map{
5969
"string": data.String("test"),

module_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package py
22

33
import (
4+
"testing"
5+
46
. "github.com/smartystreets/goconvey/convey"
57
"gopkg.in/sensorbee/py.v0/mainthread"
68
"gopkg.in/sensorbee/sensorbee.v0/data"
7-
"testing"
89
)
910

1011
func TestLoadModuleError(t *testing.T) {
@@ -177,7 +178,7 @@ func TestNewInstanceAndStateness(t *testing.T) {
177178
ins := &ObjectInstance{obj}
178179
actual, err := ins.Call("confirm")
179180
So(err, ShouldBeNil)
180-
So(actual, ShouldEqual, "55_5_{'v1': 'homhom'}")
181+
So(actual, ShouldEqual, "55_5_homhom")
181182
})
182183
})
183184

@@ -249,7 +250,7 @@ func TestNewInstanceWithKeywordArgument(t *testing.T) {
249250

250251
actual, err := ins.Call("confirm_init")
251252
So(err, ShouldBeNil)
252-
So(actual, ShouldBeIn, []string{"1_2_{'c': 3, 'd': 4}", "1_2_{'d': 4, 'c': 3}"})
253+
So(actual, ShouldEqual, "1_2_3_4_")
253254
})
254255
})
255256

@@ -268,14 +269,14 @@ func TestNewInstanceWithKeywordArgument(t *testing.T) {
268269

269270
actual, err := ins.Call("confirm_init")
270271
So(err, ShouldBeNil)
271-
So(actual, ShouldEqual, "1_5_{}")
272+
So(actual, ShouldEqual, "1_5___")
272273
})
273274
})
274275

275276
Convey("When get a new test python instance with named and non-named arguments mixed", func() {
276277
arg := data.Int(1)
277278
kwdArg := data.Map{
278-
"v1": data.String("homhom"),
279+
"e": data.String("homhom"),
279280
}
280281
ins, err := mdl.NewInstance("PythonTestForKwd", []data.Value{arg}, kwdArg)
281282
Reset(func() {
@@ -288,7 +289,7 @@ func TestNewInstanceWithKeywordArgument(t *testing.T) {
288289

289290
actual, err := ins.Call("confirm_init")
290291
So(err, ShouldBeNil)
291-
So(actual, ShouldEqual, "1_5_{'v1': 'homhom'}")
292+
So(actual, ShouldEqual, "1_5___homhom")
292293
})
293294
})
294295

pystate/_test_creator_module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def create(*args, **params):
5151

5252
@staticmethod
5353
def load(filepath, *args, **kwargs):
54-
with open(filepath, 'r') as f:
54+
with open(filepath, 'rb') as f:
5555
return six.moves.cPickle.load(f)
5656

5757
def modify_params(self):
@@ -62,7 +62,7 @@ def confirm(self):
6262
return self.params
6363

6464
def save(self, filepath, *args, **kwargs):
65-
with open(filepath, 'w') as f:
65+
with open(filepath, 'wb') as f:
6666
six.moves.cPickle.dump(self, f)
6767

6868

0 commit comments

Comments
 (0)