Skip to content

Commit

Permalink
All tests pass
Browse files Browse the repository at this point in the history
Add mapping overrides and Bidirectional properties for relationships :S
Some tests checking Ids where removed as I can't see the point of them with NH.
Add AssemblySetup for ServiceLocator and log4net
  • Loading branch information
seif committed Jan 31, 2012
1 parent 4a41878 commit 5f91113
Show file tree
Hide file tree
Showing 27 changed files with 190 additions and 70 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -21,4 +21,5 @@ error-*.xml
StyleCop.Cache
SpecificationReports
NugetWorkspace
[Pp]ackages
[Pp]ackages
[Ll]ogs
2 changes: 1 addition & 1 deletion Solutions/Suteki.TardisBank.Domain/Account.cs
Expand Up @@ -45,7 +45,7 @@ void RemoveOldTransactions()

public virtual void AddPaymentSchedule(DateTime startDate, Interval interval, decimal amount, string description)
{
PaymentSchedules.Add(new PaymentSchedule(startDate, interval, amount, description));
PaymentSchedules.Add(new PaymentSchedule(startDate, interval, amount, description, this));
}

public virtual void TriggerScheduledPayments(DateTime now)
Expand Down
6 changes: 5 additions & 1 deletion Solutions/Suteki.TardisBank.Domain/Message.cs
Expand Up @@ -6,10 +6,11 @@ namespace Suteki.TardisBank.Model

public class Message : Entity
{
public Message(DateTime date, string text)
public Message(DateTime date, string text, User user)
{
Date = date;
Text = text;
User = user;
HasBeenRead = false;
}

Expand All @@ -24,6 +25,9 @@ public virtual void Read()

public virtual DateTime Date { get; protected set; }
public virtual string Text { get; protected set; }

public virtual User User { get; set; }

public virtual bool HasBeenRead { get; protected set; }
}
}
30 changes: 7 additions & 23 deletions Solutions/Suteki.TardisBank.Domain/Parent.cs
Expand Up @@ -10,12 +10,12 @@ namespace Suteki.TardisBank.Model

public class Parent : User
{
public virtual IList<ChildProxy> Children { get; protected set; }
public virtual IList<Child> Children { get; protected set; }
public virtual string ActivationKey { get; protected set; }

public Parent(string name, string userName, string password) : base(name, userName, password)
{
Children = new List<ChildProxy>();
Children = new List<Child>();
}

protected Parent()
Expand All @@ -32,8 +32,8 @@ public virtual Parent Initialise()
public virtual Child CreateChild(string name, string userName, string password)
{
var child = new Child(name, userName, password, Id);
var childProxy = new ChildProxy(child.Id, name);
Children.Add(childProxy);

Children.Add(child);
return child;
}

Expand All @@ -53,7 +53,7 @@ public virtual void MakePaymentTo(Child child, decimal amount, string descriptio

public virtual bool HasChild(Child child)
{
return Children.Any(x => x.ChildId == child.Id);
return Children.Any(x => x == child);
}

public override void Activate()
Expand All @@ -64,32 +64,16 @@ public override void Activate()

public virtual bool HasChild(int childId)
{
return Children.Any(x => x.ChildId == childId);
return Children.Any(x => x.Id == childId);
}

public virtual void RemoveChild(int childId)
{
var childToRemove = Children.SingleOrDefault(x => x.ChildId == childId);
var childToRemove = Children.SingleOrDefault(x => x.Id == childId);
if (childToRemove != null)
{
Children.Remove(childToRemove);
}
}
}

public class ChildProxy : Entity
{
public ChildProxy(int childId, string name)
{
ChildId = childId;
Name = name;
}

protected ChildProxy()
{
}

public virtual int ChildId { get; protected set; }
public virtual string Name { get; protected set; }
}
}
5 changes: 4 additions & 1 deletion Solutions/Suteki.TardisBank.Domain/PaymentSchedule.cs
Expand Up @@ -6,12 +6,13 @@ namespace Suteki.TardisBank.Model

