-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tqd-not-deleted' of github.com:nmshd/backbone into tqd-…
…not-deleted
- Loading branch information
Showing
5 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
public interface ICreatedAt | ||
{ | ||
public DateTime CreatedAt { get; set; } | ||
public DateTime CreatedAt { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
Modules/Quotas/test/Quotas.Application.Tests/Tests/Repositories/MessagesRepositoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
using Backbone.Modules.Messages.Domain.Entities; | ||
using Backbone.Modules.Messages.Infrastructure.Persistence.Database; | ||
using Backbone.Modules.Quotas.Domain.Aggregates.Identities; | ||
using Backbone.Modules.Quotas.Infrastructure.Persistence.Database; | ||
using Backbone.Modules.Quotas.Infrastructure.Persistence.Repository; | ||
using Enmeshed.DevelopmentKit.Identity.ValueObjects; | ||
using Enmeshed.Tooling; | ||
using Enmeshed.UnitTestTools.Data; | ||
using Enmeshed.UnitTestTools.TestDoubles.Fakes; | ||
using FluentAssertions; | ||
using FluentAssertions.Execution; | ||
using Xunit; | ||
|
||
namespace Backbone.Modules.Quotas.Application.Tests.Tests.Repositories; | ||
public class MessagesRepositoryTests | ||
{ | ||
private readonly IdentityAddress _identityAddress1 = TestDataGenerator.CreateRandomIdentityAddress(); | ||
private readonly IdentityAddress _identityAddress2 = TestDataGenerator.CreateRandomIdentityAddress(); | ||
|
||
private readonly MessagesDbContext _messagesArrangeContext; | ||
private readonly QuotasDbContext _actContext; | ||
|
||
private static readonly DateTime YESTERDAY = DateTime.UtcNow.AddDays(-1); | ||
private static readonly DateTime TOMORROW = DateTime.UtcNow.AddDays(1); | ||
private static readonly DateTime LAST_YEAR = DateTime.UtcNow.AddYears(-1); | ||
private static readonly DateTime NEXT_YEAR = DateTime.UtcNow.AddYears(1); | ||
|
||
public MessagesRepositoryTests() | ||
{ | ||
AssertionScope.Current.FormattingOptions.MaxLines = 1000; | ||
|
||
var connection = FakeDbContextFactory.CreateDbConnection(); | ||
(_messagesArrangeContext, _, _) = FakeDbContextFactory.CreateDbContexts<MessagesDbContext>(connection); | ||
(_, _, _actContext) = FakeDbContextFactory.CreateDbContexts<QuotasDbContext>(connection); | ||
} | ||
|
||
[Fact] | ||
public async Task Counts_entities_within_timeframe_hour_quotaPeriod() | ||
{ | ||
// Arrange | ||
var messages = new List<Message>() { | ||
CreateMessage(DateTime.Now, _identityAddress1), | ||
CreateMessage(YESTERDAY, _identityAddress1), | ||
CreateMessage(TOMORROW, _identityAddress1) | ||
}; | ||
await _messagesArrangeContext.Messages.AddRangeAsync(messages); | ||
await _messagesArrangeContext.SaveChangesAsync(); | ||
|
||
var repository = new MessagesRepository(_actContext); | ||
const QuotaPeriod quotaPeriod = QuotaPeriod.Hour; | ||
|
||
// Act | ||
var count = await repository.Count(_identityAddress1, quotaPeriod.CalculateBegin(), quotaPeriod.CalculateEnd(), CancellationToken.None); | ||
|
||
// Assert | ||
count.Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public async Task Counts_entities_within_timeframe_month_quotaPeriod() | ||
{ | ||
// Arrange | ||
var halfOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15); | ||
SystemTime.Set(halfOfMonth); | ||
|
||
var messages = new List<Message>() { | ||
CreateMessage(DateTime.Now, _identityAddress1), | ||
CreateMessage(YESTERDAY, _identityAddress1), | ||
CreateMessage(TOMORROW, _identityAddress1), | ||
CreateMessage(LAST_YEAR, _identityAddress1), | ||
CreateMessage(NEXT_YEAR, _identityAddress1) | ||
}; | ||
await _messagesArrangeContext.Messages.AddRangeAsync(messages); | ||
await _messagesArrangeContext.SaveChangesAsync(); | ||
|
||
var repository = new MessagesRepository(_actContext); | ||
const QuotaPeriod quotaPeriod = QuotaPeriod.Month; | ||
|
||
// Act | ||
var count = await repository.Count(_identityAddress1, quotaPeriod.CalculateBegin(), quotaPeriod.CalculateEnd(), CancellationToken.None); | ||
|
||
// Assert | ||
count.Should().Be(3); | ||
} | ||
|
||
[Fact] | ||
public async Task Counts_entities_total_quotaPeriod() | ||
{ | ||
// Arrange | ||
var messages = new List<Message>() { | ||
CreateMessage(DateTime.Now, _identityAddress1), | ||
CreateMessage(TOMORROW, _identityAddress1), | ||
CreateMessage(NEXT_YEAR, _identityAddress1) | ||
}; | ||
await _messagesArrangeContext.Messages.AddRangeAsync(messages); | ||
await _messagesArrangeContext.SaveChangesAsync(); | ||
|
||
var repository = new MessagesRepository(_actContext); | ||
const QuotaPeriod quotaPeriod = QuotaPeriod.Total; | ||
|
||
// Act | ||
var count = await repository.Count(_identityAddress1, quotaPeriod.CalculateBegin(), quotaPeriod.CalculateEnd(), CancellationToken.None); | ||
|
||
// Assert | ||
count.Should().Be(3); | ||
} | ||
|
||
[Fact] | ||
public async Task Counts_entities_only_for_requested_identityAddress() | ||
{ | ||
// Arrange | ||
var messages = new List<Message>() { | ||
CreateMessage(DateTime.Now, _identityAddress1), | ||
CreateMessage(TOMORROW, _identityAddress2), | ||
CreateMessage(NEXT_YEAR, _identityAddress1) | ||
}; | ||
await _messagesArrangeContext.Messages.AddRangeAsync(messages); | ||
await _messagesArrangeContext.SaveChangesAsync(); | ||
|
||
var repository = new MessagesRepository(_actContext); | ||
const QuotaPeriod quotaPeriod = QuotaPeriod.Total; | ||
|
||
// Act | ||
var count = await repository.Count(_identityAddress1, quotaPeriod.CalculateBegin(), quotaPeriod.CalculateEnd(), CancellationToken.None); | ||
|
||
// Assert | ||
count.Should().Be(2); | ||
} | ||
|
||
private static Message CreateMessage(DateTime createdAt, IdentityAddress identityAddress) | ||
{ | ||
SystemTime.Set(createdAt); | ||
|
||
var message = new Message( | ||
identityAddress, | ||
DeviceId.New(), | ||
null, | ||
Array.Empty<byte>(), | ||
new List<Attachment>(), | ||
new List<RecipientInformation>() | ||
); | ||
|
||
SystemTime.Reset(); | ||
|
||
return message; | ||
} | ||
} |