-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
89 lines (75 loc) · 1.73 KB
/
json_test.go
File metadata and controls
89 lines (75 loc) · 1.73 KB
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
package spannertest
import (
"cloud.google.com/go/spanner"
"context"
"encoding/json"
"fmt"
"log"
"math/rand"
"reflect"
"testing"
"time"
)
func TestIntegration_Json(t *testing.T) {
rand.Seed(time.Now().UnixNano())
tableName := "JsonTestTable"
client, adminClient, _, cleanup := makeClient(t)
defer cleanup()
if err := updateDDL(t, adminClient,
`CREATE TABLE `+tableName+` (
Name STRING(50) NOT NULL,
jv JSON,
) PRIMARY KEY (Name)`); err != nil {
t.Fatal(err)
}
inJson := Metadata{
Creators: []Creators{
{
Address: fmt.Sprintf("creator_%v", rand.Int()),
},
},
}
rowKey := fmt.Sprintf("rowKey_%v", rand.Int())
m := spanner.InsertOrUpdate(tableName,
[]string{"Name", "jv"},
[]interface{}{rowKey, spanner.NullJSON{Value: inJson, Valid: true}})
if _, err := client.Apply(context.Background(), []*spanner.Mutation{m}); err != nil {
t.Fatal(err)
}
row, err := client.Single().ReadRow(context.Background(), tableName, spanner.Key{rowKey}, []string{"jv"})
if err != nil {
t.Fatal(err)
}
{
var outStr string
if err := row.Columns(&outStr); err == nil {
t.Fatal("should not be able to decode json to string")
}
}
{
out := &spanner.NullJSON{}
if err := row.Columns(out); err != nil {
t.Fatal(err)
}
outJson := &Metadata{}
if err := json.Unmarshal([]byte(out.String()), outJson); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(toJson(inJson), toJson(outJson)) {
t.Fatalf("wanted %v, but got %v", inJson, outJson)
}
}
}
func toJson(value interface{}) string {
out, err := json.Marshal(value)
if err != nil {
log.Fatal(err)
}
return string(out)
}
type Metadata struct {
Creators []Creators `json:"creators"`
}
type Creators struct {
Address string `json:"address"`
}