using System; using System.Collections.Generic; using System.Reactive.Linq; using System.Threading.Tasks; using NSubstitute; using Octokit.Internal; using Octokit.Reactive; using Xunit; namespace Octokit.Tests.Reactive { public class ObservableRepositoriesClientTests { public class TheGetMethod { // This isn't really a test specific to this method. This is just as good a place as any to test // that our API methods returns the right kind of observables. [Fact] public async Task IsALukeWarmObservable() { var repository = new Repository(); var response = Task.Factory.StartNew>(() => new ApiResponse(new Response(), repository)); var connection = Substitute.For(); connection.Get(Args.Uri, null, null).Returns(response); var gitHubClient = new GitHubClient(connection); var client = new ObservableRepositoriesClient(gitHubClient); var observable = client.Get("stark", "ned"); connection.Received(1).Get(Args.Uri, null, null); var result = await observable; connection.Received(1).Get(Args.Uri, null, null); var result2 = await observable; // TODO: If we change this to a warm observable, we'll need to change this to Received(2) connection.Received(1).Get(Args.Uri, null, null); Assert.Same(repository, result); Assert.Same(repository, result2); } } public class TheGetAllForCurrentMethod { [Fact] public async Task ReturnsEveryPageOfRepositories() { var firstPageUrl = new Uri("user/repos", UriKind.Relative); var secondPageUrl = new Uri("https://example.com/page/2"); var firstPageLinks = new Dictionary {{"next", secondPageUrl}}; var firstPageResponse = new ApiResponse>( CreateResponseWithApiInfo(firstPageLinks), new List { new Repository(1), new Repository(2), new Repository(3) }); var thirdPageUrl = new Uri("https://example.com/page/3"); var secondPageLinks = new Dictionary {{"next", thirdPageUrl}}; var secondPageResponse = new ApiResponse> ( CreateResponseWithApiInfo(secondPageLinks), new List { new Repository(4), new Repository(5), new Repository(6) }); var lastPageResponse = new ApiResponse>( new Response(), new List { new Repository(7) }); var gitHubClient = Substitute.For(); gitHubClient.Connection.GetResponse>(firstPageUrl) .Returns(Task.Factory.StartNew>>(() => firstPageResponse)); gitHubClient.Connection.GetResponse>(secondPageUrl) .Returns(Task.Factory.StartNew>>(() => secondPageResponse)); gitHubClient.Connection.GetResponse>(thirdPageUrl) .Returns(Task.Factory.StartNew>>(() => lastPageResponse)); var repositoriesClient = new ObservableRepositoriesClient(gitHubClient); var results = await repositoriesClient.GetAllForCurrent().ToArray(); Assert.Equal(7, results.Length); gitHubClient.Connection.Received(1).Get>(firstPageUrl, null, null); gitHubClient.Connection.Received(1).Get>(secondPageUrl, null, null); gitHubClient.Connection.Received(1).Get>(thirdPageUrl, null, null); } [Fact] public async Task StopsMakingNewRequestsWhenTakeIsFulfilled() { var firstPageUrl = new Uri("user/repos", UriKind.Relative); var secondPageUrl = new Uri("https://example.com/page/2"); var firstPageLinks = new Dictionary { { "next", secondPageUrl } }; var firstPageResponse = new ApiResponse> ( CreateResponseWithApiInfo(firstPageLinks), new List { new Repository(1), new Repository(2), new Repository(3) } ); var thirdPageUrl = new Uri("https://example.com/page/3"); var secondPageLinks = new Dictionary { { "next", thirdPageUrl } }; var secondPageResponse = new ApiResponse> ( CreateResponseWithApiInfo(secondPageLinks), new List { new Repository(4), new Repository(5), new Repository(6) } ); var fourthPageUrl = new Uri("https://example.com/page/4"); var thirdPageLinks = new Dictionary { { "next", fourthPageUrl } }; var thirdPageResponse = new ApiResponse> ( CreateResponseWithApiInfo(thirdPageLinks), new List { new Repository(7) } ); var lastPageResponse = new ApiResponse> ( new Response(), new List { new Repository(8) } ); var gitHubClient = Substitute.For(); gitHubClient.Connection.GetResponse>(firstPageUrl) .Returns(Task.Factory.StartNew>>(() => firstPageResponse)); gitHubClient.Connection.GetResponse>(secondPageUrl) .Returns(Task.Factory.StartNew>>(() => secondPageResponse)); gitHubClient.Connection.GetResponse>(thirdPageUrl) .Returns(Task.Factory.StartNew>>(() => thirdPageResponse)); gitHubClient.Connection.GetResponse>(fourthPageUrl) .Returns(Task.Factory.StartNew>>(() => lastPageResponse)); var repositoriesClient = new ObservableRepositoriesClient(gitHubClient); var results = await repositoriesClient.GetAllForCurrent().Take(4).ToArray(); Assert.Equal(4, results.Length); gitHubClient.Connection.Received(1).Get>(firstPageUrl, null, null); gitHubClient.Connection.Received(1).Get>(secondPageUrl, null, null); gitHubClient.Connection.Received(0).Get>(thirdPageUrl, null, null); gitHubClient.Connection.Received(0).Get>(fourthPageUrl, null, null); } } public class TheGetAllPublicRepositoriesSinceMethod { [Fact] public async Task ReturnsEveryPageOfRepositories() { var firstPageUrl = new Uri("/repositories?since=364", UriKind.Relative); var secondPageUrl = new Uri("https://example.com/page/2"); var firstPageLinks = new Dictionary { { "next", secondPageUrl } }; IApiResponse> firstPageResponse = new ApiResponse>( CreateResponseWithApiInfo(firstPageLinks), new List { new Repository(364), new Repository(365), new Repository(366) }); var thirdPageUrl = new Uri("https://example.com/page/3"); var secondPageLinks = new Dictionary { { "next", thirdPageUrl } }; IApiResponse> secondPageResponse = new ApiResponse> ( CreateResponseWithApiInfo(secondPageLinks), new List { new Repository(367), new Repository(368), new Repository(369) }); IApiResponse> lastPageResponse = new ApiResponse>( new Response(), new List { new Repository(370) }); var gitHubClient = Substitute.For(); gitHubClient.Connection.Get>(firstPageUrl, null, null) .Returns(Task.FromResult(firstPageResponse)); gitHubClient.Connection.Get>(secondPageUrl, null, null) .Returns(Task.FromResult(secondPageResponse)); gitHubClient.Connection.Get>(thirdPageUrl, null, null) .Returns(Task.FromResult(lastPageResponse)); var repositoriesClient = new ObservableRepositoriesClient(gitHubClient); var results = await repositoriesClient.GetAllPublic(new PublicRepositoryRequest(364)).ToArray(); Assert.Equal(7, results.Length); gitHubClient.Connection.Received(1).Get>(firstPageUrl, null, null); gitHubClient.Connection.Received(1).Get>(secondPageUrl, null, null); gitHubClient.Connection.Received(1).Get>(thirdPageUrl, null, null); } } public class TheGetAllBranchesMethod { [Fact] public void EnsuresArguments() { var client = new ObservableRepositoriesClient(Substitute.For()); Assert.Throws(() => client.GetAllBranches(null, "repo")); Assert.Throws(() => client.GetAllBranches("owner", null)); Assert.Throws(() => client.GetAllBranches("", "repo")); Assert.Throws(() => client.GetAllBranches("owner", "")); } [Fact] public void GetsCorrectUrl() { var github = Substitute.For(); var client = new ObservableRepositoriesClient(github); var expected = new Uri("repos/owner/repo/branches", UriKind.Relative); client.GetAllBranches("owner", "repo"); github.Connection.Received(1).GetResponse>(expected); } } public class TheGetCommitMethod { [Fact] public void EnsuresArguments() { var client = new ObservableRepositoriesClient(Substitute.For()); Assert.Throws(() => client.Commits.Get(null, "repo", "reference")); Assert.Throws(() => client.Commits.Get("owner", null, "reference")); Assert.Throws(() => client.Commits.Get("owner", "repo", null)); Assert.Throws(() => client.Commits.Get("", "repo", "reference")); Assert.Throws(() => client.Commits.Get("owner", "", "reference")); Assert.Throws(() => client.Commits.Get("owner", "repo", "")); } [Fact] public void CallsCorrectApi() { var github = Substitute.For(); var client = new ObservableRepositoriesClient(github); client.Commits.Get("owner", "repo", "reference"); github.Repository.Commits.Received(1).Get("owner", "repo", "reference"); } } public class TheGetAllCommitsMethod { [Fact] public void EnsuresArguments() { var client = new ObservableRepositoriesClient(Substitute.For()); Assert.Throws(() => client.Commits.GetAll(null, "repo")); Assert.Throws(() => client.Commits.GetAll("owner", null)); Assert.Throws(() => client.Commits.GetAll("owner", "repo", null)); Assert.Throws(() => client.Commits.GetAll("", "repo")); Assert.Throws(() => client.Commits.GetAll("owner", "")); } [Fact] public void GetsCorrectUrl() { var github = Substitute.For(); var client = new ObservableRepositoriesClient(github); var expected = new Uri("repos/owner/repo/commits", UriKind.Relative); client.Commits.GetAll("owner", "repo"); github.Connection.Received(1).Get>(expected, Arg.Any>(), null); } } public class TheGetAllContributorsMethod { [Fact] public void EnsuresArguments() { var client = new ObservableRepositoriesClient(Substitute.For()); Assert.Throws(() => client.GetAllContributors(null, "repo")); Assert.Throws(() => client.GetAllContributors("owner", null)); Assert.Throws(() => client.GetAllContributors("", "repo")); Assert.Throws(() => client.GetAllContributors("owner", "")); } [Fact] public void GetsCorrectUrl() { var github = Substitute.For(); var client = new ObservableRepositoriesClient(github); var expected = new Uri("repos/owner/repo/contributors", UriKind.Relative); client.GetAllContributors("owner", "repo"); github.Connection.Received(1) .Get>(expected, Arg.Any>(), Arg.Any()); } // TODO: Needs test for 'includeAnonymous' } public class TheGetAllLanguagesMethod { [Fact] public void EnsuresNonNullArguments() { var client = new ObservableRepositoriesClient(Substitute.For()); Assert.Throws(() => client.GetAllLanguages(null, "repo")); Assert.Throws(() => client.GetAllLanguages("owner", null)); Assert.Throws(() => client.GetAllLanguages("", "repo")); Assert.Throws(() => client.GetAllLanguages("owner", "")); } [Fact] public void GetsCorrectUrl() { var github = Substitute.For(); var client = new ObservableRepositoriesClient(github); var expected = new Uri("repos/owner/repo/languages", UriKind.Relative); client.GetAllLanguages("owner", "repo"); github.Connection.Received(1).GetResponse>>(expected); } } public class TheGetAllTeamsMethod { [Fact] public void EnsuresArguments() { var client = new ObservableRepositoriesClient(Substitute.For()); Assert.Throws(() => client.GetAllTeams(null, "repo")); Assert.Throws(() => client.GetAllTeams("owner", null)); Assert.Throws(() => client.GetAllTeams("", "repo")); Assert.Throws(() => client.GetAllTeams("owner", "")); } [Fact] public void GetsCorrectUrl() { var github = Substitute.For(); var client = new ObservableRepositoriesClient(github); var expected = new Uri("repos/owner/repo/teams", UriKind.Relative); client.GetAllTeams("owner", "repo"); github.Connection.Received(1).GetResponse>(expected); } } public class TheGetAllTagsMethod { [Fact] public void EnsuresArguments() { var client = new ObservableRepositoriesClient(Substitute.For()); Assert.Throws(() => client.GetAllTags(null, "repo")); Assert.Throws(() => client.GetAllTags("owner", null)); Assert.Throws(() => client.GetAllTags("", "repo")); Assert.Throws(() => client.GetAllTags("owner", "")); } [Fact] public void GetsCorrectUrl() { var github = Substitute.For(); var client = new ObservableRepositoriesClient(github); var expected = new Uri("repos/owner/repo/tags", UriKind.Relative); client.GetAllTags("owner", "repo"); github.Connection.Received(1).GetResponse>(expected); } } public class TheGetBranchMethod { [Fact] public async Task EnsuresArguments() { var github = Substitute.For(); var nonreactiveClient = new RepositoriesClient(Substitute.For()); github.Repository.Returns(nonreactiveClient); var client = new ObservableRepositoriesClient(github); Assert.Throws(() => client.GetBranch(null, "repo", "branch")); Assert.Throws(() => client.GetBranch("owner", null, "branch")); Assert.Throws(() => client.GetBranch("owner", "repo", null)); Assert.Throws(() => client.GetBranch("", "repo", "branch")); Assert.Throws(() => client.GetBranch("owner", "", "branch")); Assert.Throws(() => client.GetBranch("owner", "repo", "")); } [Fact] public void CallsIntoClient() { var github = Substitute.For(); var client = new ObservableRepositoriesClient(github); client.GetBranch("owner", "repo", "branch"); github.Repository.Received(1).GetBranch("owner", "repo", "branch"); } } public class TheEditMethod { [Fact] public async Task EnsuresArguments() { var github = Substitute.For(); var nonreactiveClient = new RepositoriesClient(Substitute.For()); github.Repository.Returns(nonreactiveClient); var client = new ObservableRepositoriesClient(github); var update = new RepositoryUpdate(); Assert.Throws(() => client.Edit(null, "repo", update)); Assert.Throws(() => client.Edit("owner", null, update)); Assert.Throws(() => client.Edit("owner", "repo", null)); Assert.Throws(() => client.Edit("", "repo", update)); Assert.Throws(() => client.Edit("owner", "", update)); } [Fact] public void CallsIntoClient() { var github = Substitute.For(); var client = new ObservableRepositoriesClient(github); var update = new RepositoryUpdate(); client.Edit("owner", "repo", update); github.Repository.Received(1).Edit("owner", "repo", update); } } static IResponse CreateResponseWithApiInfo(IDictionary links) { var response = Substitute.For(); response.ApiInfo.Returns(new ApiInfo(links, new List(), new List(), "etag", new RateLimit(new Dictionary()))); return response; } } }