Skip to content

Commit

Permalink
wrote tests for databases
Browse files Browse the repository at this point in the history
  • Loading branch information
eze-kiel committed Apr 19, 2021
1 parent c6b53a3 commit 6f03df2
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 19 deletions.
26 changes: 13 additions & 13 deletions database/mariadb/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (db *Database) parseMariaDBHeader(line string, q *query.Query) {
}
}

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

if len(matches) != 5 {
p.srv.Binary = "unable to parse line"
p.srv.VersionShort = p.srv.Binary
p.srv.Version = p.srv.Binary
p.srv.VersionDescription = p.srv.Binary
p.srv.Port = 0
p.srv.Socket = p.srv.Binary
db.srv.Binary = "unable to parse line"
db.srv.VersionShort = db.srv.Binary
db.srv.Version = db.srv.Binary
db.srv.VersionDescription = db.srv.Binary
db.srv.Port = 0
db.srv.Socket = db.srv.Binary
} else {
p.srv.Binary = matches[1]
p.srv.VersionShort = matches[2]
p.srv.Version = p.srv.VersionShort + matches[3]
p.srv.VersionDescription = matches[4]
p.srv.Port, _ = strconv.Atoi(strings.Split(net, " ")[2])
p.srv.Socket = strings.TrimLeft(strings.Split(net, ":")[2], " ")
db.srv.Binary = matches[1]
db.srv.VersionShort = matches[2]
db.srv.Version = db.srv.VersionShort + matches[3]
db.srv.VersionDescription = matches[4]
db.srv.Port, _ = strconv.Atoi(strings.Split(net, " ")[2])
db.srv.Socket = strings.TrimLeft(strings.Split(net, ":")[2], " ")
}

return
Expand Down
186 changes: 186 additions & 0 deletions database/mariadb/mariadb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package mariadb

import (
"testing"
"time"

"github.com/devops-works/slowql/query"
"github.com/devops-works/slowql/server"
)

// parseTime is a helper function that allow us to cast a string into a time.Time
// value in the tests
func parseTime(t string) time.Time {
time, _ := time.Parse("060102 15:04:05", t)
return time
}
func TestDatabase_parseMariaDBHeader(t *testing.T) {
type args struct {
line string
}
tests := []struct {
name string
args args
refQuery query.Query
}{
{
name: "time",
args: args{
line: "# Time: 210323 11:31:57",
},
refQuery: query.Query{
Time: parseTime("210323 11:31:57"),
},
},
{
name: "user, host",
args: args{
line: "# User@Host: hugo[hugo] @ [172.18.0.3]",
},
refQuery: query.Query{
User: "hugo",
Host: "172.18.0.3",
},
},
{
name: "id, schema", // QC_Hit is not parsed yet
args: args{
line: "# Thread_id: 12794 Schema: QC_hit: No",
},
refQuery: query.Query{
ID: 12794,
Schema: "",
},
},
{
name: "query time, lock time, rows sent, rows examined",
args: args{
line: "# Query_time: 0.000035 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0",
},
refQuery: query.Query{
QueryTime: 0.000035,
LockTime: 0.000000,
RowsSent: 0,
RowsExamined: 0,
},
},
{
name: "rows affected, bytes sent",
args: args{
line: "# Rows_affected: 0 Bytes_sent: 11",
},
refQuery: query.Query{
RowsAffected: 0,
BytesSent: 11,
},
},
}
for _, tt := range tests {
db := New(nil)
t.Run(tt.name, func(t *testing.T) {
q := query.Query{}
db.parseMariaDBHeader(tt.args.line, &q)
if q != tt.refQuery {
t.Errorf("got = %v, want %v", q, tt.refQuery)
}
})
}
}

func TestDatabase_ParseServerMeta(t *testing.T) {
tests := []struct {
name string
lines []string
refSrv server.Server
}{
{
name: "parsable",
lines: []string{
"/opt/bitnami/mariadb/sbin/mysqld, Version: 10.5.9-MariaDB (Source distribution). started with:",
"Tcp port: 3306 Unix socket: /opt/bitnami/mariadb/tmp/mysql.sock",
"Time Id Command Argument",
},
refSrv: server.Server{
Binary: "/opt/bitnami/mariadb/sbin/mysqld",
Port: 3306,
Socket: "/opt/bitnami/mariadb/tmp/mysql.sock",
Version: "10.5.9-MariaDB",
VersionShort: "10.5.9",
VersionDescription: "Source distribution",
},
},
{
name: "unparsable",
lines: []string{"Version: 8.0.23 (MySQL Community Server - GPL). started with:",
"Tcp port: 3306",
"Time Id Command Argument"},
refSrv: server.Server{
Binary: "unable to parse line",
Port: 0,
Socket: "unable to parse line",
Version: "unable to parse line",
VersionShort: "unable to parse line",
VersionDescription: "unable to parse line",
},
},
}
for _, tt := range tests {
lines := make(chan []string, 2)
t.Run(tt.name, func(t *testing.T) {
db := New(nil)
lines <- tt.lines
db.ParseServerMeta(lines)
if db.srv != tt.refSrv {
t.Errorf("got = %v, want = %v", db.srv, tt.refSrv)
}
})
}
}

func TestDatabase_ParseBlocs(t *testing.T) {
tests := []struct {
name string
bloc []string
refQuery query.Query
}{
{
name: "testing",
bloc: []string{
"# Time: 210323 11:31:57",
"# User@Host: hugo[hugo] @ [172.18.0.3]",
"# Thread_id: 12794 Schema: QC_hit: No",
"# Query_time: 0.000035 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0",
"# Rows_affected: 0 Bytes_sent: 11",
"SET timestamp=1616499117;",
"SET NAMES utf8mb4;",
},
refQuery: query.Query{
Time: parseTime("210323 11:31:57"),
User: "hugo",
Host: "172.18.0.3",
ID: 12794,
Schema: "",
QueryTime: 0.000035,
LockTime: 0.000000,
RowsSent: 0,
RowsExamined: 0,
RowsAffected: 0,
BytesSent: 11,
Query: "SET timestamp=1616499117;SET NAMES utf8mb4;",
},
},
}
for _, tt := range tests {
rawBlocs := make(chan []string, 10)
qc := make(chan query.Query)
db := New(qc)
t.Run(tt.name, func(t *testing.T) {
rawBlocs <- tt.bloc
go db.ParseBlocs(rawBlocs)
q := <-db.WaitingList
if q != tt.refQuery {
t.Errorf("got = %v, want = %v", q, tt.refQuery)
}
})
}
}
6 changes: 0 additions & 6 deletions database/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func TestDatabase_ParseServerMeta(t *testing.T) {
}

func TestDatabase_ParseBlocs(t *testing.T) {
t.Log("beginning of test")
tests := []struct {
name string
bloc []string
Expand Down Expand Up @@ -172,18 +171,13 @@ func TestDatabase_ParseBlocs(t *testing.T) {
},
},
}
t.Log("before for")
for _, tt := range tests {
rawBlocs := make(chan []string, 10)
t.Log("rawBlocs channel created")
qc := make(chan query.Query)
db := New(qc)
t.Log("db created")
t.Run(tt.name, func(t *testing.T) {
rawBlocs <- tt.bloc
t.Log("test bloc sent to rawBlocs")
go db.ParseBlocs(rawBlocs)
t.Log("bloc parsed")
q := <-db.WaitingList
if q != tt.refQuery {
t.Errorf("got = %v, want = %v", q, tt.refQuery)
Expand Down

0 comments on commit 6f03df2

Please sign in to comment.