-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
114 lines (92 loc) · 2.51 KB
/
main_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
package main
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestRootHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(rootHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestRoomHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/12345", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(roomHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestNewRoom(t *testing.T) {
req, err := http.NewRequest("GET", "/12345/all", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(allHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
wantedBody := "null\n"
if content := rr.Body.String(); content != wantedBody {
t.Errorf("all api returned incorrect data for new room: got %v want %v",
content, wantedBody)
}
}
func TestSetDeleteMood(t *testing.T) {
var jsonStr = []byte(`{"username": "alice", "mood": "Agree", "room": "12345", "roomUser": "12345alice"}`)
req, err := http.NewRequest("POST", "/12345/mood", bytes.NewBuffer(jsonStr))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(addMoodHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
person, err := getFirstRecord()
if err != nil {
t.Fatal(err)
}
if person.Mood != "Agree" || person.Room != "12345" || person.Username != "alice" || person.RoomUser != "12345alice" {
t.Errorf("saved item didn't match: got %v", person)
}
Delete("12345alice", db)
_, err = getFirstRecord()
if err == nil {
t.Errorf("item wasn't deleted")
}
}
func getFirstRecord() (UserMoodStruct, error) {
var txn = db.Txn(false)
defer txn.Abort()
it, err := txn.Get("usermood", "id")
if err != nil {
return UserMoodStruct{}, err
}
obj := it.Next()
if obj != nil {
return obj.(UserMoodStruct), nil
} else {
return UserMoodStruct{}, errors.New("no record returned")
}
}