diff options
Diffstat (limited to 'slips/src/test/Marina.Test/Unit/DataAccess/Repositories')
4 files changed, 301 insertions, 0 deletions
diff --git a/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/CustomerRepositoryTest.cs b/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/CustomerRepositoryTest.cs new file mode 100644 index 0000000..afc21fc --- /dev/null +++ b/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/CustomerRepositoryTest.cs @@ -0,0 +1,142 @@ +using Marina.DataAccess;
+using Marina.DataAccess.DataMappers;
+using Marina.DataAccess.Repositories;
+using Marina.Domain.Interfaces;
+using Marina.Domain.Repositories;
+using MbUnit.Framework;
+using Rhino.Mocks;
+
+namespace Marina.Test.Unit.DataAccess.Repositories {
+ [TestFixture]
+ public class CustomerRepositoryTest {
+ private MockRepository _mockery;
+ private IIdentityMap< ICustomer > _mockIdentityMap;
+ private ICustomerDataMapper _mockMapper;
+
+ [SetUp]
+ public void Setup() {
+ _mockery = new MockRepository( );
+ _mockIdentityMap = _mockery.DynamicMock< IIdentityMap< ICustomer > >( );
+ _mockMapper = _mockery.DynamicMock< ICustomerDataMapper >( );
+ }
+
+ public ICustomerRepository CreateSUT() {
+ return CreateSUT( _mockIdentityMap, _mockMapper );
+ }
+
+ private ICustomerRepository CreateSUT( IIdentityMap< ICustomer > identityMap, ICustomerDataMapper mapper ) {
+ return new CustomerRepository( identityMap, mapper );
+ }
+
+ [Test]
+ public void Should_check_identity_map_to_see_if_customer_is_loaded() {
+ ICustomer customer = _mockery.DynamicMock< ICustomer >( );
+ long customerId = 23455;
+
+ using ( _mockery.Record( ) ) {
+ Expect.Call( _mockIdentityMap.ContainsObjectWithIdOf( customerId ) ).Return( true );
+ Expect.Call( _mockIdentityMap.FindObjectWithIdOf( customerId ) ).Return( customer );
+ }
+
+ using ( _mockery.Playback( ) ) {
+ Assert.AreEqual( customer, CreateSUT( ).FindBy( customerId ) );
+ }
+ }
+
+ [Test]
+ public void Should_leverage_mapper_to_load_customer_if_not_in_identity_map() {
+ ICustomer customer = _mockery.DynamicMock< ICustomer >( );
+ long customerId = 23455;
+
+ using ( _mockery.Record( ) ) {
+ SetupResult.For( _mockIdentityMap.ContainsObjectWithIdOf( customerId ) ).Return( false );
+ Expect.Call( _mockMapper.FindBy( customerId ) ).Return( customer );
+ }
+
+ using ( _mockery.Playback( ) ) {
+ Assert.AreEqual( customer, CreateSUT( ).FindBy( customerId ) );
+ }
+ }
+
+ [Test]
+ public void Should_add_customer_to_identity_map_after_retrieving_from_mapper() {
+ ICustomer customer = _mockery.DynamicMock< ICustomer >( );
+ long customerId = 23455;
+
+ using ( _mockery.Record( ) ) {
+ SetupResult.For( _mockIdentityMap.ContainsObjectWithIdOf( customerId ) ).Return( false );
+ SetupResult.For( _mockMapper.FindBy( customerId ) ).Return( customer );
+ _mockIdentityMap.Add( customer );
+ }
+
+ using ( _mockery.Playback( ) ) {
+ Assert.AreEqual( customer, CreateSUT( ).FindBy( customerId ) );
+ }
+ }
+
+ [Test]
+ public void Should_create_a_new_customer() {
+ using ( _mockery.Record( ) ) {}
+
+ using ( _mockery.Playback( ) ) {
+ ICustomer newCustomer = CreateSUT( ).NewCustomer( );
+ Assert.AreEqual( -1, newCustomer.ID( ) );
+ }
+ }
+
+ [Test]
+ public void Should_leverage_mapper_to_insert_new_customer() {
+ ICustomer customer = _mockery.DynamicMock< ICustomer >( );
+ long customerId = 34;
+
+ using ( _mockery.Record( ) ) {
+ SetupResult.For( customer.ID( ) ).Return( customerId );
+ SetupResult.For( _mockIdentityMap.ContainsObjectWithIdOf( customerId ) ).Return( false );
+
+ _mockMapper.Insert( customer );
+ }
+
+ using ( _mockery.Playback( ) ) {
+ CreateSUT( ).Save( customer );
+ }
+ }
+
+ [Test]
+ public void Should_add_new_customer_to_identity_map_after_insert() {
+ ICustomer customer = _mockery.DynamicMock< ICustomer >( );
+ long customerId = 34;
+
+ using ( _mockery.Record( ) ) {
+ SetupResult.For( customer.ID( ) ).Return( customerId );
+ SetupResult.For( _mockIdentityMap.ContainsObjectWithIdOf( customerId ) ).Return( false );
+ using ( _mockery.Ordered( ) ) {
+ _mockMapper.Insert( customer );
+ _mockIdentityMap.Add( customer );
+ }
+ }
+
+ using ( _mockery.Playback( ) ) {
+ CreateSUT( ).Save( customer );
+ }
+ }
+
+ [Test]
+ public void Should_leverage_mapper_to_update_customer() {
+ ICustomer customer = _mockery.DynamicMock< ICustomer >( );
+ IIdentityMap< ICustomer > identityMap = _mockery.CreateMock< IIdentityMap< ICustomer > >( );
+ ICustomerDataMapper dataMapper = _mockery.CreateMock< ICustomerDataMapper >( );
+ long customerId = 46;
+
+ using ( _mockery.Record( ) ) {
+ SetupResult.For( customer.ID( ) ).Return( customerId );
+ SetupResult.For( identityMap.ContainsObjectWithIdOf( customerId ) ).Return( true );
+
+ dataMapper.Update( customer );
+ }
+
+ using ( _mockery.Playback( ) ) {
+ CreateSUT( identityMap, dataMapper ).Save( customer );
+ }
+ }
+ }
+}
\ No newline at end of file diff --git a/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/DockRepositoryTest.cs b/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/DockRepositoryTest.cs new file mode 100644 index 0000000..5d17679 --- /dev/null +++ b/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/DockRepositoryTest.cs @@ -0,0 +1,38 @@ +using Marina.DataAccess.DataMappers;
+using Marina.DataAccess.Repositories;
+using Marina.Domain.Interfaces;
+using Marina.Domain.Repositories;
+using MbUnit.Framework;
+using Rhino.Mocks;
+
+namespace Marina.Test.Unit.DataAccess.Repositories {
+ [TestFixture]
+ public class DockRepositoryTest {
+ private MockRepository _mockery;
+ private IDockDataMapper mockMapper;
+
+ [SetUp]
+ public void Setup() {
+ _mockery = new MockRepository( );
+ mockMapper = _mockery.DynamicMock< IDockDataMapper >( );
+ }
+
+ public IDockRepository CreateSUT() {
+ return new DockRepository( mockMapper );
+ }
+
+ [Test]
+ public void Should_leverage_mapper_to_retrieve_dock_by_id() {
+ IDock dock = _mockery.DynamicMock< IDock >( );
+ long dockId = 2;
+
+ using ( _mockery.Record( ) ) {
+ Expect.Call( mockMapper.FindBy( dockId ) ).Return( dock );
+ }
+
+ using ( _mockery.Playback( ) ) {
+ Assert.AreEqual( dock, CreateSUT( ).FindBy( dockId ) );
+ }
+ }
+ }
+}
\ No newline at end of file diff --git a/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/IdentityMapTest.cs b/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/IdentityMapTest.cs new file mode 100644 index 0000000..9ff01c8 --- /dev/null +++ b/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/IdentityMapTest.cs @@ -0,0 +1,71 @@ +using Marina.DataAccess;
+using Marina.DataAccess.Exceptions;
+using Marina.Domain;
+using Marina.Domain.Interfaces;
+using MbUnit.Framework;
+
+namespace Marina.Test.Unit.DataAccess.Repositories {
+ [TestFixture]
+ public class IdentityMapTest {
+ public IIdentityMap< IDomainObject > CreateSUT() {
+ return new IdentityMap< IDomainObject >( );
+ }
+
+ // should not be able to add an object with an id of -1
+
+ [Test]
+ public void Should_be_able_to_add_a_new_domain_object_to_the_identitiy_map() {
+ IDomainObject objectThatHasBeenAddedToMap = new DomainObject( 1 );
+ IDomainObject objectThatHasNotBeenAddedToMap = new DomainObject( 2 );
+
+ IIdentityMap< IDomainObject > map = CreateSUT( );
+ map.Add( objectThatHasBeenAddedToMap );
+
+ Assert.IsTrue( map.ContainsObjectWithIdOf( objectThatHasBeenAddedToMap.ID( ) ) );
+ Assert.IsFalse( map.ContainsObjectWithIdOf( objectThatHasNotBeenAddedToMap.ID( ) ) );
+ }
+
+ [Test]
+ public void Should_return_true_if_searching_for_a_() {
+ IIdentityMap< IDomainObject > map = CreateSUT( );
+
+ int id = 1;
+ map.Add( new DomainObject( id ) );
+
+ Assert.IsTrue( map.ContainsObjectWithIdOf( id ) );
+ Assert.IsFalse( map.ContainsObjectWithIdOf( 2 ) );
+ }
+
+ [Test]
+ public void Should_be_able_to_retrieve_an_object_that_has_been_added_to_the_map() {
+ IIdentityMap< IDomainObject > map = CreateSUT( );
+ IDomainObject objectAddedToMap = new DomainObject( 1 );
+ map.Add( objectAddedToMap );
+
+ Assert.AreEqual( objectAddedToMap, map.FindObjectWithIdOf( 1 ) );
+ }
+
+ [Test]
+ public void Should_return_null_if_the_object_is_not_in_the_map() {
+ Assert.IsNull( CreateSUT( ).FindObjectWithIdOf( 1 ) );
+ }
+
+ [Test]
+ [ExpectedException( typeof( ObjectAlreadyAddedToIdentityMapException ) )]
+ public void Should_not_be_able_to_add_an_object_that_has_already_been_added() {
+ IIdentityMap< IDomainObject > map = CreateSUT( );
+ IDomainObject addedObject = new DomainObject( 1 );
+ map.Add( addedObject );
+ map.Add( addedObject );
+ }
+
+ [Test]
+ [ExpectedException( typeof( ObjectAlreadyAddedToIdentityMapException ) )]
+ public void Should_not_be_able_to_add_an_object_with_the_same_id_as_one_that_was_already_added() {
+ IIdentityMap< IDomainObject > map = CreateSUT( );
+ int id = 1;
+ map.Add( new DomainObject( id ) );
+ map.Add( new DomainObject( id ) );
+ }
+ }
+}
\ No newline at end of file diff --git a/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/SlipRepositoryTest.cs b/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/SlipRepositoryTest.cs new file mode 100644 index 0000000..31b5d7a --- /dev/null +++ b/slips/src/test/Marina.Test/Unit/DataAccess/Repositories/SlipRepositoryTest.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic;
+using Marina.DataAccess.DataMappers;
+using Marina.DataAccess.Repositories;
+using Marina.Domain.Interfaces;
+using Marina.Domain.Repositories;
+using Marina.Infrastructure;
+using MbUnit.Framework;
+using Rhino.Mocks;
+
+namespace Marina.Test.Unit.DataAccess.Repositories {
+ [TestFixture]
+ public class SlipRepositoryTest {
+ private MockRepository _mockery;
+ private ISlipDataMapper _mockMapper;
+
+ [SetUp]
+ public void Setup() {
+ _mockery = new MockRepository( );
+ _mockMapper = _mockery.DynamicMock< ISlipDataMapper >( );
+ }
+
+ public ISlipsRepository CreateSUT() {
+ return new SlipsRepository( _mockMapper );
+ }
+
+ [Test]
+ public void Should_leverage_mapper_to_convert_from_table_to_a_domain_object() {
+ ISlip unleasedSlip = _mockery.DynamicMock< ISlip >( );
+ ISlip leasedSlip = _mockery.DynamicMock< ISlip >( );
+
+ IList< ISlip > slips = new List< ISlip >( );
+ slips.Add( unleasedSlip );
+ slips.Add( leasedSlip );
+
+ using ( _mockery.Record( ) ) {
+ SetupResult.For( unleasedSlip.IsLeased( ) ).Return( false );
+ SetupResult.For( leasedSlip.IsLeased( ) ).Return( true );
+
+ Expect.Call( _mockMapper.AllSlips( ) ).Return( new RichEnumerable< ISlip >( slips ) );
+ }
+
+ using ( _mockery.Playback( ) ) {
+ IRichList< ISlip > foundSlips = ListFactory.From( CreateSUT( ).AllAvailableSlips( ) );
+
+ Assert.IsTrue( foundSlips.Contains( unleasedSlip ) );
+ Assert.IsFalse( foundSlips.Contains( leasedSlip ) );
+ }
+ }
+ }
+}
\ No newline at end of file |
