Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Closes #252
  • Loading branch information
cd21h committed Oct 27, 2021
1 parent 6ae6991 commit b5f7d4e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Suteki.TardisBank.Tests.Model
using Domain;
using FluentAssertions;
using SharpArch.NHibernate;
using SharpArch.NHibernate.Impl;
using SharpArch.Testing.Xunit.NHibernate;
using Xunit;

Expand All @@ -33,40 +32,39 @@ protected override async Task LoadTestData(CancellationToken cancellationToken)

var child = parent.CreateChild("Leo", "leohadlow", "xxx");
await Session.SaveAsync(child, cancellationToken);
await FlushSessionAndEvict(child, cancellationToken);
await FlushSessionAndEvict(parent, cancellationToken);
await Session.FlushAndEvictAsync(cancellationToken, child, parent);
_childId = child.Id;
}

[Fact]
public async Task Should_be_able_to_add_schedule_to_account()
{
var childToTestOn = await _childRepository.GetAsync(_childId);
var childToTestOn = (await _childRepository.GetAsync(_childId))!;
childToTestOn.Should().NotBeNull();

childToTestOn.Account.AddPaymentSchedule(DateTime.UtcNow, Interval.Week, 10, "Weekly pocket money");
await FlushSessionAndEvict(childToTestOn);

var child = await _childRepository.GetAsync(_childId);
var child = (await _childRepository.GetAsync(_childId))!;
child.Should().NotBeNull();
child.Account.PaymentSchedules[0].Id.Should().BePositive("schedule was not persisted");
}

[Fact]
public async Task Should_be_able_to_add_transaction_to_account()
{
var childToTestOn = await _childRepository.GetAsync(_childId);
var childToTestOn = (await _childRepository.GetAsync(_childId))!;
childToTestOn.ReceivePayment(10, "Reward");
await FlushSessionAndEvict(childToTestOn);

var child = await _childRepository.GetAsync(_childId);
var child = (await _childRepository.GetAsync(_childId))!;
child.Account.Transactions[0].Id.Should().BePositive();
}

