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

     1  // Copyright 2015 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 model
    15  
    16  import (
    17  	"testing"
    18  
    19  	. "github.com/insionng/yougam/libraries/pingcap/check"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    21  )
    22  
    23  func TestT(t *testing.T) {
    24  	TestingT(t)
    25  }
    26  
    27  var _ = Suite(&testSuite{})
    28  
    29  type testSuite struct {
    30  }
    31  
    32  func (*testSuite) TestT(c *C) {
    33  	abc := NewCIStr("aBC")
    34  	c.Assert(abc.O, Equals, "aBC")
    35  	c.Assert(abc.L, Equals, "abc")
    36  	c.Assert(abc.String(), Equals, "aBC")
    37  }
    38  
    39  func (*testSuite) TestClone(c *C) {
    40  	column := &ColumnInfo{
    41  		ID:           1,
    42  		Name:         NewCIStr("c"),
    43  		Offset:       0,
    44  		DefaultValue: 0,
    45  		FieldType:    *types.NewFieldType(0),
    46  	}
    47  
    48  	index := &IndexInfo{
    49  		Name:  NewCIStr("key"),
    50  		Table: NewCIStr("t"),
    51  		Columns: []*IndexColumn{
    52  			{
    53  				Name:   NewCIStr("c"),
    54  				Offset: 0,
    55  				Length: 10,
    56  			}},
    57  		Unique:  true,
    58  		Primary: true,
    59  	}
    60  
    61  	table := &TableInfo{
    62  		ID:          1,
    63  		Name:        NewCIStr("t"),
    64  		Charset:     "utf8",
    65  		Collate:     "utf8",
    66  		Columns:     []*ColumnInfo{column},
    67  		Indices:     []*IndexInfo{index},
    68  		ForeignKeys: []*FKInfo{},
    69  	}
    70  
    71  	dbInfo := &DBInfo{
    72  		ID:      1,
    73  		Name:    NewCIStr("test"),
    74  		Charset: "utf8",
    75  		Collate: "utf8",
    76  		Tables:  []*TableInfo{table},
    77  	}
    78  
    79  	n := dbInfo.Clone()
    80  	c.Assert(n, DeepEquals, dbInfo)
    81  }
    82  
    83  func (*testSuite) TestJobCodec(c *C) {
    84  	type A struct {
    85  		Name string
    86  	}
    87  	job := &Job{
    88  		ID:   1,
    89  		Args: []interface{}{NewCIStr("a"), A{Name: "abc"}},
    90  	}
    91  
    92  	b, err := job.Encode()
    93  	c.Assert(err, IsNil)
    94  
    95  	newJob := &Job{}
    96  	err = newJob.Decode(b)
    97  	c.Assert(err, IsNil)
    98  
    99  	name := CIStr{}
   100  	a := A{}
   101  	err = newJob.DecodeArgs(&name, &a)
   102  	c.Assert(err, IsNil)
   103  	c.Assert(name, DeepEquals, NewCIStr("a"))
   104  	c.Assert(a, DeepEquals, A{Name: "abc"})
   105  
   106  	c.Assert(len(newJob.String()), Greater, 0)
   107  
   108  	job.State = JobDone
   109  	c.Assert(job.IsFinished(), IsTrue)
   110  	c.Assert(job.IsRunning(), IsFalse)
   111  }
   112  
   113  func (testSuite) TestState(c *C) {
   114  	schemaTbl := []SchemaState{
   115  		StateDeleteOnly,
   116  		StateWriteOnly,
   117  		StateWriteReorganization,
   118  		StateDeleteReorganization,
   119  		StatePublic,
   120  	}
   121  
   122  	for _, state := range schemaTbl {
   123  		c.Assert(len(state.String()), Greater, 0)
   124  	}
   125  
   126  	jobTbl := []JobState{
   127  		JobRunning,
   128  		JobDone,
   129  		JobCancelled,
   130  	}
   131  
   132  	for _, state := range jobTbl {
   133  		c.Assert(len(state.String()), Greater, 0)
   134  	}
   135  
   136  	actionTbl := []ActionType{
   137  		ActionCreateSchema,
   138  		ActionDropSchema,
   139  		ActionCreateTable,
   140  		ActionDropTable,
   141  		ActionAddColumn,
   142  		ActionDropColumn,
   143  		ActionAddIndex,
   144  		ActionDropIndex,
   145  	}
   146  
   147  	for _, action := range actionTbl {
   148  		c.Assert(len(action.String()), Greater, 0)
   149  	}
   150  }