Tested out all of the existing Pull Request code

Still missing integration tests for Pull Requests, though.  I need to do
a bit more research before I can start to tackle that one.
This commit is contained in:
Josh Sullivan
2013-11-05 03:10:07 -05:00
committed by Brendan Forster
parent dd220d7bf9
commit c81d8d08f7
7 changed files with 430 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NSubstitute;
using Octokit;
using Octokit.Tests.Helpers;
using Xunit;
namespace Octokit.Tests.Clients
{
public class PullRequestsClientTests
{
public class TheGetMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
client.Get("fake", "repo", 42);
connection.Received().Get<PullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls/42"),
null);
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new PullRequestsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentNullException>(async () => await client.Get(null, "name", 1));
await AssertEx.Throws<ArgumentNullException>(async () => await client.Get("owner", null, 1));
await AssertEx.Throws<ArgumentException>(async () => await client.Get(null, "", 1));
await AssertEx.Throws<ArgumentException>(async () => await client.Get("", null, 1));
}
}
public class TheGetForRepositoryMethod
{
[Fact]
public async Task RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
await client.GetForRepository("fake", "repo");
connection.Received().GetAll<PullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls"),
Arg.Any<Dictionary<string, string>>());
}
[Fact]
public void SendsAppropriateParameters()
{
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
client.GetForRepository("fake", "repo", new PullRequestRequest { Head = "user:ref-head", Base = "fake_base_branch" });
connection.Received().GetAll<PullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls"),
Arg.Is<Dictionary<string, string>>(d => d.Count == 3
&& d["head"] == "user:ref-head"
&& d["state"] == "open"
&& d["base"] == "fake_base_branch"));
}
}
public class TheCreateMethod
{
[Fact]
public void PostsToCorrectUrl()
{
var newPullRequest = new NewPullRequest("some title");
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
client.Create("fake", "repo", newPullRequest);
connection.Received().Post<PullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls"),
newPullRequest);
}
[Fact]
public async Task EnsuresArgumentsNotNull()
{
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create(null, "name", new NewPullRequest("title")));
AssertEx.Throws<ArgumentException>(async () => await
client.Create("", "name", new NewPullRequest("x")));
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create("owner", null, new NewPullRequest("x")));
AssertEx.Throws<ArgumentException>(async () => await
client.Create("owner", "", new NewPullRequest("x")));
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create("owner", "name", null));
}
}
public class TheUpdateMethod
{
[Fact]
public void PostsToCorrectUrl()
{
var pullRequestUpdate = new PullRequestUpdate();
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
client.Update("fake", "repo", 42, pullRequestUpdate);
connection.Received().Patch<PullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls/42"),
pullRequestUpdate);
}
[Fact]
public async Task EnsuresArgumentsNotNull()
{
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create(null, "name", new NewPullRequest("title")));
AssertEx.Throws<ArgumentException>(async () => await
client.Create("", "name", new NewPullRequest("x")));
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create("owner", null, new NewPullRequest("x")));
AssertEx.Throws<ArgumentException>(async () => await
client.Create("owner", "", new NewPullRequest("x")));
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create("owner", "name", null));
}
}
public class TheCtor
{
[Fact]
public void EnsuresArgument()
{
Assert.Throws<ArgumentNullException>(() => new PullRequestsClient(null));
}
}
}
}

View File

@@ -0,0 +1,35 @@
using Octokit;
using Xunit;
public class PullRequestRequestTests
{
public class TheToParametersDictionaryMethod
{
[Fact]
public void ContainsSetValues()
{
var request = new PullRequestRequest
{
State = ItemState.Closed,
Head = "user:ref-name",
Base = "fake_base_branch"
};
var parameters = request.ToParametersDictionary();
Assert.Equal("closed", parameters["state"]);
Assert.Equal("user:ref-name", parameters["head"]);
Assert.Equal("fake_base_branch", parameters["base"]);
}
[Fact]
public void ReturnsDefaultValuesForDefaultRequest()
{
var request = new PullRequestRequest();
var parameters = request.ToParametersDictionary();
Assert.Equal("open", parameters["state"]);
}
}
}

View File

