Skip to content

Commit

Permalink
test: add test for store.Record
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 9, 2021
1 parent b766187 commit a2e8596
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion store/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ParseRecord(s string) (Record, error) {

ss := strings.SplitN(s, "\t", 5)
if len(ss) != 5 {
return Record{}, fmt.Errorf("unexpected value count")
return Record{}, fmt.Errorf("unexpected column count")
}

timestamp = ss[0]
Expand Down
84 changes: 84 additions & 0 deletions store/record_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package store_test

import (
"net/url"
"testing"
"time"

"github.com/macrat/ayd/store"
)

func TestRecord(t *testing.T) {
tokyo := time.FixedZone("UTC+9", +9*60*60)

tests := []struct {
String string
Record store.Record
Error string
}{
{
String: "2021-01-02T15:04:05+09:00\tOK\t123.456\tping:example.com\thello world",
Record: store.Record{
CheckedAt: time.Date(2021, 1, 2, 15, 4, 5, 0, tokyo),
Target: &url.URL{Scheme: "ping", Opaque: "example.com"},
Status: store.STATUS_OK,
Message: "hello world",
Latency: 123456 * time.Microsecond,
},
},
{
String: "2021-01-02T15:04:05+09:00\tOK\t123.456",
Error: "unexpected column count",
},
{
String: "2021-01-02T15:04:05+09:00\tOK\t123abc\tping:example.com\thello world",
Error: `strconv.ParseFloat: parsing "123abc": invalid syntax`,
},
{
String: "2021/01/02 15:04:05\tOK\t123.456\tping:example.com\thello world",
Error: `parsing time "2021/01/02 15:04:05" as "2006-01-02T15:04:05Z07:00": cannot parse "/01/02 15:04:05" as "-"`,
},
{
String: "2021-01-02T15:04:05+09:00\tOK\t123.456\t::invalid target::\thello world",
Error: `parse "::invalid target::": missing protocol scheme`,
},
}

for _, tt := range tests {
r, err := store.ParseRecord(tt.String)
if tt.Error != "" {
if err == nil || tt.Error != err.Error() {
t.Errorf("expected error when parse %#v\nexpected \"%s\" but got \"%s\"", tt.String, tt.Error, err)
}
continue
}
if err != nil {
t.Errorf("failed to parse %#v: %s", tt.String, err)
continue
}

if !r.CheckedAt.Equal(tt.Record.CheckedAt) {
t.Errorf("unexpected parsed timestamp\nexpected: %#v\n but got: %#v", tt.Record.CheckedAt, r.CheckedAt)
}

if tt.Record.Target.String() != r.Target.String() {
t.Errorf("unexpected parsed target\nexpected: %s\n but got: %s", tt.Record.Target, r.Target)
}

if tt.Record.Status != r.Status {
t.Errorf("unexpected parsed status\nexpected: %s\n but got: %s", tt.Record.Status, r.Status)
}

if tt.Record.Latency != r.Latency {
t.Errorf("unexpected parsed latency\nexpected: %#v\n but got: %#v", tt.Record.Latency, r.Latency)
}

if tt.Record.Message != r.Message {
t.Errorf("unexpected parsed message\nexpected: %#v\n but got: %#v", tt.Record.Message, r.Message)
}

if tt.Record.String() != tt.String {
t.Errorf("expected: %#v\n but got: %#v", tt.String, tt.Record.String())
}
}
}

0 comments on commit a2e8596

Please sign in to comment.