github.com/turingchain2020/turingchain@v1.1.21/common/db/go_pegasus_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package db 6 7 import ( 8 "context" 9 "testing" 10 "time" 11 12 "github.com/XiaoMi/pegasus-go-client/pegasus" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/mock" 15 ) 16 17 func TestPegasusDB_Get(t *testing.T) { 18 key := []byte("my_key") 19 key1 := []byte("my_key1") 20 val := []byte("my_value") 21 22 db := new(PegasusDB) 23 tbl := new(MyTable) 24 tbl.On("Get", context.Background(), getHashKey(key), key).Return(val, nil) 25 tbl.On("Get", context.Background(), getHashKey(key1), key1).Return(nil, nil) 26 db.table = tbl 27 28 data, err := db.Get(key) 29 assert.Nil(t, err) 30 assert.Equal(t, data, val) 31 32 data, err = db.Get(key1) 33 assert.Nil(t, data) 34 assert.EqualError(t, err, ErrNotFoundInDb.Error()) 35 tbl.AssertExpectations(t) 36 } 37 38 type MyTable struct { 39 pegasus.TableConnector 40 mock.Mock 41 } 42 43 func (tbl *MyTable) Get(ctx context.Context, hashKey []byte, sortKey []byte) ([]byte, error) { 44 args := tbl.Called(ctx, hashKey, sortKey) 45 if args.Get(0) == nil { 46 return nil, args.Error(1) 47 } 48 return args.Get(0).([]byte), args.Error(1) 49 } 50 func (tbl *MyTable) SetTTL(ctx context.Context, hashKey []byte, sortKey []byte, value []byte, ttl time.Duration) error { 51 return nil 52 } 53 func (tbl *MyTable) Del(ctx context.Context, hashKey []byte, sortKey []byte) error { return nil } 54 55 func (tbl *MyTable) MultiGet(ctx context.Context, hashKey []byte, sortKeys [][]byte) ([]*pegasus.KeyValue, bool, error) { 56 args := tbl.Called(ctx, hashKey, sortKeys) 57 return args.Get(0).([]*pegasus.KeyValue), args.Bool(1), args.Error(2) 58 } 59 func (tbl *MyTable) MultiGetOpt(ctx context.Context, hashKey []byte, sortKeys [][]byte, options *pegasus.MultiGetOptions) ([]*pegasus.KeyValue, bool, error) { 60 return nil, false, nil 61 } 62 func (tbl *MyTable) MultiGetRange(ctx context.Context, hashKey []byte, startSortKey []byte, stopSortKey []byte) ([]*pegasus.KeyValue, bool, error) { 63 return nil, false, nil 64 } 65 func (tbl *MyTable) MultiGetRangeOpt(ctx context.Context, hashKey []byte, startSortKey []byte, stopSortKey []byte, options *pegasus.MultiGetOptions) ([]*pegasus.KeyValue, bool, error) { 66 return nil, false, nil 67 } 68 func (tbl *MyTable) MultiSet(ctx context.Context, hashKey []byte, sortKeys [][]byte, values [][]byte) error { 69 return nil 70 } 71 func (tbl *MyTable) MultiSetOpt(ctx context.Context, hashKey []byte, sortKeys [][]byte, values [][]byte, ttl time.Duration) error { 72 return nil 73 } 74 func (tbl *MyTable) MultiDel(ctx context.Context, hashKey []byte, sortKeys [][]byte) error { 75 return nil 76 } 77 func (tbl *MyTable) TTL(ctx context.Context, hashKey []byte, sortKey []byte) (int, error) { 78 return 0, nil 79 } 80 func (tbl *MyTable) Exist(ctx context.Context, hashKey []byte, sortKey []byte) (bool, error) { 81 return false, nil 82 } 83 func (tbl *MyTable) GetScanner(ctx context.Context, hashKey []byte, startSortKey []byte, stopSortKey []byte, options *pegasus.ScannerOptions) (pegasus.Scanner, error) { 84 return nil, nil 85 } 86 func (tbl *MyTable) GetUnorderedScanners(ctx context.Context, maxSplitCount int, options *pegasus.ScannerOptions) ([]pegasus.Scanner, error) { 87 return nil, nil 88 } 89 func (tbl *MyTable) CheckAndSet(ctx context.Context, hashKey []byte, checkSortKey []byte, checkType pegasus.CheckType, 90 checkOperand []byte, setSortKey []byte, setValue []byte, options *pegasus.CheckAndSetOptions) (*pegasus.CheckAndSetResult, error) { 91 return nil, nil 92 } 93 func (tbl *MyTable) SortKeyCount(ctx context.Context, hashKey []byte) (int64, error) { return 0, nil } 94 func (tbl *MyTable) Close() error { return nil }