@@ -243,16 +243,44 @@ func TestGithubService_GetContributionsByUsername__DatesZeroValue(t *testing.T)
243243 githubClient .AssertExpectations (t )
244244}
245245
246- // TODO Tests: error table test on GetContributionsByUsername
247- // labels: tests, good first issue
248- // Need to run a table test on GetContributionsByUsername to hit
249- // ErrMissingUsername and ErrToDateBeforeFromDate
250246func TestGithubService_GetContributionsByUsername__Errors (t * testing.T ) {
247+ assert := assert .New (t )
248+ githubClient := & MockGithubClient {}
249+ githubService := NewGithubService (githubClient )
250+
251+ ctx := context .Background ()
252+
253+ tests := []struct {
254+ name string
255+ options GetContributionsByUsernameOptions
256+ wantErr error
257+ }{
258+ {
259+ name : "test for ErrMissingUsername" ,
260+ options : GetContributionsByUsernameOptions {},
261+ wantErr : ErrMissingUsername ,
262+ },
263+ {
264+ name : "test for ErrToDateBeforeFromDate" ,
265+ options : GetContributionsByUsernameOptions {
266+ Username : "test" ,
267+ From : time .Date (2020 , 0 , 0 , 0 , 0 , 0 , 0 , time .UTC ),
268+ To : time .Date (2019 , 0 , 0 , 0 , 0 , 0 , 0 , time .UTC ),
269+ },
270+ wantErr : ErrToDateBeforeFromDate ,
271+ },
272+ }
251273
274+ for _ , tt := range tests {
275+ t .Run (tt .name , func (t * testing.T ) {
276+ resp , err := githubService .GetContributionsByUsername (ctx , tt .options )
277+
278+ assert .Nil (resp )
279+ assert .Equal (tt .wantErr , err )
280+ })
281+ }
252282}
253283
254- // TODO Tests: GetFirstContributionYearByUsername
255- // labels: tests, good first issue
256284func TestGithubService_GetFirstContributionYearByUsername (t * testing.T ) {
257285 assert := assert .New (t )
258286 githubClient := & MockGithubClient {}
@@ -613,14 +641,63 @@ func TestGithubService_GetLongestContributionStreakByUsername__NoContributionDay
613641 assert .Equal (0 , resp .Streak )
614642}
615643
616- // TODO Tests: TotalContribution.String()
617- // labels: tests, good first issue
618- func TestTotalContribution_String (t * testing.T ) {
644+ func TestGithubService_GetTotalContributionsByUsername (t * testing.T ) {
645+ assert := assert .New (t )
646+ githubClient := & MockGithubClient {}
647+ githubService := NewGithubService (githubClient )
619648
620- }
649+ ctx := context . Background ()
621650
622- // TODO Tests: GetTotalContributionsByUsername
623- // labels: tests, good first issue
624- func TestGithubService_GetTotalContributionsByUsername (t * testing.T ) {
651+ username := "devy"
652+ to := time .Now ()
625653
654+ githubClient .On (
655+ "Query" ,
656+ ctx ,
657+ mock .AnythingOfType ("*github.contributionYears" ),
658+ mock .MatchedBy (func (params map [string ]interface {}) bool {
659+ return githubv4 .String (username ) == params ["username" ]
660+ }),
661+ ).Return (nil ).Run (func (args mock.Arguments ) {
662+ a := args .Get (1 ).(* contributionYears )
663+ (* a ) = contributionYears {
664+ User : userContributionYears {
665+ ContributionsCollection : contributionsCollectionContributionYears {
666+ ContributionYears : []int {
667+ to .Year (),
668+ },
669+ },
670+ },
671+ }
672+ }).Once ()
673+
674+ year := time .Date (to .Year (), 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
675+
676+ githubClient .On (
677+ "Query" ,
678+ ctx ,
679+ mock .AnythingOfType ("*github.contributionsQuery" ),
680+ mock .MatchedBy (func (params map [string ]interface {}) bool {
681+ assert .WithinDuration (year , params ["from" ].(githubv4.DateTime ).Time , time .Millisecond )
682+ assert .WithinDuration (time .Now (), params ["to" ].(githubv4.DateTime ).Time , time .Millisecond )
683+ return githubv4 .String (username ) == params ["username" ]
684+ }),
685+ ).Return (nil ).Run (func (args mock.Arguments ) {
686+ a := args .Get (1 ).(* contributionsQuery )
687+ (* a ) = contributionsQuery {
688+ User : user {
689+ ContributionsCollection : contributionsCollection {
690+ ContributionCalendar : contributionCalendar {
691+ TotalContributions : 1000 ,
692+ },
693+ },
694+ },
695+ }
696+ }).Once ()
697+
698+ resp , err := githubService .GetTotalContributionsByUsername (ctx , username )
699+
700+ assert .NoError (err )
701+ assert .Equal (resp .String (), "total github contributions: 1000" )
702+ assert .Equal (resp .Total , 1000 )
626703}
0 commit comments