mysql_test.go 3.7 KB
Newer Older
martianzhang's avatar
martianzhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2018 Xiaomi, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package database

import (
20
	"flag"
martianzhang's avatar
martianzhang 已提交
21
	"fmt"
22
	"os"
martianzhang's avatar
martianzhang 已提交
23 24 25
	"testing"

	"github.com/XiaoMi/soar/common"
26

martianzhang's avatar
martianzhang 已提交
27 28 29
	"github.com/kr/pretty"
)

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
var connTest *Connector

var update = flag.Bool("update", false, "update .golden files")

func init() {
	common.BaseDir = common.DevPath
	common.ParseConfig("")
	connTest = &Connector{
		Addr:     common.Config.OnlineDSN.Addr,
		User:     common.Config.OnlineDSN.User,
		Pass:     common.Config.OnlineDSN.Password,
		Database: common.Config.OnlineDSN.Schema,
		Charset:  common.Config.OnlineDSN.Charset,
	}
	if _, err := connTest.Version(); err != nil {
		common.Log.Critical("Test env Error: %v", err)
		os.Exit(0)
	}
}

func TestNewConnection(t *testing.T) {
	_, err := connTest.NewConnection()
	if err != nil {
		t.Errorf("TestNewConnection, Error: %s", err.Error())
	}
}

martianzhang's avatar
martianzhang 已提交
57 58
// TODO: go test -race不通过待解决
func TestQuery(t *testing.T) {
59 60 61 62 63 64 65 66 67 68 69 70 71
	res, err := connTest.Query("select 0")
	if err != nil {
		t.Error(err.Error())
	}
	for res.Rows.Next() {
		var val int
		err = res.Rows.Scan(&val)
		if err != nil {
			t.Error(err.Error())
		}
		if val != 0 {
			t.Error("should return 0")
		}
martianzhang's avatar
martianzhang 已提交
72
	}
73
	// TODO: timeout test
martianzhang's avatar
martianzhang 已提交
74 75
}

76 77 78 79 80 81 82 83
func TestColumnCardinality(t *testing.T) {
	orgDatabase := connTest.Database
	connTest.Database = "sakila"
	a := connTest.ColumnCardinality("actor", "first_name")
	if a >= 1 || a <= 0 {
		t.Error("sakila.actor.first_name cardinality should in (0, 1), now it's", a)
	}
	connTest.Database = orgDatabase
martianzhang's avatar
martianzhang 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
}

func TestDangerousSQL(t *testing.T) {
	testCase := map[string]bool{
		"select * from tb;delete from tb;": true,
		"show database;":                   false,
		"select * from t;":                 false,
		"explain delete from t;":           false,
	}

	db := Connector{}
	for sql, want := range testCase {
		got := db.dangerousQuery(sql)
		if got != want {
			t.Errorf("SQL:%s got:%v want:%v", sql, got, want)
		}
	}
}

func TestWarningsAndQueryCost(t *testing.T) {
	common.Config.ShowWarnings = true
	common.Config.ShowLastQueryCost = true
	res, err := connTest.Query("explain select * from sakila.film")
	if err != nil {
		t.Error("Query Error: ", err)
	} else {
110 111 112 113 114 115 116
		for res.Warning.Next() {
			var str string
			err = res.Warning.Scan(str)
			if err != nil {
				t.Error(err.Error())
			}
			pretty.Println(str)
martianzhang's avatar
martianzhang 已提交
117
		}
118
		fmt.Println(res.QueryCost, err)
martianzhang's avatar
martianzhang 已提交
119 120 121 122 123 124 125 126 127 128 129
	}
}

func TestVersion(t *testing.T) {
	version, err := connTest.Version()
	if err != nil {
		t.Error(err.Error())
	}
	fmt.Println(version)
}

martianzhang's avatar
martianzhang 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
func TestRemoveSQLComments(t *testing.T) {
	SQLs := []string{
		`-- comment`,
		`--`,
		`# comment`,
		`/* multi-line
comment*/`,
		`--
-- comment`,
	}

	err := common.GoldenDiff(func() {
		for _, sql := range SQLs {
			fmt.Println(RemoveSQLComments(sql))
		}
	}, t.Name(), update)
	if err != nil {
		t.Error(err)
	}
}
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

func TestSingleIntValue(t *testing.T) {
	val, err := connTest.SingleIntValue("read_only")
	if err != nil {
		t.Error(err)
	}
	if val < 0 {
		t.Error("SingleIntValue, return should large than zero")
	}
}

func TestIsView(t *testing.T) {
	originalDatabase := connTest.Database
	connTest.Database = "sakila"
	if !connTest.IsView("actor_info") {
		t.Error("actor_info should be a VIEW")
	}
	connTest.Database = originalDatabase
}