Skip to content

Commit

Permalink
Merge pull request #862 from octokit/haacked/836-handle-empty-content
Browse files Browse the repository at this point in the history
Handle empty content better
  • Loading branch information
shiftkey committed Sep 10, 2015
2 parents b858613 + 4e09d71 commit 82a3e76
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 86 deletions.
10 changes: 8 additions & 2 deletions Octokit.Tests.Conventions/ModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public void CheckPaginationGetAllMethodNames(Type clientInterface)
.Where(x => x.Name.StartsWith("Get"));

var invalidMethods = methodsThatCanPaginate
.Where(x => !x.Name.StartsWith("GetAll"));
.Where(x => !x.Name.StartsWith("GetAll"))
.ToList();

if (invalidMethods.Any())
{
Expand All @@ -81,7 +82,12 @@ public void CheckPaginationGetAllMethodNames(Type clientInterface)

public static IEnumerable<object[]> GetClientInterfaces()
{
return typeof(IEventsClient).Assembly.ExportedTypes.Where(TypeExtensions.IsClientInterface).Select(type => new[] { type });
return typeof(IEventsClient)
.Assembly
.ExportedTypes
.Where(TypeExtensions.IsClientInterface)
.Where(t => t != typeof(IStatisticsClient)) // This convention doesn't apply to this one type.
.Select(type => new[] { type });
}

public static IEnumerable<object[]> ModelTypes
Expand Down
13 changes: 8 additions & 5 deletions Octokit.Tests.Conventions/SyncObservableClients.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reflection;
using Octokit.Tests.Helpers;
using Xunit;
using Xunit.Extensions;

namespace Octokit.Tests.Conventions
{
Expand All @@ -22,13 +20,13 @@ public void CheckObservableClients(Type clientInterface)
var mainNames = Array.ConvertAll(mainMethods, m => m.Name);
var observableNames = Array.ConvertAll(observableMethods, m => m.Name);

var methodsMissingOnReactiveClient = mainNames.Except(observableNames);
var methodsMissingOnReactiveClient = mainNames.Except(observableNames).ToList();
if (methodsMissingOnReactiveClient.Any())
{
throw new InterfaceMissingMethodsException(observableClient, methodsMissingOnReactiveClient);
}

var additionalMethodsOnReactiveClient = observableNames.Except(mainNames);
var additionalMethodsOnReactiveClient = observableNames.Except(mainNames).ToList();
if (additionalMethodsOnReactiveClient.Any())
{
throw new InterfaceHasAdditionalMethodsException(observableClient, additionalMethodsOnReactiveClient);
Expand Down Expand Up @@ -122,7 +120,12 @@ private static void CheckParameters(MethodInfo mainMethod, MethodInfo observable

public static IEnumerable<object[]> GetClientInterfaces()
{
return typeof(IEventsClient).Assembly.ExportedTypes.Where(TypeExtensions.IsClientInterface).Select(type => new[] { type });
return typeof(IEventsClient)
.Assembly
.ExportedTypes
.Where(TypeExtensions.IsClientInterface)
.Where(t => t != typeof(IStatisticsClient)) // This convention doesn't apply to this one type.
.Select(type => new[] { type });
}
}
}
31 changes: 19 additions & 12 deletions Octokit.Tests.Integration/Clients/StatisticsClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Xunit;

Expand All @@ -18,21 +17,31 @@ public StatisticsClientTests()
}

[IntegrationTest]
public async Task CanCreateAndRetrieveCommit()
public async Task CanCreateAndRetrieveContributors()
{
var repository = await CreateRepository();
await CommitToRepository(repository);
var contributors = await _client.Repository.Statistics.GetContributors(repository.Owner, repository.Name);

Assert.NotNull(contributors);
Assert.True(contributors.Count() == 1);
Assert.Equal(1, contributors.Count);

var soleContributor = contributors.First();
Assert.NotNull(soleContributor.Author);
Assert.True(soleContributor.Author.Login == repository.Owner);

Assert.True(soleContributor.Weeks.Count() == 1);
Assert.True(soleContributor.Total == 1);
Assert.Equal(1, soleContributor.Weeks.Count);
Assert.Equal(1, soleContributor.Total);
}

[IntegrationTest]
public async Task CanCreateAndRetrieveEmptyContributors()
{
var repository = await CreateRepository(autoInit: false);
var contributors = await _client.Repository.Statistics.GetContributors(repository.Owner, repository.Name);

Assert.NotNull(contributors);
Assert.Empty(contributors);
}

[IntegrationTest]
Expand All @@ -42,10 +51,10 @@ public async Task CanGetCommitActivityForTheLastYear()
await CommitToRepository(repository);
var commitActivities = await _client.Repository.Statistics.GetCommitActivity(repository.Owner, repository.Name);
Assert.NotNull(commitActivities);
Assert.True(commitActivities.Activity.Count() == 52);
Assert.Equal(52, commitActivities.Activity.Count);

var thisWeek = commitActivities.Activity.Last();
Assert.True(thisWeek.Total == 1);
Assert.Equal(1, thisWeek.Total);
Assert.NotNull(thisWeek.Days);
}

Expand All @@ -65,9 +74,7 @@ public async Task CanGetParticipationStatistics()
var repository = await CreateRepository();
await CommitToRepository(repository);
var weeklyCommitCounts = await _client.Repository.Statistics.GetParticipation(repository.Owner, repository.Name);
Assert.NotNull(weeklyCommitCounts);
Assert.NotNull(weeklyCommitCounts.All);
Assert.NotNull(weeklyCommitCounts.Owner);
Assert.Equal(52, weeklyCommitCounts.All.Count);
}

[IntegrationTest]
Expand All @@ -80,10 +87,10 @@ public async Task CanGetPunchCardForRepository()
Assert.NotNull(punchCard.PunchPoints);
}

async Task<RepositorySummary> CreateRepository()
async Task<RepositorySummary> CreateRepository(bool autoInit = true)
{
var repoName = Helper.MakeNameWithTimestamp("public-repo");
var repository = await _client.Repository.Create(new NewRepository(repoName) { AutoInit = true });
var repository = await _client.Repository.Create(new NewRepository(repoName) { AutoInit = autoInit });
return new RepositorySummary
{
Owner = repository.Owner.Login,
Expand Down
62 changes: 47 additions & 15 deletions Octokit.Tests/Clients/StatisticsClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using NSubstitute;
using Octokit.Tests.Helpers;
using Octokit.Helpers;
using Xunit;

namespace Octokit.Tests.Clients
Expand All @@ -21,16 +22,18 @@ public void DoesThrowOnBadArguments()
public class TheGetContributorsMethod
{
[Fact]
public void RequestsCorrectUrl()
public async Task RetrievesContributorsForCorrectUrl()
{
var expectedEndPoint = new Uri("/repos/username/repositoryName/stats/contributors", UriKind.Relative);

var client = Substitute.For<IApiConnection>();
IReadOnlyList<Contributor> contributors = new ReadOnlyCollection<Contributor>(new[] { new Contributor() });
client.GetQueuedOperation<Contributor>(expectedEndPoint, Args.CancellationToken)
.Returns(Task.FromResult(contributors));
var statisticsClient = new StatisticsClient(client);

statisticsClient.GetContributors("username","repositoryName");
var result = await statisticsClient.GetContributors("username","repositoryName");

client.Received().GetQueuedOperation<IEnumerable<Contributor>>(expectedEndPoint,Args.CancellationToken);
Assert.Equal(1, result.Count);
}

[Fact]
Expand All @@ -51,16 +54,24 @@ public async Task ThrowsIfGivenNullRepositoryName()
public class TheGetCommitActivityForTheLastYearMethod
{
[Fact]
public void RequestsCorrectUrl()
public async Task RequestsCorrectUrl()
{
var expectedEndPoint = new Uri("/repos/username/repositoryName/stats/commit_activity", UriKind.Relative);

var data = new WeeklyCommitActivity(new[] { 1, 2 }, 100, 42);
IReadOnlyList<WeeklyCommitActivity> response = new ReadOnlyCollection<WeeklyCommitActivity>(new[] { data });
var client = Substitute.For<IApiConnection>();
client.GetQueuedOperation<WeeklyCommitActivity>(expectedEndPoint, Args.CancellationToken)
.Returns(Task.FromResult(response));
var statisticsClient = new StatisticsClient(client);

statisticsClient.GetCommitActivity("username", "repositoryName");
var result = await statisticsClient.GetCommitActivity("username", "repositoryName");

client.Received().GetQueuedOperation<IReadOnlyList<WeeklyCommitActivity>>(expectedEndPoint, Args.CancellationToken);
Assert.Equal(2, result.Activity[0].Days.Count);
Assert.Equal(1, result.Activity[0].Days[0]);
Assert.Equal(2, result.Activity[0].Days[1]);
Assert.Equal(100, result.Activity[0].Total);
Assert.Equal(42, result.Activity[0].Week);
}

[Fact]
Expand All @@ -81,16 +92,31 @@ public async Task ThrowsIfGivenNullRepositoryName()
public class TheGetAdditionsAndDeletionsPerWeekMethod
{
[Fact]
public void RequestsCorrectUrl()
public async Task RequestsCorrectUrl()
{
var expectedEndPoint = new Uri("/repos/username/repositoryName/stats/code_frequency", UriKind.Relative);

long firstTimestamp = 159670861;
long secondTimestamp = 0;
IReadOnlyList<long[]> data = new ReadOnlyCollection<long[]>(new[]
{
new[] { firstTimestamp, 10, 52 },
new[] { secondTimestamp, 0, 9 }
});
var client = Substitute.For<IApiConnection>();
client.GetQueuedOperation<long[]>(expectedEndPoint, Args.CancellationToken)
.Returns(Task.FromResult(data));
var statisticsClient = new StatisticsClient(client);

statisticsClient.GetCodeFrequency("username", "repositoryName");
var codeFrequency = await statisticsClient.GetCodeFrequency("username", "repositoryName");

client.Received().GetQueuedOperation<IEnumerable<long[]>>(expectedEndPoint, Args.CancellationToken);
Assert.Equal(2, codeFrequency.AdditionsAndDeletionsByWeek.Count);
Assert.Equal(firstTimestamp.FromUnixTime(), codeFrequency.AdditionsAndDeletionsByWeek[0].Timestamp);
Assert.Equal(10, codeFrequency.AdditionsAndDeletionsByWeek[0].Additions);
Assert.Equal(52, codeFrequency.AdditionsAndDeletionsByWeek[0].Deletions);
Assert.Equal(secondTimestamp.FromUnixTime(), codeFrequency.AdditionsAndDeletionsByWeek[1].Timestamp);
Assert.Equal(0, codeFrequency.AdditionsAndDeletionsByWeek[1].Additions);
Assert.Equal(9, codeFrequency.AdditionsAndDeletionsByWeek[1].Deletions);
}

[Fact]
Expand Down Expand Up @@ -141,16 +167,22 @@ public async Task ThrowsIfGivenNullRepositoryName()
public class TheGetHourlyCommitCountsMethod
{
[Fact]
public void RequestsCorrectUrl()
public async Task RetrievesPunchCard()
{
var expectedEndPoint = new Uri("/repos/username/repositoryName/stats/punch_card", UriKind.Relative);

var client = Substitute.For<IApiConnection>();
IReadOnlyList<int[]> data = new ReadOnlyCollection<int[]>(new[] { new[] { 2, 8, 42 } });
client.GetQueuedOperation<int[]>(expectedEndPoint, Args.CancellationToken)
.Returns(Task.FromResult(data));
var statisticsClient = new StatisticsClient(client);

statisticsClient.GetPunchCard("username", "repositoryName");
var result = await statisticsClient.GetPunchCard("username", "repositoryName");

client.Received().GetQueuedOperation<IEnumerable<int[]>>(expectedEndPoint, Args.CancellationToken);
Assert.Equal(1, result.PunchPoints.Count);
Assert.Equal(DayOfWeek.Tuesday, result.PunchPoints[0].DayOfWeek);
Assert.Equal(8, result.PunchPoints[0].HourOfTheDay);
Assert.Equal(42, result.PunchPoints[0].CommitCount);
}

[Fact]
Expand All @@ -168,4 +200,4 @@ public async Task ThrowsIfGivenNullRepositoryName()
}
}
}
}
}
46 changes: 46 additions & 0 deletions Octokit.Tests/Helpers/UnixTimestampExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using Octokit.Helpers;
using Xunit;

public class UnixTimestampExtensionsTests
{
public class TheToUnixTimeMethod
{
[Fact]
public void ReturnsUnixEpochCorrectly()
{
var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
Assert.Equal(0, epoch.ToUnixTime());
}

[Fact]
public void ReturnsRandomDateCorrectly()
{
var epoch = new DateTimeOffset(1975, 1, 23, 1, 1, 1, TimeSpan.Zero);
Assert.Equal(159670861, epoch.ToUnixTime());
}
}

public class TheFromUnixTimeMethod
{
[Fact]
public void ReturnsDateFromUnixEpochCorrectly()
{
var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);

var result = ((long)0).FromUnixTime();

Assert.Equal(epoch, result);
}

[Fact]
public void ReturnsDateFromRandomTimeCorrectly()
{
var expected = new DateTimeOffset(1975, 1, 23, 1, 1, 2, TimeSpan.Zero);

var result = ((long)159670862).FromUnixTime();

Assert.Equal(expected, result);
}
}
}
Loading

0 comments on commit 82a3e76

Please sign in to comment.