forked from talent-plan/tinysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_test.go
148 lines (128 loc) · 4.15 KB
/
schema_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2017 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
"fmt"
. "github.com/pingcap/check"
)
// generateKeys4Schema will generate keys for a given schema. Used only in this file.
func generateKeys4Schema(schema *Schema) {
keyCount := len(schema.Columns) - 1
keys := make([]KeyInfo, 0, keyCount)
for i := 0; i < keyCount; i++ {
keys = append(keys, []*Column{schema.Columns[i]})
}
schema.Keys = keys
}
var _ = Suite(&testEvalSuite{})
type testEvalSuite struct {
colID int64
}
func (s *testEvalSuite) SetUpSuite(c *C) {
s.colID = 0
}
func (s *testEvalSuite) allocColID() int64 {
s.colID++
return s.colID
}
func (s *testEvalSuite) TearDownTest(c *C) {
s.colID = 0
}
// generateSchema will generate a schema for test. Used only in this file.
func (s *testEvalSuite) generateSchema(colCount int) *Schema {
cols := make([]*Column, 0, colCount)
for i := 0; i < colCount; i++ {
cols = append(cols, &Column{
UniqueID: s.allocColID(),
})
}
return NewSchema(cols...)
}
func (s *testEvalSuite) TestSchemaString(c *C) {
schema := s.generateSchema(5)
c.Assert(schema.String(), Equals, "Column: [Column#1,Column#2,Column#3,Column#4,Column#5] Unique key: []")
generateKeys4Schema(schema)
c.Assert(schema.String(), Equals, "Column: [Column#1,Column#2,Column#3,Column#4,Column#5] Unique key: [[Column#1],[Column#2],[Column#3],[Column#4]]")
}
func (s *testEvalSuite) TestSchemaRetrieveColumn(c *C) {
schema := s.generateSchema(5)
colOutSchema := &Column{
UniqueID: 100,
}
for _, col := range schema.Columns {
c.Assert(schema.RetrieveColumn(col), Equals, col)
}
c.Assert(schema.RetrieveColumn(colOutSchema), IsNil)
}
func (s *testEvalSuite) TestSchemaIsUniqueKey(c *C) {
schema := s.generateSchema(5)
generateKeys4Schema(schema)
colOutSchema := &Column{
UniqueID: 100,
}
for i, col := range schema.Columns {
if i < len(schema.Columns)-1 {
c.Assert(schema.IsUniqueKey(col), Equals, true)
} else {
c.Assert(schema.IsUniqueKey(col), Equals, false)
}
}
c.Assert(schema.IsUniqueKey(colOutSchema), Equals, false)
}
func (s *testEvalSuite) TestSchemaContains(c *C) {
schema := s.generateSchema(5)
colOutSchema := &Column{
UniqueID: 100,
}
for _, col := range schema.Columns {
c.Assert(schema.Contains(col), Equals, true)
}
c.Assert(schema.Contains(colOutSchema), Equals, false)
}
func (s *testEvalSuite) TestSchemaColumnsIndices(c *C) {
schema := s.generateSchema(5)
colOutSchema := &Column{
UniqueID: 100,
}
for i := 0; i < len(schema.Columns)-1; i++ {
colIndices := schema.ColumnsIndices([]*Column{schema.Columns[i], schema.Columns[i+1]})
for j, res := range colIndices {
c.Assert(res, Equals, i+j)
}
}
c.Assert(schema.ColumnsIndices([]*Column{schema.Columns[0], schema.Columns[1], colOutSchema, schema.Columns[2]}), IsNil)
}
func (s *testEvalSuite) TestSchemaColumnsByIndices(c *C) {
schema := s.generateSchema(5)
indices := []int{0, 1, 2, 3}
retCols := schema.ColumnsByIndices(indices)
for i, ret := range retCols {
c.Assert(fmt.Sprintf("%p", schema.Columns[i]), Equals, fmt.Sprintf("%p", ret))
}
}
func (s *testEvalSuite) TestSchemaMergeSchema(c *C) {
lSchema := s.generateSchema(5)
generateKeys4Schema(lSchema)
rSchema := s.generateSchema(5)
generateKeys4Schema(rSchema)
c.Assert(MergeSchema(nil, nil), IsNil)
c.Assert(MergeSchema(lSchema, nil).String(), Equals, lSchema.String())
c.Assert(MergeSchema(nil, rSchema).String(), Equals, rSchema.String())
schema := MergeSchema(lSchema, rSchema)
for i := 0; i < len(lSchema.Columns); i++ {
c.Assert(schema.Columns[i].UniqueID, Equals, lSchema.Columns[i].UniqueID)
}
for i := 0; i < len(rSchema.Columns); i++ {
c.Assert(schema.Columns[i+len(lSchema.Columns)].UniqueID, Equals, rSchema.Columns[i].UniqueID)
}
}