@@ -66,6 +66,7 @@
<Compile Include="Clients\MiscellaneousClientTests.cs" /> <Compile Include="Clients\MiscellaneousClientTests.cs" />
<Compile Include="Clients\NotificationsClientTests.cs" /> <Compile Include="Clients\NotificationsClientTests.cs" />
<Compile Include="Clients\OrganizationMembersClientTests.cs" /> <Compile Include="Clients\OrganizationMembersClientTests.cs" />
<Compile Include="Clients\PullRequestsClientTests.cs" />
<Compile Include="Clients\ReleasesClientTests.cs" /> <Compile Include="Clients\ReleasesClientTests.cs" />
<Compile Include="Clients\SshKeysClientTests.cs" /> <Compile Include="Clients\SshKeysClientTests.cs" />
<Compile Include="Clients\StarredClientTests.cs" /> <Compile Include="Clients\StarredClientTests.cs" />

View File

@@ -76,6 +76,7 @@
<Compile Include="Clients\TeamsClientTests.cs" /> <Compile Include="Clients\TeamsClientTests.cs" />
<Compile Include="Clients\GitDatabaseClientTests.cs" /> <Compile Include="Clients\GitDatabaseClientTests.cs" />
<Compile Include="Clients\OrganizationMembersClientTests.cs" /> <Compile Include="Clients\OrganizationMembersClientTests.cs" />
<Compile Include="Clients\PullRequestsClientTests.cs" />
<Compile Include="Clients\TagsClientTests.cs" /> <Compile Include="Clients\TagsClientTests.cs" />
<Compile Include="Clients\IssuesEventsClientTests.cs" /> <Compile Include="Clients\IssuesEventsClientTests.cs" />
<Compile Include="Clients\IssueCommentsClientTests.cs" /> <Compile Include="Clients\IssueCommentsClientTests.cs" />
@@ -121,6 +122,7 @@
<Compile Include="Models\MilestoneRequestTests.cs" /> <Compile Include="Models\MilestoneRequestTests.cs" />
<Compile Include="Models\IssueRequestTests.cs" /> <Compile Include="Models\IssueRequestTests.cs" />
<Compile Include="Models\ModelExtensionsTests.cs" /> <Compile Include="Models\ModelExtensionsTests.cs" />
<Compile Include="Models\PullRequestRequestTests.cs" />
<Compile Include="Models\ReadOnlyPagedCollectionTests.cs" /> <Compile Include="Models\ReadOnlyPagedCollectionTests.cs" />
<Compile Include="Models\RequestParametersTests.cs" /> <Compile Include="Models\RequestParametersTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
@@ -134,6 +136,7 @@
<Compile Include="Reactive\ObservableIssuesClientTests.cs" /> <Compile Include="Reactive\ObservableIssuesClientTests.cs" />
<Compile Include="Reactive\ObservableMilestonesClientTests.cs" /> <Compile Include="Reactive\ObservableMilestonesClientTests.cs" />
<Compile Include="Reactive\ObservableOrganizationMembersClientTests.cs" /> <Compile Include="Reactive\ObservableOrganizationMembersClientTests.cs" />
<Compile Include="Reactive\ObservablePullRequestsClientTests.cs" />
<Compile Include="Reactive\ObservableRepositoriesClientTests.cs" /> <Compile Include="Reactive\ObservableRepositoriesClientTests.cs" />
<Compile Include="Reactive\ObservableStarredClientTests.cs" /> <Compile Include="Reactive\ObservableStarredClientTests.cs" />
<Compile Include="Reactive\ObservableTreesClientTests.cs" /> <Compile Include="Reactive\ObservableTreesClientTests.cs" />

View File

