-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpbot_test.go
54 lines (46 loc) · 1.24 KB
/
helpbot_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
package helpbot
import (
"fmt"
"os"
"testing"
"github.com/andersfylling/disgord"
"github.com/jinzhu/gorm"
)
var db *gorm.DB
func TestMain(m *testing.M) {
var err error
db, err = OpenDatabase("file::memory:?cache=shared")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// viper.Set("db-path", "test.db")
ret := m.Run()
db.Close()
os.Exit(ret)
}
func TestCreateAndRetrieveHelpRequests(t *testing.T) {
db.Create(&HelpRequest{StudentUserID: 1, Student: Student{}, Done: true})
var req HelpRequest
db.Find(&req, "student_user_id = ?", 1)
if req.StudentUserID != 1 {
t.Fatalf("Failed to create and retrieve users")
}
}
func TestGetPosInQueue(t *testing.T) {
db.Create(&HelpRequest{StudentUserID: 1, Student: Student{}})
db.Create(&HelpRequest{StudentUserID: 2, Student: Student{}})
db.Create(&HelpRequest{StudentUserID: 3, Student: Student{}, Done: true})
db.Create(&HelpRequest{StudentUserID: 4, Student: Student{}})
check := func(name disgord.Snowflake, want int) {
if pos, err := getPosInQueue(db, name); err != nil {
t.Errorf("getPosInQueue(%d): %v", name, err)
} else if pos != want {
t.Errorf("getPosInQueue(%d): got %d, want %d", name, pos, want)
}
}
check(1, 1)
check(2, 2)
check(3, 0)
check(4, 3)
}