github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/table/tables/memory_tables_test.go (about)

     1  // Copyright 2016 PingCAP, 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 tables_test
    15  
    16  import (
    17  	. "github.com/insionng/yougam/libraries/pingcap/check"
    18  	"github.com/insionng/yougam/libraries/pingcap/tidb"
    19  	"github.com/insionng/yougam/libraries/pingcap/tidb/column"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/context"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/kv"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/meta/autoid"
    23  	"github.com/insionng/yougam/libraries/pingcap/tidb/model"
    24  	"github.com/insionng/yougam/libraries/pingcap/tidb/mysql"
    25  	"github.com/insionng/yougam/libraries/pingcap/tidb/store/localstore"
    26  	"github.com/insionng/yougam/libraries/pingcap/tidb/store/localstore/goleveldb"
    27  	"github.com/insionng/yougam/libraries/pingcap/tidb/table"
    28  	"github.com/insionng/yougam/libraries/pingcap/tidb/table/tables"
    29  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    30  )
    31  
    32  var _ = Suite(&testMemoryTableSuite{})
    33  
    34  type testMemoryTableSuite struct {
    35  	store kv.Storage
    36  	se    tidb.Session
    37  	tbl   table.Table
    38  }
    39  
    40  func (ts *testMemoryTableSuite) SetUpSuite(c *C) {
    41  	driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
    42  	store, err := driver.Open("memory")
    43  	c.Check(err, IsNil)
    44  	ts.store = store
    45  	ts.se, err = tidb.CreateSession(ts.store)
    46  	c.Assert(err, IsNil)
    47  
    48  	// create table
    49  	tp1 := types.NewFieldType(mysql.TypeLong)
    50  	col1 := &model.ColumnInfo{
    51  		ID:        1,
    52  		Name:      model.NewCIStr("a"),
    53  		Offset:    0,
    54  		FieldType: *tp1,
    55  	}
    56  	tp2 := types.NewFieldType(mysql.TypeVarchar)
    57  	tp2.Flen = 255
    58  	col2 := &model.ColumnInfo{
    59  		ID:        2,
    60  		Name:      model.NewCIStr("b"),
    61  		Offset:    1,
    62  		FieldType: *tp2,
    63  	}
    64  
    65  	tblInfo := &model.TableInfo{
    66  		ID:      100,
    67  		Name:    model.NewCIStr("t"),
    68  		Columns: []*model.ColumnInfo{col1, col2},
    69  	}
    70  	alloc := autoid.NewMemoryAllocator(int64(10))
    71  	ts.tbl, _ = tables.MemoryTableFromMeta(alloc, tblInfo)
    72  }
    73  
    74  func (ts *testMemoryTableSuite) TestMemoryBasic(c *C) {
    75  	ctx := ts.se.(context.Context)
    76  	tb := ts.tbl
    77  	c.Assert(tb.Meta(), NotNil)
    78  	c.Assert(tb.Meta().ID, Greater, int64(0))
    79  	c.Assert(tb.Meta().Name.L, Equals, "t")
    80  	c.Assert(tb.Indices(), IsNil)
    81  	c.Assert(string(tb.FirstKey()), Not(Equals), "")
    82  	c.Assert(string(tb.RecordPrefix()), Not(Equals), "")
    83  
    84  	autoid, err := tb.AllocAutoID()
    85  	c.Assert(err, IsNil)
    86  	c.Assert(autoid, Greater, int64(0))
    87  
    88  	rid, err := tb.AddRecord(ctx, types.MakeDatums(1, "abc"))
    89  	c.Assert(err, IsNil)
    90  	row, err := tb.Row(ctx, rid)
    91  	c.Assert(err, IsNil)
    92  	c.Assert(len(row), Equals, 2)
    93  	c.Assert(row[0].GetInt64(), Equals, int64(1))
    94  
    95  	_, err = tb.AddRecord(ctx, types.MakeDatums(1, "aba"))
    96  	c.Assert(err, IsNil)
    97  	_, err = tb.AddRecord(ctx, types.MakeDatums(2, "abc"))
    98  	c.Assert(err, IsNil)
    99  
   100  	tb.IterRecords(ctx, tb.FirstKey(), tb.Cols(), func(h int64, data []types.Datum, cols []*column.Col) (bool, error) {
   101  		return true, nil
   102  	})
   103  
   104  	// RowWithCols test
   105  	vals, err := tb.RowWithCols(ctx, rid, tb.Cols())
   106  	c.Assert(err, IsNil)
   107  	c.Assert(vals, HasLen, 2)
   108  	c.Assert(vals[0].GetInt64(), Equals, int64(1))
   109  	cols := []*column.Col{tb.Cols()[1]}
   110  	vals, err = tb.RowWithCols(ctx, rid, cols)
   111  	c.Assert(err, IsNil)
   112  	c.Assert(vals, HasLen, 1)
   113  	c.Assert(vals[0].GetString(), Equals, "abc")
   114  
   115  	c.Assert(tb.RemoveRecord(ctx, rid, types.MakeDatums(1, "cba")), IsNil)
   116  	_, err = tb.AddRecord(ctx, types.MakeDatums(1, "abc"))
   117  	c.Assert(err, IsNil)
   118  	c.Assert(tb.Truncate(ctx), IsNil)
   119  	_, err = tb.Row(ctx, rid)
   120  	c.Assert(err, NotNil)
   121  }