Skip to content

Commit

Permalink
unit test and fix date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
facundoolano committed Jul 27, 2024
1 parent e333b51 commit 8b76793
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func resolveWhereConditions(clauses []string) (map[string][]string, error) {
func parseDuration(duration string) (time.Time, error) {
t := NowTimeFun().UTC()
if duration != "now" {
re := regexp.MustCompile(`^(\d+)([smhdM])$`)
re := regexp.MustCompile(`^(\d+)([smhdwM])$`)
matches := re.FindStringSubmatch(duration)
if len(matches) != 3 {
return t, fmt.Errorf("invalid duration %s", duration)
Expand Down
67 changes: 66 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,68 @@ func TestFieldsParsing(t *testing.T) {
}

func TestDurationParsing(t *testing.T) {
var duration time.Time
var err error

now := NowTimeFun()
assertEqual(t, now, time.Date(2024, time.July, 24, 0, 7, 0, 0, time.UTC))

// default to now
// support each unit s, m, h, d, M
duration, err = parseDuration("now")
assertEqual(t, nil, err)
assertEqual(t, duration, now)

// support each unit s, m, h, d, w, M
duration, err = parseDuration("1s")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.July, 24, 0, 6, 59, 0, time.UTC))
duration, err = parseDuration("10s")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.July, 24, 0, 6, 50, 0, time.UTC))

duration, err = parseDuration("1m")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.July, 24, 0, 6, 0, 0, time.UTC))
duration, err = parseDuration("5m")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.July, 24, 0, 2, 0, 0, time.UTC))

duration, err = parseDuration("1h")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.July, 23, 23, 7, 0, 0, time.UTC))
duration, err = parseDuration("5h")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.July, 23, 19, 7, 0, 0, time.UTC))

duration, err = parseDuration("1d")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.July, 23, 0, 7, 0, 0, time.UTC))
duration, err = parseDuration("5d")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.July, 19, 0, 7, 0, 0, time.UTC))

duration, err = parseDuration("1w")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.July, 17, 0, 7, 0, 0, time.UTC))
duration, err = parseDuration("2w")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.July, 10, 0, 7, 0, 0, time.UTC))

// (months are just 30 days not actual calendar months)
duration, err = parseDuration("1M")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.June, 24, 0, 7, 0, 0, time.UTC))
duration, err = parseDuration("2M")
assertEqual(t, nil, err)
assertEqual(t, duration, time.Date(2024, time.May, 25, 0, 7, 0, 0, time.UTC))

// fail on unknown unit
duration, err = parseDuration("1x")

Check failure on line 74 in main_test.go

View workflow job for this annotation

GitHub Actions / build

this value of duration is never used (SA4006)
assert(t, err != nil)

// fail on bad syntax
duration, err = parseDuration("asdassd")

Check failure on line 78 in main_test.go

View workflow job for this annotation

GitHub Actions / build

this value of duration is never used (SA4006)
assert(t, err != nil)
}

func TestWhereConditionParsing(t *testing.T) {
Expand Down Expand Up @@ -210,6 +268,13 @@ func TestMain(m *testing.M) {
m.Run()
}

func assert(t *testing.T, cond bool) {
t.Helper()
if !cond {
t.Fatalf("condition is false")
}
}

func assertEqual(t *testing.T, a interface{}, b interface{}) {
t.Helper()
if !reflect.DeepEqual(a, b) {
Expand Down

0 comments on commit 8b76793

Please sign in to comment.