-
Notifications
You must be signed in to change notification settings - Fork 1
/
thing_test.go
79 lines (60 loc) · 1.19 KB
/
thing_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
package surrealdb_go_utils
import (
"testing"
"github.com/goccy/go-json"
)
func TestThing(t *testing.T) {
thing := NewThing("user:one")
if thing.Table() != "user" {
t.Error("Invalid table")
}
if thing.Id() != "one" {
t.Error("Invalid id")
}
if thing.Value() != "user:one" {
t.Error("Invalid value")
}
thing = NewThing("user", "one")
if thing.Table() != "user" {
t.Error("Invalid table")
}
if thing.Id() != "one" {
t.Error("Invalid id")
}
if thing.Value() != "user:one" {
t.Error("Invalid value")
}
}
func TestThingJsonDecoding(t *testing.T) {
type rec struct {
Id *Thing `json:"id"`
}
jsonValue := `{"id":"user:one"}`
var r rec
err := json.Unmarshal([]byte(jsonValue), &r)
if err != nil {
t.Error(err)
}
if r.Id.Table() != "user" {
t.Error("Invalid table")
}
if r.Id.Id() != "one" {
t.Error("Invalid id")
}
if r.Id.Value() != "user:one" {
t.Error("Invalid value")
}
}
func TestThingJsonEncoding(t *testing.T) {
type rec struct {
Id *Thing `json:"id"`
}
value := rec{Id: NewThing("user:one")}
jsonValue, err := json.Marshal(value)
if err != nil {
t.Error(err)
}
if string(jsonValue) != `{"id":"user:one"}` {
t.Error("Invalid json")
}
}