Skip to content

Commit

Permalink
created tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eze-kiel committed Feb 24, 2021
1 parent f2c97a3 commit 4ce5285
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
32 changes: 16 additions & 16 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ func main() {
if q.Query == "" {
break
}
fmt.Printf("Time: %s\nUser: %s\nHost: %s\nID: %d\nSchema: %s\nLast_errno: %d\nKilled: %d\nQuery_time: %s\nLock_time: %s\nRows_sent: %d\nRows_examined: %d\nRows_affected: %d\nBytes_sent: %d\nQuery: %s\n",
q.Time,
q.User,
q.Host,
q.ID,
q.Schema,
q.LastErrNo,
q.Killed,
q.QueryTime,
q.LockTime,
q.RowsSent,
q.RowsExamined,
q.RowsAffected,
q.BytesSent,
q.Query,
)
// fmt.Printf("Time: %s\nUser: %s\nHost: %s\nID: %d\nSchema: %s\nLast_errno: %d\nKilled: %d\nQuery_time: %s\nLock_time: %s\nRows_sent: %d\nRows_examined: %d\nRows_affected: %d\nBytes_sent: %d\nQuery: %s\n",
// q.Time,
// q.User,
// q.Host,
// q.ID,
// q.Schema,
// q.LastErrNo,
// q.Killed,
// q.QueryTime,
// q.LockTime,
// q.RowsSent,
// q.RowsExamined,
// q.RowsAffected,
// q.BytesSent,
// q.Query,
// )
count++
}

Expand Down
9 changes: 6 additions & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ type Query struct {
}

// Fingerprint returns Query.query's MD5 fingerprint.
func (q Query) Fingerprint() string {
func (q Query) Fingerprint() (string, error) {
h := md5.New()
io.WriteString(h, q.Query)
return fmt.Sprintf("%x", h.Sum(nil))
_, err := io.WriteString(h, q.Query)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}

// parseHeader parses everything that begin with #
Expand Down
29 changes: 29 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package slowql

import "testing"

func TestQuery_Fingerprint(t *testing.T) {
tests := []struct {
name string
query string
want string
wantErr bool
}{
{name: "pass", query: "foobar", want: "3858f62230ac3c915f300c664312c63f", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q := Query{
Query: tt.query,
}
got, err := q.Fingerprint()
if (err != nil) != tt.wantErr {
t.Errorf("Query.Fingerprint() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Query.Fingerprint() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 4ce5285

Please sign in to comment.