-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver_test.go
170 lines (136 loc) · 4.22 KB
/
resolver_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
package dbresolver
import (
"database/sql"
"testing"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/require"
)
func TestIsDML(t *testing.T) {
t.Run("valid queries", func(t *testing.T) {
createQuery := "CREATE TABLE users()"
require.Equal(t, isDML(createQuery), true)
selectQuery := "SELECT COUNT(1) FROM users"
require.Equal(t, isDML(selectQuery), false)
insertQuery := "INSERT INTO users VALUES()"
require.Equal(t, isDML(insertQuery), true)
updateQuery := `UPDATE users SET email="[email protected]"`
require.Equal(t, isDML(updateQuery), true)
deleteQuery := "DELETE FROM users"
require.Equal(t, isDML(deleteQuery), true)
selectShareQuery := "SELECT * FROM FOR UPDATE"
require.Equal(t, isDML(selectShareQuery), true)
selectShareQuery = "SELECT * FROM FOR SHARE"
require.Equal(t, isDML(selectShareQuery), true)
})
t.Run("invalid queries, defaults to modification query, to allow select masteer db", func(t *testing.T) {
invalidQuery := "FOOBAR"
require.Equal(t, isDML(invalidQuery), true)
})
}
func setupTestDB() (*Database, error) {
masterDB, err := sql.Open("sqlite3", "./tmp/master.db")
if err != nil {
return nil, err
}
replicaDB, err := sql.Open("sqlite3", "./tmp/replica.db")
if err != nil {
return nil, err
}
// Create tables in master and replica to simulate behavior
if _, err := masterDB.Exec("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, name TEXT); DELETE FROM test;"); err != nil {
return nil, err
}
if _, err := replicaDB.Exec("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, name TEXT); DELETE FROM test;"); err != nil {
return nil, err
}
return Register(
DBConfig{
Master: AsMaster(masterDB, "master"),
Replicas: []*ResolverDB{AsReplica(replicaDB, "replica")},
},
), nil
}
func TestExec_WriteMode(t *testing.T) {
db, err := setupTestDB()
if err != nil {
t.Fatalf("failed to do whatever %v\n", err)
}
db = db.WithMode(DbWriteMode)
_, err = db.Exec("INSERT INTO test (name) VALUES (?)", "write-test")
if err != nil {
t.Fatalf("unexpected error executing write: %v", err)
}
rows, err := db.Query("SELECT name FROM test")
if err != nil {
t.Fatalf("failed to get rows %v\n", err)
}
if len(rows) == 0 {
t.Fatalf("expected row in master")
}
replicaRows, err := db.getReplica().DB.Query("SELECT name FROM test")
if err != nil {
t.Fatalf("failed to execute query from replica")
}
defer replicaRows.Close()
if replicaRows.Next() {
t.Fatalf("did not expect row in replica")
}
}
func TestQuery_ReadMode(t *testing.T) {
db, err := setupTestDB()
if err != nil {
t.Fatalf("failed to do whatever %v\n", err)
}
// Insert data directly into replica for testing
db.getReplica().DB.Exec("INSERT INTO test (name) VALUES (?)", "read-test")
rows, err := db.Query("SELECT name FROM test")
if err != nil {
t.Fatalf("unexpected error querying: %v", err)
}
if len(rows) == 0 {
t.Fatal("expected row in replica")
}
}
func Test_InvalidMode(t *testing.T) {
db, err := setupTestDB()
if err != nil {
t.Fatalf("failed to setup db. %v\n", err)
}
_, err = db.Exec("INSERT INTO test (name) VALUES ('switch-test-2')")
if err == nil {
t.Fatal("should have failed to insert in read mode")
}
}
func TestDBModeSwitch(t *testing.T) {
db, err := setupTestDB()
if err != nil {
t.Fatalf("failed to do whatever %v\n", err)
}
_, err = db.WithMode(DbWriteMode).Exec("INSERT INTO test (name) VALUES (?)", "switch-test")
if err != nil {
t.Fatalf("unexpected error executing write: %v", err)
}
rows, err := db.Query("SELECT name FROM test")
if err != nil {
t.Fatalf("failed to select name, %v\n", err)
}
if len(rows) != 0 {
t.Fatalf("did not expect row in replica")
}
rows, err = db.WithMode(DbWriteMode).Query("SELECT name FROM test")
if err != nil {
t.Fatalf("failed to select name, %v\n", err)
}
if len(rows) != 1 {
t.Fatalf("expected 1 row in master")
}
_, err = db.WithMode(DbWriteMode).Exec("INSERT INTO test (name) VALUES (?)", "switch-test-3")
if err != nil {
t.Fatalf("unexpected error executing write: %v", err)
}
// Ensure master has inserted and result is not cached by koalescer
rows, err = db.WithMode(DbWriteMode).Query("SELECT name FROM test")
if len(rows) != 2 {
t.Fatalf("expected 2 rows in master")
}
}