public class PaymentSchedule : Entity
{
public PaymentSchedule(DateTime nextRun, Interval interval, decimal amount, string description)
public PaymentSchedule(DateTime nextRun, Interval interval, decimal amount, string description, Account account)
{
NextRun = nextRun;
Interval = interval;
Amount = amount;
Description = description;
Account = account;
}

protected PaymentSchedule()
Expand All @@ -23,6 +24,8 @@ protected PaymentSchedule()
public virtual decimal Amount { get; protected set; }
public virtual string Description { get; protected set; }

public virtual Account Account { get; protected set; }

public virtual void CalculateNextRunDate()
{
switch (Interval)
Expand Down
2 changes: 1 addition & 1 deletion Solutions/Suteki.TardisBank.Domain/User.cs
Expand Up @@ -33,7 +33,7 @@ protected User(string name, string userName, string password)

public virtual void SendMessage(string text)
{
Messages.Add(new Message(DateTime.Now.Date, text));
Messages.Add(new Message(DateTime.Now.Date, text, this));
RemoveOldMessages();

DomainEvents.Raise(new SendMessageEvent(this, text));
Expand Down
@@ -0,0 +1,15 @@
namespace Suteki.TardisBank.Infrastructure.NHibernateMaps
{
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;

using Suteki.TardisBank.Model;

public class AccountMap : IAutoMappingOverride<Account>
{
public void Override(AutoMapping<Account> mapping)
{
mapping.HasMany(a => a.PaymentSchedules).Cascade.AllDeleteOrphan().Inverse();
}
}
}
@@ -0,0 +1,15 @@
namespace Suteki.TardisBank.Infrastructure.NHibernateMaps
{
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;

using Suteki.TardisBank.Model;

public class ChildMap : IAutoMappingOverride<Child>
{
public void Override(AutoMapping<Child> mapping)
{
mapping.References(c => c.Account).Cascade.All();
}
}
}
Expand Up @@ -11,6 +11,7 @@ public class PrimaryKeyConvention : IIdConvention
public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
{
instance.Column(instance.EntityType.Name + "Id");
instance.GeneratedBy.HiLo("1001");
}
}
}
@@ -0,0 +1,15 @@
namespace Suteki.TardisBank.Infrastructure.NHibernateMaps
{
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;

using Suteki.TardisBank.Model;

public class MessageMap : IAutoMappingOverride<Message>
{
public void Override(AutoMapping<Message> mapping)
{
mapping.References(m => m.User);
}
}
}
@@ -0,0 +1,15 @@
namespace Suteki.TardisBank.Infrastructure.NHibernateMaps
{
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;

using Suteki.TardisBank.Model;

public class PaymentScheduleMap : IAutoMappingOverride<PaymentSchedule>
{
public void Override(AutoMapping<PaymentSchedule> mapping)
{
mapping.References(p => p.Account);
}
}
}
@@ -0,0 +1,15 @@
namespace Suteki.TardisBank.Infrastructure.NHibernateMaps
{
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;

using Suteki.TardisBank.Model;

public class UserMap : IAutoMappingOverride<User>
{
public void Override(AutoMapping<User> mapping)
{
mapping.HasMany(u => u.Messages).Cascade.AllDeleteOrphan().Inverse();
}
}
}
Expand Up @@ -106,12 +106,17 @@
<Compile Include="..\..\Common\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="NHibernateMaps\AccountMap.cs" />
<Compile Include="NHibernateMaps\AutomappingConfiguration.cs" />
<Compile Include="NHibernateMaps\ChildMap.cs" />
<Compile Include="NHibernateMaps\Conventions\CustomForeignKeyConvention.cs" />
<Compile Include="NHibernateMaps\Conventions\HasManyConvention.cs" />
<Compile Include="NHibernateMaps\Conventions\PrimaryKeyConvention.cs" />
<Compile Include="NHibernateMaps\Conventions\TableNameConvention.cs" />
<Compile Include="NHibernateMaps\AutoPersistenceModelGenerator.cs" />
<Compile Include="NHibernateMaps\MessageMap.cs" />
<Compile Include="NHibernateMaps\PaymentScheduleMap.cs" />
<Compile Include="NHibernateMaps\UserMap.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Suteki.TardisBank.Domain\Suteki.TardisBank.Domain.csproj">
Expand Down
14 changes: 13 additions & 1 deletion Solutions/Suteki.TardisBank.Tasks/SchedulerService.cs
Expand Up @@ -4,6 +4,10 @@

namespace Suteki.TardisBank.Services
{
using NHibernate.Linq;

using SharpArch.NHibernate;

public interface ISchedulerService
{
void ExecuteUpdates(DateTime now);
Expand All @@ -20,7 +24,15 @@ public SchedulerService()
/// </summary>
public void ExecuteUpdates(DateTime now)
{
//TODO implement query Child/ByPendingSchedule .WhereLessThanOrEqual("NextRun", now)
var today = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);

var results = NHibernateSession.Current.Query<Child>().
Where(c => c.Account.PaymentSchedules.Any(p => p.NextRun < today)).Fetch(c => c.Account);

foreach (var child in results.ToList())
{
child.Account.TriggerScheduledPayments(now);
}
}
}
}
Expand Up @@ -75,9 +75,11 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Packages\Newtonsoft.Json.4.0.7\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NHibernate, Version=3.2.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL" />
<Reference Include="SharpArch.Domain">
<HintPath>..\..\Packages\SharpArch.Domain.2.0.0-RC2-612\lib\NET40\SharpArch.Domain.dll</HintPath>
</Reference>
<Reference Include="SharpArch.NHibernate, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.ComponentModel.DataAnnotations" />
Expand Down
16 changes: 14 additions & 2 deletions Solutions/Suteki.TardisBank.Tests/App.config
Expand Up @@ -22,9 +22,21 @@
</appender>
<root>
<!-- Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
<priority value="DEBUG" />
<priority value="WARN" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="LogToFile" />
</root>
</log4net>
<runtime></runtime>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.7.0" newVersion="4.0.7.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Castle.Windsor" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
15 changes: 15 additions & 0 deletions Solutions/Suteki.TardisBank.Tests/AssemblySetup.cs
@@ -0,0 +1,15 @@
namespace Suteki.TardisBank.Tests
{
using NUnit.Framework;

[SetUpFixture]
public class AssemblySetup
{
[SetUp]
public void RunBeforeAllTests()
{
log4net.Config.XmlConfigurator.Configure();
ServiceLocatorInitializer.Init();
}
}
}
24 changes: 21 additions & 3 deletions Solutions/Suteki.TardisBank.Tests/Model/ChildTests.cs
Expand Up @@ -4,22 +4,29 @@

