From 6f55ca8af7a81a75a5f529172fb869ef35fa0c04 Mon Sep 17 00:00:00 2001 From: Alexander Efremov Date: Sun, 12 Jun 2016 13:26:12 +0700 Subject: [PATCH] added new unit tests --- .../Clients/DeploymentStatusClientTests.cs | 157 ++++++++++------- .../ObservableDeploymentStatusClientTests.cs | 158 +++++++++++------- 2 files changed, 193 insertions(+), 122 deletions(-) diff --git a/Octokit.Tests/Clients/DeploymentStatusClientTests.cs b/Octokit.Tests/Clients/DeploymentStatusClientTests.cs index 87c97bfb..ca84220d 100644 --- a/Octokit.Tests/Clients/DeploymentStatusClientTests.cs +++ b/Octokit.Tests/Clients/DeploymentStatusClientTests.cs @@ -10,6 +10,68 @@ public class DeploymentStatusClientTests { public class TheGetAllMethod { + [Fact] + public async Task RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new DeploymentStatusClient(connection); + var expectedUrl = "repos/owner/name/deployments/1/statuses"; + + await client.GetAll("owner", "name", 1); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == expectedUrl), Args.ApiOptions); + } + + [Fact] + public async Task RequestsCorrectUrlWithRepositoryId() + { + var connection = Substitute.For(); + var client = new DeploymentStatusClient(connection); + var expectedUrl = "repositories/1/deployments/1/statuses"; + + await client.GetAll(1, 1); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == expectedUrl), Args.ApiOptions); + } + + [Fact] + public async Task RequestsCorrectUrlWithApiOptions() + { + var connection = Substitute.For(); + var client = new DeploymentStatusClient(connection); + var expectedUrl = "repos/owner/name/deployments/1/statuses"; + + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + await client.GetAll("owner", "name", 1, options); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == expectedUrl), options); + } + + [Fact] + public async Task RequestsCorrectUrlWithRepositoryIdWithApiOptions() + { + var connection = Substitute.For(); + var client = new DeploymentStatusClient(connection); + var expectedUrl = "repositories/1/deployments/1/statuses"; + + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + await client.GetAll(1, 1, options); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == expectedUrl), options); + } + [Fact] public async Task EnsuresNonNullArguments() { @@ -21,16 +83,11 @@ public class DeploymentStatusClientTests await Assert.ThrowsAsync(() => client.GetAll(null, "name", 1, ApiOptions.None)); await Assert.ThrowsAsync(() => client.GetAll("owner", null, 1, ApiOptions.None)); await Assert.ThrowsAsync(() => client.GetAll("owner", "name", 1, null)); - } - [Fact] - public async Task EnsuresNonEmptyArguments() - { - var client = new DeploymentStatusClient(Substitute.For()); + await Assert.ThrowsAsync(() => client.GetAll(1, 1, null)); await Assert.ThrowsAsync(() => client.GetAll("", "name", 1)); await Assert.ThrowsAsync(() => client.GetAll("owner", "", 1)); - await Assert.ThrowsAsync(() => client.GetAll("", "name", 1, ApiOptions.None)); await Assert.ThrowsAsync(() => client.GetAll("owner", "", 1, ApiOptions.None)); } @@ -51,61 +108,38 @@ public class DeploymentStatusClientTests await Assert.ThrowsAsync(() => client.GetAll(whitespace, "name", 1, ApiOptions.None)); await Assert.ThrowsAsync(() => client.GetAll("owner", whitespace, 1, ApiOptions.None)); } - - [Fact] - public void RequestsCorrectUrl() - { - var connection = Substitute.For(); - var client = new DeploymentStatusClient(connection); - var expectedUrl = "repos/owner/name/deployments/1/statuses"; - - client.GetAll("owner", "name", 1); - connection.Received().GetAll(Arg.Is(u => u.ToString() == expectedUrl), - Arg.Any>(), - Arg.Any(), - Args.ApiOptions); - } - - [Fact] - public void RequestsCorrectUrlWithApiOptions() - { - var connection = Substitute.For(); - var client = new DeploymentStatusClient(connection); - var expectedUrl = "repos/owner/name/deployments/1/statuses"; - - var options = new ApiOptions - { - StartPage = 1, - PageCount = 1, - PageSize = 1 - }; - - client.GetAll("owner", "name", 1, options); - connection.Received().GetAll(Arg.Is(u => u.ToString() == expectedUrl), - Arg.Any>(), - Arg.Any(), - options); - } - - [Fact] - public void RequestsCorrectUrlWithPreviewAcceptHeaders() - { - var connection = Substitute.For(); - var client = new DeploymentStatusClient(connection); - var expectedUrl = "repos/owner/name/deployments/1/statuses"; - - client.GetAll("owner", "name", 1); - connection.Received().GetAll(Arg.Is(u => u.ToString() == expectedUrl), - Arg.Any>(), - Arg.Is(a => a == AcceptHeaders.DeploymentApiPreview), - Args.ApiOptions); - } } public class TheCreateMethod { readonly NewDeploymentStatus newDeploymentStatus = new NewDeploymentStatus(DeploymentState.Success); + [Fact] + public void PostsToCorrectUrl() + { + var connection = Substitute.For(); + var client = new DeploymentStatusClient(connection); + var expectedUrl = "repos/owner/repo/deployments/1/statuses"; + + client.Create("owner", "repo", 1, newDeploymentStatus); + + connection.Received().Post(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Any()); + } + + [Fact] + public void PostsToCorrectUrlWithRepositoryId() + { + var connection = Substitute.For(); + var client = new DeploymentStatusClient(connection); + var expectedUrl = "repositories/1/deployments/1/statuses"; + + client.Create(1, 1, newDeploymentStatus); + + connection.Received().Post(Arg.Is(u => u.ToString() == expectedUrl), + Arg.Any()); + } + [Fact] public async Task EnsuresNonNullArguments() { @@ -113,15 +147,12 @@ public class DeploymentStatusClientTests await Assert.ThrowsAsync(() => client.Create(null, "name", 1, newDeploymentStatus)); await Assert.ThrowsAsync(() => client.Create("owner", null, 1, newDeploymentStatus)); - } + await Assert.ThrowsAsync(() => client.Create("owner", "name", 1, null)); - [Fact] - public async Task EnsuresNonEmptyArguments() - { - var client = new DeploymentStatusClient(Substitute.For()); + await Assert.ThrowsAsync(() => client.Create(1, 1, null)); - await Assert.ThrowsAsync(() => client.GetAll("", "name", 1)); - await Assert.ThrowsAsync(() => client.GetAll("owner", "", 1)); + await Assert.ThrowsAsync(() => client.Create("", "name", 1, newDeploymentStatus)); + await Assert.ThrowsAsync(() => client.Create("owner", "", 1, newDeploymentStatus)); } [Theory] @@ -189,4 +220,4 @@ public class DeploymentStatusClientTests Assert.Throws(() => new DeploymentStatusClient(null)); } } -} \ No newline at end of file +} diff --git a/Octokit.Tests/Reactive/ObservableDeploymentStatusClientTests.cs b/Octokit.Tests/Reactive/ObservableDeploymentStatusClientTests.cs index 21567fba..6fd20001 100644 --- a/Octokit.Tests/Reactive/ObservableDeploymentStatusClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableDeploymentStatusClientTests.cs @@ -22,23 +22,85 @@ namespace Octokit.Tests.Reactive _client = new ObservableDeploymentStatusClient(_githubClient); } + [Fact] + public void RequestsCorrectUrl() + { + var expectedUri = string.Format("repos/{0}/{1}/deployments/{2}/statuses", "owner", "repo", 1); + + _client.GetAll("owner", "repo", 1); + + _githubClient.Connection.Received(1) + .Get>(Arg.Is(uri => uri.ToString() == expectedUri), + Args.EmptyDictionary, + null); + } + + [Fact] + public void RequestsCorrectUrlWithRepositoryId() + { + var expectedUri = string.Format("repositories/{0}/deployments/{1}/statuses", 1, 1); + + _client.GetAll(1, 1); + + _githubClient.Connection.Received(1) + .Get>(Arg.Is(uri => uri.ToString() == expectedUri), + Args.EmptyDictionary, + null); + } + + [Fact] + public void RequestsCorrectUrlWithApiOptions() + { + var expectedUri = string.Format("repos/{0}/{1}/deployments/{2}/statuses", "owner", "repo", 1); + + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + _client.GetAll("owner", "repo", 1, options); + + _githubClient.Connection.Received(1) + .Get>(Arg.Is(uri => uri.ToString() == expectedUri), + Arg.Is>(dictionary => dictionary.Count == 2), + null); + } + + [Fact] + public void RequestsCorrectUrlWithRepositoryIdWithApiOptions() + { + var expectedUri = string.Format("repositories/{0}/deployments/{1}/statuses", 1, 1); + + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + _client.GetAll(1, 1, options); + + _githubClient.Connection.Received(1) + .Get>(Arg.Is(uri => uri.ToString() == expectedUri), + Arg.Is>(dictionary => dictionary.Count == 2), + null); + } + [Fact] public void EnsuresNonNullArguments() { Assert.Throws(() => _client.GetAll(null, "repo", 1)); Assert.Throws(() => _client.GetAll("owner", null, 1)); - Assert.Throws(() => _client.GetAll(null, "repo", 1, ApiOptions.None)); Assert.Throws(() => _client.GetAll("owner", null, 1, ApiOptions.None)); Assert.Throws(() => _client.GetAll("owner", "repo", 1, null)); - } - [Fact] - public void EnsuresNonEmptyArguments() - { + Assert.Throws(() => _client.GetAll(1, 1, null)); + Assert.Throws(() => _client.GetAll("", "repo", 1)); Assert.Throws(() => _client.GetAll("owner", "", 1)); - Assert.Throws(() => _client.GetAll("", "repo", 1, ApiOptions.None)); Assert.Throws(() => _client.GetAll("owner", "", 1, ApiOptions.None)); } @@ -56,39 +118,6 @@ namespace Octokit.Tests.Reactive await AssertEx.ThrowsWhenGivenWhitespaceArgument( async whitespace => await _client.GetAll("owner", whitespace, 1, ApiOptions.None)); } - - [Fact] - public void RequestsCorrectUrl() - { - var expectedUri = ApiUrls.DeploymentStatuses("owner", "repo", 1); - - _client.GetAll("owner", "repo", 1); - - _githubClient.Connection.Received(1) - .Get>(Arg.Is(expectedUri), - Args.EmptyDictionary, - null); - } - - [Fact] - public void RequestsCorrectUrlWitApiOptions() - { - var expectedUri = ApiUrls.DeploymentStatuses("owner", "repo", 1); - - var options = new ApiOptions - { - StartPage = 1, - PageCount = 1, - PageSize = 1 - }; - - _client.GetAll("owner", "repo", 1, options); - - _githubClient.Connection.Received(1) - .Get>(Arg.Is(expectedUri), - Arg.Is>(dictionary => dictionary.Count == 2), - null); - } } public class TheCreateMethod @@ -109,18 +138,42 @@ namespace Octokit.Tests.Reactive } [Fact] - public async Task EnsuresNonNullArguments() + public void CallsIntoDeploymentStatusClient() { - SetupWithNonReactiveClient(); - Assert.Throws(() => _client.Create(null, "repo", 1, new NewDeploymentStatus(DeploymentState.Success))); - Assert.Throws(() => _client.Create("owner", null, 1, new NewDeploymentStatus(DeploymentState.Success))); - Assert.Throws(() => _client.Create("owner", "repo", 1, null)); + SetupWithoutNonReactiveClient(); + + var newStatus = new NewDeploymentStatus(DeploymentState.Success); + + _client.Create("owner", "repo", 1, newStatus); + + _githubClient.Repository.Deployment.Status.Received(1) + .Create("owner", "repo", 1, newStatus); } [Fact] - public async Task EnsuresNonEmptyArguments() + public void CallsIntoDeploymentStatusClientWithRepositoryId() + { + SetupWithoutNonReactiveClient(); + + var newStatus = new NewDeploymentStatus(DeploymentState.Success); + + _client.Create(1, 1, newStatus); + + _githubClient.Repository.Deployment.Status.Received(1) + .Create(1, 1, newStatus); + } + + [Fact] + public async Task EnsuresNonNullArguments() { SetupWithNonReactiveClient(); + + Assert.Throws(() => _client.Create(null, "repo", 1, new NewDeploymentStatus(DeploymentState.Success))); + Assert.Throws(() => _client.Create("owner", null, 1, new NewDeploymentStatus(DeploymentState.Success))); + Assert.Throws(() => _client.Create("owner", "repo", 1, null)); + + Assert.Throws(() => _client.Create(1, 1, null)); + Assert.Throws(() => _client.Create("", "repo", 1, new NewDeploymentStatus(DeploymentState.Success))); Assert.Throws(() => _client.Create("owner", "", 1, new NewDeploymentStatus(DeploymentState.Success))); } @@ -129,25 +182,12 @@ namespace Octokit.Tests.Reactive public async Task EnsureNonWhitespaceArguments() { SetupWithNonReactiveClient(); + await AssertEx.ThrowsWhenGivenWhitespaceArgument( async whitespace => await _client.Create(whitespace, "repo", 1, new NewDeploymentStatus(DeploymentState.Success))); await AssertEx.ThrowsWhenGivenWhitespaceArgument( async whitespace => await _client.Create("owner", whitespace, 1, new NewDeploymentStatus(DeploymentState.Success))); } - - [Fact] - public void CallsIntoDeploymentStatusClient() - { - SetupWithoutNonReactiveClient(); - - var newStatus = new NewDeploymentStatus(DeploymentState.Success); - _client.Create("owner", "repo", 1, newStatus); - _githubClient.Repository.Deployment.Status.Received(1) - .Create(Arg.Is("owner"), - Arg.Is("repo"), - Arg.Is(1), - Arg.Is(newStatus)); - } } public class TheCtor