github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/state/types_test.go (about) 1 /* 2 * Copyright (c) 2022-present unTill Pro, Ltd. 3 */ 4 5 package state 6 7 import ( 8 "fmt" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/voedger/voedger/pkg/appdef" 14 "github.com/voedger/voedger/pkg/istructs" 15 ) 16 17 func appStructsFunc(app istructs.IAppStructs) AppStructsFunc { 18 return func() istructs.IAppStructs { 19 return app 20 } 21 } 22 23 func TestBundle(t *testing.T) { 24 newKey := func(qname appdef.QName, id istructs.RecordID) (k istructs.IStateKeyBuilder) { 25 k = &viewKeyBuilder{ 26 IKeyBuilder: &keyBuilder{data: make(map[string]interface{})}, 27 view: qname, 28 } 29 k.PutRecordID(Field_ID, id) 30 return 31 } 32 t.Run("put", func(t *testing.T) { 33 b := newBundle() 34 35 b.put(newKey(testRecordQName1, istructs.RecordID(1)), ApplyBatchItem{}) 36 b.put(newKey(testRecordQName1, istructs.RecordID(1)), ApplyBatchItem{}) 37 b.put(newKey(testRecordQName1, istructs.RecordID(2)), ApplyBatchItem{}) 38 b.put(newKey(testRecordQName2, istructs.RecordID(1)), ApplyBatchItem{}) 39 40 require.Equal(t, 3, b.size()) 41 }) 42 t.Run("get", func(t *testing.T) { 43 b := newBundle() 44 b.put(newKey(testRecordQName1, istructs.RecordID(1)), ApplyBatchItem{}) 45 46 tests := []struct { 47 name string 48 key istructs.IStateKeyBuilder 49 want bool 50 }{ 51 { 52 name: "Should be ok", 53 key: newKey(testRecordQName1, istructs.RecordID(1)), 54 want: true, 55 }, 56 { 57 name: "Should be not ok", 58 key: newKey(testRecordQName1, istructs.RecordID(2)), 59 want: false, 60 }, 61 } 62 for _, test := range tests { 63 _, ok := b.get(test.key) 64 65 require.Equal(t, test.want, ok) 66 } 67 }) 68 t.Run("containsKeysForSameView", func(t *testing.T) { 69 require := require.New(t) 70 b := newBundle() 71 72 b.put(newKey(testRecordQName1, istructs.RecordID(1)), ApplyBatchItem{}) 73 b.put(newKey(testRecordQName2, istructs.RecordID(2)), ApplyBatchItem{}) 74 b.put(newKey(testRecordQName2, istructs.RecordID(3)), ApplyBatchItem{}) 75 b.put(newKey(testRecordQName2, istructs.RecordID(4)), ApplyBatchItem{}) 76 b.put(newKey(testRecordQName1, istructs.RecordID(5)), ApplyBatchItem{}) 77 78 require.Equal(5, b.size(), "initial bundle size") 79 80 require.False(b.containsKeysForSameEntity(&viewKeyBuilder{view: testRecordQName3})) 81 82 k := &viewKeyBuilder{view: testRecordQName2} 83 84 require.True(b.containsKeysForSameEntity(k)) 85 require.Equal(5, b.size(), "remain bundle size") 86 }) 87 } 88 89 func TestKeyBuilder(t *testing.T) { 90 require := require.New(t) 91 92 k := newKeyBuilder(testStorage, appdef.NullQName) 93 94 require.Equal(testStorage, k.storage) 95 require.PanicsWithValue(ErrNotSupported, func() { k.PartitionKey() }) 96 require.PanicsWithValue(ErrNotSupported, func() { k.ClusteringColumns() }) 97 } 98 func TestHttpStorageKeyBuilder_headers(t *testing.T) { 99 require := require.New(t) 100 k := newHttpKeyBuilder() 101 k.PutString(Field_Header, "key: hello:world") 102 103 headers := k.headers 104 105 require.Equal("hello:world", headers["key"]) 106 } 107 func TestWLogKeyBuilder(t *testing.T) { 108 t.Run("String", func(t *testing.T) { 109 s := &wLogStorage{ 110 wsidFunc: func() istructs.WSID { return istructs.WSID(42) }, 111 } 112 kb := s.NewKeyBuilder(appdef.NullQName, nil) 113 kb.PutInt64(Field_Count, 10) 114 kb.PutInt64(Field_Offset, 20) 115 kb.PutInt64(Field_WSID, 30) 116 117 require.Equal(t, "wlog wsid - 30, offset - 20, count - 10", kb.(fmt.Stringer).String()) 118 }) 119 }