Skip to content

Commit

Permalink
Rework reaction payload tests for IssueComments to handle Get, GetAll…
Browse files Browse the repository at this point in the history
…ForIssue and GetAllForRepository calls
  • Loading branch information
ryangribble committed Jul 7, 2016
1 parent 1185da2 commit e319f0e
Showing 1 changed file with 148 additions and 45 deletions.
193 changes: 148 additions & 45 deletions Octokit.Tests.Integration/Clients/IssueCommentsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
using Octokit.Tests.Integration;
using Octokit.Tests.Integration.Helpers;
using Xunit;
using Octokit.Tests.Integration.Helpers;
using System;
using System.Linq;
using System.Collections.Generic;

public class IssueCommentsClientTests
{
public class TheGetMethod
{
readonly IGitHubClient _github;
readonly IIssueCommentsClient _issueCommentsClient;

const string owner = "octokit";
const string name = "octokit.net";
const int id = 12067722;
const int repositoryId = 7528679;

public TheGetMethod()
{
var github = Helper.GetAuthenticatedClient();
_github = Helper.GetAuthenticatedClient();

_issueCommentsClient = github.Issue.Comment;
_issueCommentsClient = _github.Issue.Comment;
}

[IntegrationTest]
Expand All @@ -39,10 +41,37 @@ public async Task ReturnsIssueCommentWithRepositoryId()

Assert.NotNull(comment);
}

[IntegrationTest]
public async Task CanGetReactionPayload()
{
using (var context = await _github.CreateRepositoryContext(new NewRepository(Helper.MakeNameWithTimestamp("IssueCommentsReactionTests"))))
{
// Create a test issue
var issueNumber = await HelperCreateIssue(context.RepositoryOwner, context.RepositoryName);

// Create a test comment with reactions
var commentId = await HelperCreateIssueCommentWithReactions(context.RepositoryOwner, context.RepositoryName, issueNumber);

// Retrieve the issue
var retrieved = await _issueCommentsClient.Get(context.RepositoryOwner, context.RepositoryName, commentId);

// Check the reactions
Assert.True(retrieved.Id > 0);
Assert.Equal(6, retrieved.Reactions.TotalCount);
Assert.Equal(1, retrieved.Reactions.Plus1);
Assert.Equal(1, retrieved.Reactions.Hooray);
Assert.Equal(1, retrieved.Reactions.Heart);
Assert.Equal(1, retrieved.Reactions.Laugh);
Assert.Equal(1, retrieved.Reactions.Confused);
Assert.Equal(1, retrieved.Reactions.Minus1);
}
}
}

public class TheGetAllForRepositoryMethod
{
readonly IGitHubClient _github;
readonly IIssueCommentsClient _issueCommentsClient;

const string owner = "octokit";
Expand All @@ -51,9 +80,9 @@ public class TheGetAllForRepositoryMethod

public TheGetAllForRepositoryMethod()
{
var github = Helper.GetAuthenticatedClient();
_github = Helper.GetAuthenticatedClient();

_issueCommentsClient = github.Issue.Comment;
_issueCommentsClient = _github.Issue.Comment;
}

[IntegrationTest]
Expand Down Expand Up @@ -183,10 +212,50 @@ public async Task ReturnsDistinctResultsBasedOnStartPageWithRepositoryId()
Assert.NotEqual(firstPageIssueComments[3].Id, secondPageIssueComments[3].Id);
Assert.NotEqual(firstPageIssueComments[4].Id, secondPageIssueComments[4].Id);
}

[IntegrationTest]
public async Task CanGetReactionPayload()
{
var numberToCreate = 2;
using (var context = await _github.CreateRepositoryContext(new NewRepository(Helper.MakeNameWithTimestamp("IssueCommentsReactionTests"))))
{
var commentIds = new List<int>();

// Create multiple test issues
for (int count = 1; count <= numberToCreate; count++)
{
var issueNumber = await HelperCreateIssue(context.RepositoryOwner, context.RepositoryName);

// Each with a comment with reactions
var commentId = await HelperCreateIssueCommentWithReactions(context.RepositoryOwner, context.RepositoryName, issueNumber);
commentIds.Add(commentId);
}
Assert.Equal(numberToCreate, commentIds.Count);

// Retrieve all issue comments for the repo
var issueComments = await _issueCommentsClient.GetAllForRepository(context.RepositoryOwner, context.RepositoryName);

// Check the reactions
foreach (var commentId in commentIds)
{
var retrieved = issueComments.FirstOrDefault(x => x.Id == commentId);

Assert.NotNull(retrieved);
Assert.Equal(6, retrieved.Reactions.TotalCount);
Assert.Equal(1, retrieved.Reactions.Plus1);
Assert.Equal(1, retrieved.Reactions.Hooray);
Assert.Equal(1, retrieved.Reactions.Heart);
Assert.Equal(1, retrieved.Reactions.Laugh);
Assert.Equal(1, retrieved.Reactions.Confused);
Assert.Equal(1, retrieved.Reactions.Minus1);
}
}
}
}

