forked from xwb1989/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
analyzer_test.go
102 lines (85 loc) · 2.17 KB
/
analyzer_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
/*
Tests for analyzer.go
*/
package sqlparser
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestPrimaryKey(t *testing.T) {
sql := `create table t1 (
LastName varchar(255),
FirstName varchar(255),
ID int primary key
)`
tree, err := Parse(sql)
assert.Nil(t, err)
primary_key, err := GetPrimaryKey(tree)
assert.Nil(t, err)
assert.Equal(t, "ID", primary_key.ColName)
}
func TestNoPrimaryKey(t *testing.T) {
sql := `create table t1 (
LastName varchar(255),
FirstName varchar(255),
ID int unique key
)`
tree, err := Parse(sql)
assert.Nil(t, err)
_, err = GetPrimaryKey(tree)
assert.NotNil(t, err)
}
func TestChangePrimaryKeyProperty(t *testing.T) {
sql := `create table t1 (
LastName varchar(255),
FirstName varchar(255),
ID int primary key
)`
tree, err := Parse(sql)
assert.Nil(t, err)
primary_key, err := GetPrimaryKey(tree)
assert.Nil(t, err)
primary_key.ColumnAtts = append(primary_key.ColumnAtts, AST_AUTO_INCREMENT)
sql_actual := String(tree)
sql_expected := `create table t1 (
LastName varchar(255),
FirstName varchar(255),
ID int primary key auto_increment
)`
assert.Equal(t, sql_expected, sql_actual)
}
func TestGetColumnByName(t *testing.T) {
sql := `create table t1 (
LastName varchar(255),
FirstName varchar(255),
ID int auto_increment
)`
tree, err := Parse(sql)
assert.Nil(t, err)
col, err := GetColumnByName(tree, "ID")
assert.Nil(t, err)
assert.Equal(t, "ID", col.ColName)
col.ColumnAtts = append(col.ColumnAtts, AST_PRIMARY_KEY)
primary_key, err := GetPrimaryKey(tree)
assert.Nil(t, err)
assert.Equal(t, "ID", primary_key.ColName)
}
func TestModifyColumns(t *testing.T) {
sql := `create table t1 (
LastName varchar(255),
FirstName varchar(255)
)`
tree, err := Parse(sql)
assert.Nil(t, err)
table, ok := tree.(*CreateTable)
assert.True(t, ok)
col := &ColumnDefinition{ColName: "ID", ColType: "int", ColumnAtts: ColumnAtts{AST_AUTO_INCREMENT, AST_PRIMARY_KEY}}
table.ColumnDefinitions = append(table.ColumnDefinitions, col)
sql_expected := `create table t1 (
LastName varchar(255),
FirstName varchar(255),
ID int auto_increment primary key
)`
sql_actual := String(tree)
assert.Equal(t, sql_expected, sql_actual)
}