11package replication
22
33import (
4+ "encoding/binary"
5+ "math"
46 "testing"
57
68 "github.com/goccy/go-json"
@@ -169,9 +171,9 @@ func TestJsonBinaryDecoder_decodeDoubleWithTrailingZero(t *testing.T) {
169171 useFloatWithTrailingZero : true ,
170172 }
171173
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 ))
175177
176178 result := decoder .decodeDoubleWithTrailingZero (testData )
177179 require .NoError (t , decoder .err )
@@ -190,28 +192,46 @@ func TestJsonBinaryDecoder_decodeValue_JSONB_DOUBLE(t *testing.T) {
190192 tests := []struct {
191193 name string
192194 useFloatWithTrailingZero bool
195+ value float64
193196 expectedType interface {}
194197 expectedJSONString string
195198 }{
196199 {
197- name : "with trailing zero enabled" ,
200+ name : "positive number with trailing zero enabled" ,
198201 useFloatWithTrailingZero : true ,
202+ value : 5.0 ,
199203 expectedType : FloatWithTrailingZero (0 ),
200204 expectedJSONString : "5.0" ,
201205 },
202206 {
203- name : "with trailing zero disabled" ,
207+ name : "positive number with trailing zero disabled" ,
204208 useFloatWithTrailingZero : false ,
209+ value : 5.0 ,
205210 expectedType : float64 (0 ),
206211 expectedJSONString : "5" ,
207212 },
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+ },
208227 }
209228
210- // 5.0 as IEEE 754 double precision in little endian
211- testData := []byte {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x14 , 0x40 }
212-
213229 for _ , tt := range tests {
214230 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+
215235 decoder := & jsonBinaryDecoder {
216236 useFloatWithTrailingZero : tt .useFloatWithTrailingZero ,
217237 }
@@ -228,26 +248,6 @@ func TestJsonBinaryDecoder_decodeValue_JSONB_DOUBLE(t *testing.T) {
228248 }
229249}
230250
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-
251251func TestBinlogParser_SetUseFloatWithTrailingZero (t * testing.T ) {
252252 parser := NewBinlogParser ()
253253
@@ -263,14 +263,6 @@ func TestBinlogParser_SetUseFloatWithTrailingZero(t *testing.T) {
263263 require .False (t , parser .useFloatWithTrailingZero )
264264}
265265
266- func TestBinlogSyncerConfig_UseFloatWithTrailingZero (t * testing.T ) {
267- cfg := BinlogSyncerConfig {
268- UseFloatWithTrailingZero : true ,
269- }
270-
271- require .True (t , cfg .UseFloatWithTrailingZero )
272- }
273-
274266func TestFloatWithTrailingZero_EdgeCases (t * testing.T ) {
275267 tests := []struct {
276268 name string
@@ -292,11 +284,6 @@ func TestFloatWithTrailingZero_EdgeCases(t *testing.T) {
292284 input : FloatWithTrailingZero (1e6 ),
293285 expected : "1000000.0" ,
294286 },
295- {
296- name : "negative zero" ,
297- input : FloatWithTrailingZero (- 0.0 ),
298- expected : "0.0" ,
299- },
300287 {
301288 name : "number that looks whole but has tiny fractional part" ,
302289 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) {
367354 err := tableMapEvent .Decode (tableMapEventData )
368355 require .NoError (t , err )
369356
357+ require .Greater (t , tableMapEvent .TableID , uint64 (0 ))
358+
370359 // Test with useFloatWithTrailingZero enabled
371360 rowsWithTrailingZero := & RowsEvent {
372- tableIDSize : 6 ,
373361 tables : make (map [uint64 ]* TableMapEvent ),
374- Version : 2 ,
375362 useFloatWithTrailingZero : true ,
376363 }
377364 rowsWithTrailingZero .tables [tableMapEvent .TableID ] = tableMapEvent
378365
379366 // Test with useFloatWithTrailingZero disabled
380367 rowsWithoutTrailingZero := & RowsEvent {
381- tableIDSize : 6 ,
382368 tables : make (map [uint64 ]* TableMapEvent ),
383- Version : 2 ,
384369 useFloatWithTrailingZero : false ,
385370 }
386371 rowsWithoutTrailingZero .tables [tableMapEvent .TableID ] = tableMapEvent
0 commit comments