-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathintegration_test_suite.go
87 lines (77 loc) · 2.22 KB
/
integration_test_suite.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
package testy
import (
"context"
"log"
"os"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"github.com/trustwallet/go-libs/cache/redis"
)
const (
testDbDsnEnvKey = "TEST_DB_DSN"
testRedisUrlEnvKey = "TEST_REDIS_URL"
)
// IntegrationTestSuite is an integration testing suite with methods
// for retrieving the real database and redis connection.
// Just absorb the built-in IntegrationTestSuite by defining your own suite,
// you can also use it along with `testify`'s suite.
// Example:
//
// type SomeTestSuite struct {
// suite.Suite
// IntegrationTestSuite
// }
type IntegrationTestSuite struct {
db *gorm.DB
redis *redis.Redis
}
// GetDb retrieves the current *gorm.DB connection, and it's lazy loaded.
func (s *IntegrationTestSuite) GetDb() *gorm.DB {
if s.db == nil {
db, err := NewIntegrationTestDb()
if err != nil {
log.Fatalln("can not connect integration test db", err)
}
s.db = db
}
return s.db
}
// GetRedis retrieves the current *redis.Redis connection, and it's lazy loaded.
func (s *IntegrationTestSuite) GetRedis() *redis.Redis {
if s.redis == nil {
r, err := NewIntegrationTestRedis()
if err != nil {
log.Fatalln("can not connect integration redis db", err)
}
s.redis = r
}
return s.redis
}
// NewIntegrationTestDb creates a *gorm.DB connection to a real database which is only for integration test.
// The DSN for test database connection should be set by defining the TEST_DB_DSN env.
func NewIntegrationTestDb() (*gorm.DB, error) {
return gorm.Open(
postgres.Open(MustGetTestDbDSN()),
&gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
SkipDefaultTransaction: true,
},
)
}
// NewIntegrationTestRedis creates a *redis.Redis connection to a real redis pool which is only for integration test.
// The url for test redis connection should be set by defining the TEST_REDIS_URL env.
func NewIntegrationTestRedis() (*redis.Redis, error) {
url, ok := os.LookupEnv(testRedisUrlEnvKey)
if !ok {
log.Fatalln(testRedisUrlEnvKey, "env not found")
}
return redis.Init(context.Background(), url)
}
func MustGetTestDbDSN() string {
dsn, ok := os.LookupEnv(testDbDsnEnvKey)
if !ok {
log.Fatal(testDbDsnEnvKey, "env not found")
}
return dsn
}