-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
115 lines (97 loc) · 2.92 KB
/
db_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
package main
import (
"fmt"
"github.com/axieinfinity/susanoo/database/postgresql/gorm"
"github.com/axieinfinity/susanoo/log/logger/zerolog"
"github.com/axieinfinity/susanoo/x/viper"
zerolog2 "github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
gorm2 "gorm.io/gorm"
"testing"
)
func initDB() (*gorm2.DB, error) {
loggerProvider, err := zerolog.NewExportPipeline(
zerolog.Options.Level(zerolog2.DebugLevel),
zerolog.Options.OutputPath("/dev/stdout"),
)
if err != nil {
return nil, err
}
v, err := viper.NewViperFrom("./")
if err != nil {
return nil, err
}
return gorm.NewClusters(
gorm.ClustersOptions.LoadConfig(v.Sub("gorm")),
gorm.ClustersOptions.Logger(loggerProvider.Logger("GORM")),
)
}
// TestWriteToGamesTable executes a write query to "games" table.
// >> Watch for logs in "games" MASTER container to see that the query is executed there.
func TestWriteToGamesTable(t *testing.T) {
db, err := initDB()
assert.NoError(t, err)
// Write query: add a game
game := &Game{
Name: "Axie Infinity",
AdminID: 1,
}
err = db.Create(game).Error
assert.NoError(t, err)
fmt.Printf("Write to games table: %+v\n", game)
}
// TestReadFromGamesTableWithPreload executes a 2 read queries to "games" and "users" table.
// RUN TestWriteToGameTable above FIRST before running this test.
// >> Watch for logs in "users" SLAVE & "games" SLAVE container to see that the queries are executed there.
func TestReadFromGamesTableWithPreload(t *testing.T) {
db, err := initDB()
assert.NoError(t, err)
// Read query: get a game preload with user
var game Game
err = db.
Table("games").
Preload("Admin").
Where("name = ?", "Axie Infinity").
First(&game).Error
assert.NoError(t, err)
assert.Equal(t, "Axie Infinity", game.Name)
assert.Equal(t, 1, game.AdminID)
assert.NotNil(t, game.Admin)
assert.Equal(t, "Alice", game.Admin.Name)
fmt.Printf("Read from games table with preload: %+v, admin:%+v\n", game, game.Admin)
}
// TestWriteToUsersTable executes a write query to "users" table.
// >> Watch for logs in "users" MASTER container to see that the query is executed there.
func TestWriteToUsersTable(t *testing.T) {
db, err := initDB()
assert.NoError(t, err)
// Write query: add a user
user := &User{
Name: "Dave",
}
err = db.Create(user).Error
assert.NoError(t, err)
fmt.Printf("Write to users table: %+v\n", user)
}
// TestReadFromUsersTable executes a read query to "users" table.
// >> Watch for logs in "users" SLAVE container to see that the query is executed there.
func TestReadFromUsersTable(t *testing.T) {
db, err := initDB()
assert.NoError(t, err)
// Read query: get a user
var user User
err = db.First(&user).Error
assert.NoError(t, err)
assert.Equal(t, "Alice", user.Name)
fmt.Printf("Read from users table: %+v\n", user)
}
type User struct {
ID int `gorm:"primary_key"`
Name string
}
type Game struct {
ID int `gorm:"primary_key"`
Name string
AdminID int
Admin *User
}