Fleshed out most of the API methods

This commit is contained in:
Kristian Hellang
2013-11-24 23:20:11 +01:00
parent 742ce856e8
commit b9b60ed77c
8 changed files with 110 additions and 19 deletions
+65 -12
View File
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using NSubstitute;
@@ -10,19 +11,19 @@ namespace Octokit.Tests.Clients
{
public class ReferencesClientTests
{
public class TheCtor
{
[Fact]
public void EnsuresArgument()
{
Assert.Throws<ArgumentNullException>(() => new ReferencesClient(null));
}
}
public class TheGetMethod
{
public class TheCtor
{
[Fact]
public void EnsuresArgument()
{
Assert.Throws<ArgumentNullException>(() => new ReferencesClient(null));
}
}
[Fact]
public async void EnsuresNonNullArguments()
public async Task EnsuresNonNullArguments()
{
var client = new ReferencesClient(Substitute.For<IApiConnection>());
@@ -35,15 +36,67 @@ namespace Octokit.Tests.Clients
}
[Fact]
public void RequestsCorrectUrl()
public async Task RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new ReferencesClient(connection);
client.Get("owner", "repo", "heads/develop");
await client.Get("owner", "repo", "heads/develop");
connection.Received().Get<Reference>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/git/refs/heads/develop"), null);
}
}
public class TheGetAllMethod
{
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new ReferencesClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentNullException>(async () => await client.GetAll(null, "name", "heads"));
await AssertEx.Throws<ArgumentNullException>(async () => await client.GetAll("owner", null, "heads"));
await AssertEx.Throws<ArgumentException>(async () => await client.GetAll("", "name", "heads"));
await AssertEx.Throws<ArgumentException>(async () => await client.GetAll("owner", "", "heads"));
}
[Fact]
public async Task RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new ReferencesClient(connection);
await client.GetAll("owner", "repo", "heads");
connection.Received().GetAll<Reference>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/git/refs/heads"));
}
}
public class TheCreateMethod
{
[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new ReferencesClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentNullException>(async () => await client.Create(null, "name", new NewReference()));
await AssertEx.Throws<ArgumentNullException>(async () => await client.Create("owner", null, new NewReference()));
await AssertEx.Throws<ArgumentNullException>(async () => await client.Create("owner", "name", null));
await AssertEx.Throws<ArgumentException>(async () => await client.Create("", "name", new NewReference()));
await AssertEx.Throws<ArgumentException>(async () => await client.Create("owner", "", new NewReference()));
}
[Fact]
public async Task PostsToCorrectUrl()
{
var newReference = new NewReference();
var connection = Substitute.For<IApiConnection>();
var client = new ReferencesClient(connection);
await client.Create("owner", "repo", newReference);
connection.Received().Post<Reference>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/git/refs"), newReference);
}
}
}
}
+1 -1
View File
@@ -11,7 +11,7 @@ namespace Octokit
Task<Reference> Create(string owner, string name, NewReference reference);
Task<Reference> Update(string owner, string name, string reference, string sha, bool force = false);
Task<Reference> Update(string owner, string name, string reference, ReferenceUpdate update);
Task Delete(string owner, string name, string reference);
}
+21 -5
View File
@@ -21,22 +21,38 @@ namespace Octokit
public Task<IReadOnlyList<Reference>> GetAll(string owner, string name, string subNamespace = null)
{
throw new System.NotImplementedException();
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.GetAll<Reference>(ApiUrls.Reference(owner, name, subNamespace));
}
public Task<Reference> Create(string owner, string name, NewReference reference)
{
throw new System.NotImplementedException();
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(reference, "reference");
return ApiConnection.Post<Reference>(ApiUrls.Reference(owner, name), reference);
}
public Task<Reference> Update(string owner, string name, string reference, string sha, bool force = false)
public Task<Reference> Update(string owner, string name, string reference, ReferenceUpdate update)
{
throw new System.NotImplementedException();
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(reference, "reference");
Ensure.ArgumentNotNull(update, "update");
return ApiConnection.Patch<Reference>(ApiUrls.Reference(owner, name, reference), update);
}
public Task Delete(string owner, string name, string reference)
{
throw new System.NotImplementedException();
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(reference, "reference");
return ApiConnection.Delete(ApiUrls.Reference(owner, name, reference));
}
}
}
+6 -1
View File
@@ -476,8 +476,13 @@ namespace Octokit
/// <param name="name">The name of the repository</param>
/// <param name="reference">The reference name</param>
/// <returns></returns>
public static Uri Reference(string owner, string name, string reference)
public static Uri Reference(string owner, string name, string reference = null)
{
if (string.IsNullOrEmpty(reference))
{
return "repos/{0}/{1}/git/refs".FormatUri(owner, name);
}
return "repos/{0}/{1}/git/refs/{2}".FormatUri(owner, name, reference);
}
+14
View File
@@ -0,0 +1,14 @@
namespace Octokit
{
public class ReferenceUpdate
{
public ReferenceUpdate(string sha, bool force = false)
{
Sha = sha;
Force = force;
}
public string Sha { get; set; }
public bool Force { get; set; }
}
}
+1
View File
@@ -90,6 +90,7 @@
<Compile Include="Models\Request\NewTreeItem.cs" />
<Compile Include="Models\Request\NewTeam.cs" />
<Compile Include="Models\Request\Permission.cs" />
<Compile Include="Models\Request\ReferenceUpdate.cs" />
<Compile Include="Models\Request\RequestParameters.cs" />
<Compile Include="Models\Request\StarredRequest.cs" />
<Compile Include="Models\Request\UpdateTeam.cs" />
+1
View File
@@ -170,6 +170,7 @@
<Compile Include="Models\Request\NewTreeItem.cs" />
<Compile Include="Models\Request\NewTeam.cs" />
<Compile Include="Models\Request\Permission.cs" />
<Compile Include="Models\Request\ReferenceUpdate.cs" />
<Compile Include="Models\Request\ReleaseUpdate.cs" />
<Compile Include="Models\Request\RepositoryIssueRequest.cs" />
<Compile Include="Models\Request\RequestParameters.cs" />
+1
View File
@@ -58,6 +58,7 @@
<Compile Include="Clients\IReferencesClient.cs" />
<Compile Include="Clients\ReferencesClient.cs" />
<Compile Include="Models\Request\NewReference.cs" />
<Compile Include="Models\Request\ReferenceUpdate.cs" />
<Compile Include="Models\Response\BlobReference.cs" />
<Compile Include="Clients\IBlobsClient.cs" />
<Compile Include="Models\Request\NewBlob.cs" />