[Fact]
public async Task Should_be_able_to_create_and_retrieve_a_child()
{
var child = await _childRepository.GetAsync(_childId);
var child = (await _childRepository.GetAsync(_childId))!;
child.Name.Should().Be("Leo");
child.UserName.Should().Be(@"leohadlow");
child.ParentId.Should().Be(_parentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ namespace Suteki.TardisBank.Tests.Model
using MediatR;
using Moq;
using SharpArch.NHibernate;
using SharpArch.NHibernate.Impl;
using SharpArch.Testing.NHibernate;
using SharpArch.Testing.Xunit.NHibernate;
using Xunit;


public class MessageTests : TransientDatabaseTests<TransientDatabaseSetup>
{
int _userId;
Mock<IMediator> _mediator;
readonly Mock<IMediator> _mediator;

public MessageTests(TransientDatabaseSetup dbSetup) : base(dbSetup)
{
Expand All @@ -36,13 +34,13 @@ protected override async Task LoadTestData(CancellationToken cancellationToken)
public async Task Should_be_able_to_add_a_message_to_a_user()
{
var parentRepository = new LinqRepository<Parent, int>(TransactionManager);
User userToTestWith = await parentRepository.GetAsync(_userId);
User userToTestWith = (await parentRepository.GetAsync(_userId))!;

userToTestWith.SendMessage("some message", _mediator.Object);

await FlushSessionAndEvict(userToTestWith);

Parent parent = await parentRepository.GetAsync(_userId);
Parent parent = (await parentRepository.GetAsync(_userId))!;
parent.Messages.Count.Should().Be(1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Suteki.TardisBank.Tests.Model
using System.Threading.Tasks;
using Domain;
using FluentAssertions;
using SharpArch.NHibernate.Impl;
using SharpArch.NHibernate;
using SharpArch.Testing.Xunit.NHibernate;
using Xunit;

Expand All @@ -30,15 +30,15 @@ protected override async Task LoadTestData(CancellationToken cancellationToken)
public async Task Should_be_able_to_add_a_child_to_a_parent()
{
var linqRepository = new LinqRepository<Parent, int>(TransactionManager);
Parent savedParent = await linqRepository.GetAsync(_parentId);
Parent savedParent = (await linqRepository.GetAsync(_parentId))!;
savedParent.Should().NotBeNull();

savedParent.CreateChild("jim", "jim123", "passw0rd1");
savedParent.CreateChild("jenny", "jenny123", "passw0rd2");
savedParent.CreateChild("jez", "jez123", "passw0rd3");
await FlushSessionAndEvict(savedParent);

Parent parent = await linqRepository.GetAsync(_parentId);
Parent parent = (await linqRepository.GetAsync(_parentId))!;
parent.Children.Count.Should().Be(3);

parent.Children[0].Name.Should().Be("jim");
Expand All @@ -49,7 +49,7 @@ public async Task Should_be_able_to_add_a_child_to_a_parent()
[Fact]
public async Task Should_be_able_to_create_and_retrieve_Parent()
{
Parent parent = await new LinqRepository<Parent, int>(TransactionManager).GetAsync(_parentId);
Parent parent = (await new LinqRepository<Parent, int>(TransactionManager).GetAsync(_parentId))!;
parent.Should().NotBeNull();
parent.Name.Should().Be("Mike Hadlow");
parent.UserName.Should().Be("mike@yahoo.com");
Expand Down
1 change: 0 additions & 1 deletion Samples/TardisBank/Src/Suteki.TardisBank.WebApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Suteki.TardisBank.WebApi
using SharpArch.Domain.PersistenceSupport;
using SharpArch.NHibernate;
using SharpArch.NHibernate.Extensions.DependencyInjection;
using SharpArch.NHibernate.Impl;
using SharpArch.Web.AspNetCore.Transaction;

#if NETCOREAPP2_1
Expand Down
33 changes: 30 additions & 3 deletions Src/SharpArch.NHibernate/SessionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,34 @@ public static async Task FlushAndEvictAsync(this ISession session, object entity

// Evicts the entity from the current session so that it can be loaded during testing;
// this gives the test a clean slate, if you will, to work with
await session.EvictAsync(entity, cancellationToken);
await session.EvictAsync(entity, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Flushes session and evict entities from the session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <param name="entities">Entities.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="session" /> or <paramref name="entities" /> or it's item is <c>null</c>.
/// </exception>
public static async Task FlushAndEvictAsync(this ISession session, CancellationToken cancellationToken, params object[] entities)
{
if (session == null) throw new ArgumentNullException(nameof(session));
if (entities == null) throw new ArgumentNullException(nameof(entities));

// Commits any changes up to this point to the database
await session.FlushAsync(cancellationToken).ConfigureAwait(false);

for (var i = entities.Length - 1; i >= 0; i--)
{
var entity = entities[i];

if (entity == null)
throw new ArgumentNullException(nameof(entities), $"Item at index {i} it null.");
await session.EvictAsync(entity, cancellationToken).ConfigureAwait(false);
}
}

/// <summary>
Expand All @@ -72,7 +99,7 @@ public static Task IncrementVersionAsync(this ISession session, object? entity,

// don't process deleted entity.
var entry = session.GetSessionImplementation().PersistenceContext.GetEntry(entity);
if (entry == null || entry.Status == Status.Deleted || entry.Status == Status.Gone) return Task.CompletedTask;
if (entry == null || entry.Status is Status.Deleted or Status.Gone) return Task.CompletedTask;
return session.LockAsync(entity, LockMode.Force, cancellationToken);
}

Expand All @@ -91,7 +118,7 @@ public static void IncrementVersion(this ISession session, object? entity)

// ignore deleted entity
var entry = session.GetSessionImplementation().PersistenceContext.GetEntry(entity);
if (entry == null || entry.Status == Status.Deleted || entry.Status == Status.Gone) return;
if (entry == null || entry.Status is Status.Deleted or Status.Gone) return;
session.Lock(entity, LockMode.Force);
}

Expand Down

0 comments on commit b5f7d4e

Please sign in to comment.