forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_test.go
157 lines (133 loc) · 3.98 KB
/
index_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
149
150
151
152
153
154
155
156
157
package gorp_test
import (
"testing"
"github.com/go-gorp/gorp/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type IndexTestSuite struct {
suite.Suite
idx *gorp.IndexMap
}
func (s *IndexTestSuite) SetupTest() {
s.idx = gorp.NewIndex("test_idx")
}
func (s *IndexTestSuite) TestNewIndex() {
// Test basic index creation
idx := gorp.NewIndex("test_idx")
s.NotNil(idx)
s.Equal("test_idx", idx.IndexName)
s.False(idx.IsUnique())
s.Empty(idx.GetColumns())
s.Empty(idx.GetType())
}
func (s *IndexTestSuite) TestRename() {
// Test renaming an index
s.idx.Rename("new_idx")
s.Equal("new_idx", s.idx.IndexName)
}
func (s *IndexTestSuite) TestSetUnique() {
// Test setting unique flag
s.idx.SetUnique(true)
s.True(s.idx.IsUnique())
s.idx.SetUnique(false)
s.False(s.idx.IsUnique())
}
func (s *IndexTestSuite) TestSetType() {
testCases := []struct {
name string
indexType gorp.IndexType
}{
{"BTree", gorp.IndexTypeBTree},
{"Hash", gorp.IndexTypeHash},
{"GiST", gorp.IndexTypeGiST},
{"GIN", gorp.IndexTypeGIN},
{"SQLite", gorp.IndexTypeSQLite},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
s.idx.SetType(tc.indexType)
s.Equal(tc.indexType, s.idx.GetType())
})
}
}
func (s *IndexTestSuite) TestAddColumns() {
// Test adding single column
s.idx.AddColumns("col1")
s.Equal([]string{"col1"}, s.idx.GetColumns())
// Test adding multiple columns
s.idx = gorp.NewIndex("test_idx")
s.idx.AddColumns("col1", "col2", "col3")
s.Equal([]string{"col1", "col2", "col3"}, s.idx.GetColumns())
// Test adding columns incrementally
s.idx = gorp.NewIndex("test_idx")
s.idx.AddColumns("col1").AddColumns("col2")
s.Equal([]string{"col1", "col2"}, s.idx.GetColumns())
}
func (s *IndexTestSuite) TestString() {
// Test string representation of a simple index
s.idx.SetType(gorp.IndexTypeBTree).AddColumns("col1")
s.Contains(s.idx.String(), "test_idx")
s.Contains(s.idx.String(), "BTREE")
s.Contains(s.idx.String(), "col1")
// Test string representation of a unique multi-column index
s.idx = gorp.NewIndex("test_idx")
s.idx.SetType(gorp.IndexTypeHash).
SetUnique(true).
AddColumns("col1", "col2")
str := s.idx.String()
s.Contains(str, "test_idx")
s.Contains(str, "HASH")
s.Contains(str, "UNIQUE")
s.Contains(str, "col1")
s.Contains(str, "col2")
}
func (s *IndexTestSuite) TestValidate() {
// Test validation of a valid index
s.idx.SetType(gorp.IndexTypeBTree).AddColumns("col1")
s.NoError(s.idx.Validate())
// Test validation with no columns
s.idx = gorp.NewIndex("test_idx")
s.idx.SetType(gorp.IndexTypeBTree)
s.Error(s.idx.Validate())
// Test validation with empty index name
s.idx = gorp.NewIndex("")
s.idx.SetType(gorp.IndexTypeBTree).AddColumns("col1")
s.Error(s.idx.Validate())
}
func (s *IndexTestSuite) TestFluentInterface() {
// Test fluent interface chaining
s.idx.SetType(gorp.IndexTypeBTree).
SetUnique(true).
AddColumns("col1", "col2").
Rename("new_idx")
s.Equal("new_idx", s.idx.IndexName)
s.Equal(gorp.IndexTypeBTree, s.idx.GetType())
s.True(s.idx.IsUnique())
s.Equal([]string{"col1", "col2"}, s.idx.GetColumns())
}
func (s *IndexTestSuite) TestIndexTypeConstants() {
// Verify index type constants
s.Equal(gorp.IndexType("BTREE"), gorp.IndexTypeBTree)
s.Equal(gorp.IndexType("HASH"), gorp.IndexTypeHash)
s.Equal(gorp.IndexType("GIST"), gorp.IndexTypeGiST)
s.Equal(gorp.IndexType("GIN"), gorp.IndexTypeGIN)
s.Equal(gorp.IndexType(""), gorp.IndexTypeSQLite)
}
func TestIndexSuite(t *testing.T) {
suite.Run(t, new(IndexTestSuite))
}
// Example test to demonstrate usage
func TestIndexExample(t *testing.T) {
// Create a new index for email field
idx := gorp.NewIndex("user_email_idx").
SetType(gorp.IndexTypeBTree).
SetUnique(true).
AddColumns("email")
// Verify the configuration
assert.Equal(t, "user_email_idx", idx.IndexName)
assert.Equal(t, gorp.IndexTypeBTree, idx.GetType())
assert.True(t, idx.IsUnique())
assert.Equal(t, []string{"email"}, idx.GetColumns())
assert.NoError(t, idx.Validate())
}