@@ -0,0 +1,234 @@
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Threading.Tasks;
using NSubstitute;
using Octokit;
using Octokit.Internal;
using Octokit.Reactive.Clients;
using Octokit.Tests.Helpers;
using Xunit;
namespace Octokit.Tests.Reactive
{
public class ObservablePullRequestsClientTests
{
public class TheGetMethod
{
[Fact]
public void GetsFromClientIssueMilestone()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservablePullRequestsClient(gitHubClient);
client.Get("fake", "repo", 42);
gitHubClient.Repository.PullRequest.Received().Get("fake", "repo", 42);
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new ObservablePullRequestsClient(Substitute.For<IGitHubClient>());
await AssertEx.Throws<ArgumentNullException>(async () => await client.Get(null, "name", 1));
await AssertEx.Throws<ArgumentNullException>(async () => await client.Get("owner", null, 1));
await AssertEx.Throws<ArgumentException>(async () => await client.Get(null, "", 1));
await AssertEx.Throws<ArgumentException>(async () => await client.Get("", null, 1));
}
}
public class TheGetForRepositoryMethod
{
[Fact]
public void ReturnsEveryPageOfMilestones()
{
var firstPageUrl = new Uri("repos/fake/repo/pulls", UriKind.Relative);
var secondPageUrl = new Uri("https://example.com/page/2");
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<PullRequest>>
{
BodyAsObject = new List<PullRequest>
{
new PullRequest {Number = 1},
new PullRequest {Number = 2},
new PullRequest {Number = 3},
},
ApiInfo = CreateApiInfo(firstPageLinks)
};
var thirdPageUrl = new Uri("https://example.com/page/3");
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<PullRequest>>
{
BodyAsObject = new List<PullRequest>
{
new PullRequest {Number = 4},
new PullRequest {Number = 5},
new PullRequest {Number = 6},
},
ApiInfo = CreateApiInfo(secondPageLinks)
};
var lastPageResponse = new ApiResponse<List<PullRequest>>
{
BodyAsObject = new List<PullRequest>
{
new PullRequest {Number = 7},
},
ApiInfo = CreateApiInfo(new Dictionary<string, Uri>())
};
var gitHubClient = Substitute.For<IGitHubClient>();
gitHubClient.Connection.GetAsync<List<PullRequest>>(firstPageUrl, null, null)
.Returns(Task.Factory.StartNew<IResponse<List<PullRequest>>>(() => firstPageResponse));
gitHubClient.Connection.GetAsync<List<PullRequest>>(secondPageUrl, null, null)
.Returns(Task.Factory.StartNew<IResponse<List<PullRequest>>>(() => secondPageResponse));
gitHubClient.Connection.GetAsync<List<PullRequest>>(thirdPageUrl, null, null)
.Returns(Task.Factory.StartNew<IResponse<List<PullRequest>>>(() => lastPageResponse));
var client = new ObservablePullRequestsClient(gitHubClient);
var results = client.GetForRepository("fake", "repo").ToArray().Wait();
Assert.Equal(7, results.Length);
Assert.Equal(firstPageResponse.BodyAsObject[0].Number, results[0].Number);
Assert.Equal(secondPageResponse.BodyAsObject[1].Number, results[4].Number);
Assert.Equal(lastPageResponse.BodyAsObject[0].Number, results[6].Number);
}
[Fact]
public void SendsAppropriateParameters()
{
var firstPageUrl = new Uri("repos/fake/repo/pulls", UriKind.Relative);
var secondPageUrl = new Uri("https://example.com/page/2");
var firstPageLinks = new Dictionary<string, Uri> { { "next", secondPageUrl } };
var firstPageResponse = new ApiResponse<List<PullRequest>>
{
BodyAsObject = new List<PullRequest>
{
new PullRequest {Number = 1},
new PullRequest {Number = 2},
new PullRequest {Number = 3},
},
ApiInfo = CreateApiInfo(firstPageLinks)
};
var thirdPageUrl = new Uri("https://example.com/page/3");
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<PullRequest>>
{
BodyAsObject = new List<PullRequest>
{
new PullRequest {Number = 4},
new PullRequest {Number = 5},
new PullRequest {Number = 6},
},
ApiInfo = CreateApiInfo(secondPageLinks)
};
var lastPageResponse = new ApiResponse<List<PullRequest>>
{
BodyAsObject = new List<PullRequest>
{
new PullRequest {Number = 7},
},
ApiInfo = CreateApiInfo(new Dictionary<string, Uri>())
};
var gitHubClient = Substitute.For<IGitHubClient>();
gitHubClient.Connection.GetAsync<List<PullRequest>>(Arg.Is(firstPageUrl),
Arg.Is<Dictionary<string, string>>(d => d.Count == 3
&& d["head"] == "user:ref-name"
&& d["state"] == "open"
&& d["base"] == "fake_base_branch"), Arg.Any<string>())
.Returns(Task.Factory.StartNew<IResponse<List<PullRequest>>>(() => firstPageResponse));
gitHubClient.Connection.GetAsync<List<PullRequest>>(secondPageUrl, null, null)
.Returns(Task.Factory.StartNew<IResponse<List<PullRequest>>>(() => secondPageResponse));
gitHubClient.Connection.GetAsync<List<PullRequest>>(thirdPageUrl, null, null)
.Returns(Task.Factory.StartNew<IResponse<List<PullRequest>>>(() => lastPageResponse));
var client = new ObservablePullRequestsClient(gitHubClient);
var results = client.GetForRepository("fake", "repo", new PullRequestRequest { Head = "user:ref-name", Base = "fake_base_branch" }).ToArray().Wait();
Assert.Equal(7, results.Length);
Assert.Equal(firstPageResponse.BodyAsObject[0].Number, results[0].Number);
Assert.Equal(secondPageResponse.BodyAsObject[1].Number, results[4].Number);
Assert.Equal(lastPageResponse.BodyAsObject[0].Number, results[6].Number);
}
}
public class TheCreateMethod
{
[Fact]
public void CreatesFromClientIssueMilestone()
{
var newPullRequest = new NewPullRequest("some title");
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservablePullRequestsClient(gitHubClient);
client.Create("fake", "repo", newPullRequest);
gitHubClient.Repository.PullRequest.Received().Create("fake", "repo", newPullRequest);
}
[Fact]
public async Task EnsuresArgumentsNotNull()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservablePullRequestsClient(gitHubClient);
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create(null, "name", new NewPullRequest("title")));
AssertEx.Throws<ArgumentException>(async () => await
client.Create("", "name", new NewPullRequest("x")));
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create("owner", null, new NewPullRequest("x")));
AssertEx.Throws<ArgumentException>(async () => await
client.Create("owner", "", new NewPullRequest("x")));
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create("owner", "name", null));
}
}
public class TheUpdateMethod
{
[Fact]
public void UpdatesClientIssueMilestone()
{
var pullRequestUpdate = new PullRequestUpdate();
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservablePullRequestsClient(gitHubClient);
client.Update("fake", "repo", 42, pullRequestUpdate);
gitHubClient.Repository.PullRequest.Received().Update("fake", "repo", 42, pullRequestUpdate);
}
[Fact]
public async Task EnsuresArgumentsNotNull()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservablePullRequestsClient(gitHubClient);
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create(null, "name", new NewPullRequest("title")));
AssertEx.Throws<ArgumentException>(async () => await
client.Create("", "name", new NewPullRequest("x")));
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create("owner", null, new NewPullRequest("x")));
AssertEx.Throws<ArgumentException>(async () => await
client.Create("owner", "", new NewPullRequest("x")));
AssertEx.Throws<ArgumentNullException>(async () => await
client.Create("owner", "name", null));
}
}
public class TheCtor
{
[Fact]
public void EnsuresArgument()
{
Assert.Throws<ArgumentNullException>(() => new PullRequestsClient(null));
}
}
static ApiInfo CreateApiInfo(IDictionary<string, Uri> links)
{
return new ApiInfo(links, new List<string>(), new List<string>(), "etag", new RateLimit(new Dictionary<string, string>()));
}
}
}

