forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete_handler_test.go
52 lines (43 loc) · 1.11 KB
/
delete_handler_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
package uadmin
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
// TestProcessDelete is a unit testing function for processDelete() function
func TestProcessDelete(t *testing.T) {
// Setup
w := httptest.NewRecorder()
idList := []string{}
for i := 0; i < 10; i++ {
record := TestStruct{
Name: fmt.Sprintf("test%d", i),
}
Save(&record)
idList = append(idList, fmt.Sprint(record.ID))
}
examples := []struct {
r *http.Request
count int
}{
{httptest.NewRequest("POST", "/", nil), 0},
{httptest.NewRequest("POST", "/", nil), len(idList)},
}
examples[1].r.PostForm = url.Values{}
examples[1].r.PostForm.Set("listID", strings.Join(idList, ","))
user := User{}
Get(&user, "id = ?", 1)
for _, e := range examples {
countBefore := Count(&TestStruct{}, "")
processDelete("teststruct", w, e.r, nil, &user)
countAfter := Count(TestStruct{}, "")
if (countBefore - countAfter) != e.count {
t.Errorf("Invalid number of deleted records by processDelete. Expected %d, Got %d", e.count, (countBefore - countAfter))
}
}
// Clean up
DeleteList(TestStruct{}, "")
}