forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_test.go
149 lines (142 loc) · 4.19 KB
/
export_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
package uadmin
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
"github.com/xuri/excelize/v2"
)
// TestGetFilter is a unit testing function for getFilter() function
func TestGetFilter(t *testing.T) {
// Setup
var record TestStruct2
baseDate := time.Now()
baseDate = time.Date(baseDate.Year(), baseDate.Month(), baseDate.Day(), 0, 0, 0, 0, baseDate.Location())
om := TestStruct1{Name: "Test Model"}
Save(&om)
tx := db.Begin()
for i := 0; i < 100; i++ {
record = TestStruct2{
Name: fmt.Sprintf("Record No %d", i),
Count: i + 1,
Value: float64(i) + 0.5,
Start: baseDate.AddDate(0, 0, -i),
OtherModelID: om.ID,
}
if i%2 == 0 {
tempDate := record.Start.AddDate(0, 0, 1)
record.End = &tempDate
record.AnotherModelID = om.ID
record.Active = true
}
record.Type = TestType(i % 3)
tx.Create(&record)
}
tx.Commit()
examples := []struct {
r *http.Request
count int
header []string
}{
{
r: httptest.NewRequest("GET", fmt.Sprintf("/export/?m=teststruct2&start__lte=%s&start__gte=%s", baseDate.Format("2006-01-02"), baseDate.AddDate(0, 0, -10).Format("2006-01-02")), nil),
count: 11,
header: []string{
"Name", "Count", "Value", "Start", "End", "Type", "Other Model", "Another Model", "Active",
},
},
{
r: httptest.NewRequest("GET", fmt.Sprintf("/export/?m=teststruct2&start__lt=%s&start__gt=%s", baseDate.Format("2006-01-02"), baseDate.AddDate(0, 0, -10).Format("2006-01-02")), nil),
count: 9,
header: []string{
"Name", "Count", "Value", "Start", "End", "Type", "Other Model", "Another Model", "Active",
},
},
{
r: httptest.NewRequest("GET", fmt.Sprintf("/export/?m=teststruct2&count__in=%s", "1,2,3,4"), nil),
count: 4,
header: []string{
"Name", "Count", "Value", "Start", "End", "Type", "Other Model", "Another Model", "Active",
},
},
{
r: httptest.NewRequest("GET", fmt.Sprintf("/export/?m=teststruct2&name__contains=%s", url.QueryEscape("Record No 1")), nil),
count: 11,
header: []string{
"Name", "Count", "Value", "Start", "End", "Type", "Other Model", "Another Model", "Active",
},
},
{
r: httptest.NewRequest("GET", fmt.Sprintf("/export/?m=teststruct2&active=%s", "1"), nil),
count: 50,
header: []string{
"Name", "Count", "Value", "Start", "End", "Type", "Other Model", "Another Model", "Active",
},
},
}
s1 := Session{
Active: true,
UserID: 1,
}
s1.GenerateKey()
s1.Save()
Preload(&s1)
for _, e := range examples {
w := httptest.NewRecorder()
exportHandler(w, e.r, &s1)
// Check if 303
if w.Code != http.StatusSeeOther {
t.Errorf("exportHandler returned invalid code. Expected %d, got %d", 303, w.Code)
continue
}
// Check if the file exists
if _, ok := w.Header()["Location"]; !ok {
t.Error("exportHandler returned no Location in header")
continue
}
if len(w.Header()["Location"]) < 1 {
t.Error("exportHandler returned empty Location in header")
continue
}
if _, err := os.Stat("." + w.Header()["Location"][0]); os.IsNotExist(err) {
t.Errorf("exportHandler didn't create a file. Expected %s", w.Header()["Location"][0])
continue
}
f, err := excelize.OpenFile("." + w.Header()["Location"][0])
if err != nil {
t.Errorf("exportHandler created an invalid xlsx file. %s", err)
continue
}
if f.SheetCount != 1 {
t.Errorf("exportHandler invalid number of sheets. Expected 1, got %d", f.SheetCount)
continue
}
sheetName := "Sheet1"
for i := range e.header {
colName, _ := excelize.ColumnNumberToName(i + 1)
header, err := f.GetCellValue(sheetName, colName+"1")
if err != nil {
t.Errorf("exportHandler cell was not found. %s", err)
continue
}
if header != e.header[i] {
t.Errorf("exportHandler invalid header. Expected %s, got %s", e.header[i], header)
continue
}
}
// TODO: Test data
// for col := range e.header {
// for row := range sheet.Rows{
// if sheet.Cell(i+1, 1).String() == e.header[i] {
// t.Errorf("exportHandler invalid header. Expected %s, got %s", e.header[i], sheet.Cell(i+1, 1).String())
// }
// }
// }
}
Delete(s1)
DeleteList(&TestStruct1{}, "")
DeleteList(&TestStruct2{}, "")
}