View File

@@ -60,6 +60,7 @@
<Compile Include="Clients\IIssuesLabelsClient.cs" /> <Compile Include="Clients\IIssuesLabelsClient.cs" />
<Compile Include="Clients\IOrganizationMembersClient.cs" /> <Compile Include="Clients\IOrganizationMembersClient.cs" />
<Compile Include="Clients\IReferencesClient.cs" /> <Compile Include="Clients\IReferencesClient.cs" />
<Compile Include="Clients\IPullRequestsClient.cs" />
<Compile Include="Clients\IssueCommentsClient.cs" /> <Compile Include="Clients\IssueCommentsClient.cs" />
<Compile Include="Clients\IssuesClient.cs" /> <Compile Include="Clients\IssuesClient.cs" />
<Compile Include="Clients\IssuesEventsClient.cs" /> <Compile Include="Clients\IssuesEventsClient.cs" />
@@ -73,6 +74,7 @@
<Compile Include="Clients\OrganizationMembersClient.cs" /> <Compile Include="Clients\OrganizationMembersClient.cs" />
<Compile Include="Clients\ReferencesClient.cs" /> <Compile Include="Clients\ReferencesClient.cs" />
<Compile Include="Clients\StarredClient.cs" /> <Compile Include="Clients\StarredClient.cs" />
<Compile Include="Clients\PullRequestsClient.cs" />
<Compile Include="Clients\TagsClient.cs" /> <Compile Include="Clients\TagsClient.cs" />
<Compile Include="Clients\TreesClient.cs" /> <Compile Include="Clients\TreesClient.cs" />
<Compile Include="Clients\TeamsClient.cs" /> <Compile Include="Clients\TeamsClient.cs" />
@@ -98,6 +100,10 @@
<Compile Include="Models\Request\NewTeam.cs" /> <Compile Include="Models\Request\NewTeam.cs" />
<Compile Include="Models\Request\Permission.cs" /> <Compile Include="Models\Request\Permission.cs" />
<Compile Include="Models\Request\ReferenceUpdate.cs" /> <Compile Include="Models\Request\ReferenceUpdate.cs" />
<Compile Include="Models\Request\NewPullRequest.cs" />
<Compile Include="Models\Request\NewTag.cs" />
<Compile Include="Models\Request\PullRequestRequest.cs" />
<Compile Include="Models\Request\PullRequestUpdate.cs" />
<Compile Include="Models\Request\RequestParameters.cs" /> <Compile Include="Models\Request\RequestParameters.cs" />
<Compile Include="Models\Request\StarredRequest.cs" /> <Compile Include="Models\Request\StarredRequest.cs" />
<Compile Include="Models\Request\UpdateTeam.cs" /> <Compile Include="Models\Request\UpdateTeam.cs" />

