From 1bfa625f085b01e8dc85f058fae5bd0f7b5cf38a Mon Sep 17 00:00:00 2001 From: Ryan Gribble Date: Mon, 8 Feb 2016 22:46:47 +1000 Subject: [PATCH] Add unit tests for CreateFile DeleteFile and UpdateFile --- .../Clients/RepositoryContentsClientTests.cs | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/Octokit.Tests/Clients/RepositoryContentsClientTests.cs b/Octokit.Tests/Clients/RepositoryContentsClientTests.cs index 46324cbe19..85707ee9f7 100644 --- a/Octokit.Tests/Clients/RepositoryContentsClientTests.cs +++ b/Octokit.Tests/Clients/RepositoryContentsClientTests.cs @@ -89,5 +89,135 @@ public async Task ReturnsContents() Assert.Equal(1, contents.Count); } } + + public class TheCreateFileMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + string expectedUri = "repos/org/repo/contents/path/to/file"; + client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")); + + connection.Received().Put(Arg.Is(u => u.ToString() == expectedUri), Arg.Any()); + } + + [Fact] + public void PassesRequestObject() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")); + + connection.Received().Put( + Arg.Any(), + Arg.Is(a => + a.Message == "message" + && a.Content == "myfilecontents" + && a.Branch == "mybranch")); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + await Assert.ThrowsAsync(() => client.CreateFile(null, "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"))); + await Assert.ThrowsAsync(() => client.CreateFile("org", null, "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"))); + await Assert.ThrowsAsync(() => client.CreateFile("org", "repo", null, new CreateFileRequest("message", "myfilecontents", "mybranch"))); + await Assert.ThrowsAsync(() => client.CreateFile("org", "repo", "path/to/file", null)); + } + } + + public class TheDeleteFileMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + string expectedUri = "repos/org/repo/contents/path/to/file"; + client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")); + + connection.Received().Delete(Arg.Is(u => u.ToString() == expectedUri), Arg.Any()); + } + + [Fact] + public void PassesRequestObject() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")); + + connection.Received().Delete( + Arg.Any(), + Arg.Is(a => + a.Message == "message" + && a.Sha == "1234abc" + && a.Branch == "mybranch")); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + await Assert.ThrowsAsync(() => client.DeleteFile(null, "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.DeleteFile("org", null, "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.DeleteFile("org", "repo", null, new DeleteFileRequest("message", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.DeleteFile("org", "repo", "path/to/file", null)); + } + } + + public class TheUpdateFileMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + string expectedUri = "repos/org/repo/contents/path/to/file"; + client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")); + + connection.Received().Put(Arg.Is(u => u.ToString() == expectedUri), Arg.Any()); + } + + [Fact] + public void PassesRequestObject() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")); + + connection.Received().Put( + Arg.Any(), + Arg.Is(a => + a.Message == "message" + && a.Content == "myfilecontents" + && a.Sha == "1234abc" + && a.Branch == "mybranch")); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var connection = Substitute.For(); + var client = new RepositoryContentsClient(connection); + + await Assert.ThrowsAsync(() => client.UpdateFile(null, "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.UpdateFile("org", null, "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.UpdateFile("org", "repo", null, new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"))); + await Assert.ThrowsAsync(() => client.UpdateFile("org", "repo", "path/to/file", null)); + } + } } } \ No newline at end of file