From da35ae04afe7b0dc59ef2e25ddc029a14cfd6209 Mon Sep 17 00:00:00 2001 From: "aedampir@gmail.com" Date: Wed, 8 Jun 2016 17:44:56 +0700 Subject: [PATCH] added new unit tests --- .../Clients/DeploymentsClientTests.cs | 59 +++++++++--- .../ObservableDeploymentsClientTests.cs | 90 ++++++++++++++++++- 2 files changed, 136 insertions(+), 13 deletions(-) diff --git a/Octokit.Tests/Clients/DeploymentsClientTests.cs b/Octokit.Tests/Clients/DeploymentsClientTests.cs index a29e555a..53aede91 100644 --- a/Octokit.Tests/Clients/DeploymentsClientTests.cs +++ b/Octokit.Tests/Clients/DeploymentsClientTests.cs @@ -9,8 +9,9 @@ public class DeploymentsClientTests { public class TheGetAllMethod { - private const string name = "name"; - private const string owner = "owner"; + const string name = "name"; + const string owner = "owner"; + const int repositoryId = 1; [Fact] public async Task EnsuresNonNullArguments() @@ -20,6 +21,8 @@ public class DeploymentsClientTests await Assert.ThrowsAsync(() => client.GetAll(null, name)); await Assert.ThrowsAsync(() => client.GetAll(owner, null)); await Assert.ThrowsAsync(() => client.GetAll(owner, name, null)); + + await Assert.ThrowsAsync(() => client.GetAll(repositoryId, null)); } [Fact] @@ -46,19 +49,33 @@ public class DeploymentsClientTests } [Fact] - public void RequestsCorrectUrl() + public async Task RequestsCorrectUrl() { var connection = Substitute.For(); var client = new DeploymentsClient(connection); var expectedUrl = string.Format("repos/{0}/{1}/deployments", owner, name); - client.GetAll(owner, name); + await client.GetAll(owner, name); + connection.Received(1) .GetAll(Arg.Is(u => u.ToString() == expectedUrl), Args.ApiOptions); } [Fact] - public void RequestsCorrectUrlWithApiOptions() + public async Task RequestsCorrectUrlWithRepositoryId() + { + var connection = Substitute.For(); + var client = new DeploymentsClient(connection); + var expectedUrl = string.Format("repositories/{0}/deployments", repositoryId); + + await client.GetAll(repositoryId); + + connection.Received(1) + .GetAll(Arg.Is(u => u.ToString() == expectedUrl), Args.ApiOptions); + } + + [Fact] + public async Task RequestsCorrectUrlWithApiOptions() { var connection = Substitute.For(); var client = new DeploymentsClient(connection); @@ -71,7 +88,28 @@ public class DeploymentsClientTests StartPage = 1 }; - client.GetAll(owner, name, options); + await client.GetAll(owner, name, options); + + connection.Received(1) + .GetAll(Arg.Is(u => u.ToString() == expectedUrl), options); + } + + [Fact] + public async Task RequestsCorrectUrlWithRepostoryIdWithApiOptions() + { + var connection = Substitute.For(); + var client = new DeploymentsClient(connection); + var expectedUrl = string.Format("repositories/{0}/deployments", repositoryId); + + var options = new ApiOptions + { + PageSize = 1, + PageCount = 1, + StartPage = 1 + }; + + await client.GetAll(repositoryId, options); + connection.Received(1) .GetAll(Arg.Is(u => u.ToString() == expectedUrl), options); } @@ -124,18 +162,19 @@ public class DeploymentsClientTests client.Create("owner", "name", newDeployment); connection.Received(1).Post(Arg.Is(u => u.ToString() == expectedUrl), - Arg.Any()); + newDeployment); } [Fact] - public void PassesNewDeploymentRequest() + public void PostsToDeploymentsUrlWithRepositoryId() { var connection = Substitute.For(); var client = new DeploymentsClient(connection); + var expectedUrl = "repositories/1/deployments"; - client.Create("owner", "name", newDeployment); + client.Create(1, newDeployment); - connection.Received(1).Post(Arg.Any(), + connection.Received(1).Post(Arg.Is(u => u.ToString() == expectedUrl), newDeployment); } } diff --git a/Octokit.Tests/Reactive/ObservableDeploymentsClientTests.cs b/Octokit.Tests/Reactive/ObservableDeploymentsClientTests.cs index 38328dfc..927adf4a 100644 --- a/Octokit.Tests/Reactive/ObservableDeploymentsClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableDeploymentsClientTests.cs @@ -17,6 +17,7 @@ namespace Octokit.Tests.Reactive private readonly ObservableDeploymentsClient _client; private const string owner = "owner"; private const string name = "name"; + private const int repositoryId = 1; public TheGetAllMethod() { @@ -30,6 +31,8 @@ namespace Octokit.Tests.Reactive Assert.Throws(() => _client.GetAll(null, name)); Assert.Throws(() => _client.GetAll(owner, null)); Assert.Throws(() => _client.GetAll(owner, name, null)); + + Assert.Throws(() => _client.GetAll(repositoryId, null)); } [Fact] @@ -54,6 +57,20 @@ namespace Octokit.Tests.Reactive var expectedUrl = string.Format("repos/{0}/{1}/deployments", owner, name); _client.GetAll(owner, name); + + _githubClient.Connection.Received(1) + .Get>(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Is>(dictionary => dictionary.Count == 0), + Arg.Any()); + } + + [Fact] + public void RequestsCorrectUrlWithRepositoryId() + { + var expectedUrl = string.Format("repositories/{0}/deployments", repositoryId); + + _client.GetAll(repositoryId); + _githubClient.Connection.Received(1) .Get>(Arg.Is(u => u.ToString() == expectedUrl), Arg.Is>(dictionary => dictionary.Count == 0), @@ -103,6 +120,53 @@ namespace Octokit.Tests.Reactive Arg.Is>(dictionary => dictionary.Count == 0), null); } + + [Fact] + public void RequestsCorrectUrlWithRepositoryIdWithApiOptions() + { + var expectedUrl = string.Format("repositories/{0}/deployments", repositoryId); + + // all properties are setted => only 2 options (StartPage, PageSize) in dictionary + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + _client.GetAll(repositoryId, options); + + _githubClient.Connection.Received(1) + .Get>(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Is>(dictionary => dictionary.Count == 2), + null); + + // StartPage is setted => only 1 option (StartPage) in dictionary + options = new ApiOptions + { + StartPage = 1 + }; + + _client.GetAll(repositoryId, options); + + _githubClient.Connection.Received(1) + .Get>(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Is>(dictionary => dictionary.Count == 1), + null); + + // PageCount is setted => none of options in dictionary + options = new ApiOptions + { + PageCount = 1 + }; + + _client.GetAll(repositoryId, options); + + _githubClient.Connection.Received(1) + .Get>(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Is>(dictionary => dictionary.Count == 0), + null); + } } public class TheCreateMethod @@ -135,6 +199,8 @@ namespace Octokit.Tests.Reactive Assert.Throws(() => _client.Create(null, "repo", new NewDeployment("ref"))); Assert.Throws(() => _client.Create("owner", null, new NewDeployment("ref"))); Assert.Throws(() => _client.Create("owner", "repo", null)); + + Assert.Throws(() => _client.Create(1, null)); } [Fact] @@ -163,11 +229,22 @@ namespace Octokit.Tests.Reactive SetupWithoutNonReactiveClient(); var newDeployment = new NewDeployment("ref"); + _client.Create("owner", "repo", newDeployment); - _githubClient.Repository.Deployment.Received(1).Create(Arg.Is("owner"), - Arg.Is("repo"), - Arg.Is(newDeployment)); + _githubClient.Repository.Deployment.Received(1).Create("owner", "repo", newDeployment); + } + + [Fact] + public void CallsCreateOnRegularDeploymentsClientWithRepositoryId() + { + SetupWithoutNonReactiveClient(); + + var newDeployment = new NewDeployment("ref"); + + _client.Create(1, newDeployment); + + _githubClient.Repository.Deployment.Received(1).Create(1, newDeployment); } } @@ -178,6 +255,13 @@ namespace Octokit.Tests.Reactive { Assert.Throws(() => new ObservableDeploymentsClient(null)); } + + [Fact] + public void SetsStatusesClient() + { + var client = new ObservableDeploymentsClient(Substitute.For()); + Assert.NotNull(client.Status); + } } } }