stardb/converter_test.go
2026-03-07 19:27:44 +08:00

184 lines
4.1 KiB
Go

package stardb
import (
"testing"
"time"
)
func TestConvertToInt64(t *testing.T) {
tests := []struct {
name string
input interface{}
expected int64
}{
{"nil", nil, 0},
{"int", 42, 42},
{"int32", int32(42), 42},
{"int64", int64(42), 42},
{"uint64", uint64(42), 42},
{"float32", float32(42.7), 42},
{"float64", float64(42.7), 42},
{"string", "42", 42},
{"bool true", true, 1},
{"bool false", false, 0},
{"bytes", []byte("42"), 42},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := convertToInt64(tt.input)
if result != tt.expected {
t.Errorf("Expected %d, got %d", tt.expected, result)
}
})
}
}
func TestConvertToUint64(t *testing.T) {
tests := []struct {
name string
input interface{}
expected uint64
}{
{"nil", nil, 0},
{"int", 42, 42},
{"int32", int32(42), 42},
{"int64", int64(42), 42},
{"uint64", uint64(42), 42},
{"float32", float32(42.7), 42},
{"float64", float64(42.7), 42},
{"string", "42", 42},
{"bool true", true, 1},
{"bool false", false, 0},
{"bytes", []byte("42"), 42},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := convertToUint64(tt.input)
if result != tt.expected {
t.Errorf("Expected %d, got %d", tt.expected, result)
}
})
}
}
func TestConvertToFloat64(t *testing.T) {
tests := []struct {
name string
input interface{}
expected float64
}{
{"nil", nil, 0.0},
{"int", 42, 42.0},
{"int32", int32(42), 42.0},
{"int64", int64(42), 42.0},
{"uint64", uint64(42), 42.0},
{"float32", float32(42.5), 42.5},
{"float64", float64(42.5), 42.5},
{"string", "42.5", 42.5},
{"bool true", true, 1.0},
{"bool false", false, 0.0},
{"bytes", []byte("42.5"), 42.5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := convertToFloat64(tt.input)
if result != tt.expected {
t.Errorf("Expected %f, got %f", tt.expected, result)
}
})
}
}
func TestConvertToBool(t *testing.T) {
tests := []struct {
name string
input interface{}
expected bool
}{
{"nil", nil, false},
{"bool true", true, true},
{"bool false", false, false},
{"int positive", 1, true},
{"int zero", 0, false},
{"int negative", -1, true},
{"float positive", 1.5, true},
{"float zero", 0.0, false},
{"string true", "true", true},
{"string false", "false", false},
{"string 1", "1", true},
{"string 0", "0", false},
{"bytes true", []byte("true"), true},
{"bytes false", []byte("false"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := convertToBool(tt.input)
if result != tt.expected {
t.Errorf("Expected %v, got %v", tt.expected, result)
}
})
}
}
func TestConvertToString(t *testing.T) {
now := time.Now()
tests := []struct {
name string
input interface{}
expected string
}{
{"nil", nil, ""},
{"string", "hello", "hello"},
{"int", 42, "42"},
{"int32", int32(42), "42"},
{"int64", int64(42), "42"},
{"float32", float32(42.5), "42.5"},
{"float64", float64(42.5), "42.5"},
{"bool true", true, "true"},
{"bool false", false, "false"},
{"time", now, now.String()},
{"bytes", []byte("hello"), "hello"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := convertToString(tt.input)
if result != tt.expected {
t.Errorf("Expected '%s', got '%s'", tt.expected, result)
}
})
}
}
func TestConvertToTime(t *testing.T) {
layout := "2006-01-02 15:04:05"
timeStr := "2024-01-01 10:00:00"
expectedTime, _ := time.Parse(layout, timeStr)
tests := []struct {
name string
input interface{}
layout string
expected time.Time
}{
{"nil", nil, layout, time.Time{}},
{"time.Time", expectedTime, layout, expectedTime},
{"int64 unix", int64(1704103200), layout, time.Unix(1704103200, 0)},
{"string", timeStr, layout, expectedTime},
{"bytes", []byte(timeStr), layout, expectedTime},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := convertToTime(tt.input, tt.layout)
if !result.Equal(tt.expected) {
t.Errorf("Expected %v, got %v", tt.expected, result)
}
})
}
}