本文整理汇总了Golang中github.com/pingcap/tidb/util/mock.NewContext函数的典型用法代码示例。如果您正苦于以下问题:Golang NewContext函数的具体用法?Golang NewContext怎么用?Golang NewContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestStat
func (s *testStatSuite) TestStat(c *C) {
defer testleak.AfterTest(c)()
store := testCreateStore(c, "test_stat")
defer store.Close()
lease := 50 * time.Millisecond
d := newDDL(store, nil, nil, lease)
defer d.close()
time.Sleep(lease)
dbInfo := testSchemaInfo(c, d, "test")
testCreateSchema(c, mock.NewContext(), d, dbInfo)
m, err := d.Stats()
c.Assert(err, IsNil)
c.Assert(m[ddlOwnerID], Equals, d.uuid)
job := &model.Job{
SchemaID: dbInfo.ID,
Type: model.ActionDropSchema,
Args: []interface{}{dbInfo.Name},
}
ctx := mock.NewContext()
done := make(chan error, 1)
go func() {
done <- d.doDDLJob(ctx, job)
}()
ticker := time.NewTicker(d.lease * 1)
defer ticker.Stop()
ver := s.getDDLSchemaVer(c, d)
LOOP:
for {
select {
case <-ticker.C:
d.close()
c.Assert(s.getDDLSchemaVer(c, d), GreaterEqual, ver)
d.start()
case err := <-done:
c.Assert(err, IsNil)
m, err := d.Stats()
c.Assert(err, IsNil)
c.Assert(m[bgOwnerID], Equals, d.uuid)
break LOOP
}
}
}
示例2: TestVersion
func (s *testEvaluatorSuite) TestVersion(c *C) {
defer testleak.AfterTest(c)()
ctx := mock.NewContext()
v, err := builtinVersion(nil, ctx)
c.Assert(err, IsNil)
c.Assert(v.GetString(), Equals, mysql.ServerVersion)
}
示例3: TestT
func (*testSuite) TestT(c *C) {
driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
store, err := driver.Open("memory")
c.Assert(err, IsNil)
defer store.Close()
ctx := mock.NewContext()
dom, err := NewDomain(store, 0)
c.Assert(err, IsNil)
store = dom.Store()
dd := dom.DDL()
c.Assert(dd, NotNil)
err = dd.CreateSchema(ctx, model.NewCIStr("aaa"))
c.Assert(err, IsNil)
is := dom.InfoSchema()
c.Assert(is, NotNil)
dom, err = NewDomain(store, 0)
c.Assert(err, IsNil)
dom.SetLease(10 * time.Second)
m, err := dom.Stats()
c.Assert(err, IsNil)
c.Assert(m[ddlLastReloadSchemaTS], GreaterEqual, int64(0))
dom.SetLease(50 * time.Millisecond)
store.Close()
time.Sleep(1 * time.Second)
}
示例4: TestSleep
func (s *testEvaluatorSuite) TestSleep(c *C) {
defer testleak.AfterTest(c)()
ctx := mock.NewContext()
sessVars := ctx.GetSessionVars()
// non-strict model
sessVars.StrictSQLMode = false
d := make([]types.Datum, 1)
ret, err := builtinSleep(d, ctx)
c.Assert(err, IsNil)
c.Assert(ret, DeepEquals, types.NewIntDatum(0))
d[0].SetInt64(-1)
ret, err = builtinSleep(d, ctx)
c.Assert(err, IsNil)
c.Assert(ret, DeepEquals, types.NewIntDatum(0))
// for error case under the strict model
sessVars.StrictSQLMode = true
d[0].SetNull()
_, err = builtinSleep(d, ctx)
c.Assert(err, NotNil)
d[0].SetFloat64(-2.5)
_, err = builtinSleep(d, ctx)
c.Assert(err, NotNil)
// strict model
d[0].SetFloat64(0.5)
start := time.Now()
ret, err = builtinSleep(d, ctx)
c.Assert(err, IsNil)
c.Assert(ret, DeepEquals, types.NewIntDatum(0))
sub := time.Since(start)
c.Assert(sub.Nanoseconds(), GreaterEqual, int64(0.5*1e9))
}
示例5: TestDistinct
func (t *testDistinctSuit) TestDistinct(c *C) {
tblPlan := &testTablePlan{distinctTestData, []string{"id", "name"}, 0}
p := plans.DistinctDefaultPlan{
SelectList: &plans.SelectList{
HiddenFieldOffset: len(tblPlan.GetFields()),
},
Src: tblPlan,
}
rset := rsets.Recordset{
Plan: &p,
Ctx: mock.NewContext(),
}
r := map[int][]interface{}{}
err := rset.Do(func(data []interface{}) (bool, error) {
r[data[0].(int)] = data
return true, nil
})
c.Assert(err, IsNil)
expected := map[int][]interface{}{
10: {10, "hello"},
40: {40, "hello"},
60: {60, "hello"},
}
c.Assert(reflect.DeepEqual(r, expected), Equals, true)
}
示例6: TestCount
func (s *testAggFuncSuite) TestCount(c *C) {
// Compose aggregate exec for "select c1, count(c2) from t";
// c1 c2
// 1 1
// 2 1
// 3 nil
c1 := ast.NewValueExpr(0)
rf1 := &ast.ResultField{Expr: c1}
col1 := &ast.ColumnNameExpr{Refer: rf1}
fc1 := &ast.AggregateFuncExpr{
F: ast.AggFuncFirstRow,
Args: []ast.ExprNode{col1},
}
c2 := ast.NewValueExpr(0)
rf2 := &ast.ResultField{Expr: c2}
col2 := &ast.ColumnNameExpr{Refer: rf2}
fc2 := &ast.AggregateFuncExpr{
F: ast.AggFuncCount,
Args: []ast.ExprNode{col2},
}
row1 := []interface{}{1, 1}
row2 := []interface{}{2, 1}
row3 := []interface{}{3, nil}
data := []([]interface{}){row1, row2, row3}
rows := make([]*Row, 0, 3)
for _, d := range data {
rows = append(rows, &Row{Data: d})
}
src := &mockExec{
rows: rows,
fields: []*ast.ResultField{rf1, rf2},
}
agg := &AggregateExec{
AggFuncs: []*ast.AggregateFuncExpr{fc1, fc2},
Src: src,
}
var (
row *Row
cnt int
)
for {
r, err := agg.Next()
c.Assert(err, IsNil)
if r == nil {
break
}
row = r
cnt++
}
c.Assert(cnt, Equals, 1)
c.Assert(row, NotNil)
ctx := mock.NewContext()
val, err := evaluator.Eval(ctx, fc1)
c.Assert(err, IsNil)
c.Assert(val, Equals, 1)
val, err = evaluator.Eval(ctx, fc2)
c.Assert(err, IsNil)
c.Assert(val, Equals, int64(2))
}
示例7: TestRegexp
func (s *testEvaluatorSuite) TestRegexp(c *C) {
tbl := []struct {
pattern string
input string
match int64
}{
{"^$", "a", 0},
{"a", "a", 1},
{"a", "b", 0},
{"aA", "aA", 1},
{".", "a", 1},
{"^.$", "ab", 0},
{"..", "b", 0},
{".ab", "aab", 1},
{".*", "abcd", 1},
}
ctx := mock.NewContext()
for _, v := range tbl {
pattern := &ast.PatternRegexpExpr{
Pattern: ast.NewValueExpr(v.pattern),
Expr: ast.NewValueExpr(v.input),
}
match, err := Eval(ctx, pattern)
c.Assert(err, IsNil)
c.Assert(match, Equals, v.match, Commentf("%v", v))
}
}
示例8: TestSchema
func (s *testSchemaSuite) TestSchema(c *C) {
store := testCreateStore(c, "test_schema")
defer store.Close()
lease := 100 * time.Millisecond
d1 := newDDL(store, nil, nil, lease)
defer d1.close()
ctx := mock.NewContext()
dbInfo := testSchemaInfo(c, d1, "test")
job := testCreateSchema(c, ctx, d1, dbInfo)
testCheckSchemaState(c, d1, dbInfo, model.StatePublic)
testCheckJobDone(c, d1, job, true)
job = testDropSchema(c, ctx, d1, dbInfo)
testCheckSchemaState(c, d1, dbInfo, model.StateNone)
testCheckJobDone(c, d1, job, false)
job = &model.Job{
SchemaID: dbInfo.ID,
Type: model.ActionDropSchema,
}
err := d1.startDDLJob(ctx, job)
c.Assert(terror.ErrorEqual(err, infoschema.DatabaseNotExists), IsTrue)
}
示例9: SetUpSuite
func (p *testShowSuit) SetUpSuite(c *C) {
nc := mock.NewContext()
p.ctx = nc
variable.BindSessionVars(p.ctx)
variable.BindGlobalVarAccessor(p.ctx, nc)
variable.RegisterStatistics(p.ms)
p.dbName = "testshowplan"
p.store = newStore(c, p.dbName)
p.txn, _ = p.store.Begin()
se := newSession(c, p.store, p.dbName)
p.createDBSQL = fmt.Sprintf("create database if not exists %s;", p.dbName)
p.dropDBSQL = fmt.Sprintf("drop database if exists %s;", p.dbName)
p.useDBSQL = fmt.Sprintf("use %s;", p.dbName)
p.createTableSQL = `CREATE TABLE test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));`
mustExecSQL(c, se, p.createDBSQL)
mustExecSQL(c, se, p.useDBSQL)
mustExecSQL(c, se, p.createTableSQL)
p.createSystemDBSQL = fmt.Sprintf("create database if not exists %s;", mysql.SystemDB)
p.createUserTableSQL = tidb.CreateUserTable
p.createDBPrivTableSQL = tidb.CreateDBPrivTable
p.createTablePrivTableSQL = tidb.CreateTablePrivTable
p.createColumnPrivTableSQL = tidb.CreateColumnPrivTable
mustExecSQL(c, se, p.createSystemDBSQL)
mustExecSQL(c, se, p.createUserTableSQL)
mustExecSQL(c, se, p.createDBPrivTableSQL)
mustExecSQL(c, se, p.createTablePrivTableSQL)
mustExecSQL(c, se, p.createColumnPrivTableSQL)
}
示例10: TestTableScanWithOrder
func (s *testPlanSuite) TestTableScanWithOrder(c *C) {
defer testleak.AfterTest(c)()
// Sort result by scanning PKHandle column.
sql := "select * from t order by a limit 1;"
stmt, err := s.ParseOneStmt(sql, "", "")
c.Assert(err, IsNil)
ast.SetFlag(stmt)
err = newMockResolve(stmt)
c.Assert(err, IsNil)
builder := &planBuilder{
allocator: new(idAllocator),
ctx: mock.NewContext(),
colMapper: make(map[*ast.ColumnNameExpr]int),
}
p := builder.build(stmt)
c.Assert(builder.err, IsNil)
logic, ok := p.(LogicalPlan)
c.Assert(ok, IsTrue)
// Get physical plan.
_, pp, _, err := logic.convert2PhysicalPlan(nil)
c.Assert(err, IsNil)
// Limit->Projection->PhysicalTableScan
// Get PhysicalTableScan plan.
cpp, ok := pp.p.GetChildByIndex(0).GetChildByIndex(0).(*PhysicalTableScan)
c.Assert(cpp, NotNil)
c.Assert(ok, IsTrue)
// Make sure KeepOrder is true.
c.Assert(cpp.KeepOrder, IsTrue)
}
示例11: TestWhere
func (t *testWhereSuit) TestWhere(c *C) {
tblPlan := &testTablePlan{t.data, []string{"id", "name"}, 0}
pln := &plans.FilterDefaultPlan{
Plan: tblPlan,
Expr: &expression.BinaryOperation{
Op: opcode.GE,
L: &expression.Ident{
CIStr: model.NewCIStr("id"),
ReferScope: expression.IdentReferFromTable,
ReferIndex: 0,
},
R: expression.Value{
Val: 30,
},
},
}
cnt := 0
rset := rsets.Recordset{Plan: pln,
Ctx: mock.NewContext()}
rset.Do(func(data []interface{}) (bool, error) {
cnt++
return true, nil
})
c.Assert(cnt, Equals, 2)
}
示例12: TestOrderBy
func (t *testOrderBySuit) TestOrderBy(c *C) {
tblPlan := &testTablePlan{t.data, []string{"id", "name"}, 0}
pln := &plans.OrderByDefaultPlan{
SelectList: &plans.SelectList{
HiddenFieldOffset: len(tblPlan.GetFields()),
ResultFields: tblPlan.GetFields(),
},
Src: tblPlan,
By: []expression.Expression{
&expression.Ident{
CIStr: model.NewCIStr("id"),
},
},
Ascs: []bool{false},
}
prev := 10000
rset := rsets.Recordset{
Plan: pln,
Ctx: mock.NewContext(),
}
err := rset.Do(func(data []interface{}) (bool, error) {
// DESC
if data[0].(int) > prev {
c.Error("should no be here", data[0], prev)
}
prev = data[0].(int)
return true, nil
})
if err != nil {
log.Error(err)
}
}
示例13: SetUpTest
func (s *testStmtSuite) SetUpTest(c *C) {
log.SetLevelByString("error")
s.dbName = "teststmts"
var err error
s.testDB, err = sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"/"+s.dbName+"/"+s.dbName)
c.Assert(err, IsNil)
// create db
s.createDBSql = fmt.Sprintf("create database if not exists %s;", s.dbName)
s.dropDBSql = fmt.Sprintf("drop database if exists %s;", s.dbName)
s.useDBSql = fmt.Sprintf("use %s;", s.dbName)
s.createTableSql = `
CREATE TABLE test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));
CREATE TABLE test1(id INT NOT NULL DEFAULT 2, name varchar(255), PRIMARY KEY(id), INDEX name(name));
CREATE TABLE test2(id INT NOT NULL DEFAULT 3, name varchar(255), PRIMARY KEY(id));`
s.selectSql = `SELECT * from test limit 2;`
mustExec(c, s.testDB, s.createDBSql)
mustExec(c, s.testDB, s.useDBSql)
s.createSystemDBSQL = fmt.Sprintf("create database if not exists %s;", mysql.SystemDB)
s.createUserTableSQL = tidb.CreateUserTable
s.createDBPrivTableSQL = tidb.CreateDBPrivTable
s.createTablePrivTableSQL = tidb.CreateTablePrivTable
s.createColumnPrivTableSQL = tidb.CreateColumnPrivTable
mustExec(c, s.testDB, s.createSystemDBSQL)
mustExec(c, s.testDB, s.createUserTableSQL)
mustExec(c, s.testDB, s.createDBPrivTableSQL)
mustExec(c, s.testDB, s.createTablePrivTableSQL)
mustExec(c, s.testDB, s.createColumnPrivTableSQL)
s.ctx = mock.NewContext()
variable.BindSessionVars(s.ctx)
}
示例14: TestSetCharsetStmt
func (s *testStmtSuite) TestSetCharsetStmt(c *C) {
testSQL := `SET NAMES utf8;`
stmtList, err := tidb.Compile(testSQL)
c.Assert(err, IsNil)
c.Assert(stmtList, HasLen, 1)
testStmt, ok := stmtList[0].(*stmts.SetCharsetStmt)
c.Assert(ok, IsTrue)
c.Assert(testStmt.IsDDL(), IsFalse)
c.Assert(len(testStmt.OriginText()), Greater, 0)
ctx := mock.NewContext()
variable.BindSessionVars(ctx)
sessionVars := variable.GetSessionVars(ctx)
for _, v := range variable.SetNamesVariables {
c.Assert(sessionVars.Systems[v] != "utf8", IsTrue)
}
_, err = testStmt.Exec(ctx)
c.Assert(err, IsNil)
for _, v := range variable.SetNamesVariables {
c.Assert(sessionVars.Systems[v], Equals, "utf8")
}
c.Assert(sessionVars.Systems[variable.CollationConnection], Equals, "utf8_general_ci")
mf := newMockFormatter()
testStmt.Explain(nil, mf)
c.Assert(mf.Len(), Greater, 0)
}
示例15: TestCast
func (s *testEvaluatorSuite) TestCast(c *C) {
f := types.NewFieldType(mysql.TypeLonglong)
expr := &ast.FuncCastExpr{
Expr: ast.NewValueExpr(1),
Tp: f,
}
ctx := mock.NewContext()
v, err := Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, Equals, int64(1))
f.Flag |= mysql.UnsignedFlag
v, err = Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, Equals, uint64(1))
f.Tp = mysql.TypeString
f.Charset = charset.CharsetBin
v, err = Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, DeepEquals, []byte("1"))
f.Tp = mysql.TypeString
f.Charset = "utf8"
v, err = Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, DeepEquals, "1")
expr.Expr = ast.NewValueExpr(nil)
v, err = Eval(ctx, expr)
c.Assert(err, IsNil)
c.Assert(v, IsNil)
}