Skip to content

Commit

Permalink
couple of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d-dot-one committed Nov 9, 2023
1 parent 9e1bf72 commit 057be0e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
48 changes: 41 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"log"
"net/http"
"os"
"regexp"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -56,6 +57,35 @@ const (
// ErrContextTimeoutExceeded is an error message that is returned when the context has
// timed out.
var ErrContextTimeoutExceeded = errors.New("context timeout exceeded")
var ErrMalformedDate = errors.New("date format is malformed. should be YYYY-MM-DD")
var ErrRegexFailed = errors.New("regex failed")

// LogLevelForError is a type that describes the log level for an error message.
type LogLevelForError string

// LogMessage is the message that you would like to see in the log.
type LogMessage string

// YearMonthDay is a type that describes a date in the format YYYY-MM-DD.
type YearMonthDay string

// verify is a private helper function that will verify that the date is in the correct
// format. It will return a boolean value.
func (y YearMonthDay) verify() (bool, error) {
match, err := regexp.MatchString(`\d{4}-\d{2}-\d{2}`, y.String())
if err != nil {
return false, ErrRegexFailed
}
if !match {
return false, ErrMalformedDate
}
return true, nil
}

// String is a public helper function that will return the YearMonthDay object as a string.
func (y YearMonthDay) String() string {
return string(y)
}

// The ConvertTimeToEpoch help function can convert a string, formatted as a time.DateOnly
// object (2023-01-01) to a Unix epoch time in milliseconds. This can be helpful when you
Expand All @@ -65,11 +95,18 @@ var ErrContextTimeoutExceeded = errors.New("context timeout exceeded")
// Basic Usage:
//
// epochTime, err := ConvertTimeToEpoch("2023-01-01")
func ConvertTimeToEpoch(t string) (int64, error) {
parsed, err := time.Parse(time.DateOnly, t)
_ = CheckReturn(err, "unable to parse time", "warning")
func ConvertTimeToEpoch(ymd YearMonthDay) (int64, error) {
result, err := ymd.verify()
if err != nil {
return 0, ErrMalformedDate
}

return parsed.UnixMilli(), err
if result {
parsed, err := time.Parse(time.DateOnly, ymd.String())
_ = CheckReturn(err, "unable to parse time", "warning")
return parsed.UnixMilli(), err
}
return 0, ErrMalformedDate
}

// The CreateAwnClient function is used to create a new resty-based API client. This client
Expand Down Expand Up @@ -211,9 +248,6 @@ func GetHistoricalData(ctx context.Context, funcData FunctionData) ([]DeviceData
return deviceResponse, nil
}

type LogLevelForError string
type LogMessage string

// CheckReturn is a helper function to remove the usual error checking cruft while also
// logging the error message. It takes an error, a message and a log level as inputs and
// returns an error (can be nil of course).
Expand Down
37 changes: 26 additions & 11 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,49 @@ import (
)

func TestConvertTimeToEpoch(t *testing.T) {
type args struct {
t string
}
tests := []struct {
name string
t string
t YearMonthDay
want int64
}{
{"Test01Jan2014ToEpoch", "2014-01-01", 1388534400000},
{"Test15Nov2023ToEpoch", "2023-11-15", 1700006400000},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i, err := ConvertTimeToEpoch(tt.t)
err = CheckReturn(err, "Error converting time to epoch", "warning")
if err != nil {
t.Errorf("CheckReturn() error = %v", err)
}
if got, _ := ConvertTimeToEpoch(tt.t); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ConvertTimeToEpoch() = %v, want %v", got, tt.want)
}
})
}
}

func TestConvertTimeToEpochBadFormat(t *testing.T) {
t.Parallel()
tests := []struct {
name string
t YearMonthDay
want error
}{
{"TestWrongDateFormat", "11-15-2021", ErrMalformedDate},
{"TestWrongDateFormat", "11152021", ErrMalformedDate},
{"TestWrongDateFormat", "11-15-2021:12:42", ErrMalformedDate},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ConvertTimeToEpoch(tt.t)
if err == nil {
t.Errorf("ConvertTimeToEpoch() = %v, want %v", err, tt.want)
}
})
}
}

func TestCheckReturn(t *testing.T) {
t.Parallel()
type args struct {
e error
msg string
msg LogMessage
level LogLevelForError
}
tests := []struct {
Expand All @@ -47,8 +61,9 @@ func TestCheckReturn(t *testing.T) {
{"TestCheckReturnDebug", args{nil, "Debug log message", "debug"}},
{"TestCheckReturnInfo", args{nil, "Info log message", "info"}},
{"TestCheckReturnWarning", args{nil, "Warning log message", "warning"}},
{"TestCheckReturnFatal", args{nil, "Fatal log message", "fatal"}},
{"TestCheckReturnPanic", args{nil, "Panic log message", "panic"}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = CheckReturn(tt.args.e, tt.args.msg, tt.args.level)
Expand Down

0 comments on commit 057be0e

Please sign in to comment.