-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfilter_test.go
More file actions
198 lines (182 loc) · 5.56 KB
/
filter_test.go
File metadata and controls
198 lines (182 loc) · 5.56 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"testing"
)
func TestBuffer_filterByColumn(t *testing.T) {
tests := []struct {
name string
data [][]string
colIndex int
options FilterOptions
expectedRows int // including header
}{
{
name: "Filter with operator 'contains'",
data: [][]string{
{"Name", "Status", "Age"},
{"Alice", "Active", "30"},
{"Bob", "Inactive", "25"},
{"Charlie", "Active", "35"},
},
colIndex: 1, // Status column
options: FilterOptions{Query: "act", Operator: "contains", CaseSensitive: false},
expectedRows: 4, // header + 3 rows containing "act"
},
{
name: "Filter with operator 'contains' case sensitive",
data: [][]string{
{"Name", "Status", "Age"},
{"Alice", "Active", "30"},
{"Bob", "Inactive", "25"},
{"Charlie", "active", "35"},
},
colIndex: 1, // Status column
options: FilterOptions{Query: "Active", Operator: "contains", CaseSensitive: true},
expectedRows: 2, // header + "Active"
},
{
name: "Filter with operator 'equals'",
data: [][]string{
{"Name", "Status", "Age"},
{"Alice", "Active", "30"},
{"Bob", "Inactive", "25"},
{"Charlie", "Active", "35"},
},
colIndex: 1, // Status column
options: FilterOptions{Query: "Active", Operator: "equals", CaseSensitive: false},
expectedRows: 3, // header + 2 "Active" rows
},
{
name: "Filter with operator 'starts with'",
data: [][]string{
{"Name", "Status", "Age"},
{"Alice", "Active", "30"},
{"Bob", "Inactive", "25"},
{"Charlie", "Activation", "35"},
},
colIndex: 1, // Status column
options: FilterOptions{Query: "Activ", Operator: "starts with", CaseSensitive: false},
expectedRows: 3, // header + "Active", "Activation"
},
{
name: "Filter with operator 'ends with'",
data: [][]string{
{"Name", "Status", "Age"},
{"Alice", "Active", "30"},
{"Bob", "Inactive", "25"},
{"Charlie", "Proactive", "35"},
},
colIndex: 1, // Status column
options: FilterOptions{Query: "active", Operator: "ends with", CaseSensitive: false},
expectedRows: 4, // header + "Active", "Inactive", "Proactive"
},
{
name: "Filter with operator 'regex'",
data: [][]string{
{"Name", "Status", "Age"},
{"Alice", "Active", "30"},
{"Bob", "Inactive", "25"},
{"Charlie", "Pending", "35"},
},
colIndex: 1, // Status column
options: FilterOptions{Query: "^(Act|Inact)ive$", Operator: "regex", CaseSensitive: true},
expectedRows: 3, // header + "Active", "Inactive"
},
{
name: "Filter with numeric operator '>'",
data: [][]string{
{"Name", "Age"},
{"Alice", "30"},
{"Bob", "25"},
{"Charlie", "35"},
},
colIndex: 1, // Age column
options: FilterOptions{Query: "28", Operator: ">"},
expectedRows: 3, // header + "30", "35"
},
{
name: "Filter with numeric operator '<='",
data: [][]string{
{"Name", "Age"},
{"Alice", "30"},
{"Bob", "25"},
{"Charlie", "28"},
},
colIndex: 1, // Age column
options: FilterOptions{Query: "28", Operator: "<="},
expectedRows: 3, // header + "25", "28"
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create buffer with test data
b, err := createNewBufferWithData(tt.data, false)
if err != nil {
t.Fatalf("Failed to create buffer: %v", err)
}
b.rowFreeze = 1 // Set header row
b.detectAllColumnTypes()
// Apply filter
filtered := b.filterByColumn(tt.colIndex, tt.options)
// Check result
if filtered.rowLen != tt.expectedRows {
t.Errorf("Expected %d rows (including header), got %d", tt.expectedRows, filtered.rowLen)
}
// Verify header is preserved
if filtered.rowLen > 0 {
for col := 0; col < filtered.colLen && col < len(tt.data[0]); col++ {
if filtered.cont[0][col] != tt.data[0][col] {
t.Errorf("Header not preserved: expected %s, got %s", tt.data[0][col], filtered.cont[0][col])
}
}
}
// Verify freeze settings are preserved
if filtered.rowFreeze != b.rowFreeze {
t.Errorf("rowFreeze not preserved: expected %d, got %d", b.rowFreeze, filtered.rowFreeze)
}
if filtered.colFreeze != b.colFreeze {
t.Errorf("colFreeze not preserved: expected %d, got %d", b.colFreeze, filtered.colFreeze)
}
})
}
}
func TestBuffer_filterByColumn_EdgeCases(t *testing.T) {
t.Run("Filter column out of bounds", func(t *testing.T) {
data := [][]string{
{"Name", "Status"},
{"Alice", "Active"},
}
b, _ := createNewBufferWithData(data, false)
b.rowFreeze = 1
// Filter by non-existent column
filtered := b.filterByColumn(10, FilterOptions{Query: "test", Operator: "contains"})
// Should return only header
if filtered.rowLen != 1 {
t.Errorf("Expected 1 row (header only), got %d", filtered.rowLen)
}
})
t.Run("Filter empty buffer", func(t *testing.T) {
b := createNewBuffer()
b.rowFreeze = 0
// Filter empty buffer
filtered := b.filterByColumn(0, FilterOptions{Query: "test", Operator: "contains"})
// Should return empty buffer
if filtered.rowLen != 0 {
t.Errorf("Expected 0 rows, got %d", filtered.rowLen)
}
})
t.Run("Filter buffer with no header", func(t *testing.T) {
data := [][]string{
{"Alice", "Active"},
{"Bob", "Inactive"},
}
b, _ := createNewBufferWithData(data, false)
b.rowFreeze = 0 // No header
// Filter by column
filtered := b.filterByColumn(1, FilterOptions{Query: "Active", Operator: "equals", CaseSensitive: true})
// Should return 1 row
if filtered.rowLen != 1 {
t.Errorf("Expected 1 row, got %d", filtered.rowLen)
}
})
}