Skip to content

Commit 9d46fb3

Browse files
committed
add test
1 parent 731ed05 commit 9d46fb3

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

replication/binlogsyncer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type BinlogSyncerConfig struct {
7676
// Use decimal.Decimal structure for decimals.
7777
UseDecimal bool
7878

79-
// Use FloatWithTrailingZero structure for floats.
79+
// FloatWithTrailingZero structure for floats.
8080
UseFloatWithTrailingZero bool
8181

8282
// RecvBufferSize sets the size in bytes of the operating system's receive buffer associated with the connection.

replication/replication_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stretchr/testify/require"
1515
"github.com/stretchr/testify/suite"
1616

17+
"github.com/goccy/go-json"
1718
"github.com/go-mysql-org/go-mysql/client"
1819
"github.com/go-mysql-org/go-mysql/mysql"
1920
"github.com/go-mysql-org/go-mysql/test_util"
@@ -464,3 +465,80 @@ func (t *testSyncerSuite) TestMysqlBinlogCodec() {
464465
require.NoError(t.T(), err)
465466
}
466467
}
468+
469+
func (t *testSyncerSuite) TestFloatWithTrailingZeros() {
470+
t.setupTest(mysql.MySQLFlavor)
471+
472+
str := `DROP TABLE IF EXISTS test_float_zeros`
473+
t.testExecute(str)
474+
475+
// Create table with JSON column containing float values
476+
str = `CREATE TABLE test_float_zeros (
477+
id INT PRIMARY KEY,
478+
json_val JSON
479+
)`
480+
t.testExecute(str)
481+
482+
// Test with useFloatWithTrailingZero = true
483+
t.b.cfg.UseFloatWithTrailingZero = true
484+
t.testFloatWithTrailingZerosCase(true)
485+
486+
// Test with useFloatWithTrailingZero = false
487+
t.b.cfg.UseFloatWithTrailingZero = false
488+
t.testFloatWithTrailingZerosCase(false)
489+
}
490+
491+
func (t *testSyncerSuite) testFloatWithTrailingZerosCase(useTrailingZero bool) {
492+
// Insert values with trailing zeros in JSON
493+
t.testExecute(`INSERT INTO test_float_zeros VALUES (1, '{"f": 5.1}')`)
494+
t.testExecute(`INSERT INTO test_float_zeros VALUES (2, '{"f": 1.100}')`)
495+
496+
// Get current position
497+
r, err := t.c.Execute("SHOW MASTER STATUS")
498+
require.NoError(t.T(), err)
499+
binFile, _ := r.GetString(0, 0)
500+
binPos, _ := r.GetInt(0, 1)
501+
502+
// Start syncing from current position
503+
s, err := t.b.StartSync(mysql.Position{Name: binFile, Pos: uint32(binPos)})
504+
require.NoError(t.T(), err)
505+
506+
// Insert another row to trigger binlog events
507+
t.testExecute(`INSERT INTO test_float_zeros VALUES (3, '{"f": 3.0}')`)
508+
509+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
510+
defer cancel()
511+
512+
for {
513+
evt, err := s.GetEvent(ctx)
514+
require.NoError(t.T(), err)
515+
516+
// We're interested in RowsEvent
517+
if evt.Header.EventType != WRITE_ROWS_EVENTv2 {
518+
continue
519+
}
520+
521+
// Type assert to RowsEvent
522+
rowsEvent := evt.Event.(*RowsEvent)
523+
for _, row := range rowsEvent.Rows {
524+
// The third row should contain our test values
525+
if row[0].(int32) == 3 {
526+
// Get the JSON value from binlog
527+
jsonVal := row[1].([]byte)
528+
var data struct {
529+
F float64 `json:"f"`
530+
}
531+
err := json.Unmarshal(jsonVal, &data)
532+
require.NoError(t.T(), err)
533+
534+
// Check if trailing zero is preserved based on useFloatWithTrailingZero
535+
if useTrailingZero {
536+
require.Equal(t.T(), "3.0", fmt.Sprintf("%.1f", data.F))
537+
} else {
538+
require.Equal(t.T(), "3", fmt.Sprintf("%.1f", data.F))
539+
}
540+
return
541+
}
542+
}
543+
}
544+
}

0 commit comments

Comments
 (0)