-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
180 lines (147 loc) · 5.2 KB
/
integration_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"context"
"database/sql"
"fmt"
"os"
"testing"
"github.com/bwmarrin/discordgo"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/jackc/pgx/v4"
)
var testDbConn *pgx.Conn
func TestMain(m *testing.M) {
os.Exit(testMain(m))
}
func testMain(m *testing.M) int {
db, err := sql.Open("postgres", os.Getenv("TEST_DATABASE_URL"))
if err != nil {
fmt.Println("Error preparing database connection for migration: ", err)
return 1
}
driver, err := postgres.WithInstance(db, &postgres.Config{})
if err != nil {
fmt.Println("Error preparing database driver for migration: ", err)
return 1
}
migration, err := migrate.NewWithDatabaseInstance("file://migrations", "postgres", driver)
if err != nil {
fmt.Println("Error during migration: ", err)
return 1
}
migration.Up()
err = db.Close()
if err != nil {
fmt.Println("Error closing connection after migration: ", err)
return 1
}
testDbConn, err = pgx.Connect(context.Background(), os.Getenv("TEST_DATABASE_URL"))
if err != nil {
fmt.Println("Error connecting to the database: ", err)
return 1
}
defer testDbConn.Close(context.Background())
return m.Run()
}
func registerCleanup(t *testing.T) {
t.Cleanup(func() {
_, err := testDbConn.Exec(context.Background(), "TRUNCATE TABLE users;")
if err != nil {
t.Error("Error cleaning up after test: ", err)
}
})
}
func TestAddUser(t *testing.T) {
registerCleanup(t)
message := &discordgo.Message{Content: "catan! adduser hinata", GuildID: "1"}
messageCreate := &discordgo.MessageCreate{message}
sender := &MessageSenderMock{}
messageParser := &discordMessageParser{discordMessage: messageCreate}
db := &postgresDataLayer{testDbConn}
bot := catanBot{sender, messageParser, db}
bot.handleCommand()
expected := "Successfully added user: hinata"
if sender.messageSent != expected {
t.Errorf("\nGot: %s\nExpect: %s\n", sender.messageSent, expected)
}
}
func TestAddWin(t *testing.T) {
registerCleanup(t)
db := &postgresDataLayer{testDbConn}
db.addUser("kageyama", "1")
message := &discordgo.Message{Content: "catan! addwin kageyama", GuildID: "1"}
messageCreate := &discordgo.MessageCreate{message}
sender := &MessageSenderMock{}
messageParser := &discordMessageParser{discordMessage: messageCreate}
bot := catanBot{sender, messageParser, db}
bot.handleCommand()
expectedLeaderboard := `+------+----------+-----------+--------+-------+------+
| RANK | USERNAME | VICTORIES | POINTS | GAMES | PPG |
+------+----------+-----------+--------+-------+------+
| 1 | kageyama | 1 | 0 | 0 | 0.00 |
+------+----------+-----------+--------+-------+------+
`
expectedMessage := fmt.Sprintf("Congrats kageyama on the win! :tada:\n```%s```", expectedLeaderboard)
if sender.messageSent != expectedMessage {
t.Errorf("\nGot: %s\nExpect: %s\n", sender.messageSent, expectedMessage)
}
}
func TestShowLeaderboard(t *testing.T) {
registerCleanup(t)
db := &postgresDataLayer{testDbConn}
db.addUser("kageyama", "1")
db.addUser("oikawa", "1")
db.addWin("kageyama", "1")
db.addWin("oikawa", "1")
db.addWin("oikawa", "1")
message := &discordgo.Message{Content: "catan! leaderboard", GuildID: "1"}
messageCreate := &discordgo.MessageCreate{message}
sender := &MessageSenderMock{}
messageParser := &discordMessageParser{discordMessage: messageCreate}
bot := catanBot{sender, messageParser, db}
bot.handleCommand()
expectedLeaderboard := `+------+----------+-----------+--------+-------+------+
| RANK | USERNAME | VICTORIES | POINTS | GAMES | PPG |
+------+----------+-----------+--------+-------+------+
| 1 | oikawa | 2 | 0 | 0 | 0.00 |
| 2 | kageyama | 1 | 0 | 0 | 0.00 |
+------+----------+-----------+--------+-------+------+
`
expectedValue := fmt.Sprintf("```%s```", expectedLeaderboard)
if sender.messageSent != expectedValue {
t.Errorf("\nGot: %s\nExpect: %s\n", sender.messageSent, expectedValue)
}
}
func TestRecordGame(t *testing.T) {
registerCleanup(t)
db := &postgresDataLayer{testDbConn}
db.addUser("hinata", "1")
db.updateGameStats("hinata", "3", "1")
message := &discordgo.Message{Content: "catan! record hinata 10", GuildID: "1"}
messageCreate := &discordgo.MessageCreate{message}
sender := &MessageSenderMock{}
messageParser := &discordMessageParser{discordMessage: messageCreate}
bot := catanBot{sender, messageParser, db}
bot.handleCommand()
expectedLeaderboard := `+------+----------+-----------+--------+-------+------+
| RANK | USERNAME | VICTORIES | POINTS | GAMES | PPG |
+------+----------+-----------+--------+-------+------+
| 1 | hinata | 0 | 13 | 2 | 6.50 |
+------+----------+-----------+--------+-------+------+
`
expectedMessage := fmt.Sprintf("Added 10 points for hinata\n```%s```", expectedLeaderboard)
if sender.messageSent != expectedMessage {
t.Errorf("\nGot: %s\nExpect: %s\n", sender.messageSent, expectedMessage)
}
}
type MessageSenderMock struct {
messageSent string
messageEmbed *discordgo.MessageEmbed
}
func (sender *MessageSenderMock) sendMessage(message string) {
sender.messageSent = message
}
func (sender *MessageSenderMock) sendEmbedMessage(embed *discordgo.MessageEmbed) {
sender.messageEmbed = embed
}