github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/ekv/mock_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package ekv
    15  
    16  import (
    17  	"context"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  )
    21  
    22  var _ = Suite(testMockSuite{})
    23  
    24  type testMockSuite struct {
    25  }
    26  
    27  func (s testMockSuite) TestInterface(c *C) {
    28  	storage := newMockStorage()
    29  	storage.GetClient()
    30  	storage.UUID()
    31  	version, err := storage.CurrentVersion()
    32  	c.Check(err, IsNil)
    33  	snapshot, err := storage.GetSnapshot(version)
    34  	c.Check(err, IsNil)
    35  	_, err = snapshot.BatchGet(context.Background(), []Key{Key("abc"), Key("def")})
    36  	c.Check(err, IsNil)
    37  	snapshot.SetOption(Priority, PriorityNormal)
    38  
    39  	transaction, err := storage.Begin()
    40  	c.Check(err, IsNil)
    41  	err = transaction.LockKeys(context.Background(), new(LockCtx), Key("dagger"))
    42  	c.Check(err, IsNil)
    43  	transaction.SetOption(Option(23), struct{}{})
    44  	if mock, ok := transaction.(*mockTxn); ok {
    45  		mock.GetOption(Option(23))
    46  	}
    47  	transaction.StartTS()
    48  	transaction.DelOption(Option(23))
    49  	if transaction.IsReadOnly() {
    50  		_, err = transaction.Get(context.TODO(), Key("dagger"))
    51  		c.Check(err, IsNil)
    52  		err = transaction.Set(Key("dagger"), []byte{})
    53  		c.Check(err, IsNil)
    54  		_, err = transaction.Iter(Key("dagger"), nil)
    55  		c.Check(err, IsNil)
    56  		_, err = transaction.IterReverse(Key("dagger"))
    57  		c.Check(err, IsNil)
    58  	}
    59  	transaction.Commit(context.Background())
    60  
    61  	transaction, err = storage.Begin()
    62  	c.Check(err, IsNil)
    63  
    64  	// Test for mockTxn interface.
    65  	c.Assert(transaction.String(), Equals, "")
    66  	c.Assert(transaction.Valid(), Equals, true)
    67  	c.Assert(transaction.Len(), Equals, 0)
    68  	c.Assert(transaction.Size(), Equals, 0)
    69  	c.Assert(transaction.GetMemBuffer(), IsNil)
    70  	transaction.Reset()
    71  	err = transaction.Rollback()
    72  	c.Check(err, IsNil)
    73  	c.Assert(transaction.Valid(), Equals, false)
    74  	c.Assert(transaction.IsPessimistic(), Equals, false)
    75  	c.Assert(transaction.Delete(nil), IsNil)
    76  
    77  	// Test for mockStorage interface.
    78  	c.Assert(storage.GetOracle(), IsNil)
    79  	c.Assert(storage.Name(), Equals, "KVMockStorage")
    80  	c.Assert(storage.Describe(), Equals, "KVMockStorage is a mock CausetStore implementation, only for unittests in KV package")
    81  	c.Assert(storage.SupportDeleteRange(), IsFalse)
    82  
    83  	status, err := storage.ShowStatus(nil, "")
    84  	c.Assert(status, IsNil)
    85  	c.Assert(err, IsNil)
    86  
    87  	err = storage.Close()
    88  	c.Check(err, IsNil)
    89  }
    90  
    91  func (s testMockSuite) TestIsPoint(c *C) {
    92  	kr := KeyRange{
    93  		StartKey: Key("rowkey1"),
    94  		EndKey:   Key("rowkey2"),
    95  	}
    96  	c.Check(kr.IsPoint(), IsTrue)
    97  
    98  	kr.EndKey = Key("rowkey3")
    99  	c.Check(kr.IsPoint(), IsFalse)
   100  
   101  	kr = KeyRange{
   102  		StartKey: Key(""),
   103  		EndKey:   []byte{0},
   104  	}
   105  	c.Check(kr.IsPoint(), IsTrue)
   106  }