2022-10-01 21:09:54 +03:00
|
|
|
package meta_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-06-16 14:07:27 +03:00
|
|
|
"git.bit5.ru/backend/meta/v2"
|
2022-10-01 21:09:54 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFieldsMask(t *testing.T) {
|
|
|
|
t.Run("IsFilled", func(t *testing.T) {
|
|
|
|
t.Run("default value", func(t *testing.T) {
|
|
|
|
var mask meta.FieldsMask
|
|
|
|
|
|
|
|
actualIsFilled := mask.IsFilled()
|
|
|
|
require.False(t, actualIsFilled)
|
|
|
|
})
|
|
|
|
t.Run("filled value", func(t *testing.T) {
|
|
|
|
var mask meta.FieldsMask
|
2023-06-06 15:14:33 +03:00
|
|
|
mask.SetPartFromInt64(0, 1)
|
2022-10-01 21:09:54 +03:00
|
|
|
|
|
|
|
actualIsFilled := mask.IsFilled()
|
|
|
|
require.True(t, actualIsFilled)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("FieldChanged", func(t *testing.T) {
|
|
|
|
t.Run("field not changed", func(t *testing.T) {
|
|
|
|
var mask meta.FieldsMask
|
|
|
|
var fieldIndex uint64 = 4
|
|
|
|
|
|
|
|
fieldChanged := mask.FieldChanged(fieldIndex)
|
|
|
|
require.False(t, fieldChanged)
|
|
|
|
})
|
|
|
|
t.Run("filled changed #1", func(t *testing.T) {
|
|
|
|
var mask meta.FieldsMask
|
|
|
|
var fieldIndex uint64 = 4
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
mask.SetPartFromInt64(0, 16)
|
2022-10-01 21:09:54 +03:00
|
|
|
|
|
|
|
fieldChanged := mask.FieldChanged(fieldIndex)
|
|
|
|
require.True(t, fieldChanged)
|
|
|
|
})
|
|
|
|
t.Run("filled changed #2", func(t *testing.T) {
|
|
|
|
var mask meta.FieldsMask
|
|
|
|
var fieldIndex uint64 = 68
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
mask.SetPartFromInt64(1, 16)
|
2022-10-01 21:09:54 +03:00
|
|
|
|
|
|
|
fieldChanged := mask.FieldChanged(fieldIndex)
|
|
|
|
require.True(t, fieldChanged)
|
|
|
|
})
|
|
|
|
t.Run("filled changed #3", func(t *testing.T) {
|
|
|
|
var mask meta.FieldsMask
|
|
|
|
var fieldIndex uint64 = 131
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
mask.SetPartFromInt64(2, 8)
|
2022-10-01 21:09:54 +03:00
|
|
|
|
|
|
|
fieldChanged := mask.FieldChanged(fieldIndex)
|
|
|
|
require.True(t, fieldChanged)
|
|
|
|
})
|
|
|
|
t.Run("filled changed #4", func(t *testing.T) {
|
|
|
|
var mask meta.FieldsMask
|
|
|
|
var fieldIndex uint64 = 194
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
mask.SetPartFromInt64(3, 4)
|
2022-10-01 21:09:54 +03:00
|
|
|
|
|
|
|
fieldChanged := mask.FieldChanged(fieldIndex)
|
|
|
|
require.True(t, fieldChanged)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFieldsMaskItem(t *testing.T) {
|
|
|
|
t.Run("FieldIsDirty", func(t *testing.T) {
|
|
|
|
cases := []struct {
|
2023-06-06 15:14:33 +03:00
|
|
|
maskItem meta.FieldsMaskPart
|
2022-10-01 21:09:54 +03:00
|
|
|
expectedDirtyIndexes []uint64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
maskItem: 0,
|
|
|
|
expectedDirtyIndexes: []uint64{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
maskItem: 1, // 0b0001
|
|
|
|
expectedDirtyIndexes: []uint64{0},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
maskItem: 2, // 0b0010
|
|
|
|
expectedDirtyIndexes: []uint64{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
maskItem: 10, // 0b1010
|
|
|
|
expectedDirtyIndexes: []uint64{1, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
maskItem: 11, // 0b1011
|
|
|
|
expectedDirtyIndexes: []uint64{0, 1, 3},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cases {
|
2023-06-06 15:14:33 +03:00
|
|
|
actualDirtyIndexes := make([]uint64, 0, meta.FieldsMaskPartBitSize)
|
|
|
|
for j := uint64(0); j < meta.FieldsMaskPartBitSize; j++ {
|
2022-10-01 21:09:54 +03:00
|
|
|
if c.maskItem.FieldIsDirty(j) {
|
|
|
|
actualDirtyIndexes = append(actualDirtyIndexes, j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equalf(t, c.expectedDirtyIndexes, actualDirtyIndexes, "case #%d", i)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|