MCPcopy Create free account
hub / github.com/PostHog/duckgres / TestEncodeDecodeInterval

Function TestEncodeDecodeInterval

server/types_test.go:1284–1358  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1282}
1283
1284func TestEncodeDecodeInterval(t *testing.T) {
1285 tests := []struct {
1286 name string
1287 input duckdb.Interval
1288 expectStr string
1289 }{
1290 {
1291 "1 day",
1292 duckdb.Interval{Days: 1, Months: 0, Micros: 0},
1293 "1 day",
1294 },
1295 {
1296 "1 month",
1297 duckdb.Interval{Days: 0, Months: 1, Micros: 0},
1298 "1 month",
1299 },
1300 {
1301 "1 year 2 months 3 days",
1302 duckdb.Interval{Days: 3, Months: 14, Micros: 0},
1303 "1 year 2 month 3 day",
1304 },
1305 {
1306 "1 hour",
1307 duckdb.Interval{Days: 0, Months: 0, Micros: 3600000000},
1308 "01:00:00",
1309 },
1310 {
1311 "complex",
1312 duckdb.Interval{Days: 5, Months: 3, Micros: 7200000000 + 1800000000}, // 2h30m
1313 "3 month 5 day 02:30:00",
1314 },
1315 }
1316
1317 for _, tt := range tests {
1318 t.Run(tt.name, func(t *testing.T) {
1319 encoded := encodeInterval(tt.input)
1320 if encoded == nil {
1321 t.Fatalf("encodeInterval(%v) returned nil", tt.input)
1322 }
1323 if len(encoded) != 16 {
1324 t.Fatalf("encodeInterval(%v) length = %d, want 16", tt.input, len(encoded))
1325 }
1326
1327 // Verify binary layout
1328 gotMicros := int64(binary.BigEndian.Uint64(encoded[0:8]))
1329 gotDays := int32(binary.BigEndian.Uint32(encoded[8:12]))
1330 gotMonths := int32(binary.BigEndian.Uint32(encoded[12:16]))
1331 if gotMicros != tt.input.Micros {
1332 t.Errorf("micros = %d, want %d", gotMicros, tt.input.Micros)
1333 }
1334 if gotDays != tt.input.Days {
1335 t.Errorf("days = %d, want %d", gotDays, tt.input.Days)
1336 }
1337 if gotMonths != tt.input.Months {
1338 t.Errorf("months = %d, want %d", gotMonths, tt.input.Months)
1339 }
1340
1341 decoded, err := decodeInterval(encoded)

Callers

nothing calls this directly

Calls 4

encodeIntervalFunction · 0.85
decodeIntervalFunction · 0.85
RunMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected