Added unit tests

This commit is contained in:
Henrik Andersson
2014-06-15 08:34:39 +10:00
parent fd66ea5472
commit 7f9429b8e9
6 changed files with 142 additions and 9 deletions
@@ -0,0 +1,128 @@
using NSubstitute;
using System;
using Xunit;
namespace Octokit.Tests.Clients
{
/// <summary>
/// Client tests mostly just need to make sure they call the IApiConnection with the correct
/// relative Uri. No need to fake up the response. All *those* tests are in ApiConnectionTests.cs.
/// </summary>
public class RepositoryDeployKeysClientTests
{
public class TheConstructor
{
[Fact]
public void ThrowsForBadArgs()
{
Assert.Throws<ArgumentNullException>(() => new RepositoryDeployKeysClient(null));
}
}
public class TheGetMethod
{
[Fact]
public void GetsADeployKey()
{
var apiConnection = Substitute.For<IApiConnection>();
var deployKeysClient = new RepositoryDeployKeysClient(apiConnection);
deployKeysClient.Get("user", "repo", 42);
apiConnection.Received().Get<DeployKey>(Arg.Is<Uri>(u => u.ToString() == "repos/user/repo/keys/42"),
null);
}
[Fact]
public void EnsureNonNullArguments()
{
var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For<IApiConnection>());
Assert.Throws<ArgumentNullException>(() => deployKeysClient.Get(null, "repo", 1));
Assert.Throws<ArgumentException>(() => deployKeysClient.Get("", "repo", 1));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.Get("user", null, 1));
Assert.Throws<ArgumentException>(() => deployKeysClient.Get("user", "", 1));
}
}
public class TheGetAllMethod
{
[Fact]
public void GetsAListOfDeployKeys()
{
var apiConnection = Substitute.For<IApiConnection>();
var deployKeysClient = new RepositoryDeployKeysClient(apiConnection);
deployKeysClient.GetAll("user", "repo");
apiConnection.Received().GetAll<DeployKey>(Arg.Is<Uri>(u => u.ToString() == "repos/user/repo/keys"));
}
[Fact]
public void EnsuresNonNullArguments()
{
var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For<IApiConnection>());
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll(null, "repo"));
Assert.Throws<ArgumentException>(() => deployKeysClient.GetAll("", "repo"));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.GetAll("user", null));
Assert.Throws<ArgumentException>(() => deployKeysClient.GetAll("user", ""));
}
}
public class TheCreateMethod
{
[Fact]
public void SendsCreateToCorrectUrl()
{
var apiConnection = Substitute.For<IApiConnection>();
var deployKeysClient = new RepositoryDeployKeysClient(apiConnection);
deployKeysClient.Create("user", "repo", new NewDeployKey { Key = "ABC123", Title = "user@repo" });
apiConnection.Received().Post<DeployKey>(Arg.Is<Uri>(u => u.ToString() == "repos/user/repo/keys"),
Args.NewDeployKey);
}
[Fact]
public void EnsuresNonNullArguments()
{
var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For<IApiConnection>());
Assert.Throws<ArgumentNullException>(() => deployKeysClient.Create(null, "repo", new NewDeployKey()));
Assert.Throws<ArgumentException>(() => deployKeysClient.Create("", "repo", new NewDeployKey()));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.Create("user", null, new NewDeployKey()));
Assert.Throws<ArgumentException>(() => deployKeysClient.Create("user", "", new NewDeployKey()));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.Create("user", "repo", null));
Assert.Throws<ArgumentException>(() => deployKeysClient.Create("user", "repo", new NewDeployKey()));
Assert.Throws<ArgumentException>(() => deployKeysClient.Create("user", "repo", new NewDeployKey { Key = "ABC123" }));
Assert.Throws<ArgumentException>(() => deployKeysClient.Create("user", "repo", new NewDeployKey { Title = "user@repo" }));
}
}
public class TheDeleteMethod
{
[Fact]
public void DeletesCorrectUrl()
{
var apiConnection = Substitute.For<IApiConnection>();
var deployKeysClient = new RepositoryDeployKeysClient(apiConnection);
deployKeysClient.Delete("user", "repo", 42);
apiConnection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "repos/user/repo/keys/42"));
}
[Fact]
public void EnsuresNonNullArguments()
{
var deployKeysClient = new RepositoryDeployKeysClient(Substitute.For<IApiConnection>());
Assert.Throws<ArgumentNullException>(() => deployKeysClient.Delete(null, "repo", 1));
Assert.Throws<ArgumentException>(() => deployKeysClient.Delete("", "repo", 1));
Assert.Throws<ArgumentNullException>(() => deployKeysClient.Delete("user", null, 1));
Assert.Throws<ArgumentException>(() => deployKeysClient.Delete("user", "", 1));
}
}
}
}
+5
View File
@@ -57,5 +57,10 @@ namespace Octokit.Tests
{
get { return Arg.Any<CancellationToken>(); }
}
public static NewDeployKey NewDeployKey
{
get { return Arg.Any<NewDeployKey>(); }
}
}
}
+1
View File
@@ -67,6 +67,7 @@
<Compile Include="Clients\DeploymentsClientTests.cs" />
<Compile Include="Clients\DeploymentStatusClientTests.cs" />
<Compile Include="Clients\FeedsClientTests.cs" />
<Compile Include="Clients\RepositoryDeployKeysClientTests.cs" />
<Compile Include="Clients\SearchClientTests.cs" />
<Compile Include="Clients\GistCommentsClientTests.cs" />
<Compile Include="Clients\GistsClientTests.cs" />
@@ -28,7 +28,7 @@ namespace Octokit
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
Task<IReadOnlyList<DeployKey>> GetForRepository(string owner, string name);
Task<IReadOnlyList<DeployKey>> GetAll(string owner, string name);
/// <summary>
/// Creates a new deploy key for a repository.
@@ -1,7 +1,7 @@
using System;
#if NET_45
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endif
using System.Threading.Tasks;
namespace Octokit
@@ -36,7 +36,6 @@ namespace Octokit
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(number, "number");
return ApiConnection.Get<DeployKey>(ApiUrls.RepositoryDeployKey(owner, name, number));
}
@@ -49,12 +48,12 @@ namespace Octokit
/// </remarks>
/// <param name="owner">The owner of the repository.</param>
/// <param name="name">The name of the repository.</param>
public Task<IReadOnlyList<DeployKey>> GetForRepository(string owner, string name)
public Task<IReadOnlyList<DeployKey>> GetAll(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.Get<IReadOnlyList<DeployKey>>(ApiUrls.RepositoryDeployKeys(owner, name));
return ApiConnection.GetAll<DeployKey>(ApiUrls.RepositoryDeployKeys(owner, name));
}
/// <summary>
@@ -82,11 +81,12 @@ namespace Octokit
return ApiConnection.Post<DeployKey>(ApiUrls.RepositoryDeployKeys(owner, name), newDeployKey);
}
/// <summary>
/// Deploy keys are immutable. If you need to update a key, remove the key and create a new one instead.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/repos/keys/#edit
/// See the <a href="https://developer.github.com/v3/repos/keys/#edit"> API documentation</a> for more information.
/// </remarks>
/// <param name="owner"></param>
/// <param name="name"></param>
@@ -94,7 +94,6 @@ namespace Octokit
/// <param name="newDeployKey"></param>
/// <returns></returns>
/// Task<DeployKey> Update(string owner, string name, int number, NewDeployKey newDeployKey);
/// <summary>
/// Deletes a deploy key from a repository.
/// </summary>
+1 -1
View File
@@ -1121,7 +1121,7 @@ namespace Octokit
/// <returns></returns>
public static Uri RepositoryDeployKey(string owner, string name, int number)
{
return "repos/{0}/{1}/keys/{1}".FormatUri(owner, name, number);
return "repos/{0}/{1}/keys/{2}".FormatUri(owner, name, number);
}
/// <summary>