From fc0fbe5a2287ac7672caa632aec155cc69815133 Mon Sep 17 00:00:00 2001 From: pltaylor Date: Mon, 11 Nov 2013 08:06:38 -0500 Subject: [PATCH] Add Tests for Tree Client --- Octokit.Tests/Clients/TreeClientTests.cs | 123 +++++++++++++++++++++++ Octokit.Tests/Octokit.Tests.csproj | 1 + Octokit/Clients/ITreeClient.cs | 1 + Octokit/Clients/TreeClient.cs | 2 +- Octokit/Helpers/ApiUrls.cs | 11 ++ Octokit/Models/Request/NewTree.cs | 10 +- Octokit/Models/Request/NewTreeItem.cs | 1 + Octokit/Models/Response/TreeItem.cs | 1 + Octokit/Models/Response/TreeResponse.cs | 5 +- 9 files changed, 150 insertions(+), 5 deletions(-) create mode 100644 Octokit.Tests/Clients/TreeClientTests.cs diff --git a/Octokit.Tests/Clients/TreeClientTests.cs b/Octokit.Tests/Clients/TreeClientTests.cs new file mode 100644 index 00000000..0ab638d8 --- /dev/null +++ b/Octokit.Tests/Clients/TreeClientTests.cs @@ -0,0 +1,123 @@ +using System; +using System.Threading.Tasks; +using NSubstitute; +using Octokit.Internal; +using Octokit.Tests.Helpers; +using Xunit; + +namespace Octokit.Tests +{ + public class TreeClientTests + { + public class TheGetMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new TreeClient(connection); + + client.Get("fake", "repo", "123456ABCD"); + + connection.Received().Get(Arg.Is(u => u.ToString() == "repos/fake/repo/git/trees/123456ABCD"), + null); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new TreeClient(Substitute.For()); + + await AssertEx.Throws(async () => await client.Get(null, "name", "123456ABCD")); + await AssertEx.Throws(async () => await client.Get("", "name", "123456ABCD")); + await AssertEx.Throws(async () => await client.Get("owner", null, "123456ABCD")); + await AssertEx.Throws(async () => await client.Get("owner", "", "123456ABCD")); + await AssertEx.Throws(async () => await client.Get("owner", "name", null)); + await AssertEx.Throws(async () => await client.Get("owner", "name", "")); + } + } + + public class TheCreateMethod + { + [Fact] + public void PostsToCorrectUrl() + { + var newTree = new NewTree(); + var connection = Substitute.For(); + var client = new TreeClient(connection); + + client.Create("fake", "repo", newTree); + + connection.Received().Post(Arg.Is(u => u.ToString() == "repos/fake/repo/git/trees"), newTree); + } + + [Fact] + public async Task EnsuresArgumentsNotNull() + { + var connection = Substitute.For(); + var client = new TreeClient(connection); + + await AssertEx.Throws(async () => await client.Create(null, "name", new NewTree())); + await AssertEx.Throws(async () => await client.Create("", "name", new NewTree())); + await AssertEx.Throws(async () => await client.Create("owner", null, new NewTree())); + await AssertEx.Throws(async () => await client.Create("owner", "", new NewTree())); + } + } + + public class TheCtor + { + [Fact] + public void EnsuresArgument() + { + Assert.Throws(() => new TreeClient(null)); + } + } + + [Fact] + public void CanDeserializeIssueComment() + { + const string issueResponseJson = + "{" + + "\"sha\": \"9fb037999f264ba9a7fc6274d15fa3ae2ab98312\"," + + "\"url\": \"https://api.github.com/repos/octocat/Hello-World/trees/9fb037999f264ba9a7fc6274d15fa3ae2ab98312\"," + + "\"tree\": [" + + "{" + + "\"path\": \"file.rb\"," + + "\"mode\": \"100644\"," + + "\"type\": \"blob\"," + + "\"size\": 30," + + "\"sha\": \"44b4fc6d56897b048c772eb4087f854f46256132\"," + + "\"url\": \"https://api.github.com/repos/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132\"" + + "}," + + "{" + + "\"path\": \"subdir\"," + + "\"mode\": \"040000\"," + + "\"type\": \"tree\"," + + "\"sha\": \"f484d249c660418515fb01c2b9662073663c242e\"," + + "\"url\": \"https://api.github.com/repos/octocat/Hello-World/git/blobs/f484d249c660418515fb01c2b9662073663c242e\"" + + "}," + + "{" + + "\"path\": \"exec_file\"," + + "\"mode\": \"100755\"," + + "\"type\": \"blob\"," + + "\"size\": 75," + + "\"sha\": \"45b983be36b73c0788dc9cbcb76cbb80fc7bb057\"," + + "\"url\": \"https://api.github.com/repos/octocat/Hello-World/git/blobs/45b983be36b73c0788dc9cbcb76cbb80fc7bb057\"" + + "}" + + "]" + + "}"; + var response = new ApiResponse + { + Body = issueResponseJson, + ContentType = "application/json" + }; + var jsonPipeline = new JsonHttpPipeline(); + + jsonPipeline.DeserializeResponse(response); + + Assert.NotNull(response.BodyAsObject); + Assert.Equal(issueResponseJson, response.Body); + Assert.Equal("9fb037999f264ba9a7fc6274d15fa3ae2ab98312", response.BodyAsObject.Sha); + } + } +} diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index 6f2f5ee2..623f83bc 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -75,6 +75,7 @@ + diff --git a/Octokit/Clients/ITreeClient.cs b/Octokit/Clients/ITreeClient.cs index 681fb865..8fcced96 100644 --- a/Octokit/Clients/ITreeClient.cs +++ b/Octokit/Clients/ITreeClient.cs @@ -14,6 +14,7 @@ namespace Octokit /// The name of the repository /// The SHA that references the tree /// The for the specified Tree. + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get")] Task Get(string owner, string name, string reference); /// diff --git a/Octokit/Clients/TreeClient.cs b/Octokit/Clients/TreeClient.cs index c18e3881..f2ec72ba 100644 --- a/Octokit/Clients/TreeClient.cs +++ b/Octokit/Clients/TreeClient.cs @@ -44,7 +44,7 @@ namespace Octokit Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(newTree, "newTree"); - return ApiConnection.Post(ApiUrls.IssueComments(owner, name), newTree); + return ApiConnection.Post(ApiUrls.Tree(owner, name), newTree); } } } diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index db84fad0..9f47ef45 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -473,6 +473,17 @@ namespace Octokit return "users/{0}/events/orgs/{1}".FormatUri(user, organization); } + /// + /// Returns the for the specified tree. + /// + /// The owner of the repository + /// The name of the repository + /// + public static Uri Tree(string owner, string name) + { + return "repos/{0}/{1}/git/trees".FormatUri(owner, name); + } + /// /// Returns the for the specified tree. /// diff --git a/Octokit/Models/Request/NewTree.cs b/Octokit/Models/Request/NewTree.cs index d5db4986..35e6c400 100644 --- a/Octokit/Models/Request/NewTree.cs +++ b/Octokit/Models/Request/NewTree.cs @@ -1,15 +1,19 @@ -namespace Octokit +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; + +namespace Octokit { public class NewTree { /// /// The SHA1 of the tree you want to update with new data. /// - public string Base_tree { get; set; } + public string BaseTree { get; set; } /// /// The list of Tree Items for this new Tree item. /// - public NewTreeItem[] Tree { get; set; } + [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] + public ICollection Tree { get; set; } } } \ No newline at end of file diff --git a/Octokit/Models/Request/NewTreeItem.cs b/Octokit/Models/Request/NewTreeItem.cs index de4bac97..41087e67 100644 --- a/Octokit/Models/Request/NewTreeItem.cs +++ b/Octokit/Models/Request/NewTreeItem.cs @@ -18,6 +18,7 @@ /// /// The type of tree item this is. /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")] public TreeType Type { get; set; } /// diff --git a/Octokit/Models/Response/TreeItem.cs b/Octokit/Models/Response/TreeItem.cs index d9792f30..96a118a8 100644 --- a/Octokit/Models/Response/TreeItem.cs +++ b/Octokit/Models/Response/TreeItem.cs @@ -17,6 +17,7 @@ namespace Octokit /// /// The type of this Tree Item. /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")] public TreeType Type { get; set; } /// diff --git a/Octokit/Models/Response/TreeResponse.cs b/Octokit/Models/Response/TreeResponse.cs index 71d1bb2d..0f53ad8e 100644 --- a/Octokit/Models/Response/TreeResponse.cs +++ b/Octokit/Models/Response/TreeResponse.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Octokit { @@ -17,6 +19,7 @@ namespace Octokit /// /// The list of Tree Items for this Tree response. /// - public TreeItem[] Tree { get; set; } + [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] + public ICollection Tree { get; set; } } } \ No newline at end of file