71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package db0404
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestRowEncode(t *testing.T) {
|
|
schema := &Schema{
|
|
Table: "link",
|
|
Cols: []Column{
|
|
{Name: "time", Type: TypeI64},
|
|
{Name: "src", Type: TypeStr},
|
|
{Name: "dst", Type: TypeStr},
|
|
},
|
|
PKey: []int{2, 1}, // (dst, src)
|
|
}
|
|
|
|
row := Row{
|
|
Cell{Type: TypeI64, I64: 123},
|
|
Cell{Type: TypeStr, Str: []byte("a")},
|
|
Cell{Type: TypeStr, Str: []byte("b")},
|
|
}
|
|
key := []byte{'l', 'i', 'n', 'k', 0, 'b', 0, 'a', 0}
|
|
val := []byte{123, 0, 0, 0, 0, 0, 0, 0}
|
|
assert.Equal(t, key, row.EncodeKey(schema))
|
|
assert.Equal(t, val, row.EncodeVal(schema))
|
|
|
|
decoded := schema.NewRow()
|
|
err := decoded.DecodeKey(schema, key)
|
|
assert.Nil(t, err)
|
|
err = decoded.DecodeVal(schema, val)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, row, decoded)
|
|
|
|
rows := []Row{
|
|
{
|
|
Cell{Type: TypeI64, I64: 123},
|
|
Cell{Type: TypeStr, Str: []byte("ba")},
|
|
Cell{Type: TypeStr, Str: []byte("b")},
|
|
},
|
|
{
|
|
Cell{Type: TypeI64, I64: 123},
|
|
Cell{Type: TypeStr, Str: []byte("a")},
|
|
Cell{Type: TypeStr, Str: []byte("bb")},
|
|
},
|
|
{
|
|
Cell{Type: TypeI64, I64: 123},
|
|
Cell{Type: TypeStr, Str: []byte("a")},
|
|
Cell{Type: TypeStr, Str: []byte("bba")},
|
|
},
|
|
}
|
|
keys := []string{}
|
|
for _, row = range rows {
|
|
key = row.EncodeKey(schema)
|
|
keys = append(keys, string(key))
|
|
|
|
decoded = schema.NewRow()
|
|
err = decoded.DecodeKey(schema, key)
|
|
assert.Nil(t, err)
|
|
err = decoded.DecodeVal(schema, val)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, row, decoded)
|
|
}
|
|
assert.True(t, slices.IsSorted(keys))
|
|
}
|
|
|
|
// QzBQWVJJOUhU https://trialofcode.org/
|