public class TheGetAllForIssueMethod
{
readonly IGitHubClient _github;
readonly IIssueCommentsClient _issueCommentsClient;

const string owner = "octokit";
Expand All @@ -196,9 +265,9 @@ public class TheGetAllForIssueMethod

public TheGetAllForIssueMethod()
{
var github = Helper.GetAuthenticatedClient();
_github = Helper.GetAuthenticatedClient();

_issueCommentsClient = github.Issue.Comment;
_issueCommentsClient = _github.Issue.Comment;
}

[IntegrationTest]
Expand Down Expand Up @@ -328,14 +397,52 @@ public async Task ReturnsDistinctResultsBasedOnStartPageWithRepositoryId()
Assert.NotEqual(firstPageIssueComments[3].Id, secondPageIssueComments[3].Id);
Assert.NotEqual(firstPageIssueComments[4].Id, secondPageIssueComments[4].Id);
}

[IntegrationTest]
public async Task CanGetReactionPayload()
{
var numberToCreate = 2;
using (var context = await _github.CreateRepositoryContext(new NewRepository(Helper.MakeNameWithTimestamp("IssueCommentsReactionTests"))))
{
var commentIds = new List<int>();

// Create a single test issue
var issueNumber = await HelperCreateIssue(context.RepositoryOwner, context.RepositoryName);

// With multiple comments with reactions
for (int count = 1; count <= numberToCreate; count++)
{
var commentId = await HelperCreateIssueCommentWithReactions(context.RepositoryOwner, context.RepositoryName, issueNumber);
commentIds.Add(commentId);
}
Assert.Equal(numberToCreate, commentIds.Count);

// Retrieve all comments for the issue
var issueComments = await _issueCommentsClient.GetAllForIssue(context.RepositoryOwner, context.RepositoryName, issueNumber);

// Check the reactions
foreach (var commentId in commentIds)
{
var retrieved = issueComments.FirstOrDefault(x => x.Id == commentId);

Assert.NotNull(retrieved);
Assert.Equal(6, retrieved.Reactions.TotalCount);
Assert.Equal(1, retrieved.Reactions.Plus1);
Assert.Equal(1, retrieved.Reactions.Hooray);
Assert.Equal(1, retrieved.Reactions.Heart);
Assert.Equal(1, retrieved.Reactions.Laugh);
Assert.Equal(1, retrieved.Reactions.Confused);
Assert.Equal(1, retrieved.Reactions.Minus1);
}
}
}
}

public class TheCreateMethod
{
readonly IIssueCommentsClient _issueCommentsClient;
readonly RepositoryContext _context;
readonly IIssuesClient _issuesClient;
readonly IReactionsClient _reactionsClient;

public TheCreateMethod()
{
Expand All @@ -347,42 +454,6 @@ public TheCreateMethod()

_issuesClient = gitHubClient.Issue;
_issueCommentsClient = gitHubClient.Issue.Comment;
_reactionsClient = gitHubClient.Reaction;
}

[IntegrationTest]
public async Task CanGetReactionPayload()
{
var newIssue = new NewIssue("a test issue") { Body = "A new unassigned issue" };
var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue);

Assert.NotNull(issue);

var issueComment = await _issueCommentsClient.Create(_context.RepositoryOwner, _context.RepositoryName, issue.Number, "A test comment");

Assert.NotNull(issueComment);

foreach (ReactionType reactionType in Enum.GetValues(typeof(ReactionType)))
{
var newReaction = new NewReaction(reactionType);

var reaction = await _reactionsClient.IssueComment.Create(_context.RepositoryOwner, _context.RepositoryName, issueComment.Id, newReaction);

Assert.IsType<Reaction>(reaction);
Assert.Equal(reactionType, reaction.Content);
Assert.Equal(issueComment.User.Id, reaction.User.Id);
}

var retrieved = await _issueCommentsClient.Get(_context.RepositoryOwner, _context.RepositoryName, issueComment.Id);

Assert.True(retrieved.Id > 0);
Assert.Equal(6, retrieved.Reactions.TotalCount);
Assert.Equal(1, retrieved.Reactions.Plus1);
Assert.Equal(1, retrieved.Reactions.Hooray);
Assert.Equal(1, retrieved.Reactions.Heart);
Assert.Equal(1, retrieved.Reactions.Laugh);
Assert.Equal(1, retrieved.Reactions.Confused);
Assert.Equal(1, retrieved.Reactions.Minus1);
}

[IntegrationTest]
Expand Down Expand Up @@ -511,4 +582,36 @@ public async Task DeleteIssueWithRepositoryId()
await Assert.ThrowsAsync<NotFoundException>(() => _issueCommentsClient.Get(_context.Repository.Id, comment.Id));
}
}

async static Task<int> HelperCreateIssue(string owner, string repo)
{
var github = Helper.GetAuthenticatedClient();

var newIssue = new NewIssue("A test issue") { Body = "A new unassigned issue" };
var issue = await github.Issue.Create(owner, repo, newIssue);
Assert.NotNull(issue);

return issue.Number;
}

async static Task<int> HelperCreateIssueCommentWithReactions(string owner, string repo, int number)
{
var github = Helper.GetAuthenticatedClient();

var issueComment = await github.Issue.Comment.Create(owner, repo, number, "A test issue comment");
Assert.NotNull(issueComment);

foreach (ReactionType reactionType in Enum.GetValues(typeof(ReactionType)))
{
var newReaction = new NewReaction(reactionType);

var reaction = await github.Reaction.IssueComment.Create(owner, repo, issueComment.Id, newReaction);

Assert.IsType<Reaction>(reaction);
Assert.Equal(reactionType, reaction.Content);
Assert.Equal(issueComment.User.Id, reaction.User.Id);
}

return issueComment.Id;
}
}

0 comments on commit e319f0e

Please sign in to comment.