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: githubClient.AssertExpectations failing #61

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

Tests: githubClient.AssertExpectations failing #61

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

Comments

@github-actions
Copy link

Tests: githubClient.AssertExpectations failing

panic: interface conversion: interface {} is nil, not githubv4.DateTime [recovered]

panic: interface conversion: interface {} is nil, not githubv4.DateTime

githubClient.AssertExpectations(t)

https://github.com/adavila0703/devy/blob/5f764a679d6602e52e92fb147e12b86bda72ad76/internal/github/contributions_test.go#L403

}

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

	ctx := context.Background()

	username := "devy"
	year := time.Now()

	githubClient.On(
		"Query",
		ctx,
		mock.AnythingOfType("*github.contributionYears"),
		mock.MatchedBy(func(params map[string]interface{}) bool {
			return githubv4.String(username) == params["username"]
		}),
	).Return(nil).Run(func(args mock.Arguments) {
		a := args.Get(1).(*contributionYears)
		(*a) = contributionYears{
			User: userContributionYears{
				ContributionsCollection: contributionsCollectionContributionYears{
					ContributionYears: []int{
						year.Year(),
					},
				},
			},
		}
	}).Once()

	from := time.Date(year.Year(), 1, 1, 0, 0, 0, 0, time.UTC)

	githubClient.On(
		"Query",
		ctx,
		mock.AnythingOfType("*github.contributionsQuery"),
		mock.MatchedBy(func(params map[string]interface{}) bool {
			assert.WithinDuration(from, params["from"].(githubv4.DateTime).Time, time.Millisecond)
			assert.WithinDuration(year, params["to"].(githubv4.DateTime).Time, time.Millisecond)
			return githubv4.String(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: 0,
										Weekday:           7,
										Date:              time.Date(time.Now().Year(), 7, 10, 0, 0, 0, 0, time.UTC).Format("2006-01-02"),
									},
									{
										ContributionCount: 5,
										Weekday:           7,
										Date:              time.Date(time.Now().Year(), 7, 10, 0, 0, 0, 0, time.UTC).Format("2006-01-02"),
									},
									{
										ContributionCount: 5,
										Weekday:           6,
										Date:              time.Date(time.Now().Year(), 7, 9, 0, 0, 0, 0, time.UTC).Format("2006-01-02"),
									},
									{
										ContributionCount: 0,
										Weekday:           5,
										Date:              time.Date(time.Now().Year(), 7, 8, 0, 0, 0, 0, time.UTC).Format("2006-01-02"),
									},
								},
							},
						},
					},
				},
			},
		}
	}).Once()

	resp, err := githubService.GetLongestContributionStreakByUsername(ctx, username)

	assert.NoError(err)
	assert.Equal(time.Date(time.Now().Year(), 7, 10, 0, 0, 0, 0, time.UTC), resp.EndedAt)
	assert.Equal(time.Date(time.Now().Year(), 7, 9, 0, 0, 0, 0, time.UTC), resp.StartedAt)
	assert.Equal(2, resp.Streak)

	// TODO Tests: githubClient.AssertExpectations failing
	//panic: interface conversion: interface {} is nil, not githubv4.DateTime [recovered]
	//panic: interface conversion: interface {} is nil, not githubv4.DateTime
	// githubClient.AssertExpectations(t)
}

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

	ctx := context.Background()

	username := "devy"
	year := time.Now()

	githubClient.On(
		"Query",
		ctx,
		mock.AnythingOfType("*github.contributionYears"),
		mock.MatchedBy(func(params map[string]interface{}) bool {
			return githubv4.String(username) == params["username"]
		}),
	).Return(nil).Run(func(args mock.Arguments) {
		a := args.Get(1).(*contributionYears)
		(*a) = contributionYears{
			User: userContributionYears{
				ContributionsCollection: contributionsCollectionContributionYears{
					ContributionYears: []int{
						year.Year(),
					},
				},
			},
		}
	}).Once()

	from := time.Date(year.Year(), 1, 1, 0, 0, 0, 0, time.UTC)

	githubClient.On(
		"Query",
		ctx,
		mock.AnythingOfType("*github.contributionsQuery"),
		mock.MatchedBy(func(params map[string]interface{}) bool {
			assert.WithinDuration(from, params["from"].(githubv4.DateTime).Time, time.Millisecond)
			assert.WithinDuration(year, params["to"].(githubv4.DateTime).Time, time.Millisecond)
			return githubv4.String(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:           7,
										Date:              time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC).Format("2006-01-02"),
									},
									{
										ContributionCount: 5,
										Weekday:           6,
										Date:              time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, 0, 0, 0, 0, time.UTC).Format("2006-01-02"),
									},
									{
										ContributionCount: 0,
										Weekday:           5,
										Date:              time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-2, 0, 0, 0, 0, time.UTC).Format("2006-01-02"),
									},
								},
							},
						},
					},
				},
			},
		}
	}).Once()

	resp, err := githubService.GetLongestContributionStreakByUsername(ctx, username)

	assert.NoError(err)
	assert.Equal(time.Time{}, resp.EndedAt)
	assert.Equal(time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, 0, 0, 0, 0, time.UTC), resp.StartedAt)
	assert.Equal(2, resp.Streak)

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

	ctx := context.Background()

	username := "devy"
	year := time.Now()

	githubClient.On(
		"Query",
		ctx,
		mock.AnythingOfType("*github.contributionYears"),
		mock.MatchedBy(func(params map[string]interface{}) bool {
			return githubv4.String(username) == params["username"]
		}),
	).Return(nil).Run(func(args mock.Arguments) {
		a := args.Get(1).(*contributionYears)
		(*a) = contributionYears{
			User: userContributionYears{
				ContributionsCollection: contributionsCollectionContributionYears{
					ContributionYears: []int{
						year.Year(),
					},
				},
			},
		}
	}).Once()

	from := time.Date(year.Year(), 1, 1, 0, 0, 0, 0, time.UTC)

	githubClient.On(
		"Query",
		ctx,
		mock.AnythingOfType("*github.contributionsQuery"),
		mock.MatchedBy(func(params map[string]interface{}) bool {
			assert.WithinDuration(from, params["from"].(githubv4.DateTime).Time, time.Millisecond)
			assert.WithinDuration(year, params["to"].(githubv4.DateTime).Time, time.Millisecond)
			return githubv4.String(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{},
							},
						},
					},
				},
			},
		}
	}).Once()

	resp, err := githubService.GetLongestContributionStreakByUsername(ctx, username)

	assert.NoError(err)
	assert.Equal(time.Time{}, resp.EndedAt)
	assert.Equal(time.Time{}, resp.StartedAt)
	assert.Equal(0, resp.Streak)
}

// TODO Tests: TotalContribution.String()

bcc4b4b82a15fe64cf0ea49aed5d6053b22f8c74

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

No branches or pull requests

1 participant