Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: error table test on GetContributionsByUsername #31

Open
github-actions bot opened this issue Dec 8, 2021 · 0 comments
Open

Tests: error table test on GetContributionsByUsername #31

github-actions bot opened this issue Dec 8, 2021 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Dec 8, 2021

Tests: error table test on GetContributionsByUsername

Need to run a table test on GetContributionsByUsername to hit

ErrMissingUsername and ErrToDateBeforeFromDate

https://github.com/adavila0703/devy/blob/6fe79f88ef4d479d6ef45f63d655a35d371ec492/internal/github/contributions_test.go#L252

package github

import (
	"context"
	"fmt"
	"testing"
	"time"

	"github.com/shurcooL/githubv4"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
)

func TestGetContributionsByUsername(t *testing.T) {
	assert := assert.New(t)
	githubClient := &MockGithubClient{}
	githubService := NewGithubService(githubClient)

	ctx := context.Background()
	from := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
	to := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)

	options := GetContributionsByUsernameOptions{
		Username: "devy",
		From:     from,
		To:       to,
	}

	githubClient.On(
		"Query",
		ctx,
		mock.AnythingOfType("*github.contributionsQuery"),
		mock.MatchedBy(func(params map[string]interface{}) bool {
			assert.Equal(githubv4.String(options.Username), params["username"])
			assert.Equal(githubv4.DateTime{Time: from}, params["from"])
			assert.Equal(githubv4.DateTime{Time: to}, params["to"])
			return true
		}),
	).Return(nil).Run(func(args mock.Arguments) {
		a := args.Get(1).(*contributionsQuery)
		(*a) = contributionsQuery{
			User: user{
				ContributionsCollection: contributionsCollection{
					ContributionCalendar: contributionCalendar{
						TotalContributions: 100,
						Weeks: []week{
							{
								[]contributionDays{
									{
										ContributionCount: 5,
										Weekday:           1,
										Date:              "2019-01-01",
									},
								},
							},
							{
								[]contributionDays{
									{
										ContributionCount: 10,
										Weekday:           0,
										Date:              "2019-01-02",
									},
								},
							},
						},
					},
				},
			},
		}
	}).Once()

	resp, err := githubService.GetContributionsByUsername(ctx, options)

	assert.NoError(err)

	assert.Equal(10, resp.Days[0].ContributionCount)
	assert.Equal(0, resp.Days[0].Weekday)
	assert.Equal(time.Date(2019, 01, 02, 0, 0, 0, 0, time.UTC), resp.Days[0].Date)

	assert.Equal(5, resp.Days[1].ContributionCount)
	assert.Equal(1, resp.Days[1].Weekday)
	assert.Equal(time.Date(2019, 01, 01, 0, 0, 0, 0, time.UTC), resp.Days[1].Date)

	githubClient.AssertExpectations(t)
}

func TestGetContributionsByUsername_MultiYear(t *testing.T) {
	assert := assert.New(t)
	githubClient := &MockGithubClient{}
	githubService := NewGithubService(githubClient)

	ctx := context.Background()
	from := time.Date(2020, 12, 6, 0, 0, 0, 0, time.UTC)
	to := time.Date(2021, 12, 7, 0, 0, 0, 0, time.UTC)

	options := GetContributionsByUsernameOptions{
		Username: "devy",
		From:     from,
		To:       to,
	}

	fmt.Println("check here", from)

	githubClient.On(
		"Query",
		ctx,
		mock.AnythingOfType("*github.contributionsQuery"),
		mock.MatchedBy(func(params map[string]interface{}) bool {
			return githubv4.String(options.Username) == params["username"] &&
				githubv4.DateTime{Time: time.Date(2020, 12, 7, 0, 0, 0, 0, time.UTC)} == params["from"] &&
				githubv4.DateTime{Time: time.Date(2021, 12, 7, 0, 0, 0, 0, time.UTC)} == params["to"]
		}),
	).Return(nil).Run(func(args mock.Arguments) {
		a := args.Get(1).(*contributionsQuery)
		(*a) = contributionsQuery{
			User: user{
				ContributionsCollection: contributionsCollection{
					ContributionCalendar: contributionCalendar{
						TotalContributions: 100,
						Weeks: []week{
							{
								[]contributionDays{
									{
										ContributionCount: 5,
										Weekday:           1,
										Date:              "2019-01-01",
									},
								},
							},
						},
					},
				},
			},
		}
	}).Once()

	githubClient.On(
		"Query",
		ctx,
		mock.AnythingOfType("*github.contributionsQuery"),
		mock.MatchedBy(func(params map[string]interface{}) bool {
			return githubv4.String(options.Username) == params["username"] &&
				githubv4.DateTime{Time: time.Date(2020, 12, 6, 0, 0, 0, 0, time.UTC)} == params["from"] &&
				githubv4.DateTime{Time: time.Date(2020, 12, 6, 0, 0, 0, 0, time.UTC)} == params["to"]
		}),
	).Return(nil).Run(func(args mock.Arguments) {
		a := args.Get(1).(*contributionsQuery)
		(*a) = contributionsQuery{
			User: user{
				ContributionsCollection: contributionsCollection{
					ContributionCalendar: contributionCalendar{
						TotalContributions: 100,
						Weeks: []week{
							{
								[]contributionDays{
									{
										ContributionCount: 5,
										Weekday:           1,
										Date:              "2019-01-01",
									},
								},
							},
						},
					},
				},
			},
		}
	}).Once()

	resp, err := githubService.GetContributionsByUsername(ctx, options)

	fmt.Println(resp.TotalContributions)

	assert.NoError(err)

	assert.Equal(5, resp.Days[0].ContributionCount)
	assert.Equal(1, resp.Days[0].Weekday)
	assert.Equal(time.Date(2019, 01, 01, 0, 0, 0, 0, time.UTC), resp.Days[0].Date)
	assert.Equal(200, resp.TotalContributions)

	githubClient.AssertExpectations(t)
}

