Skip to content

Commit

Permalink
Merge branch 'tqd-not-deleted' of github.com:nmshd/backbone into tqd-…
Browse files Browse the repository at this point in the history
…not-deleted
  • Loading branch information
Dannyps committed Oct 13, 2023
2 parents 3b48d7f + 6ea0667 commit 273ac1e
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace Enmeshed.UnitTestTools.TestDoubles.Fakes;

public static class FakeDbContextFactory
{
public static (TContext arrangeContext, TContext assertionContext, TContext actContext)
CreateDbContexts<TContext>() where TContext : DbContext
CreateDbContexts<TContext>(SqliteConnection? connection = null) where TContext : DbContext
{
var connection = new SqliteConnection("DataSource=:memory:");
connection ??= CreateDbConnection();
connection.Open();

var options = new DbContextOptionsBuilder<TContext>()
Expand All @@ -27,4 +29,9 @@ public static (TContext arrangeContext, TContext assertionContext, TContext actC

return (arrangeContext, assertionContext, actContext);
}

public static SqliteConnection CreateDbConnection()
{
return new SqliteConnection("DataSource=:memory:");
}
}
1 change: 1 addition & 0 deletions Modules/Messages/src/Messages.Domain/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion Modules/Quotas/src/Quotas.Domain/Aggregates/ICreatedAt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface ICreatedAt
{
public DateTime CreatedAt { get; set; }
public DateTime CreatedAt { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<ItemGroup>
<ProjectReference Include="..\..\..\..\BuildingBlocks\src\UnitTestTools\UnitTestTools.csproj" />
<ProjectReference Include="..\..\..\Messages\src\Messages.Infrastructure\Messages.Infrastructure.csproj" />
<ProjectReference Include="..\..\src\Quotas.Application\Quotas.Application.csproj" />
<ProjectReference Include="..\..\src\Quotas.Infrastructure\Quotas.Infrastructure.csproj" />
</ItemGroup>
Expand Down
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;
}
}

0 comments on commit 273ac1e

Please sign in to comment.