Add unit tests for TokensService.cs
This commit is contained in:
parent
348e69cd88
commit
3da48bb65f
4 changed files with 109 additions and 13 deletions
|
@ -0,0 +1,84 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Driver;
|
||||
using Moq;
|
||||
using Retroactiune.Models;
|
||||
using Retroactiune.Services;
|
||||
using Retroactiune.Settings;
|
||||
using Xunit;
|
||||
|
||||
namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
|
||||
{
|
||||
public class TestTokensService
|
||||
{
|
||||
[Fact]
|
||||
public async Task Test_GenerateTokensAsync_InvalidNumberOfTokens()
|
||||
{
|
||||
// Setup
|
||||
var mongoDatabaseMock = new Mock<IMongoDatabase>();
|
||||
var mongoClientMock = new Mock<IMongoClient>();
|
||||
var mongoSettingsMock = new Mock<IMongoDbSettings>();
|
||||
var mongoCollectionMock = new Mock<IMongoCollection<Token>>();
|
||||
|
||||
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
|
||||
mongoSettingsMock.SetupGet(i => i.TokensCollectionName).Returns("tokens");
|
||||
|
||||
mongoClientMock
|
||||
.Setup(stub => stub.GetDatabase(It.IsAny<string>(),
|
||||
It.IsAny<MongoDatabaseSettings>()))
|
||||
.Returns(mongoDatabaseMock.Object);
|
||||
|
||||
mongoDatabaseMock
|
||||
.Setup(i => i.GetCollection<Token>(It.IsAny<string>(),
|
||||
It.IsAny<MongoCollectionSettings>()))
|
||||
.Returns(mongoCollectionMock.Object);
|
||||
|
||||
// Test & Assert
|
||||
var service = new TokensService(mongoClientMock.Object, mongoSettingsMock.Object);
|
||||
await Assert.ThrowsAsync<ArgumentException>(async () => { await service.GenerateTokensAsync(-1, ""); });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Test_GenerateTokensAsync_Success()
|
||||
{
|
||||
// Setup
|
||||
var mongoDatabaseMock = new Mock<IMongoDatabase>();
|
||||
var mongoClientMock = new Mock<IMongoClient>();
|
||||
var mongoSettingsMock = new Mock<IMongoDbSettings>();
|
||||
var mongoCollectionMock = new Mock<IMongoCollection<Token>>();
|
||||
|
||||
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
|
||||
mongoSettingsMock.SetupGet(i => i.TokensCollectionName).Returns("tokens");
|
||||
|
||||
mongoClientMock
|
||||
.Setup(stub => stub.GetDatabase(It.IsAny<string>(),
|
||||
It.IsAny<MongoDatabaseSettings>()))
|
||||
.Returns(mongoDatabaseMock.Object);
|
||||
|
||||
mongoDatabaseMock
|
||||
.Setup(i => i.GetCollection<Token>(It.IsAny<string>(),
|
||||
It.IsAny<MongoCollectionSettings>()))
|
||||
.Returns(mongoCollectionMock.Object);
|
||||
|
||||
// Test
|
||||
var expiryTime = DateTime.UtcNow;
|
||||
var service = new TokensService(mongoClientMock.Object, mongoSettingsMock.Object);
|
||||
await service.GenerateTokensAsync(3, "Hello", expiryTime);
|
||||
|
||||
// Assert
|
||||
var item = new Token
|
||||
{
|
||||
Id = null,
|
||||
ExpiryTime = null,
|
||||
TimeUsed = null,
|
||||
FeedbackReceiverId = "Hello",
|
||||
CreatedAt = expiryTime
|
||||
};
|
||||
mongoCollectionMock.Verify(
|
||||
i => i.InsertManyAsync(new[] {item, item, item},
|
||||
It.IsAny<InsertManyOptions>(),
|
||||
It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -27,7 +27,7 @@ namespace Retroactiune.Models
|
|||
return false;
|
||||
}
|
||||
|
||||
return Id.Equals(convertedObj.Id) && Name.Equals(convertedObj.Name) &&
|
||||
return string.Equals(Id, convertedObj.Id) && string.Equals(Name, convertedObj.Name) &&
|
||||
Description.Equals(convertedObj.Description) && CreatedAt.Equals(convertedObj.CreatedAt);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,13 +18,26 @@ namespace Retroactiune.Models
|
|||
[BsonRepresentation(BsonType.ObjectId), JsonPropertyName("feedback_receiver_id")]
|
||||
public string FeedbackReceiverId { get; set; }
|
||||
|
||||
[JsonPropertyName("time_used")]
|
||||
public DateTime? TimeUsed { get; set; }
|
||||
[JsonPropertyName("time_used")] public DateTime? TimeUsed { get; set; }
|
||||
|
||||
[JsonPropertyName("created_at")]
|
||||
public DateTime CreatedAt { get; set; }
|
||||
[JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; }
|
||||
|
||||
[JsonPropertyName("expiry_time")]
|
||||
public DateTime? ExpiryTime { get; set; }
|
||||
[JsonPropertyName("expiry_time")] public DateTime? ExpiryTime { get; set; }
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is Token convertedObj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return string.Equals(Id, convertedObj.Id) && string.Equals(FeedbackReceiverId, convertedObj.FeedbackReceiverId) &&
|
||||
TimeUsed == convertedObj.TimeUsed && ExpiryTime == convertedObj.ExpiryTime;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return base.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -20,7 +20,6 @@ namespace Retroactiune.Services
|
|||
public async Task GenerateTokensAsync(int numberOfTokens, string feedbackReceiverGuid,
|
||||
DateTime? expiryTime = null)
|
||||
{
|
||||
// TODO: Test unit
|
||||
if (numberOfTokens <= 0)
|
||||
{
|
||||
throw new ArgumentException("numberOfTokens must be positive");
|
||||
|
|
Loading…
Reference in a new issue