-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathchecksum_row_iterator_test.go
205 lines (196 loc) · 8 KB
/
checksum_row_iterator_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package spannerdriver
import (
"bytes"
"encoding/gob"
"math/big"
"testing"
"time"
"cloud.google.com/go/civil"
"cloud.google.com/go/spanner"
)
func TestUpdateChecksum(t *testing.T) {
buffer1 := &bytes.Buffer{}
enc1 := gob.NewEncoder(buffer1)
buffer2 := &bytes.Buffer{}
enc2 := gob.NewEncoder(buffer2)
buffer3 := &bytes.Buffer{}
enc3 := gob.NewEncoder(buffer3)
row1, err := spanner.NewRow(
[]string{
"ColBool", "ColInt64", "ColFloat64", "ColNumeric", "ColString", "ColBytes", "ColDate", "ColTimestamp", "ColJson",
"ArrBool", "ArrInt64", "ArrFloat64", "ArrNumeric", "ArrString", "ArrBytes", "ArrDate", "ArrTimestamp", "ArrJson",
},
[]interface{}{
true, int64(1), 3.14, numeric("6.626"), "test", []byte("testbytes"), civil.Date{Year: 2021, Month: 8, Day: 5},
time.Date(2021, 8, 5, 13, 19, 23, 123456789, time.UTC),
nullJson(true, `"key": "value", "other-key": ["value1", "value2"]}`),
[]bool{true, false}, []int64{1, 2}, []float64{3.14, 6.626}, []big.Rat{numeric("3.14"), numeric("6.626")},
[]string{"test1", "test2"}, [][]byte{[]byte("testbytes1"), []byte("testbytes1")},
[]civil.Date{{Year: 2021, Month: 8, Day: 5}, {Year: 2021, Month: 8, Day: 6}},
[]time.Time{
time.Date(2021, 8, 5, 13, 19, 23, 123456789, time.UTC),
time.Date(2021, 8, 6, 13, 19, 23, 123456789, time.UTC),
},
[]spanner.NullJSON{
nullJson(true, `"key1": "value1", "other-key1": ["value1", "value2"]}`),
nullJson(true, `"key2": "value2", "other-key2": ["value1", "value2"]}`),
},
},
)
if err != nil {
t.Fatalf("could not create row 1: %v", err)
}
// row2 is different from row1
row2, err := spanner.NewRow(
[]string{
"ColBool", "ColInt64", "ColFloat64", "ColNumeric", "ColString", "ColBytes", "ColDate", "ColTimestamp", "ColJson",
"ArrBool", "ArrInt64", "ArrFloat64", "ArrNumeric", "ArrString", "ArrBytes", "ArrDate", "ArrTimestamp", "ArrJson",
},
[]interface{}{
true, int64(2), 6.626, numeric("3.14"), "test2", []byte("testbytes2"), civil.Date{Year: 2020, Month: 8, Day: 5},
time.Date(2020, 8, 5, 13, 19, 23, 123456789, time.UTC),
nullJson(true, `"key": "other-value", "other-key": ["other-value1", "other-value2"]}`),
[]bool{true, false}, []int64{1, 2}, []float64{3.14, 6.626}, []big.Rat{numeric("3.14"), numeric("6.626")},
[]string{"test1_", "test2_"}, [][]byte{[]byte("testbytes1_"), []byte("testbytes1_")},
[]civil.Date{{Year: 2020, Month: 8, Day: 5}, {Year: 2020, Month: 8, Day: 6}},
[]time.Time{
time.Date(2020, 8, 5, 13, 19, 23, 123456789, time.UTC),
time.Date(2020, 8, 6, 13, 19, 23, 123456789, time.UTC),
},
[]spanner.NullJSON{
nullJson(true, `"key1": "other-value1", "other-key1": ["other-value1", "other-value2"]}`),
nullJson(true, `"key2": "other-value2", "other-key2": ["other-value1", "other-value2"]}`),
},
},
)
if err != nil {
t.Fatalf("could not create row 2: %v", err)
}
// row3 is equal to row1.
row3, err := spanner.NewRow(
[]string{
"ColBool", "ColInt64", "ColFloat64", "ColNumeric", "ColString", "ColBytes", "ColDate", "ColTimestamp", "ColJson",
"ArrBool", "ArrInt64", "ArrFloat64", "ArrNumeric", "ArrString", "ArrBytes", "ArrDate", "ArrTimestamp", "ArrJson",
},
[]interface{}{
true, int64(1), 3.14, numeric("6.626"), "test", []byte("testbytes"), civil.Date{Year: 2021, Month: 8, Day: 5},
time.Date(2021, 8, 5, 13, 19, 23, 123456789, time.UTC),
nullJson(true, `"key": "value", "other-key": ["value1", "value2"]}`),
[]bool{true, false}, []int64{1, 2}, []float64{3.14, 6.626}, []big.Rat{numeric("3.14"), numeric("6.626")},
[]string{"test1", "test2"}, [][]byte{[]byte("testbytes1"), []byte("testbytes1")},
[]civil.Date{{Year: 2021, Month: 8, Day: 5}, {Year: 2021, Month: 8, Day: 6}},
[]time.Time{
time.Date(2021, 8, 5, 13, 19, 23, 123456789, time.UTC),
time.Date(2021, 8, 6, 13, 19, 23, 123456789, time.UTC),
},
[]spanner.NullJSON{
nullJson(true, `"key1": "value1", "other-key1": ["value1", "value2"]}`),
nullJson(true, `"key2": "value2", "other-key2": ["value1", "value2"]}`),
},
},
)
if err != nil {
t.Fatalf("could not create row 3: %v", err)
}
initial1 := new([32]byte)
checksum1, err := updateChecksum(enc1, buffer1, initial1, row1)
if err != nil {
t.Fatalf("could not calculate checksum 1: %v", err)
}
initial2 := new([32]byte)
checksum2, err := updateChecksum(enc2, buffer2, initial2, row2)
if err != nil {
t.Fatalf("could not calculate checksum 2: %v", err)
}
initial3 := new([32]byte)
checksum3, err := updateChecksum(enc3, buffer3, initial3, row3)
if err != nil {
t.Fatalf("could not calculate checksum 3: %v", err)
}
// row1 and row2 are different, so the checksums should be different.
if *checksum1 == *checksum2 {
t.Fatalf("checksum1 should not be equal to checksum2")
}
// row1 and row3 are equal, and should return the same checksum.
if *checksum1 != *checksum3 {
t.Fatalf("checksum1 should be equal to checksum3")
}
// Updating checksums 1 and 3 with the data from row 2 should also produce
// the same checksum.
checksum1_2, err := updateChecksum(enc1, buffer1, checksum1, row2)
if err != nil {
t.Fatalf("could not calculate checksum 1_2: %v", err)
}
checksum3_2, err := updateChecksum(enc3, buffer3, checksum3, row2)
if err != nil {
t.Fatalf("could not calculate checksum 1_2: %v", err)
}
if *checksum1_2 != *checksum3_2 {
t.Fatalf("checksum1_2 should be equal to checksum3_2")
}
// The combination of row 3 and 2 will produce a different checksum than the
// combination 2 and 3, because they are in a different order.
checksum2_3, err := updateChecksum(enc2, buffer2, checksum2, row3)
if err != nil {
t.Fatalf("could not calculate checksum 2_3: %v", err)
}
if *checksum2_3 == *checksum3_2 {
t.Fatalf("checksum2_3 should not be equal to checksum3_2")
}
}
func TestUpdateChecksumForNullValues(t *testing.T) {
buffer := &bytes.Buffer{}
enc := gob.NewEncoder(buffer)
row, err := spanner.NewRow(
[]string{
"ColBool", "ColInt64", "ColFloat64", "ColNumeric", "ColString", "ColBytes", "ColDate", "ColTimestamp", "ColJson",
"ArrBool", "ArrInt64", "ArrFloat64", "ArrNumeric", "ArrString", "ArrBytes", "ArrDate", "ArrTimestamp", "ArrJson",
},
[]interface{}{
spanner.NullBool{}, spanner.NullInt64{}, spanner.NullFloat64{}, spanner.NullNumeric{}, spanner.NullString{},
[]byte(nil), spanner.NullDate{}, spanner.NullTime{}, spanner.NullJSON{},
// Note: The following arrays all contain one NULL value.
[]spanner.NullBool{{}}, []spanner.NullInt64{{}}, []spanner.NullFloat64{{}}, []spanner.NullNumeric{{}},
[]spanner.NullString{{}}, [][]byte{[]byte(nil)}, []spanner.NullDate{{}}, []spanner.NullTime{{}},
[]spanner.NullJSON{{}},
},
)
if err != nil {
t.Fatalf("could not create row: %v", err)
}
initial := new([32]byte)
// Create the initial checksum.
checksum, err := updateChecksum(enc, buffer, initial, row)
if err != nil {
t.Fatalf("could not calculate checksum 1: %v", err)
}
// The calculated checksum should not be equal to the initial value, even though it only
// contains null values.
if *checksum == *initial {
t.Fatalf("checksum value should not be equal to the initial value")
}
// Calculating the same checksum again should yield the same result.
buffer2 := &bytes.Buffer{}
enc2 := gob.NewEncoder(buffer2)
initial2 := new([32]byte)
checksum2, err := updateChecksum(enc2, buffer2, initial2, row)
if err != nil {
t.Fatalf("failed to update checksum: %v", err)
}
if *checksum != *checksum2 {
t.Fatalf("recalculated checksum does not match the initial calculation")
}
}