-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutation_upsert_test.go
122 lines (110 loc) · 4.73 KB
/
mutation_upsert_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
package spnr
import (
"cloud.google.com/go/civil"
"cloud.google.com/go/spanner"
"context"
"github.com/stretchr/testify/assert"
"math/big"
"testing"
"time"
)
var testRecord3 = &Test{
String: "testId1",
Bytes: []byte{1},
Int64: 10,
Float64: 84.217403,
Numeric: *big.NewRat(17893, 8473),
Date: civil.DateOf(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)),
Timestamp: time.Date(2100, 1, 1, 0, 0, 0, 0, time.UTC),
NullString: NewNullString("a"),
NullInt64: NewNullInt64(100),
NullNumeric: NewNullNumeric(53, 10384),
ArrayInt64: []int64{1, 2, 3},
ArrayBytes: [][]byte{{80}, {90}},
}
var testRecord4 = &Test{
String: "testId2",
Bytes: []byte{1},
Int64: 10,
Float64: 84.217403,
Numeric: *big.NewRat(17893, 8473),
Date: civil.DateOf(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)),
Timestamp: time.Date(2100, 1, 1, 0, 0, 0, 0, time.UTC),
NullString: NewNullString("a"),
NullInt64: NewNullInt64(100),
NullNumeric: NewNullNumeric(53, 10384),
ArrayInt64: []int64{1, 2, 3},
ArrayBytes: [][]byte{{80}, {90}},
}
func TestMutation_InsertOrUpdate(t *testing.T) {
ctx := context.Background()
_, err := testRepository.ApplyInsertOrUpdate(ctx, dataClient, testRecord3)
assert.Nil(t, err)
var fetched Test
err = testRepository.Reader(ctx, dataClient.Single()).FindOne(spanner.Key{testRecord3.String, testRecord3.Int64}, &fetched)
assert.Nil(t, err)
assert.Equal(t, testRecord3.String, fetched.String)
assert.Equal(t, testRecord3.Bytes, fetched.Bytes)
assert.Equal(t, testRecord3.Int64, fetched.Int64)
assert.Equal(t, testRecord3.Float64, fetched.Float64)
assert.Equal(t, testRecord3.Date, fetched.Date)
assert.Equal(t, testRecord3.Timestamp, fetched.Timestamp)
assert.Equal(t, testRecord3.NullString, fetched.NullString)
assert.Equal(t, testRecord3.NullInt64, fetched.NullInt64)
assert.Equal(t, testRecord3.ArrayInt64, fetched.ArrayInt64)
assert.Equal(t, testRecord3.ArrayBytes, fetched.ArrayBytes)
// clean up
_, err = testRepository.ApplyDelete(ctx, dataClient, testRecord3)
assert.Nil(t, err)
}
func TestMutation_InsertOrUpdateWithSlice(t *testing.T) {
ctx := context.Background()
_, err := testRepository.ApplyInsertOrUpdate(ctx, dataClient, &([]*Test{testRecord3, testRecord4}))
assert.Nil(t, err)
var fetched Test
err = testRepository.Reader(ctx, dataClient.Single()).FindOne(spanner.Key{testRecord3.String, testRecord3.Int64}, &fetched)
assert.Nil(t, err)
assert.Equal(t, testRecord3.String, fetched.String)
assert.Equal(t, testRecord3.Bytes, fetched.Bytes)
assert.Equal(t, testRecord3.Int64, fetched.Int64)
assert.Equal(t, testRecord3.Float64, fetched.Float64)
assert.Equal(t, testRecord3.Date, fetched.Date)
assert.Equal(t, testRecord3.Timestamp, fetched.Timestamp)
assert.Equal(t, testRecord3.NullString, fetched.NullString)
assert.Equal(t, testRecord3.NullInt64, fetched.NullInt64)
assert.Equal(t, testRecord3.ArrayInt64, fetched.ArrayInt64)
assert.Equal(t, testRecord3.ArrayBytes, fetched.ArrayBytes)
err = testRepository.Reader(ctx, dataClient.Single()).FindOne(spanner.Key{testRecord4.String, testRecord4.Int64}, &fetched)
assert.Nil(t, err)
assert.Equal(t, testRecord4.String, fetched.String)
assert.Equal(t, testRecord4.Bytes, fetched.Bytes)
assert.Equal(t, testRecord4.Int64, fetched.Int64)
assert.Equal(t, testRecord4.Float64, fetched.Float64)
assert.Equal(t, testRecord4.Date, fetched.Date)
assert.Equal(t, testRecord4.Timestamp, fetched.Timestamp)
assert.Equal(t, testRecord4.NullString, fetched.NullString)
assert.Equal(t, testRecord4.NullInt64, fetched.NullInt64)
assert.Equal(t, testRecord4.ArrayInt64, fetched.ArrayInt64)
assert.Equal(t, testRecord4.ArrayBytes, fetched.ArrayBytes)
testRecord5 := *testRecord3
testRecord6 := *testRecord4
testRecord5.Bytes = testRecord6.Bytes
testRecord6.Bytes = testRecord5.Bytes
_, err = testRepository.ApplyInsertOrUpdate(ctx, dataClient, &([]Test{testRecord5, testRecord6}))
assert.Nil(t, err)
err = testRepository.Reader(ctx, dataClient.Single()).FindOne(spanner.Key{testRecord5.String, testRecord5.Int64}, &fetched)
assert.Nil(t, err)
assert.Equal(t, testRecord5.String, fetched.String)
assert.Equal(t, testRecord5.Int64, fetched.Int64)
assert.Equal(t, testRecord6.Bytes, fetched.Bytes)
assert.Equal(t, testRecord5.Float64, fetched.Float64)
err = testRepository.Reader(ctx, dataClient.Single()).FindOne(spanner.Key{testRecord6.String, testRecord6.Int64}, &fetched)
assert.Nil(t, err)
assert.Equal(t, testRecord6.String, fetched.String)
assert.Equal(t, testRecord6.Int64, fetched.Int64)
assert.Equal(t, testRecord5.Bytes, fetched.Bytes)
assert.Equal(t, testRecord6.Float64, fetched.Float64)
// clean up
_, err = testRepository.ApplyDelete(ctx, dataClient, &([]*Test{testRecord3, testRecord4}))
assert.Nil(t, err)
}