-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
47 lines (38 loc) · 1.33 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
package main
import (
"log"
"os"
"testing"
"github.com/nireo/upfi/middleware"
"github.com/joho/godotenv"
"github.com/nireo/upfi/models"
)
// TestMain setups the database connection such that the http handlers work properly. Also disabled the http logging,
// since test output looks quite confusing with http logging in the same place.
func TestMain(m *testing.M) {
if err := godotenv.Load(); err != nil {
// Stop the execution, since we need all of the environment varialbes
log.Fatal("Could not load the env file")
}
// Store most of the environment varialbes into normal variables, so that the database connection
// line becomes more easier to read.
databaseConfig := &models.DatabaseConfig{
User: os.Getenv("db_username"),
Port: os.Getenv("db_port"),
Host: os.Getenv("db_host"),
Name: os.Getenv("db_name"),
}
// Use a library function to setup the database connection. Also migrates the models
// and sets a global database variable in the lib package.
if err := models.ConnectToDatabase(databaseConfig); err != nil {
log.Fatal(err)
}
// Disable http logging
middleware.SetHTTPLogging(false)
// Run all the tests
exitCode := m.Run()
// Remove all the information created during the testing. NOTE: Also removed all of the files and database data
// from every user.
models.ResetInformation()
os.Exit(exitCode)
}