Skip to content

Commit

Permalink
Merge pull request #190 from FoldingCoin/develop
Browse files Browse the repository at this point in the history
v2.0.0-RC8
  • Loading branch information
StrungSafe authored Dec 29, 2019
2 parents 69da5f8 + da7a0b6 commit c49813a
Show file tree
Hide file tree
Showing 67 changed files with 578 additions and 406 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace StatsDownloadApi.Core.Tests
{
using System.Collections.Generic;
using Interfaces;
using Interfaces.DataTransfer;

using NUnit.Framework;

using StatsDownloadApi.Interfaces;
using StatsDownloadApi.Interfaces.DataTransfer;

[TestFixture]
public class TestStandardTokenDistributionProvider
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
namespace StatsDownloadApi.Core.Tests
{
using System;
using Interfaces;

using NSubstitute;
using NSubstitute.ClearExtensions;

using NUnit.Framework;

using StatsDownload.Email;

using StatsDownloadApi.Interfaces;

[TestFixture]
public class TestStatsDownloadApiEmailProvider
{
Expand All @@ -32,8 +36,7 @@ public void Constructor_WhenNullDependencyProvided_ThrowsException()
{
Assert.Throws<ArgumentNullException>(() =>
NewStatsDownloadApiEmailProvider(null, emailSettingsServiceMock));
Assert.Throws<ArgumentNullException>(() =>
NewStatsDownloadApiEmailProvider(emailServiceMock, null));
Assert.Throws<ArgumentNullException>(() => NewStatsDownloadApiEmailProvider(emailServiceMock, null));
}

[TestCase(null)]
Expand Down Expand Up @@ -65,7 +68,8 @@ public void SendUnhandledExceptionEmail_WhenInvoked_SendsEmail()
}

private IStatsDownloadApiEmailService NewStatsDownloadApiEmailProvider(IEmailService emailService,
IEmailSettingsService emailSettingsService)
IEmailSettingsService
emailSettingsService)
{
return new StatsDownloadApiEmailProvider(emailService, emailSettingsService);
}
Expand Down
136 changes: 69 additions & 67 deletions Api/StatsDownloadApi.Core.Tests/TestStatsDownloadApiProvider.cs

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions Api/StatsDownloadApi.Core/StandardTokenDistributionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Interfaces;
using Interfaces.DataTransfer;

using StatsDownloadApi.Interfaces;
using StatsDownloadApi.Interfaces.DataTransfer;

public class StandardTokenDistributionProvider : IStatsDownloadApiTokenDistributionService
{
Expand Down Expand Up @@ -55,8 +56,8 @@ private void AddingUserToDistro(int amount, List<DistroUser> distro, long totalP

private decimal GetRewardAmount(int amount, long totalPoints, long pointsGained)
{
decimal rawAmount = Convert.ToDecimal(pointsGained) / Convert.ToDecimal(totalPoints) *
Convert.ToDecimal(amount);
decimal rawAmount = Convert.ToDecimal(pointsGained) / Convert.ToDecimal(totalPoints)
* Convert.ToDecimal(amount);

return Round(rawAmount, MaxPrecision);
}
Expand All @@ -67,10 +68,10 @@ private long GetTotalPoints(IList<FoldingUser> foldingUsers)
}

private DistroUser NewDistroUser(int amount, long totalPoints, string bitcoinAddress, long pointsGained,
long workUnitsGained)
long workUnitsGained)
{
return new DistroUser(bitcoinAddress, pointsGained,
workUnitsGained, GetRewardAmount(amount, totalPoints, pointsGained));
return new DistroUser(bitcoinAddress, pointsGained, workUnitsGained,
GetRewardAmount(amount, totalPoints, pointsGained));
}
}
}
7 changes: 4 additions & 3 deletions Api/StatsDownloadApi.Core/StatsDownloadApiEmailProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace StatsDownloadApi.Core
{
using System;
using Interfaces;

using StatsDownload.Email;

using StatsDownloadApi.Interfaces;

public class StatsDownloadApiEmailProvider : IStatsDownloadApiEmailService
{
private readonly IEmailService emailService;
Expand All @@ -21,8 +23,7 @@ public void SendUnhandledExceptionEmail(Exception exception)
{
string subject = GetSubject(EmailMessages.UnhandledExceptionHeader);

emailService.SendEmail(subject,
string.Format(EmailMessages.UnhandledExceptionBody, exception.Message));
emailService.SendEmail(subject, string.Format(EmailMessages.UnhandledExceptionBody, exception.Message));
}

private string GetSubject(string baseSubject)
Expand Down
44 changes: 27 additions & 17 deletions Api/StatsDownloadApi.Core/StatsDownloadApiProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using StatsDownload.Core.Interfaces;
using StatsDownload.Core.Interfaces.Enums;
Expand Down Expand Up @@ -41,13 +42,15 @@ public StatsDownloadApiProvider(IStatsDownloadApiDatabaseService statsDownloadAp
nameof(statsDownloadApiDataStoreService));
}

public GetDistroResponse GetDistro(DateTime? startDate, DateTime? endDate, int? amount)
public async Task<GetDistroResponse> GetDistro(DateTime? startDate, DateTime? endDate, int? amount)
{
loggingService.LogMethodInvoked();

IList<ApiError> errors = new List<ApiError>();

if (IsNotPreparedToRunDistro(startDate, endDate, amount, errors))
bool isNotPreparedToRun = await IsNotPreparedToRunDistro(startDate, endDate, amount, errors);

if (isNotPreparedToRun)
{
loggingService.LogMethodFinished();
return new GetDistroResponse(errors);
Expand All @@ -63,13 +66,15 @@ public GetDistroResponse GetDistro(DateTime? startDate, DateTime? endDate, int?
return distroResponse;
}

public GetMemberStatsResponse GetMemberStats(DateTime? startDate, DateTime? endDate)
public async Task<GetMemberStatsResponse> GetMemberStats(DateTime? startDate, DateTime? endDate)
{
loggingService.LogMethodInvoked();

IList<ApiError> errors = new List<ApiError>();

if (IsNotPreparedToGetMemberStats(startDate, endDate, errors))
bool isNotPreparedToRun = await IsNotPreparedToGetMemberStats(startDate, endDate, errors);

if (isNotPreparedToRun)
{
loggingService.LogMethodFinished();
return new GetMemberStatsResponse(errors);
Expand All @@ -93,13 +98,15 @@ public GetMemberStatsResponse GetMemberStats(DateTime? startDate, DateTime? endD
return memberStatsResponse;
}

public GetTeamsResponse GetTeams()
public async Task<GetTeamsResponse> GetTeams()
{
loggingService.LogMethodInvoked();

var errors = new List<ApiError>();

if (IsNotPreparedToGetTeams(errors))
bool isNotPreparedToRun = await IsNotPreparedToGetTeams(errors);

if (isNotPreparedToRun)
{
loggingService.LogMethodFinished();
return new GetTeamsResponse(errors);
Expand All @@ -125,31 +132,32 @@ private IList<FoldingUser> GetFoldingMembers(DateTime? startDate, DateTime? endD
endDate.GetValueOrDefault());
}

private bool IsNotPreparedToGetMemberStats(DateTime? startDate, DateTime? endDate, IList<ApiError> errors)
private async Task<bool> IsNotPreparedToGetMemberStats(DateTime? startDate, DateTime? endDate,
IList<ApiError> errors)
{
ValidateStartDate(startDate, errors);
ValidateEndDate(endDate, errors);
ValidateDateRange(startDate, endDate, errors);
ValidateApiIsAvailable(errors);
await ValidateApiIsAvailable(errors);

return errors.Count > 0;
}

private bool IsNotPreparedToGetTeams(IList<ApiError> errors)
private async Task<bool> IsNotPreparedToGetTeams(IList<ApiError> errors)
{
ValidateApiIsAvailable(errors);
await ValidateApiIsAvailable(errors);

return errors.Count > 0;
}

private bool IsNotPreparedToRunDistro(DateTime? startDate, DateTime? endDate, int? amount,
IList<ApiError> errors)
private async Task<bool> IsNotPreparedToRunDistro(DateTime? startDate, DateTime? endDate, int? amount,
IList<ApiError> errors)
{
ValidateStartDate(startDate, errors);
ValidateEndDate(endDate, errors);
ValidateDateRange(startDate, endDate, errors);
ValidateAmount(amount, errors);
ValidateApiIsAvailable(errors);
await ValidateApiIsAvailable(errors);

return errors.Count > 0;
}
Expand All @@ -173,10 +181,10 @@ private void ValidateAmount(int? amount, IList<ApiError> errors)
}
}

private void ValidateApiIsAvailable(IList<ApiError> errors)
private async Task ValidateApiIsAvailable(IList<ApiError> errors)
{
ValidateDatabaseIsAvailable(errors);
ValidateDataStoreIsAvailable(errors);
await ValidateDataStoreIsAvailable(errors);
}

private void ValidateDatabaseIsAvailable(IList<ApiError> errors)
Expand All @@ -196,9 +204,11 @@ private void ValidateDatabaseIsAvailable(IList<ApiError> errors)
}
}

private void ValidateDataStoreIsAvailable(IList<ApiError> errors)
private async Task ValidateDataStoreIsAvailable(IList<ApiError> errors)
{
if (!statsDownloadApiDataStoreService.IsAvailable())
bool isAvailable = await statsDownloadApiDataStoreService.IsAvailable();

if (!isAvailable)
{
errors.Add(Constants.ApiErrors.DataStoreUnavailable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace StatsDownloadApi.DataStore.Tests
{
using System;
using System.Threading.Tasks;

using NSubstitute;

Expand Down Expand Up @@ -66,8 +67,6 @@ public void SetUp()
fileValidationServiceMock, filePayloadApiSettingsServiceMock, loggingServiceMock);
}

private ILoggingService loggingServiceMock;

private IStatsDownloadApiDatabaseService databaseServiceMock;

private IDataStoreService dataStoreServiceMock;
Expand All @@ -76,6 +75,8 @@ public void SetUp()

private IFileValidationService fileValidationServiceMock;

private ILoggingService loggingServiceMock;

private IStatsDownloadApiDataStoreService systemUnderTest;

private readonly ValidatedFile validatedFileMock1 =
Expand All @@ -96,13 +97,11 @@ public void GetFoldingMembers_WhenInvoked_GetsAndValidatesStatsFiles()
{
databaseServiceMock.Received(1).GetValidatedFiles(DateTime.MinValue, DateTime.MaxValue);

filePayloadApiSettingsServiceMock
.Received(1).SetFilePayloadApiSettings(Arg.Any<FilePayload>());
filePayloadApiSettingsServiceMock.Received(1).SetFilePayloadApiSettings(Arg.Any<FilePayload>());
dataStoreServiceMock.Received(1).DownloadFile(Arg.Any<FilePayload>(), validatedFileMock1);
fileValidationServiceMock.Received(1).ValidateFile(Arg.Any<FilePayload>());

filePayloadApiSettingsServiceMock
.Received(1).SetFilePayloadApiSettings(Arg.Any<FilePayload>());
filePayloadApiSettingsServiceMock.Received(1).SetFilePayloadApiSettings(Arg.Any<FilePayload>());
dataStoreServiceMock.Received(1).DownloadFile(Arg.Any<FilePayload>(), validatedFileMock3);
fileValidationServiceMock.Received(1).ValidateFile(Arg.Any<FilePayload>());
});
Expand Down Expand Up @@ -138,11 +137,11 @@ public void GetTeams_WhenInvoked()

[TestCase(true)]
[TestCase(false)]
public void IsAvailable_WhenInvoked_DefersToDataStore(bool expected)
public async Task IsAvailable_WhenInvoked_DefersToDataStore(bool expected)
{
dataStoreServiceMock.IsAvailable().Returns(expected);

bool actual = systemUnderTest.IsAvailable();
bool actual = await systemUnderTest.IsAvailable();

Assert.That(actual, Is.EqualTo(expected));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

using LazyCache;

Expand Down Expand Up @@ -39,7 +40,7 @@ public Team[] GetTeams()
return GetOrAdd(() => innerService.GetTeams(), DateTimeOffset.Now.AddHours(cacheDurationInHours));
}

public bool IsAvailable()
public Task<bool> IsAvailable()
{
return innerService.IsAvailable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using StatsDownload.Core.Interfaces;
using StatsDownload.Core.Interfaces.DataTransfer;
Expand Down Expand Up @@ -64,7 +65,7 @@ public Team[] GetTeams()
return teams;
}

public bool IsAvailable()
public Task<bool> IsAvailable()
{
return dataStoreService.IsAvailable();
}
Expand Down
3 changes: 1 addition & 2 deletions Api/StatsDownloadApi.Database/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public static class StatsDownloadApiDatabase
{
//GetFoldingMembersProcedureName,
//GetMembersProcedureName, GetTeamsProcedureName
GetValidatedFilesProcedureName,
ValidatedFilesViewName
GetValidatedFilesProcedureName, ValidatedFilesViewName
};

public static string GetValidatedFilesProcedureName =>
Expand Down
Loading

0 comments on commit c49813a

Please sign in to comment.