-
Notifications
You must be signed in to change notification settings - Fork 2
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
2 changed files
with
85 additions
and
1 deletion.
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
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,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()) | ||
} | ||
} | ||
} |