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