added unit tests

This commit is contained in:
aedampir@gmail.com
2016-05-25 14:00:04 +07:00
parent 6ece0ef037
commit 04a91d60f9
2 changed files with 291 additions and 21 deletions
@@ -44,16 +44,35 @@ namespace Octokit.Tests.Clients
await client.GetAllForRepository("fake", "repo");
connection.Received().GetAll<PullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls"),
Arg.Any<Dictionary<string, string>>());
Arg.Any<Dictionary<string, string>>(), Args.ApiOptions);
}
[Fact]
public void SendsAppropriateParameters()
public async Task RequestsCorrectUrlWithApiOptions()
{
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
client.GetAllForRepository("fake", "repo", new PullRequestRequest { Head = "user:ref-head", Base = "fake_base_branch" });
var options = new ApiOptions
{
StartPage = 1,
PageCount = 1,
PageSize = 1
};
await client.GetAllForRepository("fake", "repo", options);
connection.Received().GetAll<PullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls"),
Arg.Any<Dictionary<string, string>>(), options);
}
[Fact]
public async Task SendsAppropriateParameters()
{
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
await client.GetAllForRepository("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 == 5
@@ -61,20 +80,78 @@ namespace Octokit.Tests.Clients
&& d["state"] == "open"
&& d["base"] == "fake_base_branch"
&& d["sort"] == "created"
&& d["direction"] == "desc"));
&& d["direction"] == "desc"), Args.ApiOptions);
}
[Fact]
public async Task SendsAppropriateParametersWithApiOptions()
{
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
var options = new ApiOptions
{
StartPage = 1,
PageCount = 1,
PageSize = 1
};
await client.GetAllForRepository("fake", "repo", new PullRequestRequest { Head = "user:ref-head", Base = "fake_base_branch" }, options);
connection.Received().GetAll<PullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls"),
Arg.Is<Dictionary<string, string>>(d => d.Count == 5
&& d["head"] == "user:ref-head"
&& d["state"] == "open"
&& d["base"] == "fake_base_branch"
&& d["sort"] == "created"
&& d["direction"] == "desc"), options);
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new PullRequestsClient(Substitute.For<IApiConnection>());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, "name"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, "name", ApiOptions.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null, ApiOptions.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (ApiOptions)null));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, "name", new PullRequestRequest()));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null, new PullRequestRequest()));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (PullRequestRequest)null));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, "name", new PullRequestRequest(), ApiOptions.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null, new PullRequestRequest(), ApiOptions.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", null, ApiOptions.None));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", new PullRequestRequest(), null));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", "name"));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("owner", ""));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", "name", ApiOptions.None));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("owner", "", ApiOptions.None));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", "name", new PullRequestRequest()));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("owner", "", new PullRequestRequest()));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", "name", new PullRequestRequest(), ApiOptions.None));
await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("owner", "", new PullRequestRequest(), ApiOptions.None));
}
}
public class TheCreateMethod
{
[Fact]
public void PostsToCorrectUrl()
public async Task PostsToCorrectUrl()
{
var newPullRequest = new NewPullRequest("some title", "branch:name", "branch:name");
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
client.Create("fake", "repo", newPullRequest);
await client.Create("fake", "repo", newPullRequest);
connection.Received().Post<PullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls"),
newPullRequest);
@@ -102,13 +179,13 @@ namespace Octokit.Tests.Clients
public class TheUpdateMethod
{
[Fact]
public void PostsToCorrectUrl()
public async Task PostsToCorrectUrl()
{
var pullRequestUpdate = new PullRequestUpdate();
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
client.Update("fake", "repo", 42, pullRequestUpdate);
await client.Update("fake", "repo", 42, pullRequestUpdate);
connection.Received().Patch<PullRequest>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls/42"),
pullRequestUpdate);
@@ -136,13 +213,13 @@ namespace Octokit.Tests.Clients
public class TheMergeMethod
{
[Fact]
public void PutsToCorrectUrl()
public async Task PutsToCorrectUrl()
{
var mergePullRequest = new MergePullRequest { CommitMessage = "fake commit message" };
var connection = Substitute.For<IApiConnection>();
var client = new PullRequestsClient(connection);
client.Merge("fake", "repo", 42, mergePullRequest);
await client.Merge("fake", "repo", 42, mergePullRequest);
connection.Received().Put<PullRequestMerge>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls/42/merge"),
mergePullRequest,null, "application/vnd.github.polaris-preview+json");
@@ -166,7 +243,7 @@ namespace Octokit.Tests.Clients
public class TheMergedMethod
{
[Fact]
public void RequestsCorrectUrl()
public async Task RequestsCorrectUrl()
{
var conn = Substitute.For<IConnection>();
var connection = Substitute.For<IApiConnection>();
@@ -174,7 +251,7 @@ namespace Octokit.Tests.Clients
var client = new PullRequestsClient(connection);
client.Merged("fake", "repo", 42);
await client.Merged("fake", "repo", 42);
conn.Received().Get<object>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/pulls/42/merge"), null, null);
}
@@ -41,7 +41,7 @@ namespace Octokit.Tests.Reactive
public class TheGetForRepositoryMethod
{
[Fact]
public void ReturnsEveryPageOfPullRequests()
public async Task ReturnsEveryPageOfPullRequests()
{
var firstPageUrl = new Uri("repos/fake/repo/pulls", UriKind.Relative);
var secondPageUrl = new Uri("https://example.com/page/2");
@@ -77,15 +77,15 @@ namespace Octokit.Tests.Reactive
}
);
var gitHubClient = Substitute.For<IGitHubClient>();
gitHubClient.Connection.Get<List<PullRequest>>(firstPageUrl, null, null)
gitHubClient.Connection.Get<List<PullRequest>>(firstPageUrl, Args.EmptyDictionary, null)
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => firstPageResponse));
gitHubClient.Connection.Get<List<PullRequest>>(secondPageUrl, null, null)
gitHubClient.Connection.Get<List<PullRequest>>(secondPageUrl, Args.EmptyDictionary, null)
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => secondPageResponse));
gitHubClient.Connection.Get<List<PullRequest>>(thirdPageUrl, null, null)
gitHubClient.Connection.Get<List<PullRequest>>(thirdPageUrl, Args.EmptyDictionary, null)
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => lastPageResponse));
var client = new ObservablePullRequestsClient(gitHubClient);
var results = client.GetAllForRepository("fake", "repo").ToArray().Wait();
var results = await client.GetAllForRepository("fake", "repo").ToArray();
Assert.Equal(7, results.Length);
Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number);
@@ -94,7 +94,74 @@ namespace Octokit.Tests.Reactive
}
[Fact]
public void SendsAppropriateParameters()
public async Task ReturnsEveryPageOfPullRequestsWithApiOptions()
{
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>>
(
CreateResponseWithApiInfo(firstPageLinks),
new List<PullRequest>
{
new PullRequest(1),
new PullRequest(2),
new PullRequest(3)
}
);
var thirdPageUrl = new Uri("https://example.com/page/3");
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<PullRequest>>
(
CreateResponseWithApiInfo(secondPageLinks),
new List<PullRequest>
{
new PullRequest(4),
new PullRequest(5),
new PullRequest(6)
}
);
var lastPageResponse = new ApiResponse<List<PullRequest>>
(
new Response(),
new List<PullRequest>
{
new PullRequest(7)
}
);
var gitHubClient = Substitute.For<IGitHubClient>();
gitHubClient.Connection.Get<List<PullRequest>>(firstPageUrl, Arg.Is<Dictionary<string, string>>(d => d.Count == 2
&& d["page"] == "1"
&& d["per_page"] == "2"), null)
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => firstPageResponse));
gitHubClient.Connection.Get<List<PullRequest>>(secondPageUrl, Arg.Is<Dictionary<string, string>>(d => d.Count == 2
&& d["page"] == "1"
&& d["per_page"] == "2"), null)
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => secondPageResponse));
gitHubClient.Connection.Get<List<PullRequest>>(thirdPageUrl, Arg.Is<Dictionary<string, string>>(d => d.Count == 2
&& d["page"] == "1"
&& d["per_page"] == "2"), null)
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => lastPageResponse));
var client = new ObservablePullRequestsClient(gitHubClient);
var options = new ApiOptions
{
StartPage = 1,
PageSize = 2,
PageCount = 1
};
var results = await client.GetAllForRepository("fake", "repo", options).ToArray();
Assert.Equal(7, results.Length);
Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number);
Assert.Equal(secondPageResponse.Body[1].Number, results[4].Number);
Assert.Equal(lastPageResponse.Body[0].Number, results[6].Number);
}
[Fact]
public async Task SendsAppropriateParameters()
{
var firstPageUrl = new Uri("repos/fake/repo/pulls", UriKind.Relative);
var secondPageUrl = new Uri("https://example.com/page/2");
@@ -138,19 +205,145 @@ namespace Octokit.Tests.Reactive
&& d["sort"] == "created"
&& d["direction"] == "desc"), Arg.Any<string>())
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => firstPageResponse));
gitHubClient.Connection.Get<List<PullRequest>>(secondPageUrl, null, null)
gitHubClient.Connection.Get<List<PullRequest>>(secondPageUrl, Arg.Is<Dictionary<string, string>>(d => d.Count == 5
&& d["head"] == "user:ref-name"
&& d["state"] == "open"
&& d["base"] == "fake_base_branch"
&& d["sort"] == "created"
&& d["direction"] == "desc"), null)
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => secondPageResponse));
gitHubClient.Connection.Get<List<PullRequest>>(thirdPageUrl, null, null)
gitHubClient.Connection.Get<List<PullRequest>>(thirdPageUrl, Arg.Is<Dictionary<string, string>>(d => d.Count == 5
&& d["head"] == "user:ref-name"
&& d["state"] == "open"
&& d["base"] == "fake_base_branch"
&& d["sort"] == "created"
&& d["direction"] == "desc"), null)
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => lastPageResponse));
var client = new ObservablePullRequestsClient(gitHubClient);
var results = client.GetAllForRepository("fake", "repo", new PullRequestRequest { Head = "user:ref-name", Base = "fake_base_branch" }).ToArray().Wait();
var results = await client.GetAllForRepository("fake", "repo", new PullRequestRequest { Head = "user:ref-name", Base = "fake_base_branch" }).ToArray();
Assert.Equal(7, results.Length);
Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number);
Assert.Equal(secondPageResponse.Body[1].Number, results[4].Number);
Assert.Equal(lastPageResponse.Body[0].Number, results[6].Number);
}
[Fact]
public async Task SendsAppropriateParametersWitApiOptions()
{
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>>
(
CreateResponseWithApiInfo(firstPageLinks),
new List<PullRequest>
{
new PullRequest(1),
new PullRequest(2),
new PullRequest(3)
}
);
var thirdPageUrl = new Uri("https://example.com/page/3");
var secondPageLinks = new Dictionary<string, Uri> { { "next", thirdPageUrl } };
var secondPageResponse = new ApiResponse<List<PullRequest>>
(
CreateResponseWithApiInfo(secondPageLinks),
new List<PullRequest>
{
new PullRequest(4),
new PullRequest(5),
new PullRequest(6)
}
);
var lastPageResponse = new ApiResponse<List<PullRequest>>
(
new Response(),
new List<PullRequest>
{
new PullRequest(7)
}
);
var gitHubClient = Substitute.For<IGitHubClient>();
gitHubClient.Connection.Get<List<PullRequest>>(Arg.Is(firstPageUrl),
Arg.Is<Dictionary<string, string>>(d => d.Count == 7
&& d["head"] == "user:ref-name"
&& d["state"] == "open"
&& d["base"] == "fake_base_branch"
&& d["sort"] == "created"
&& d["direction"] == "desc"
&& d["page"] == "1"
&& d["per_page"] == "2"), Arg.Any<string>())
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => firstPageResponse));
gitHubClient.Connection.Get<List<PullRequest>>(secondPageUrl, Arg.Is<Dictionary<string, string>>(d => d.Count == 7
&& d["head"] == "user:ref-name"
&& d["state"] == "open"
&& d["base"] == "fake_base_branch"
&& d["sort"] == "created"
&& d["direction"] == "desc"), null)
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => secondPageResponse));
gitHubClient.Connection.Get<List<PullRequest>>(thirdPageUrl, Arg.Is<Dictionary<string, string>>(d => d.Count == 7
&& d["head"] == "user:ref-name"
&& d["state"] == "open"
&& d["base"] == "fake_base_branch"
&& d["sort"] == "created"
&& d["direction"] == "desc"
&& d["page"] == "1"
&& d["per_page"] == "2"), null)
.Returns(Task.Factory.StartNew<IApiResponse<List<PullRequest>>>(() => lastPageResponse));
var client = new ObservablePullRequestsClient(gitHubClient);
var options = new ApiOptions
{
StartPage = 1,
PageSize = 2,
PageCount = 1
};
var results = await client.GetAllForRepository("fake", "repo", new PullRequestRequest { Head = "user:ref-name", Base = "fake_base_branch" }, options).ToArray();
Assert.Equal(7, results.Length);
Assert.Equal(firstPageResponse.Body[0].Number, results[0].Number);
Assert.Equal(secondPageResponse.Body[1].Number, results[4].Number);
Assert.Equal(lastPageResponse.Body[0].Number, results[6].Number);
}
[Fact]
public async Task EnsuresNonNullArguments()
{
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservablePullRequestsClient(gitHubClient);
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name"));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", ApiOptions.None));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, ApiOptions.None));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (ApiOptions)null));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", new PullRequestRequest()));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, new PullRequestRequest()));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (PullRequestRequest)null));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", new PullRequestRequest(), ApiOptions.None));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, new PullRequestRequest(), ApiOptions.None));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", null, ApiOptions.None));
Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", new PullRequestRequest(), null));
Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name"));
Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", ""));
Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", ApiOptions.None));
Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", ApiOptions.None));
Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", new PullRequestRequest()));
Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", new PullRequestRequest()));
Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", new PullRequestRequest(), ApiOptions.None));
Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", new PullRequestRequest(), ApiOptions.None));
}
}
public class TheCreateMethod