Skip to content

Commit fc59ab9

Browse files
Merge pull request #77 from adavila0703/date-within-duration
add WithinDuration to date package and use in tests in matchedBy's
2 parents d83fd46 + 762eb59 commit fc59ab9

3 files changed

Lines changed: 86 additions & 15 deletions

File tree

internal/github/contributions_test.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"bot/pkg/date"
45
"context"
56
"testing"
67
"time"
@@ -191,9 +192,9 @@ func TestGithubService_GetContributionsByUsername__DatesZeroValue(t *testing.T)
191192
ctx,
192193
mock.AnythingOfType("*github.contributionsQuery"),
193194
mock.MatchedBy(func(params map[string]interface{}) bool {
194-
assert.WithinDuration(time.Now(), params["to"].(githubv4.DateTime).Time, time.Millisecond)
195-
assert.WithinDuration(time.Now().AddDate(-1, 0, 0), params["from"].(githubv4.DateTime).Time, time.Millisecond)
196-
return githubv4.String(options.Username) == params["username"]
195+
return githubv4.String(options.Username) == params["username"] &&
196+
date.WithinDuration(time.Now(), params["to"].(githubv4.DateTime).Time, time.Millisecond) &&
197+
date.WithinDuration(time.Now().AddDate(-1, 0, 0), params["from"].(githubv4.DateTime).Time, time.Millisecond)
197198
}),
198199
).Return(nil).Run(func(args mock.Arguments) {
199200
a := args.Get(1).(*contributionsQuery)
@@ -378,9 +379,9 @@ func TestGithubService_GetLongestContributionStreakByUsername(t *testing.T) {
378379
ctx,
379380
mock.AnythingOfType("*github.contributionsQuery"),
380381
mock.MatchedBy(func(params map[string]interface{}) bool {
381-
assert.WithinDuration(from, params["from"].(githubv4.DateTime).Time, time.Millisecond)
382-
assert.WithinDuration(year, params["to"].(githubv4.DateTime).Time, time.Millisecond)
383-
return githubv4.String(username) == params["username"]
382+
return githubv4.String(username) == params["username"] &&
383+
date.WithinDuration(from, params["from"].(githubv4.DateTime).Time, time.Millisecond) &&
384+
date.WithinDuration(year, params["to"].(githubv4.DateTime).Time, time.Millisecond)
384385
}),
385386
).Return(nil).Run(func(args mock.Arguments) {
386387
a := args.Get(1).(*contributionsQuery)
@@ -471,9 +472,9 @@ func TestGithubService_GetLongestContributionStreakByUsername__NoEndDateCurrentS
471472
ctx,
472473
mock.AnythingOfType("*github.contributionsQuery"),
473474
mock.MatchedBy(func(params map[string]interface{}) bool {
474-
assert.WithinDuration(from, params["from"].(githubv4.DateTime).Time, time.Millisecond)
475-
assert.WithinDuration(year, params["to"].(githubv4.DateTime).Time, time.Millisecond)
476-
return githubv4.String(username) == params["username"]
475+
return githubv4.String(username) == params["username"] &&
476+
date.WithinDuration(from, params["from"].(githubv4.DateTime).Time, time.Millisecond) &&
477+
date.WithinDuration(year, params["to"].(githubv4.DateTime).Time, time.Millisecond)
477478
}),
478479
).Return(nil).Run(func(args mock.Arguments) {
479480
a := args.Get(1).(*contributionsQuery)
@@ -554,9 +555,9 @@ func TestGithubService_GetLongestContributionStreakByUsername__NoContributionDay
554555
ctx,
555556
mock.AnythingOfType("*github.contributionsQuery"),
556557
mock.MatchedBy(func(params map[string]interface{}) bool {
557-
assert.WithinDuration(from, params["from"].(githubv4.DateTime).Time, time.Millisecond)
558-
assert.WithinDuration(year, params["to"].(githubv4.DateTime).Time, time.Millisecond)
559-
return githubv4.String(username) == params["username"]
558+
return githubv4.String(username) == params["username"] &&
559+
date.WithinDuration(from, params["from"].(githubv4.DateTime).Time, time.Millisecond) &&
560+
date.WithinDuration(year, params["to"].(githubv4.DateTime).Time, time.Millisecond)
560561
}),
561562
).Return(nil).Run(func(args mock.Arguments) {
562563
a := args.Get(1).(*contributionsQuery)
@@ -621,9 +622,9 @@ func TestGithubService_GetTotalContributionsByUsername(t *testing.T) {
621622
ctx,
622623
mock.AnythingOfType("*github.contributionsQuery"),
623624
mock.MatchedBy(func(params map[string]interface{}) bool {
624-
assert.WithinDuration(year, params["from"].(githubv4.DateTime).Time, time.Millisecond)
625-
assert.WithinDuration(time.Now(), params["to"].(githubv4.DateTime).Time, time.Millisecond)
626-
return githubv4.String(username) == params["username"]
625+
return githubv4.String(username) == params["username"] &&
626+
date.WithinDuration(year, params["from"].(githubv4.DateTime).Time, time.Millisecond) &&
627+
date.WithinDuration(time.Now(), params["to"].(githubv4.DateTime).Time, time.Millisecond)
627628
}),
628629
).Return(nil).Run(func(args mock.Arguments) {
629630
a := args.Get(1).(*contributionsQuery)

pkg/date/date.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ func SOY(t time.Time, loc *time.Location) time.Time {
2323
func EOY(t time.Time, loc *time.Location) time.Time {
2424
return time.Date(t.Year(), 12, 31, 59, 59, 0, 0, loc)
2525
}
26+
27+
// WithinDuration test to see if two time.Time's are within a duration of eachother
28+
func WithinDuration(expected, actual time.Time, delta time.Duration) bool {
29+
dt := expected.Sub(actual)
30+
31+
return dt > -delta && dt < delta
32+
}

pkg/date/date_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package date
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestWithinDuration(t *testing.T) {
9+
type args struct {
10+
expected time.Time
11+
actual time.Time
12+
delta time.Duration
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
want bool
18+
}{
19+
{
20+
name: "is within duration",
21+
args: args{
22+
expected: time.Date(2021, 12, 21, 0, 0, 0, 0, time.UTC),
23+
actual: time.Date(2021, 12, 21, 0, 0, 1, 0, time.UTC),
24+
delta: time.Millisecond * 1500,
25+
},
26+
want: true,
27+
},
28+
{
29+
name: "not within duration",
30+
args: args{
31+
expected: time.Date(2021, 12, 21, 0, 0, 0, 0, time.UTC),
32+
actual: time.Date(2021, 12, 21, 0, 0, 1, 0, time.UTC),
33+
delta: time.Millisecond * 900,
34+
},
35+
want: false,
36+
},
37+
{
38+
name: "not inclusive start",
39+
args: args{
40+
expected: time.Date(2021, 12, 21, 0, 0, 0, 0, time.UTC),
41+
actual: time.Date(2021, 12, 21, 0, 0, 1, 0, time.UTC),
42+
delta: time.Second,
43+
},
44+
want: false,
45+
},
46+
{
47+
name: "not inclusive end",
48+
args: args{
49+
expected: time.Date(2021, 12, 21, 0, 0, 1, 0, time.UTC),
50+
actual: time.Date(2021, 12, 21, 0, 0, 0, 0, time.UTC),
51+
delta: time.Second,
52+
},
53+
want: false,
54+
},
55+
}
56+
for _, tt := range tests {
57+
t.Run(tt.name, func(t *testing.T) {
58+
if got := WithinDuration(tt.args.expected, tt.args.actual, tt.args.delta); got != tt.want {
59+
t.Errorf("WithinDuration() = %v, want %v", got, tt.want)
60+
}
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)