This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk_test.go
88 lines (76 loc) · 1.73 KB
/
sdk_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
package sdk
import (
"fmt"
"testing"
"github.com/c3systems/c3-go/common/txparamcoder"
)
func TestRegisterMethod(t *testing.T) {
t.Parallel()
c3 := NewC3()
err := c3.RegisterMethod("setItem", []string{"string", "string"}, func(key, value string) error {
fmt.Printf("test setItem called with: %s %s", key, value)
return nil
})
if err != nil {
t.Error(err)
}
}
func TestStore(t *testing.T) {
t.Parallel()
c3 := NewC3()
err := c3.State().Set([]byte("foo"), []byte("bar"))
if err != nil {
t.Error(err)
}
value, found := c3.State().Get([]byte("foo"))
if !found {
t.Error("expected value")
}
if string(value) != "bar" {
t.Error("expected match")
}
}
func TestState(t *testing.T) {
t.Parallel()
c3 := NewC3()
err := c3.RegisterMethod("setItem", []string{"string", "string"}, func(key, value string) error {
fmt.Println("test setItem called with:", key, value)
err := c3.State().Set([]byte(key), []byte(value))
if err != nil {
return err
}
return nil
})
if err != nil {
t.Error(err)
}
err = c3.process(txparamcoder.AppendJSONArrays(
txparamcoder.ToJSONArray(
txparamcoder.EncodeMethodName("setItem"),
txparamcoder.EncodeParam("foo"),
txparamcoder.EncodeParam("bar"),
),
txparamcoder.ToJSONArray(
txparamcoder.EncodeMethodName("setItem"),
txparamcoder.EncodeParam("hello"),
txparamcoder.EncodeParam("mars"),
),
))
if err != nil {
t.Error(err)
}
value, found := c3.State().Get([]byte("foo"))
if !found {
t.Error("expected value")
}
if string(value) != "bar" {
t.Errorf("expected match; got %s", value)
}
value, found = c3.State().Get([]byte("hello"))
if !found {
t.Error("expected value")
}
if string(value) != "mars" {
t.Errorf("expected match; got %s", value)
}
}