View File

@@ -80,6 +80,7 @@
<Compile Include="Clients\IOrganizationMembersClient.cs" /> <Compile Include="Clients\IOrganizationMembersClient.cs" />
<Compile Include="Clients\IOrganizationsClient.cs" /> <Compile Include="Clients\IOrganizationsClient.cs" />
<Compile Include="Clients\IReferencesClient.cs" /> <Compile Include="Clients\IReferencesClient.cs" />
<Compile Include="Clients\IPullRequestsClient.cs" />
<Compile Include="Clients\IReleasesClient.cs" /> <Compile Include="Clients\IReleasesClient.cs" />
<Compile Include="Clients\IRepositoriesClient.cs" /> <Compile Include="Clients\IRepositoriesClient.cs" />
<Compile Include="Clients\ISshKeysClient.cs" /> <Compile Include="Clients\ISshKeysClient.cs" />
@@ -99,6 +100,7 @@
<Compile Include="Clients\OrganizationMembersClient.cs" /> <Compile Include="Clients\OrganizationMembersClient.cs" />
<Compile Include="Clients\OrganizationsClient.cs" /> <Compile Include="Clients\OrganizationsClient.cs" />
<Compile Include="Clients\ReferencesClient.cs" /> <Compile Include="Clients\ReferencesClient.cs" />
<Compile Include="Clients\PullRequestsClient.cs" />
<Compile Include="Clients\ReleasesClient.cs" /> <Compile Include="Clients\ReleasesClient.cs" />
<Compile Include="Clients\RepositoriesClient.cs" /> <Compile Include="Clients\RepositoriesClient.cs" />
<Compile Include="Clients\SshKeysClient.cs" /> <Compile Include="Clients\SshKeysClient.cs" />
@@ -173,6 +175,7 @@
<Compile Include="Models\Request\NewLabel.cs" /> <Compile Include="Models\Request\NewLabel.cs" />
<Compile Include="Models\Request\NewMilestone.cs" /> <Compile Include="Models\Request\NewMilestone.cs" />
<Compile Include="Models\Request\NewReference.cs" /> <Compile Include="Models\Request\NewReference.cs" />
<Compile Include="Models\Request\NewPullRequest.cs" />
<Compile Include="Models\Request\NewRepository.cs" /> <Compile Include="Models\Request\NewRepository.cs" />
<Compile Include="Models\Request\NewSubscription.cs" /> <Compile Include="Models\Request\NewSubscription.cs" />
<Compile Include="Models\Request\NewTag.cs" /> <Compile Include="Models\Request\NewTag.cs" />
@@ -181,6 +184,8 @@
<Compile Include="Models\Request\NewTeam.cs" /> <Compile Include="Models\Request\NewTeam.cs" />
<Compile Include="Models\Request\Permission.cs" /> <Compile Include="Models\Request\Permission.cs" />
<Compile Include="Models\Request\ReferenceUpdate.cs" /> <Compile Include="Models\Request\ReferenceUpdate.cs" />
<Compile Include="Models\Request\PullRequestRequest.cs" />
<Compile Include="Models\Request\PullRequestUpdate.cs" />
<Compile Include="Models\Request\ReleaseUpdate.cs" /> <Compile Include="Models\Request\ReleaseUpdate.cs" />
<Compile Include="Models\Request\RepositoryIssueRequest.cs" /> <Compile Include="Models\Request\RepositoryIssueRequest.cs" />
<Compile Include="Models\Request\RequestParameters.cs" /> <Compile Include="Models\Request\RequestParameters.cs" />