1
1
package replication
2
2
3
3
import (
4
+ "encoding/binary"
5
+ "math"
4
6
"testing"
5
7
6
8
"github.com/goccy/go-json"
@@ -169,9 +171,9 @@ func TestJsonBinaryDecoder_decodeDoubleWithTrailingZero(t *testing.T) {
169
171
useFloatWithTrailingZero : true ,
170
172
}
171
173
172
- // Test data representing float64 in binary format ( little endian)
173
- // 5.0 as IEEE 754 double precision: 0x4014000000000000
174
- testData := [] byte { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x14 , 0x40 }
174
+ // Test data representing 5.0 as IEEE 754 double precision in little endian binary format
175
+ testData := make ([] byte , 8 )
176
+ binary . LittleEndian . PutUint64 ( testData , math . Float64bits ( 5.0 ))
175
177
176
178
result := decoder .decodeDoubleWithTrailingZero (testData )
177
179
require .NoError (t , decoder .err )
@@ -190,28 +192,46 @@ func TestJsonBinaryDecoder_decodeValue_JSONB_DOUBLE(t *testing.T) {
190
192
tests := []struct {
191
193
name string
192
194
useFloatWithTrailingZero bool
195
+ value float64
193
196
expectedType interface {}
194
197
expectedJSONString string
195
198
}{
196
199
{
197
- name : "with trailing zero enabled" ,
200
+ name : "positive number with trailing zero enabled" ,
198
201
useFloatWithTrailingZero : true ,
202
+ value : 5.0 ,
199
203
expectedType : FloatWithTrailingZero (0 ),
200
204
expectedJSONString : "5.0" ,
201
205
},
202
206
{
203
- name : "with trailing zero disabled" ,
207
+ name : "positive number with trailing zero disabled" ,
204
208
useFloatWithTrailingZero : false ,
209
+ value : 5.0 ,
205
210
expectedType : float64 (0 ),
206
211
expectedJSONString : "5" ,
207
212
},
213
+ {
214
+ name : "negative zero with trailing zero enabled" ,
215
+ useFloatWithTrailingZero : true ,
216
+ value : math .Copysign (0.0 , - 1 ),
217
+ expectedType : FloatWithTrailingZero (0 ),
218
+ expectedJSONString : "-0.0" ,
219
+ },
220
+ {
221
+ name : "negative zero with trailing zero disabled" ,
222
+ useFloatWithTrailingZero : false ,
223
+ value : math .Copysign (0.0 , - 1 ),
224
+ expectedType : float64 (0 ),
225
+ expectedJSONString : "-0" ,
226
+ },
208
227
}
209
228
210
- // 5.0 as IEEE 754 double precision in little endian
211
- testData := []byte {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x14 , 0x40 }
212
-
213
229
for _ , tt := range tests {
214
230
t .Run (tt .name , func (t * testing.T ) {
231
+ // Test data as IEEE 754 double precision in little endian binary format
232
+ testData := make ([]byte , 8 )
233
+ binary .LittleEndian .PutUint64 (testData , math .Float64bits (tt .value ))
234
+
215
235
decoder := & jsonBinaryDecoder {
216
236
useFloatWithTrailingZero : tt .useFloatWithTrailingZero ,
217
237
}
@@ -228,26 +248,6 @@ func TestJsonBinaryDecoder_decodeValue_JSONB_DOUBLE(t *testing.T) {
228
248
}
229
249
}
230
250
231
- func TestRowsEvent_decodeJsonBinary_WithFloatTrailingZero (t * testing.T ) {
232
- // Create a sample JSON binary data representing {"value": 5.0}
233
- // This is a simplified test - in practice the binary format would be more complex
234
- rowsEvent := & RowsEvent {
235
- useFloatWithTrailingZero : true ,
236
- }
237
-
238
- // Mock a simple JSON binary that would contain a double value
239
- // In a real scenario, this would come from actual MySQL binlog data
240
- // For this test, we'll create a minimal valid structure
241
-
242
- // This test would need actual MySQL JSON binary data to be fully functional
243
- // For now, we'll test the decoding path exists and the option is respected
244
- decoder := & jsonBinaryDecoder {
245
- useFloatWithTrailingZero : rowsEvent .useFloatWithTrailingZero ,
246
- }
247
-
248
- require .True (t , decoder .useFloatWithTrailingZero )
249
- }
250
-
251
251
func TestBinlogParser_SetUseFloatWithTrailingZero (t * testing.T ) {
252
252
parser := NewBinlogParser ()
253
253
@@ -263,14 +263,6 @@ func TestBinlogParser_SetUseFloatWithTrailingZero(t *testing.T) {
263
263
require .False (t , parser .useFloatWithTrailingZero )
264
264
}
265
265
266
- func TestBinlogSyncerConfig_UseFloatWithTrailingZero (t * testing.T ) {
267
- cfg := BinlogSyncerConfig {
268
- UseFloatWithTrailingZero : true ,
269
- }
270
-
271
- require .True (t , cfg .UseFloatWithTrailingZero )
272
- }
273
-
274
266
func TestFloatWithTrailingZero_EdgeCases (t * testing.T ) {
275
267
tests := []struct {
276
268
name string
@@ -292,11 +284,6 @@ func TestFloatWithTrailingZero_EdgeCases(t *testing.T) {
292
284
input : FloatWithTrailingZero (1e6 ),
293
285
expected : "1000000.0" ,
294
286
},
295
- {
296
- name : "negative zero" ,
297
- input : FloatWithTrailingZero (- 0.0 ),
298
- expected : "0.0" ,
299
- },
300
287
{
301
288
name : "number that looks whole but has tiny fractional part" ,
302
289
input : FloatWithTrailingZero (5.0000000000000001 ), // This might be rounded to 5.0 due to float64 precision
@@ -367,20 +354,18 @@ func TestRowsEvent_UseFloatWithTrailingZero_Integration(t *testing.T) {
367
354
err := tableMapEvent .Decode (tableMapEventData )
368
355
require .NoError (t , err )
369
356
357
+ require .Greater (t , tableMapEvent .TableID , uint64 (0 ))
358
+
370
359
// Test with useFloatWithTrailingZero enabled
371
360
rowsWithTrailingZero := & RowsEvent {
372
- tableIDSize : 6 ,
373
361
tables : make (map [uint64 ]* TableMapEvent ),
374
- Version : 2 ,
375
362
useFloatWithTrailingZero : true ,
376
363
}
377
364
rowsWithTrailingZero .tables [tableMapEvent .TableID ] = tableMapEvent
378
365
379
366
// Test with useFloatWithTrailingZero disabled
380
367
rowsWithoutTrailingZero := & RowsEvent {
381
- tableIDSize : 6 ,
382
368
tables : make (map [uint64 ]* TableMapEvent ),
383
- Version : 2 ,
384
369
useFloatWithTrailingZero : false ,
385
370
}
386
371
rowsWithoutTrailingZero .tables [tableMapEvent .TableID ] = tableMapEvent
0 commit comments