diff --git a/Octokit.Tests/Clients/PullRequestsClientTests.cs b/Octokit.Tests/Clients/PullRequestsClientTests.cs new file mode 100644 index 00000000..4a81fe96 --- /dev/null +++ b/Octokit.Tests/Clients/PullRequestsClientTests.cs @@ -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(); + var client = new PullRequestsClient(connection); + + client.Get("fake", "repo", 42); + + connection.Received().Get(Arg.Is(u => u.ToString() == "repos/fake/repo/pulls/42"), + null); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new PullRequestsClient(Substitute.For()); + + await AssertEx.Throws(async () => await client.Get(null, "name", 1)); + await AssertEx.Throws(async () => await client.Get("owner", null, 1)); + await AssertEx.Throws(async () => await client.Get(null, "", 1)); + await AssertEx.Throws(async () => await client.Get("", null, 1)); + } + } + + public class TheGetForRepositoryMethod + { + [Fact] + public async Task RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new PullRequestsClient(connection); + + await client.GetForRepository("fake", "repo"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/fake/repo/pulls"), + Arg.Any>()); + } + + [Fact] + public void SendsAppropriateParameters() + { + var connection = Substitute.For(); + var client = new PullRequestsClient(connection); + + client.GetForRepository("fake", "repo", new PullRequestRequest { Head = "user:ref-head", Base = "fake_base_branch" }); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/fake/repo/pulls"), + Arg.Is>(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(); + var client = new PullRequestsClient(connection); + + client.Create("fake", "repo", newPullRequest); + + connection.Received().Post(Arg.Is(u => u.ToString() == "repos/fake/repo/pulls"), + newPullRequest); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new PullRequestsClient(connection); + + AssertEx.Throws(async () => await + client.Create(null, "name", new NewPullRequest("title"))); + AssertEx.Throws(async () => await + client.Create("", "name", new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", null, new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", "", new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", "name", null)); + } + } + + public class TheUpdateMethod + { + [Fact] + public void PostsToCorrectUrl() + { + var pullRequestUpdate = new PullRequestUpdate(); + var connection = Substitute.For(); + var client = new PullRequestsClient(connection); + + client.Update("fake", "repo", 42, pullRequestUpdate); + + connection.Received().Patch(Arg.Is(u => u.ToString() == "repos/fake/repo/pulls/42"), + pullRequestUpdate); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new PullRequestsClient(connection); + + AssertEx.Throws(async () => await + client.Create(null, "name", new NewPullRequest("title"))); + AssertEx.Throws(async () => await + client.Create("", "name", new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", null, new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", "", new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", "name", null)); + } + } + + public class TheCtor + { + [Fact] + public void EnsuresArgument() + { + Assert.Throws(() => new PullRequestsClient(null)); + } + } + } +} diff --git a/Octokit.Tests/Models/PullRequestRequestTests.cs b/Octokit.Tests/Models/PullRequestRequestTests.cs new file mode 100644 index 00000000..51fb7714 --- /dev/null +++ b/Octokit.Tests/Models/PullRequestRequestTests.cs @@ -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"]); + } + } +} diff --git a/Octokit.Tests/OctoKit.Tests-NetCore45.csproj b/Octokit.Tests/OctoKit.Tests-NetCore45.csproj index 2e4c8ce9..351d9303 100644 --- a/Octokit.Tests/OctoKit.Tests-NetCore45.csproj +++ b/Octokit.Tests/OctoKit.Tests-NetCore45.csproj @@ -66,6 +66,7 @@ + diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index 9b4eda2c..711cb418 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -76,6 +76,7 @@ + @@ -121,6 +122,7 @@ + @@ -134,6 +136,7 @@ + diff --git a/Octokit.Tests/Reactive/ObservablePullRequestsClientTests.cs b/Octokit.Tests/Reactive/ObservablePullRequestsClientTests.cs new file mode 100644 index 00000000..f1428853 --- /dev/null +++ b/Octokit.Tests/Reactive/ObservablePullRequestsClientTests.cs @@ -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(); + 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()); + + await AssertEx.Throws(async () => await client.Get(null, "name", 1)); + await AssertEx.Throws(async () => await client.Get("owner", null, 1)); + await AssertEx.Throws(async () => await client.Get(null, "", 1)); + await AssertEx.Throws(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 { { "next", secondPageUrl } }; + var firstPageResponse = new ApiResponse> + { + BodyAsObject = new List + { + 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 { { "next", thirdPageUrl } }; + var secondPageResponse = new ApiResponse> + { + BodyAsObject = new List + { + new PullRequest {Number = 4}, + new PullRequest {Number = 5}, + new PullRequest {Number = 6}, + }, + ApiInfo = CreateApiInfo(secondPageLinks) + }; + var lastPageResponse = new ApiResponse> + { + BodyAsObject = new List + { + new PullRequest {Number = 7}, + }, + ApiInfo = CreateApiInfo(new Dictionary()) + }; + var gitHubClient = Substitute.For(); + gitHubClient.Connection.GetAsync>(firstPageUrl, null, null) + .Returns(Task.Factory.StartNew>>(() => firstPageResponse)); + gitHubClient.Connection.GetAsync>(secondPageUrl, null, null) + .Returns(Task.Factory.StartNew>>(() => secondPageResponse)); + gitHubClient.Connection.GetAsync>(thirdPageUrl, null, null) + .Returns(Task.Factory.StartNew>>(() => 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 { { "next", secondPageUrl } }; + var firstPageResponse = new ApiResponse> + { + BodyAsObject = new List + { + 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 { { "next", thirdPageUrl } }; + var secondPageResponse = new ApiResponse> + { + BodyAsObject = new List + { + new PullRequest {Number = 4}, + new PullRequest {Number = 5}, + new PullRequest {Number = 6}, + }, + ApiInfo = CreateApiInfo(secondPageLinks) + }; + var lastPageResponse = new ApiResponse> + { + BodyAsObject = new List + { + new PullRequest {Number = 7}, + }, + ApiInfo = CreateApiInfo(new Dictionary()) + }; + var gitHubClient = Substitute.For(); + gitHubClient.Connection.GetAsync>(Arg.Is(firstPageUrl), + Arg.Is>(d => d.Count == 3 + && d["head"] == "user:ref-name" + && d["state"] == "open" + && d["base"] == "fake_base_branch"), Arg.Any()) + .Returns(Task.Factory.StartNew>>(() => firstPageResponse)); + gitHubClient.Connection.GetAsync>(secondPageUrl, null, null) + .Returns(Task.Factory.StartNew>>(() => secondPageResponse)); + gitHubClient.Connection.GetAsync>(thirdPageUrl, null, null) + .Returns(Task.Factory.StartNew>>(() => 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(); + 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(); + var client = new ObservablePullRequestsClient(gitHubClient); + + AssertEx.Throws(async () => await + client.Create(null, "name", new NewPullRequest("title"))); + AssertEx.Throws(async () => await + client.Create("", "name", new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", null, new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", "", new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", "name", null)); + } + } + + public class TheUpdateMethod + { + [Fact] + public void UpdatesClientIssueMilestone() + { + var pullRequestUpdate = new PullRequestUpdate(); + var gitHubClient = Substitute.For(); + 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(); + var client = new ObservablePullRequestsClient(gitHubClient); + + AssertEx.Throws(async () => await + client.Create(null, "name", new NewPullRequest("title"))); + AssertEx.Throws(async () => await + client.Create("", "name", new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", null, new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", "", new NewPullRequest("x"))); + AssertEx.Throws(async () => await + client.Create("owner", "name", null)); + } + } + + public class TheCtor + { + [Fact] + public void EnsuresArgument() + { + Assert.Throws(() => new PullRequestsClient(null)); + } + } + + static ApiInfo CreateApiInfo(IDictionary links) + { + return new ApiInfo(links, new List(), new List(), "etag", new RateLimit(new Dictionary())); + } + } +} diff --git a/Octokit/Octokit-Mono.csproj b/Octokit/Octokit-Mono.csproj index dccb201c..cc5f23f8 100644 --- a/Octokit/Octokit-Mono.csproj +++ b/Octokit/Octokit-Mono.csproj @@ -60,6 +60,7 @@ + @@ -73,6 +74,7 @@ + @@ -98,6 +100,10 @@ + + + + diff --git a/Octokit/Octokit-netcore45.csproj b/Octokit/Octokit-netcore45.csproj index 68d7309f..4b21fbed 100644 --- a/Octokit/Octokit-netcore45.csproj +++ b/Octokit/Octokit-netcore45.csproj @@ -80,6 +80,7 @@ + @@ -99,6 +100,7 @@ + @@ -173,6 +175,7 @@ + @@ -181,6 +184,8 @@ + +