Skip to content

Commit 6d722ea

Browse files
committed
add unit test
1 parent 647bbee commit 6d722ea

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

schema/schema.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,19 @@ func (ta *Table) FindColumn(name string) int {
183183
return -1
184184
}
185185

186-
// Get TableColumn by Index index
186+
// Get TableColumn by primary index index
187187
func (ta *Table) GetPKColumn(index int) *TableColumn {
188+
if index >= len(ta.PKColumns) {
189+
return nil
190+
}
188191
return &ta.Columns[ta.PKColumns[index]]
189192
}
190193

191-
// Get Primary Index by column index. Return nil if the column is not a primary index.
194+
// Get primary index by column index. Return nil if the column is not a primary index.
192195
func (ta *Table) GetPKIndex(colIndex int) *Index {
193-
for i, _colIndex := range ta.PKColumns {
196+
for _, _colIndex := range ta.PKColumns {
194197
if _colIndex == colIndex {
195-
return ta.Indexes[i]
198+
return ta.Indexes[0]
196199
}
197200
}
198201
return nil
@@ -424,6 +427,7 @@ func (ta *Table) fetchPrimaryKeyColumns() error {
424427
return nil
425428
}
426429

430+
// Primary key must be the first index?
427431
pkIndex := ta.Indexes[0]
428432
if pkIndex.Name != "PRIMARY" {
429433
return nil

schema/schema_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
// use docker mysql for test
1616
var host = flag.String("host", "127.0.0.1", "MySQL host")
17+
var schema = flag.String("schema", "test", "MySQL Database")
18+
var pwd = flag.String("pwd", "", "MySQL password")
1719

1820
func Test(t *testing.T) {
1921
TestingT(t)
@@ -28,10 +30,10 @@ var _ = Suite(&schemaTestSuite{})
2830

2931
func (s *schemaTestSuite) SetUpSuite(c *C) {
3032
var err error
31-
s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, 3306), "root", "", "test")
33+
s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, 3306), "root", *pwd, *schema)
3234
c.Assert(err, IsNil)
3335

34-
s.sqlDB, err = sql.Open("mysql", fmt.Sprintf("root:@%s:3306", *host))
36+
s.sqlDB, err = sql.Open("mysql", fmt.Sprintf("root:%s@%s:3306", *pwd, *host))
3537
c.Assert(err, IsNil)
3638
}
3739

@@ -75,12 +77,20 @@ func (s *schemaTestSuite) TestSchema(c *C) {
7577
_, err = s.conn.Execute(str)
7678
c.Assert(err, IsNil)
7779

78-
ta, err := NewTable(s.conn, "test", "schema_test")
80+
ta, err := NewTable(s.conn, *schema, "schema_test")
7981
c.Assert(err, IsNil)
8082

8183
c.Assert(ta.Columns, HasLen, 15)
8284
c.Assert(ta.Indexes, HasLen, 3)
8385
c.Assert(ta.PKColumns, DeepEquals, []int{2, 0})
86+
c.Assert(ta.GetPKIndex(0), Equals, ta.Indexes[0])
87+
c.Assert(ta.GetPKIndex(1), IsNil)
88+
c.Assert(ta.GetPKIndex(2), Equals, ta.Indexes[0])
89+
c.Assert(ta.GetPKIndex(3), IsNil)
90+
c.Assert(ta.GetPKColumn(0), Equals, &ta.Columns[2])
91+
c.Assert(ta.GetPKColumn(1), Equals, &ta.Columns[0])
92+
c.Assert(ta.GetPKColumn(2), IsNil)
93+
c.Assert(ta.GetPKColumn(3), IsNil)
8494
c.Assert(ta.Indexes[0].Columns, HasLen, 2)
8595
c.Assert(ta.Indexes[0].Name, Equals, "PRIMARY")
8696
c.Assert(ta.Indexes[2].Name, Equals, "name_idx")
@@ -107,7 +117,7 @@ func (s *schemaTestSuite) TestSchema(c *C) {
107117
c.Assert(ta.Columns[14].MaxSize, Equals, uint(12))
108118
c.Assert(ta.Columns[14].FixedSize, Equals, uint(0))
109119

110-
taSqlDb, err := NewTableFromSqlDB(s.sqlDB, "test", "schema_test")
120+
taSqlDb, err := NewTableFromSqlDB(s.sqlDB, *schema, "schema_test")
111121
c.Assert(err, IsNil)
112122

113123
c.Assert(taSqlDb, DeepEquals, ta)
@@ -119,7 +129,7 @@ func (s *schemaTestSuite) TestQuoteSchema(c *C) {
119129
_, err := s.conn.Execute(str)
120130
c.Assert(err, IsNil)
121131

122-
ta, err := NewTable(s.conn, "test", "a-b_test")
132+
ta, err := NewTable(s.conn, *schema, "a-b_test")
123133
c.Assert(err, IsNil)
124134

125135
c.Assert(ta.Columns[0].Name, Equals, "a.b")

0 commit comments

Comments
 (0)