-
Notifications
You must be signed in to change notification settings - Fork 0
/
DimensionService_test.go
335 lines (309 loc) · 7.93 KB
/
DimensionService_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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package tm1go_test
import (
"fmt"
"testing"
"github.com/andreyea/tm1go"
)
func TestDimensionService_Create(t *testing.T) {
tests := []struct {
name string
dimension *tm1go.Dimension
wantErr bool
}{
{
name: "Create new dimension",
dimension: &tm1go.Dimension{
Name: "TestDimension",
Hierarchies: []tm1go.Hierarchy{
{
Name: "TestDimension",
Elements: []tm1go.Element{
{Name: "Element1", Type: "Numeric"},
{Name: "Element2", Type: "Numeric"},
{Name: "Total", Type: "Consolidated"},
},
},
},
},
wantErr: false,
},
{
name: "Create dimension with existing name",
dimension: &tm1go.Dimension{
Name: "Account",
Hierarchies: []tm1go.Hierarchy{
{Name: "Account"},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tm1ServiceT.DimensionService.Create(tt.dimension)
if (err != nil) != tt.wantErr {
t.Errorf("DimensionService.Create() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
// Verify the dimension was created
createdDim, err := tm1ServiceT.DimensionService.Get(tt.dimension.Name)
if err != nil {
t.Errorf("Failed to get created dimension: %v", err)
return
}
if createdDim == nil {
t.Errorf("Created dimension not found")
return
}
if createdDim.Name != tt.dimension.Name {
t.Errorf("Created dimension name = %v, want %v", createdDim.Name, tt.dimension.Name)
}
// Clean up: delete the created dimension
err = tm1ServiceT.DimensionService.Delete(tt.dimension.Name)
if err != nil {
t.Logf("Failed to delete test dimension: %v", err)
}
}
})
}
}
func TestDimensionService_Get(t *testing.T) {
tests := []struct {
name string
dimensionName string
wantErr bool
checkFunc func(*tm1go.Dimension) error
}{
{
name: "Get existing dimension",
dimensionName: "Account",
wantErr: false,
checkFunc: func(dim *tm1go.Dimension) error {
if dim == nil {
return fmt.Errorf("dimension is nil")
}
if dim.Name != "Account" {
return fmt.Errorf("expected dimension name 'Account', got '%s'", dim.Name)
}
if len(dim.Hierarchies) == 0 {
return fmt.Errorf("dimension has no hierarchies")
}
return nil
},
},
{
name: "Get non-existent dimension",
dimensionName: "NonExistentDimension",
wantErr: true,
checkFunc: nil,
},
{
name: "Get dimension with empty name",
dimensionName: "",
wantErr: true,
checkFunc: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dimension, err := tm1ServiceT.DimensionService.Get(tt.dimensionName)
if (err != nil) != tt.wantErr {
t.Errorf("DimensionService.Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && tt.checkFunc != nil {
if err := tt.checkFunc(dimension); err != nil {
t.Errorf("DimensionService.Get() check failed: %v", err)
}
}
if tt.wantErr && dimension != nil {
t.Errorf("DimensionService.Get() returned dimension for error case: %v", dimension)
}
})
}
}
func TestDimensionService_Delete(t *testing.T) {
tests := []struct {
name string
dimensionName string
setupFunc func() error
wantErr bool
}{
{
name: "Delete existing dimension",
dimensionName: "TestDeleteDimension",
setupFunc: func() error {
dim := &tm1go.Dimension{
Name: "TestDeleteDimension",
Hierarchies: []tm1go.Hierarchy{
{Name: "TestDeleteDimension"},
},
}
return tm1ServiceT.DimensionService.Create(dim)
},
wantErr: false,
},
{
name: "Delete non-existent dimension",
dimensionName: "NonExistentDimension",
setupFunc: func() error { return nil },
wantErr: true,
},
{
name: "Delete dimension with empty name",
dimensionName: "",
setupFunc: func() error { return nil },
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup
err := tt.setupFunc()
if err != nil {
t.Fatalf("Setup failed: %v", err)
}
// Perform deletion
err = tm1ServiceT.DimensionService.Delete(tt.dimensionName)
if (err != nil) != tt.wantErr {
t.Errorf("DimensionService.Delete() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
// Verify the dimension was deleted
_, err := tm1ServiceT.DimensionService.Get(tt.dimensionName)
if err == nil {
t.Errorf("DimensionService.Delete() failed, dimension still exists")
}
}
})
}
}
func TestDimensionService_Exists(t *testing.T) {
tests := []struct {
name string
dimensionName string
setupFunc func() error
cleanupFunc func()
want bool
wantErr bool
}{
{
name: "Existing dimension",
dimensionName: "TestExistsDimension",
setupFunc: func() error {
dim := &tm1go.Dimension{
Name: "TestExistsDimension",
Hierarchies: []tm1go.Hierarchy{
{Name: "TestExistsDimension"},
},
}
return tm1ServiceT.DimensionService.Create(dim)
},
cleanupFunc: func() {
tm1ServiceT.DimensionService.Delete("TestExistsDimension")
},
want: true,
wantErr: false,
},
{
name: "Non-existent dimension",
dimensionName: "NonExistentDimension",
setupFunc: func() error { return nil },
cleanupFunc: func() {},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup
err := tt.setupFunc()
if err != nil {
t.Fatalf("Setup failed: %v", err)
}
defer tt.cleanupFunc()
// Perform existence check
got, err := tm1ServiceT.DimensionService.Exists(tt.dimensionName)
if (err != nil) != tt.wantErr {
t.Errorf("DimensionService.Exists() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("DimensionService.Exists() = %v, want %v", got, tt.want)
}
})
}
}
func TestDimensionService_GetAllNames(t *testing.T) {
tests := []struct {
name string
wantErr bool
}{
{
name: "Get all dimension names",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
names, err := tm1ServiceT.DimensionService.GetAllNames()
if (err != nil) != tt.wantErr {
t.Errorf("DimensionService.GetAllNames() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if len(names) == 0 {
t.Errorf("DimensionService.GetAllNames() returned empty list, expected at least one dimension")
}
// Check for some expected dimension names
expectedDimensions := []string{"Account", "Time", "Version"}
for _, expected := range expectedDimensions {
found := false
for _, name := range names {
if name == expected {
found = true
break
}
}
if !found {
t.Errorf("DimensionService.GetAllNames() did not return expected dimension: %s", expected)
}
}
}
})
}
}
func TestDimensionService_GetNumberOfDimensions(t *testing.T) {
tests := []struct {
name string
skipControlDims bool
wantErr bool
}{
{
name: "Get number of dimensions including control dimensions",
skipControlDims: false,
wantErr: false,
},
{
name: "Get number of dimensions excluding control dimensions",
skipControlDims: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
count, err := tm1ServiceT.DimensionService.GetNumberOfDimensions(tt.skipControlDims)
if (err != nil) != tt.wantErr {
t.Errorf("DimensionService.GetNumberOfDimensions() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if count <= 0 {
t.Errorf("DimensionService.GetNumberOfDimensions() returned %d, expected a number greater than 0", count)
}
}
})
}
}