-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpbot_test.go
More file actions
54 lines (46 loc) · 1.76 KB
/
helpbot_test.go
File metadata and controls
54 lines (46 loc) · 1.76 KB
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 (
"testing"
"github.com/Raytar/helpbot/database"
"github.com/Raytar/helpbot/models"
)
func TestCreateAndRetrieveHelpRequests(t *testing.T) {
db := setupTestDatabase(t)
defer db.Close()
db.CreateHelpRequest(&models.HelpRequest{StudentUserID: "1", GuildID: "1", Student: models.Student{}, Done: true})
req := &models.HelpRequest{StudentUserID: "1", GuildID: "1"}
got, err := db.GetHelpRequest(req)
if err != nil {
t.Fatalf("GetHelpRequest failed: %v", err)
}
if got.StudentUserID != "1" || got.GuildID != "1" || got.Done != true {
t.Errorf("GetHelpRequest returned wrong data: got %+v, want %+v", got, req)
}
}
func TestGetPosInQueue(t *testing.T) {
db := setupTestDatabase(t)
defer db.Close()
db.CreateHelpRequest(&models.HelpRequest{StudentUserID: "1", GuildID: "1", Student: models.Student{}})
db.CreateHelpRequest(&models.HelpRequest{StudentUserID: "2", GuildID: "1", Student: models.Student{}})
db.CreateHelpRequest(&models.HelpRequest{StudentUserID: "3", GuildID: "1", Student: models.Student{}, Done: true})
db.CreateHelpRequest(&models.HelpRequest{StudentUserID: "4", GuildID: "1", Student: models.Student{}})
check := func(studentID, guildID string, want int) {
if pos, err := db.GetQueuePosition(guildID, studentID); err != nil {
t.Errorf("getPosInQueue(%s, %s): %v", studentID, guildID, err)
} else if pos != want {
t.Errorf("getPosInQueue(%s, %s): got %d, want %d", studentID, guildID, pos, want)
}
}
// Check positions in the queue
check("1", "1", 1)
check("2", "1", 2)
check("3", "1", 0)
check("4", "1", 3)
}
func setupTestDatabase(t *testing.T) *database.Database {
db, err := database.OpenDatabase("file::memory:?cache=shared", nil)
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
return db
}