show_test.go 3.5 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 20 21 22
/*
 * 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 (
	"fmt"
	"testing"

23 24
	"github.com/XiaoMi/soar/common"

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

func TestShowTableStatus(t *testing.T) {
29 30 31
	orgDatabase := connTest.Database
	connTest.Database = "sakila"
	ts, err := connTest.ShowTableStatus("film")
martianzhang's avatar
martianzhang 已提交
32 33 34
	if err != nil {
		t.Error("ShowTableStatus Error: ", err)
	}
35 36 37
	if string(ts.Rows[0].Engine) != "InnoDB" {
		t.Error("film table should be InnoDB engine")
	}
martianzhang's avatar
martianzhang 已提交
38
	pretty.Println(ts)
39 40 41 42 43 44 45 46 47 48 49

	connTest.Database = "sakila"
	ts, err = connTest.ShowTableStatus("actor_info")
	if err != nil {
		t.Error("ShowTableStatus Error: ", err)
	}
	if string(ts.Rows[0].Comment) != "VIEW" {
		t.Error("actor_info should be VIEW")
	}
	pretty.Println(ts)
	connTest.Database = orgDatabase
martianzhang's avatar
martianzhang 已提交
50 51 52
}

func TestShowTables(t *testing.T) {
53 54
	orgDatabase := connTest.Database
	connTest.Database = "sakila"
martianzhang's avatar
martianzhang 已提交
55 56 57 58
	ts, err := connTest.ShowTables()
	if err != nil {
		t.Error("ShowTableStatus Error: ", err)
	}
59 60 61 62 63 64 65 66 67 68

	err = common.GoldenDiff(func() {
		for _, table := range ts {
			fmt.Println(table)
		}
	}, t.Name(), update)
	if err != nil {
		t.Error(err)
	}
	connTest.Database = orgDatabase
martianzhang's avatar
martianzhang 已提交
69 70 71
}

func TestShowCreateTable(t *testing.T) {
72 73
	orgDatabase := connTest.Database
	connTest.Database = "sakila"
martianzhang's avatar
martianzhang 已提交
74 75 76
	tables := []string{
		"film",
		"customer_list",
martianzhang's avatar
martianzhang 已提交
77
	}
martianzhang's avatar
martianzhang 已提交
78 79 80 81 82 83 84
	err := common.GoldenDiff(func() {
		for _, table := range tables {
			ts, err := connTest.ShowCreateTable(table)
			if err != nil {
				t.Error("ShowCreateTable Error: ", err)
			}
			fmt.Println(ts)
85 86 87 88 89 90 91
		}
	}, t.Name(), update)
	if err != nil {
		t.Error(err)
	}

	connTest.Database = orgDatabase
martianzhang's avatar
martianzhang 已提交
92 93 94
}

func TestShowIndex(t *testing.T) {
95 96 97
	orgDatabase := connTest.Database
	connTest.Database = "sakila"
	ti, err := connTest.ShowIndex("film")
martianzhang's avatar
martianzhang 已提交
98 99 100
	if err != nil {
		t.Error("ShowIndex Error: ", err)
	}
101 102 103 104 105 106 107 108 109 110

	err = common.GoldenDiff(func() {
		pretty.Println(ti)
		pretty.Println(ti.FindIndex(IndexKeyName, "idx_title"))
	}, t.Name(), update)
	if err != nil {
		t.Error(err)
	}

	connTest.Database = orgDatabase
martianzhang's avatar
martianzhang 已提交
111 112 113
}

func TestShowColumns(t *testing.T) {
114 115
	orgDatabase := connTest.Database
	connTest.Database = "sakila"
116
	ti, err := connTest.ShowColumns("actor_info")
martianzhang's avatar
martianzhang 已提交
117 118 119
	if err != nil {
		t.Error("ShowColumns Error: ", err)
	}
120 121 122 123 124 125 126 127 128

	err = common.GoldenDiff(func() {
		pretty.Println(ti)
	}, t.Name(), update)
	if err != nil {
		t.Error(err)
	}

	connTest.Database = orgDatabase
martianzhang's avatar
martianzhang 已提交
129 130 131
}

func TestFindColumn(t *testing.T) {
132
	ti, err := connTest.FindColumn("film_id", "sakila", "film")
martianzhang's avatar
martianzhang 已提交
133 134 135
	if err != nil {
		t.Error("FindColumn Error: ", err)
	}
136 137 138 139 140 141 142 143 144 145 146 147
	err = common.GoldenDiff(func() {
		pretty.Println(ti)
	}, t.Name(), update)
	if err != nil {
		t.Error(err)
	}
}

func TestIsFKey(t *testing.T) {
	if !connTest.IsForeignKey("sakila", "film", "language_id") {
		t.Error("want True. got false")
	}
martianzhang's avatar
martianzhang 已提交
148 149 150
}

func TestShowReference(t *testing.T) {
151
	rv, err := connTest.ShowReference("sakila", "film")
martianzhang's avatar
martianzhang 已提交
152 153 154 155
	if err != nil {
		t.Error("ShowReference Error: ", err)
	}

156 157 158 159 160
	err = common.GoldenDiff(func() {
		pretty.Println(rv)
	}, t.Name(), update)
	if err != nil {
		t.Error(err)
martianzhang's avatar
martianzhang 已提交
161 162
	}
}