Skip to content

Commit 6f03df2

Browse files
committed
wrote tests for databases
1 parent c6b53a3 commit 6f03df2

3 files changed

Lines changed: 199 additions & 19 deletions

File tree

database/mariadb/mariadb.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (db *Database) parseMariaDBHeader(line string, q *query.Query) {
140140
}
141141
}
142142

143-
func (p *Database) ParseServerMeta(lines chan []string) {
143+
func (db *Database) ParseServerMeta(lines chan []string) {
144144
for {
145145
select {
146146
case header := <-lines:
@@ -152,19 +152,19 @@ func (p *Database) ParseServerMeta(lines chan []string) {
152152
matches := versionre.FindStringSubmatch(versions)
153153

154154
if len(matches) != 5 {
155-
p.srv.Binary = "unable to parse line"
156-
p.srv.VersionShort = p.srv.Binary
157-
p.srv.Version = p.srv.Binary
158-
p.srv.VersionDescription = p.srv.Binary
159-
p.srv.Port = 0
160-
p.srv.Socket = p.srv.Binary
155+
db.srv.Binary = "unable to parse line"
156+
db.srv.VersionShort = db.srv.Binary
157+
db.srv.Version = db.srv.Binary
158+
db.srv.VersionDescription = db.srv.Binary
159+
db.srv.Port = 0
160+
db.srv.Socket = db.srv.Binary
161161
} else {
162-
p.srv.Binary = matches[1]
163-
p.srv.VersionShort = matches[2]
164-
p.srv.Version = p.srv.VersionShort + matches[3]
165-
p.srv.VersionDescription = matches[4]
166-
p.srv.Port, _ = strconv.Atoi(strings.Split(net, " ")[2])
167-
p.srv.Socket = strings.TrimLeft(strings.Split(net, ":")[2], " ")
162+
db.srv.Binary = matches[1]
163+
db.srv.VersionShort = matches[2]
164+
db.srv.Version = db.srv.VersionShort + matches[3]
165+
db.srv.VersionDescription = matches[4]
166+
db.srv.Port, _ = strconv.Atoi(strings.Split(net, " ")[2])
167+
db.srv.Socket = strings.TrimLeft(strings.Split(net, ":")[2], " ")
168168
}
169169

170170
return

database/mariadb/mariadb_test.go

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package mariadb
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/devops-works/slowql/query"
8+
"github.com/devops-works/slowql/server"
9+
)
10+
11+
// parseTime is a helper function that allow us to cast a string into a time.Time
12+
// value in the tests
13+
func parseTime(t string) time.Time {
14+
time, _ := time.Parse("060102 15:04:05", t)
15+
return time
16+
}
17+
func TestDatabase_parseMariaDBHeader(t *testing.T) {
18+
type args struct {
19+
line string
20+
}
21+
tests := []struct {
22+
name string
23+
args args
24+
refQuery query.Query
25+
}{
26+
{
27+
name: "time",
28+
args: args{
29+
line: "# Time: 210323 11:31:57",
30+
},
31+
refQuery: query.Query{
32+
Time: parseTime("210323 11:31:57"),
33+
},
34+
},
35+
{
36+
name: "user, host",
37+
args: args{
38+
line: "# User@Host: hugo[hugo] @ [172.18.0.3]",
39+
},
40+
refQuery: query.Query{
41+
User: "hugo",
42+
Host: "172.18.0.3",
43+
},
44+
},
45+
{
46+
name: "id, schema", // QC_Hit is not parsed yet
47+
args: args{
48+
line: "# Thread_id: 12794 Schema: QC_hit: No",
49+
},
50+
refQuery: query.Query{
51+
ID: 12794,
52+
Schema: "",
53+
},
54+
},
55+
{
56+
name: "query time, lock time, rows sent, rows examined",
57+
args: args{
58+
line: "# Query_time: 0.000035 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0",
59+
},
60+
refQuery: query.Query{
61+
QueryTime: 0.000035,
62+
LockTime: 0.000000,
63+
RowsSent: 0,
64+
RowsExamined: 0,
65+
},
66+
},
67+
{
68+
name: "rows affected, bytes sent",
69+
args: args{
70+
line: "# Rows_affected: 0 Bytes_sent: 11",
71+
},
72+
refQuery: query.Query{
73+
RowsAffected: 0,
74+
BytesSent: 11,
75+
},
76+
},
77+
}
78+
for _, tt := range tests {
79+
db := New(nil)
80+
t.Run(tt.name, func(t *testing.T) {
81+
q := query.Query{}
82+
db.parseMariaDBHeader(tt.args.line, &q)
83+
if q != tt.refQuery {
84+
t.Errorf("got = %v, want %v", q, tt.refQuery)
85+
}
86+
})
87+
}
88+
}
89+
90+
func TestDatabase_ParseServerMeta(t *testing.T) {
91+
tests := []struct {
92+
name string
93+
lines []string
94+
refSrv server.Server
95+
}{
96+
{
97+
name: "parsable",
98+
lines: []string{
99+
"/opt/bitnami/mariadb/sbin/mysqld, Version: 10.5.9-MariaDB (Source distribution). started with:",
100+
"Tcp port: 3306 Unix socket: /opt/bitnami/mariadb/tmp/mysql.sock",
101+
"Time Id Command Argument",
102+
},
103+
refSrv: server.Server{
104+
Binary: "/opt/bitnami/mariadb/sbin/mysqld",
105+
Port: 3306,
106+
Socket: "/opt/bitnami/mariadb/tmp/mysql.sock",
107+
Version: "10.5.9-MariaDB",
108+
VersionShort: "10.5.9",
109+
VersionDescription: "Source distribution",
110+
},
111+
},
112+
{
113+
name: "unparsable",
114+
lines: []string{"Version: 8.0.23 (MySQL Community Server - GPL). started with:",
115+
"Tcp port: 3306",
116+
"Time Id Command Argument"},
117+
refSrv: server.Server{
118+
Binary: "unable to parse line",
119+
Port: 0,
120+
Socket: "unable to parse line",
121+
Version: "unable to parse line",
122+
VersionShort: "unable to parse line",
123+
VersionDescription: "unable to parse line",
124+
},
125+
},
126+
}
127+
for _, tt := range tests {
128+
lines := make(chan []string, 2)
129+
t.Run(tt.name, func(t *testing.T) {
130+
db := New(nil)
131+
lines <- tt.lines
132+
db.ParseServerMeta(lines)
133+
if db.srv != tt.refSrv {
134+
t.Errorf("got = %v, want = %v", db.srv, tt.refSrv)
135+
}
136+
})
137+
}
138+
}
139+
140+
func TestDatabase_ParseBlocs(t *testing.T) {
141+
tests := []struct {
142+
name string
143+
bloc []string
144+
refQuery query.Query
145+
}{
146+
{
147+
name: "testing",
148+
bloc: []string{
149+
"# Time: 210323 11:31:57",
150+
"# User@Host: hugo[hugo] @ [172.18.0.3]",
151+
"# Thread_id: 12794 Schema: QC_hit: No",
152+
"# Query_time: 0.000035 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0",
153+
"# Rows_affected: 0 Bytes_sent: 11",
154+
"SET timestamp=1616499117;",
155+
"SET NAMES utf8mb4;",
156+
},
157+
refQuery: query.Query{
158+
Time: parseTime("210323 11:31:57"),
159+
User: "hugo",
160+
Host: "172.18.0.3",
161+
ID: 12794,
162+
Schema: "",
163+
QueryTime: 0.000035,
164+
LockTime: 0.000000,
165+
RowsSent: 0,
166+
RowsExamined: 0,
167+
RowsAffected: 0,
168+
BytesSent: 11,
169+
Query: "SET timestamp=1616499117;SET NAMES utf8mb4;",
170+
},
171+
},
172+
}
173+
for _, tt := range tests {
174+
rawBlocs := make(chan []string, 10)
175+
qc := make(chan query.Query)
176+
db := New(qc)
177+
t.Run(tt.name, func(t *testing.T) {
178+
rawBlocs <- tt.bloc
179+
go db.ParseBlocs(rawBlocs)
180+
q := <-db.WaitingList
181+
if q != tt.refQuery {
182+
t.Errorf("got = %v, want = %v", q, tt.refQuery)
183+
}
184+
})
185+
}
186+
}

database/mysql/mysql_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ func TestDatabase_ParseServerMeta(t *testing.T) {
138138
}
139139

140140
func TestDatabase_ParseBlocs(t *testing.T) {
141-
t.Log("beginning of test")
142141
tests := []struct {
143142
name string
144143
bloc []string
@@ -172,18 +171,13 @@ func TestDatabase_ParseBlocs(t *testing.T) {
172171
},
173172
},
174173
}
175-
t.Log("before for")
176174
for _, tt := range tests {
177175
rawBlocs := make(chan []string, 10)
178-
t.Log("rawBlocs channel created")
179176
qc := make(chan query.Query)
180177
db := New(qc)
181-
t.Log("db created")
182178
t.Run(tt.name, func(t *testing.T) {
183179
rawBlocs <- tt.bloc
184-
t.Log("test bloc sent to rawBlocs")
185180
go db.ParseBlocs(rawBlocs)
186-
t.Log("bloc parsed")
187181
q := <-db.WaitingList
188182
if q != tt.refQuery {
189183
t.Errorf("got = %v, want = %v", q, tt.refQuery)

0 commit comments

Comments
 (0)