namespace Suteki.TardisBank.Tests.Model
{
using System;

using SharpArch.Domain.PersistenceSupport;
using SharpArch.NHibernate;
using SharpArch.Testing.NUnit;
using SharpArch.Testing.NUnit.NHibernate;

[TestFixture]
public class ChildTests : RepositoryTestsBase
{
private int childId;

private int parentId;
protected override void LoadTestData()
{
var parent = new Parent("Mike Hadlow", "mike@yahoo.com", "yyy");
NHibernateSession.Current.Save(parent);

parentId = parent.Id;

var child = parent.CreateChild("Leo", "leohadlow", "xxx");
NHibernateSession.Current.Save(child);
RepositoryTestsHelper.FlushSessionAndEvict(child);
RepositoryTestsHelper.FlushSessionAndEvict(parent);
this.childId = child.Id;
}

Expand All @@ -29,11 +36,22 @@ public void Should_be_able_to_create_and_retrieve_a_child()
var child = new LinqRepository<Child>().Get(childId);
child.Name.ShouldEqual("Leo");
child.UserName.ShouldEqual("leohadlow");
child.Id.ShouldEqual("users/leohadlow");
child.ParentId.ShouldEqual("users/mike@yahoo.com");
child.ParentId.ShouldEqual(parentId);
child.Password.ShouldEqual("xxx");
child.Account.ShouldNotBeNull();
}

[Test]
public void Should_be_able_to_add_schedule_to_account()
{
var childRepository = new LinqRepository<Child>();
var childToTestOn = childRepository.Get(childId);
childToTestOn.Account.AddPaymentSchedule(DateTime.UtcNow, Interval.Week, 10, "Weekly pocket money");
FlushSessionAndEvict(childToTestOn);

var child = childRepository.Get(childId);
child.Account.PaymentSchedules[0].Id.ShouldBeGreaterThan(0);
}
}
}
// ReSharper restore InconsistentNaming

0 comments on commit 5f91113

Please sign in to comment.