func TestGetContributionsByUsername_DatesZeroValue(t *testing.T) {
	assert := assert.New(t)
	githubClient := &MockGithubClient{}
	githubService := NewGithubService(githubClient)

	ctx := context.Background()

	options := GetContributionsByUsernameOptions{
		Username: "devy",
	}

	githubClient.On(
		"Query",
		ctx,
		mock.AnythingOfType("*github.contributionsQuery"),
		mock.MatchedBy(func(params map[string]interface{}) bool {
			assert.WithinDuration(time.Now(), params["to"].(githubv4.DateTime).Time, time.Millisecond)
			assert.WithinDuration(time.Now().AddDate(-1, 0, 0), params["from"].(githubv4.DateTime).Time, time.Millisecond)
			return githubv4.String(options.Username) == params["username"]
		}),
	).Return(nil).Run(func(args mock.Arguments) {
		a := args.Get(1).(*contributionsQuery)
		(*a) = contributionsQuery{
			User: user{
				ContributionsCollection: contributionsCollection{
					ContributionCalendar: contributionCalendar{
						TotalContributions: 100,
						Weeks: []week{
							{
								[]contributionDays{
									{
										ContributionCount: 5,
										Weekday:           1,
										Date:              "2019-01-01",
									},
								},
							},
							{
								[]contributionDays{
									{
										ContributionCount: 10,
										Weekday:           0,
										Date:              "2019-01-02",
									},
								},
							},
						},
					},
				},
			},
		}
	}).Once()

	resp, err := githubService.GetContributionsByUsername(ctx, options)

	assert.NoError(err)

	assert.Equal(10, resp.Days[0].ContributionCount)
	assert.Equal(0, resp.Days[0].Weekday)
	assert.Equal(time.Date(2019, 01, 02, 0, 0, 0, 0, time.UTC), resp.Days[0].Date)

	assert.Equal(5, resp.Days[1].ContributionCount)
	assert.Equal(1, resp.Days[1].Weekday)
	assert.Equal(time.Date(2019, 01, 01, 0, 0, 0, 0, time.UTC), resp.Days[1].Date)

	githubClient.AssertExpectations(t)
}

// TODO Tests: error table test on GetContributionsByUsername
// labels: tests, good first issue
// Need to run a table test on GetContributionsByUsername to hit
// ErrMissingUsername and ErrToDateBeforeFromDate
func TestGetContributionsByUsername_Errors(t *testing.T) {

}

// TODO Tests: GetFirstContributionYearByUsername
// labels: tests, good first issue
func TestGetFirstContributionYearByUsername(t *testing.T) {

}

// TODO Tests: CurrentContributionStreak.String()
// labels: tests
func TestCurrentContributionStreak_String(t *testing.T) {

}

// TODO Tests: GetCurrentContributionStreakByUsername
// labels: tests
func TestGetCurrentContributionStreakByUsername(t *testing.T) {

}

// TODO Tests: LongestContributionStreak.String()
// labels: tests, good first issue
func TestLongestContributionStreak_String(t *testing.T) {

}

// TODO Tests: GetLongestContributionStreakByUsername
// labels: tests
func TestGetLongestContributionStreakByUsername(t *testing.T) {

}

// TODO Tests: TotalContribution.String()
// labels: tests, good first issue
func TestTotalContribution_String(t *testing.T) {

}

// TODO Tests: GetTotalContributionsByUsername
// labels: tests, good first issue
func TestGetTotalContributionsByUsername(t *testing.T) {

}

06f026486f8f48b5b2ba755819928165c43b106f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants