From f3b3a96dfd48889487e00b4dc8068ac45a982849 Mon Sep 17 00:00:00 2001 From: "aedampir@gmail.com" Date: Wed, 25 May 2016 22:31:58 +0700 Subject: [PATCH] added unit tests --- Octokit.Tests/Clients/WatchedClientTests.cs | 105 +++++++++- .../Reactive/ObservableWatchedClientTests.cs | 194 ++++++++++++------ 2 files changed, 229 insertions(+), 70 deletions(-) diff --git a/Octokit.Tests/Clients/WatchedClientTests.cs b/Octokit.Tests/Clients/WatchedClientTests.cs index 27ad95df..becf2259 100644 --- a/Octokit.Tests/Clients/WatchedClientTests.cs +++ b/Octokit.Tests/Clients/WatchedClientTests.cs @@ -23,15 +23,42 @@ namespace Octokit.Tests.Clients public class TheGetAllForCurrentMethod { [Fact] - public void RequestsCorrectUrl() + public async Task RequestsCorrectUrl() { var endpoint = new Uri("user/subscriptions", UriKind.Relative); var connection = Substitute.For(); var client = new WatchedClient(connection); - client.GetAllForCurrent(); + await client.GetAllForCurrent(); - connection.Received().GetAll(endpoint, Arg.Any()); + connection.Received().GetAll(endpoint, Args.ApiOptions); + } + + [Fact] + public async Task RequestsCorrectUrlWithApiOptions() + { + var endpoint = new Uri("user/subscriptions", UriKind.Relative); + var connection = Substitute.For(); + var client = new WatchedClient(connection); + + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + await client.GetAllForCurrent(options); + + connection.Received().GetAll(endpoint, options); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new WatchedClient(Substitute.For()); + + await Assert.ThrowsAsync(() => client.GetAllForCurrent(null)); } } @@ -46,7 +73,39 @@ namespace Octokit.Tests.Clients client.GetAllForUser("banana"); - connection.Received().GetAll(endpoint, Arg.Any()); + connection.Received().GetAll(endpoint, Args.ApiOptions); + } + + [Fact] + public void RequestsCorrectUrlWithApiOptions() + { + var endpoint = new Uri("users/banana/subscriptions", UriKind.Relative); + var connection = Substitute.For(); + var client = new WatchedClient(connection); + + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + client.GetAllForUser("banana", options); + + connection.Received().GetAll(endpoint, options); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new WatchedClient(Substitute.For()); + + await Assert.ThrowsAsync(() => client.GetAllForUser(null)); + await Assert.ThrowsAsync(() => client.GetAllForUser(null, ApiOptions.None)); + await Assert.ThrowsAsync(() => client.GetAllForUser("user", null)); + + await Assert.ThrowsAsync(() => client.GetAllForUser("")); + await Assert.ThrowsAsync(() => client.GetAllForUser("", ApiOptions.None)); } } @@ -61,7 +120,43 @@ namespace Octokit.Tests.Clients client.GetAllWatchers("fight", "club"); - connection.Received().GetAll(endpoint, Arg.Any()); + connection.Received().GetAll(endpoint, Args.ApiOptions); + } + + [Fact] + public void RequestsCorrectUrlWithApiOptions() + { + var endpoint = new Uri("repos/fight/club/subscribers", UriKind.Relative); + var connection = Substitute.For(); + var client = new WatchedClient(connection); + + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + client.GetAllWatchers("fight", "club", options); + + connection.Received().GetAll(endpoint, options); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new WatchedClient(Substitute.For()); + + await Assert.ThrowsAsync(() => client.GetAllWatchers(null, "name")); + await Assert.ThrowsAsync(() => client.GetAllWatchers("owner", null)); + await Assert.ThrowsAsync(() => client.GetAllWatchers(null, "name", ApiOptions.None)); + await Assert.ThrowsAsync(() => client.GetAllWatchers("owner", null, ApiOptions.None)); + await Assert.ThrowsAsync(() => client.GetAllWatchers("owner", "name", null)); + + await Assert.ThrowsAsync(() => client.GetAllWatchers("", "name")); + await Assert.ThrowsAsync(() => client.GetAllWatchers("owner", "")); + await Assert.ThrowsAsync(() => client.GetAllWatchers("", "name", ApiOptions.None)); + await Assert.ThrowsAsync(() => client.GetAllWatchers("owner", "", ApiOptions.None)); } } diff --git a/Octokit.Tests/Reactive/ObservableWatchedClientTests.cs b/Octokit.Tests/Reactive/ObservableWatchedClientTests.cs index b43ec121..ae05ce33 100644 --- a/Octokit.Tests/Reactive/ObservableWatchedClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableWatchedClientTests.cs @@ -1,10 +1,7 @@ using System; using System.Collections.Generic; -using System.Reactive.Threading.Tasks; -using System.Threading.Tasks; using NSubstitute; using Octokit.Reactive; -using Octokit.Reactive.Internal; using Xunit; namespace Octokit.Tests.Reactive @@ -20,43 +17,10 @@ namespace Octokit.Tests.Reactive } } - public class TheGetAllWatchersMethod - { - [Fact] - public async Task EnsuresArguments() - { - var client = new ObservableWatchedClient(Substitute.For()); - - await Assert.ThrowsAsync(() => client.GetAllWatchers(null, "name").ToTask()); - await Assert.ThrowsAsync(() => client.GetAllWatchers("owner", null).ToTask()); - await Assert.ThrowsAsync(() => client.GetAllWatchers("owner", "name", null).ToTask()); - } - - [Fact] - public void GetsWatchersFromClient() - { - var connection = Substitute.For(); - var gitHubClient = Substitute.For(); - gitHubClient.Connection.Returns(connection); - var client = new ObservableWatchedClient(gitHubClient); - - client.GetAllWatchers("jugglingnutcase", "katiejamie"); - connection.Received().Get>(ApiUrls.Watchers("jugglingnutcase", "katiejamie"), Arg.Any>(), null); - } - } - public class TheGetAllForCurrentMethod { [Fact] - public async Task EnsuresArguments() - { - var client = new ObservableWatchedClient(Substitute.For()); - - await Assert.ThrowsAsync(() => client.GetAllForCurrent(null).ToTask()); - } - - [Fact] - public void GetsStarsForCurrent() + public void RequestsCorrectUrl() { var connection = Substitute.For(); var gitHubClient = Substitute.For(); @@ -64,24 +28,41 @@ namespace Octokit.Tests.Reactive var client = new ObservableWatchedClient(gitHubClient); client.GetAllForCurrent(); - connection.Received().Get>(ApiUrls.Watched(), Arg.Any>(), null); + connection.Received().Get>(ApiUrls.Watched(), Args.EmptyDictionary, null); + } + + [Fact] + public void RequestsCorrectUrlWithApiOptions() + { + var connection = Substitute.For(); + var gitHubClient = Substitute.For(); + gitHubClient.Connection.Returns(connection); + var client = new ObservableWatchedClient(gitHubClient); + + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + client.GetAllForCurrent(options); + connection.Received().Get>(ApiUrls.Watched(), Arg.Is>(d => d.Count == 2), null); + } + + [Fact] + public void EnsuresNonNullArguments() + { + var client = new ObservableWatchedClient(Substitute.For()); + + Assert.Throws(() => client.GetAllForCurrent(null)); } } public class TheGetAllForUserMethod { [Fact] - public async Task EnsuresArguments() - { - var client = new ObservableWatchedClient(Substitute.For()); - - await Assert.ThrowsAsync(() => client.GetAllForUser(null).ToTask()); - await Assert.ThrowsAsync(() => client.GetAllForUser("").ToTask()); - await Assert.ThrowsAsync(() => client.GetAllForUser("user", null).ToTask()); - } - - [Fact] - public void GetsStarsForUser() + public void RequestsCorrectUrl() { var connection = Substitute.For(); var gitHubClient = Substitute.For(); @@ -89,21 +70,104 @@ namespace Octokit.Tests.Reactive var client = new ObservableWatchedClient(gitHubClient); client.GetAllForUser("jugglingnutcase"); - connection.Received().Get>(ApiUrls.WatchedByUser("jugglingnutcase"), Arg.Any>(), null); + connection.Received().Get>(ApiUrls.WatchedByUser("jugglingnutcase"), Args.EmptyDictionary, null); + } + + [Fact] + public void RequestsCorrectUrlWithApiOptions() + { + var connection = Substitute.For(); + var gitHubClient = Substitute.For(); + gitHubClient.Connection.Returns(connection); + var client = new ObservableWatchedClient(gitHubClient); + + var options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + client.GetAllForUser("jugglingnutcase", options); + connection.Received().Get>(ApiUrls.WatchedByUser("jugglingnutcase"), Arg.Is>(d => d.Count == 2), null); + } + + [Fact] + public void EnsuresNonNullArguments() + { + var client = new ObservableWatchedClient(Substitute.For()); + + Assert.Throws(() => client.GetAllForUser(null)); + Assert.Throws(() => client.GetAllForUser(null, ApiOptions.None)); + Assert.Throws(() => client.GetAllForUser("user", null)); + + Assert.Throws(() => client.GetAllForUser("")); + Assert.Throws(() => client.GetAllForUser("", ApiOptions.None)); + } + } + + public class TheGetAllWatchersMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var gitHubClient = Substitute.For(); + gitHubClient.Connection.Returns(connection); + var client = new ObservableWatchedClient(gitHubClient); + + client.GetAllWatchers("jugglingnutcase", "katiejamie"); + connection.Received().Get>(ApiUrls.Watchers("jugglingnutcase", "katiejamie"), Args.EmptyDictionary, null); + } + + [Fact] + public void RequestsCorrectUrlWithApiOptions() + { + var connection = Substitute.For(); + var gitHubClient = Substitute.For(); + gitHubClient.Connection.Returns(connection); + var client = new ObservableWatchedClient(gitHubClient); + + ApiOptions options = new ApiOptions + { + StartPage = 1, + PageCount = 1, + PageSize = 1 + }; + + client.GetAllWatchers("jugglingnutcase", "katiejamie", options); + connection.Received().Get>(ApiUrls.Watchers("jugglingnutcase", "katiejamie"), Arg.Is>(d => d.Count == 2), null); + } + + [Fact] + public void EnsuresNonNullArguments() + { + var client = new ObservableWatchedClient(Substitute.For()); + + Assert.Throws(() => client.GetAllWatchers(null, "name")); + Assert.Throws(() => client.GetAllWatchers("owner", null)); + Assert.Throws(() => client.GetAllWatchers(null, "name", ApiOptions.None)); + Assert.Throws(() => client.GetAllWatchers("owner", null, ApiOptions.None)); + Assert.Throws(() => client.GetAllWatchers("owner", "name", null)); + + Assert.Throws(() => client.GetAllWatchers("", "name")); + Assert.Throws(() => client.GetAllWatchers("owner", "")); + Assert.Throws(() => client.GetAllWatchers("", "name", ApiOptions.None)); + Assert.Throws(() => client.GetAllWatchers("owner", "", ApiOptions.None)); } } public class TheCheckWatchedMethod { [Fact] - public async Task EnsureArguments() + public void EnsuresNonNullArguments() { var client = new ObservableWatchedClient(Substitute.For()); - await Assert.ThrowsAsync(() => client.CheckWatched(null, "name").ToTask()); - await Assert.ThrowsAsync(() => client.CheckWatched("owner", null).ToTask()); - await Assert.ThrowsAsync(() => client.CheckWatched("", "name").ToTask()); - await Assert.ThrowsAsync(() => client.CheckWatched("owner", "").ToTask()); + Assert.Throws(() => client.CheckWatched(null, "name")); + Assert.Throws(() => client.CheckWatched("owner", null)); + Assert.Throws(() => client.CheckWatched("", "name")); + Assert.Throws(() => client.CheckWatched("owner", "")); } [Fact] @@ -121,15 +185,15 @@ namespace Octokit.Tests.Reactive public class TheWatchRepoMethod { [Fact] - public async Task EnsureArguments() + public void EnsuresNonNullArguments() { var client = new ObservableWatchedClient(Substitute.For()); var subscription = new NewSubscription(); - await Assert.ThrowsAsync(() => client.WatchRepo(null, "name", subscription).ToTask()); - await Assert.ThrowsAsync(() => client.WatchRepo("owner", null, subscription).ToTask()); - await Assert.ThrowsAsync(() => client.WatchRepo("", "name", subscription).ToTask()); - await Assert.ThrowsAsync(() => client.WatchRepo("owner", "", subscription).ToTask()); + Assert.Throws(() => client.WatchRepo(null, "name", subscription)); + Assert.Throws(() => client.WatchRepo("owner", null, subscription)); + Assert.Throws(() => client.WatchRepo("", "name", subscription)); + Assert.Throws(() => client.WatchRepo("owner", "", subscription)); } [Fact] @@ -147,14 +211,14 @@ namespace Octokit.Tests.Reactive public class TheUnWatchRepoMethod { [Fact] - public async Task EnsureArguments() + public void EnsuresNonNullArguments() { var client = new ObservableWatchedClient(Substitute.For()); - await Assert.ThrowsAsync(() => client.UnwatchRepo(null, "name").ToTask()); - await Assert.ThrowsAsync(() => client.UnwatchRepo("owner", null).ToTask()); - await Assert.ThrowsAsync(() => client.UnwatchRepo("", "name").ToTask()); - await Assert.ThrowsAsync(() => client.UnwatchRepo("owner", "").ToTask()); + Assert.Throws(() => client.UnwatchRepo(null, "name")); + Assert.Throws(() => client.UnwatchRepo("owner", null)); + Assert.Throws(() => client.UnwatchRepo("", "name")); + Assert.Throws(() => client.UnwatchRepo("owner", "")); } [Fact]