-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcopy_test.go
141 lines (125 loc) · 3.28 KB
/
copy_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
package redisql
import (
"database/sql"
"flag"
"testing"
"os"
"fmt"
"github.com/DGKSK8LIFE/redisql/utils"
"github.com/go-redis/redis/v8"
)
var createTableMySQL = `
CREATE TABLE IF NOT EXISTS user (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
uuid VARCHAR(64) NOT NULL,
height VARCHAR(5) NOT NULL,
shoesize TINYINT NOT NULL,
age TINYINT NOT NULL,
bio TEXT NOT NULL,
friends_count TINYINT NOT NULL,
favorite_animal VARCHAR(20) NOT NULL,
favorite_color VARCHAR(10) NOT NULL,
favorite_food VARCHAR(20) NOT NULL,
mobile_phone VARCHAR(50) NOT NULL
)
`
var createTablePostgres = `
CREATE TABLE IF NOT EXISTS user_table (
id SERIAL PRIMARY KEY,
name character varying NOT NULL,
uuid uuid NOT NULL,
height character varying NOT NULL,
shoesize integer NOT NULL,
age integer NOT NULL,
favorite_animal character varying(20) NOT NULL,
friends_count integer NOT NULL,
favorite_color character varying(50) NOT NULL,
favorite_food character varying(50) NOT NULL,
mobile_phone character varying(50) NOT NULL,
bio character varying NOT NULL
)
`
var config utils.Config
var rdb *redis.Client
func TestMain(m *testing.M) {
config = utils.Config{
SQLType: "",
SQLUser: "root",
SQLPassword: "password",
SQLDatabase: "users",
SQLHost: "localhost",
SQLPort: "3306",
SQLTable: "user",
RedisAddr: "localhost:6379",
RedisPass: "",
}
var rows int
flag.StringVar(&config.SQLType, "db", "mysql", "postgres or mysql")
flag.IntVar(&rows, "rows", 1000, "number of rows to insert before redisql tests run")
flag.Parse()
fmt.Println("Preparing Test...")
var db *sql.DB
var err error
switch config.SQLType {
case "mysql":
db, err = utils.OpenMySQL(config.SQLUser, config.SQLPassword, config.SQLDatabase, config.SQLHost, config.SQLPort)
if err != nil {
panic(err)
}
_, err = db.Exec(createTableMySQL)
if err != nil {
panic(err)
}
case "postgres":
config.SQLPort = "5432"
config.SQLTable = "user_table"
db, err = utils.OpenPostgres(config.SQLUser, config.SQLPassword, config.SQLDatabase, config.SQLHost, config.SQLPort)
if err != nil {
panic(err)
}
_, err = db.Exec(createTablePostgres)
if err != nil {
panic(err)
}
}
defer db.Close()
_, err = db.Exec(fmt.Sprintf(`DELETE FROM %s`, config.SQLTable))
if err != nil {
panic(err)
}
for i := 0; i < rows; i++ {
_, err = db.Exec(fmt.Sprintf(`INSERT INTO %s (name, uuid, height, shoesize, age, bio, friends_count, favorite_animal, favorite_color, favorite_food, mobile_phone) VALUES ('martin', 'f8d1c837-719f-42a9-9a37-0e2ed7c0e458', '5,9', 9, 15, 'Student and Developer', 100, 'horse', 'red', 'apple', '555-555-5555')`, config.SQLTable))
if err != nil {
panic(err)
}
}
rdb = utils.OpenRedis(config.RedisAddr, config.RedisPass)
defer rdb.Close()
rdb.FlushAll(utils.CTX)
exitVal := m.Run()
os.Exit(exitVal)
}
func TestCopyToString(t *testing.T) {
err := config.CopyToString()
if err != nil {
t.Error(err)
t.Fail()
}
}
func TestCopyToList(t *testing.T) {
rdb.FlushAll(utils.CTX)
err := config.CopyToList()
if err != nil {
t.Error(err)
t.Fail()
}
}
func TestCopyToHash(t *testing.T) {
rdb.FlushAll(utils.CTX)
err := config.CopyToHash()
if err != nil {
t.Error(err)
t.Fail()
}
}