-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
196 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/devops-works/slowql" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func Test_newApp(t *testing.T) { | ||
type args struct { | ||
loglevel string | ||
kind string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
loglevel logrus.Level | ||
kind slowql.Kind | ||
wantErr bool | ||
}{ | ||
{name: "trace - mariadb", args: args{loglevel: "trace", kind: "mariadb"}, | ||
loglevel: logrus.TraceLevel, kind: slowql.MariaDB, wantErr: false}, | ||
{name: "debug - pxc", args: args{loglevel: "debug", kind: "pxc"}, | ||
loglevel: logrus.DebugLevel, kind: slowql.PXC, wantErr: false}, | ||
{name: "info - mysql", args: args{loglevel: "info", kind: "mysql"}, | ||
loglevel: logrus.InfoLevel, kind: slowql.MySQL, wantErr: false}, | ||
{name: "warn - mysql", args: args{loglevel: "warn", kind: "mysql"}, | ||
loglevel: logrus.WarnLevel, kind: slowql.MySQL, wantErr: false}, | ||
{name: "error - mysql", args: args{loglevel: "error", kind: "mysql"}, | ||
loglevel: logrus.ErrorLevel, kind: slowql.MySQL, wantErr: false}, | ||
{name: "fatal - mysql", args: args{loglevel: "fatal", kind: "mysql"}, | ||
loglevel: logrus.FatalLevel, kind: slowql.MySQL, wantErr: false}, | ||
{name: "unknown - mysql", args: args{loglevel: "foobar", kind: "mysql"}, | ||
loglevel: logrus.InfoLevel, kind: slowql.MySQL, wantErr: true}, | ||
{name: "info - unknown", args: args{loglevel: "info", kind: "plop"}, | ||
loglevel: logrus.InfoLevel, kind: slowql.MySQL, wantErr: true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := newApp(tt.args.loglevel, tt.args.kind) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("newApp() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if tt.wantErr == true { | ||
return | ||
} | ||
if got.logger.Level != tt.loglevel { | ||
t.Errorf("wrong log level: newApp() = %v, want %v", got.logger.Level, tt.loglevel) | ||
} | ||
if got.kind != tt.kind { | ||
t.Errorf("wrong kind: newApp() = %v, want %v", got.kind, tt.kind) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/devops-works/slowql/server" | ||
) | ||
|
||
func Test_getMeta(t *testing.T) { | ||
type args struct { | ||
srv server.Server | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want serverMeta | ||
}{ | ||
{ | ||
name: "test", | ||
args: args{ | ||
srv: server.Server{ | ||
Binary: "mybin", | ||
Port: 1337, | ||
Socket: "nice socks dude", | ||
Version: "1.2.3-leet", | ||
VersionShort: "1.2.3", | ||
VersionDescription: "some leet version", | ||
}, | ||
}, | ||
want: serverMeta{ | ||
Binary: "mybin", | ||
Port: 1337, | ||
Socket: "nice socks dude", | ||
Version: "1.2.3-leet", | ||
VersionShort: "1.2.3", | ||
VersionDescription: "some leet version", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := getMeta(tt.args.srv); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("getMeta() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_options_parse(t *testing.T) { | ||
type fields struct { | ||
logfile string | ||
loglevel string | ||
kind string | ||
top int | ||
order string | ||
dec bool | ||
nocache bool | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
wantErr bool | ||
}{ | ||
{name: "working", fields: fields{logfile: "file", kind: "mysql", top: 1337, order: "random"}, wantErr: false}, | ||
{name: "no logfile", fields: fields{kind: "mysql", top: 1337, order: "random"}, wantErr: true}, | ||
{name: "no kind", fields: fields{logfile: "file", top: 1337, order: "random"}, wantErr: true}, | ||
{name: "incorrect top", fields: fields{logfile: "file", kind: "mysql", top: -1000, order: "random"}, wantErr: true}, | ||
{name: "incorrect order", fields: fields{logfile: "file", kind: "mysql", top: -1000, order: "incorrect"}, wantErr: true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
o := &options{ | ||
logfile: tt.fields.logfile, | ||
loglevel: tt.fields.loglevel, | ||
kind: tt.fields.kind, | ||
top: tt.fields.top, | ||
order: tt.fields.order, | ||
dec: tt.fields.dec, | ||
nocache: tt.fields.nocache, | ||
} | ||
got := o.parse() | ||
|
||
if len(got) > 0 && tt.wantErr == false { | ||
t.Errorf("options.parse() = %v, want %v", got, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_fsecsToDuration(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
d float64 | ||
want time.Duration | ||
}{ | ||
{name: "second", d: 1.0, want: time.Second}, | ||
{name: "millisecond", d: 0.001, want: time.Millisecond}, | ||
{name: "microsecond", d: 0.000001, want: time.Microsecond}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := fsecsToDuration(tt.d); got != tt.want { | ||
t.Errorf("fsecsToDuration() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_hash(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
q string | ||
want string | ||
}{ | ||
{name: "foobar", q: "foobar", want: "3858f62230ac3c915f300c664312c63f"}, | ||
{name: "some long string", q: "some long string", want: "2fb66bbfb88cdf9e07a3f1d1dfad71ab"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := hash(tt.q); got != tt.want { | ||
t.Errorf("hash() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |