Skip to content

Commit

Permalink
#3 Fixed all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Gerbenis committed Oct 8, 2015
1 parent d85db58 commit 74aefcf
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 164 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ namespace BetterModules.Core.Database.Tests.DataAccess.DataContext
[Collection("Database test collection")]
public class DefaultUnitOfWorkFactoryIntegrationTests
{
private readonly IUnitOfWork unitOfWork;
private DatabaseTestFixture fixture;

public DefaultUnitOfWorkFactoryIntegrationTests(DatabaseTestFixture fixture)
{
var provider = fixture.Services.BuildServiceProvider();
unitOfWork = provider.GetService<IUnitOfWork>();
this.fixture = fixture;
}

Expand All @@ -21,7 +24,7 @@ public void Should_Create_New_Unit_Of_Work()
var uow = factory.New();

Assert.NotNull(uow);
Assert.NotEqual(uow, fixture.UnitOfWork);
Assert.NotEqual(uow, unitOfWork);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using BetterModules.Core.DataAccess;
using BetterModules.Core.DataAccess.DataContext;
using BetterModules.Sample.Module.Models;
using Microsoft.Framework.DependencyInjection;
Expand All @@ -10,9 +11,14 @@ namespace BetterModules.Core.Database.Tests.DataAccess.DataContext
public class DefaultUnitOfWorkTests
{
private DatabaseTestFixture fixture;
private readonly IRepository repository;
private readonly IUnitOfWork unitOfWork;

public DefaultUnitOfWorkTests(DatabaseTestFixture fixture)
{
var provider = fixture.Services.BuildServiceProvider();
repository = provider.GetService<IRepository>();
unitOfWork = provider.GetService<IUnitOfWork>();
this.fixture = fixture;
}

Expand Down Expand Up @@ -70,20 +76,20 @@ public void Should_Rollback_Transaction_Successfully()

try
{
fixture.UnitOfWork.BeginTransaction();
unitOfWork.BeginTransaction();

fixture.Repository.Save(model1);
fixture.Repository.Save(model2);
repository.Save(model1);
repository.Save(model2);

fixture.UnitOfWork.Rollback();
unitOfWork.Rollback();
}
catch
{
// Do nothing here
}

var loadedModel1 = fixture.Repository.FirstOrDefault<TestItemModel>(model1.Id);
var loadedModel2 = fixture.Repository.FirstOrDefault<TestItemModel>(model2.Id);
var loadedModel1 = repository.FirstOrDefault<TestItemModel>(model1.Id);
var loadedModel2 = repository.FirstOrDefault<TestItemModel>(model2.Id);

Assert.Null(loadedModel1);
Assert.Null(loadedModel2);
Expand All @@ -97,21 +103,21 @@ public void Should_Commit_And_Rollback_Transactions_Successfully()

try
{
fixture.UnitOfWork.BeginTransaction();
fixture.Repository.Save(model1);
fixture.UnitOfWork.Commit();
unitOfWork.BeginTransaction();
repository.Save(model1);
unitOfWork.Commit();

fixture.UnitOfWork.BeginTransaction();
fixture.Repository.Save(model2);
fixture.UnitOfWork.Rollback();
unitOfWork.BeginTransaction();
repository.Save(model2);
unitOfWork.Rollback();
}
catch
{
// Do nothing here
}

var loadedModel1 = fixture.Repository.FirstOrDefault<TestItemModel>(model1.Id);
var loadedModel2 = fixture.Repository.FirstOrDefault<TestItemModel>(model2.Id);
var loadedModel1 = repository.FirstOrDefault<TestItemModel>(model1.Id);
var loadedModel2 = repository.FirstOrDefault<TestItemModel>(model2.Id);

Assert.NotNull(loadedModel1);
Assert.Null(loadedModel2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using BetterModules.Core.DataAccess;
using BetterModules.Core.DataAccess.DataContext;
using Microsoft.Framework.DependencyInjection;
using BetterModules.Core.Security;
using Xunit;
Expand All @@ -9,22 +11,26 @@ namespace BetterModules.Core.Database.Tests.DataAccess.DataContext.EventListener
public class DeleteEventListenerIntegrationTests
{
private DatabaseTestFixture fixture;
private readonly IRepository repository;
private readonly IUnitOfWork unitOfWork;

public DeleteEventListenerIntegrationTests(DatabaseTestFixture fixture)
{
this.fixture = fixture;
repository = fixture.Provider.GetService<IRepository>();
unitOfWork = fixture.Provider.GetService<IUnitOfWork>();
}

[Fact]
public void Should_Mark_Entity_As_Deleted()
{
var entity = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel();
fixture.Repository.Save(entity);
fixture.UnitOfWork.Commit();
repository.Save(entity);
unitOfWork.Commit();

Assert.NotSame(entity.Id.ToString(), Guid.Empty.ToString());
fixture.Repository.Delete(entity);
fixture.UnitOfWork.Commit();
repository.Delete(entity);
unitOfWork.Commit();

var principalProvider = fixture.Provider.GetService<IPrincipalProvider>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using BetterModules.Core.Security;
using System;
using BetterModules.Core.DataAccess;
using BetterModules.Core.DataAccess.DataContext;
using BetterModules.Core.Security;
using BetterModules.Sample.Module.Models;
using Microsoft.Framework.DependencyInjection;
using Xunit;
Expand All @@ -9,18 +12,22 @@ namespace BetterModules.Core.Database.Tests.DataAccess.DataContext.EventListener
public class SaveOrUpdateEventListenerTests
{
private DatabaseTestFixture fixture;
private readonly IRepository repository;
private readonly IUnitOfWork unitOfWork;

public SaveOrUpdateEventListenerTests(DatabaseTestFixture fixture)
{
this.fixture = fixture;
repository = fixture.Provider.GetService<IRepository>();
unitOfWork = fixture.Provider.GetService<IUnitOfWork>();
}

[Fact]
public void Should_Update_Entity_Properties_When_Creating()
{
var entity = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel();
fixture.Repository.Save(entity);
fixture.UnitOfWork.Commit();
repository.Save(entity);
unitOfWork.Commit();

var principalProvider = fixture.Provider.GetService<IPrincipalProvider>();

Expand All @@ -34,8 +41,8 @@ public void Should_Update_Entity_Properties_When_Creating()
public void Should_Update_Entity_Properties_When_Updating()
{
var entity = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel();
fixture.Repository.Save(entity);
fixture.UnitOfWork.Commit();
repository.Save(entity);
unitOfWork.Commit();

var principalProvider = fixture.Provider.GetService<IPrincipalProvider>();

Expand All @@ -46,10 +53,10 @@ public void Should_Update_Entity_Properties_When_Updating()

var modified = entity.ModifiedOn;

var loadedEntity = fixture.Repository.FirstOrDefault<TestItemModel>(entity.Id);
var loadedEntity = repository.FirstOrDefault<TestItemModel>(entity.Id);
loadedEntity.Name = fixture.TestDataProvider.ProvideRandomString(100);
fixture.Repository.Save(loadedEntity);
fixture.UnitOfWork.Commit();
repository.Save(loadedEntity);
unitOfWork.Commit();

Assert.NotNull(loadedEntity.CreatedOn);
Assert.Equal(loadedEntity.CreatedByUser, principalProvider.CurrentPrincipalName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BetterModules.Core.Exceptions.DataTier;
using BetterModules.Core.DataAccess;
using BetterModules.Core.DataAccess.DataContext;
using BetterModules.Core.Exceptions.DataTier;
using Microsoft.Framework.DependencyInjection;
using Xunit;

Expand All @@ -8,10 +10,15 @@ namespace BetterModules.Core.Database.Tests.DataAccess.DataContext.Interceptors
public class StaleInterceptorIntegrationTests
{
private DatabaseTestFixture fixture;
private readonly IRepository repository;
private readonly IUnitOfWork unitOfWork;

public StaleInterceptorIntegrationTests(DatabaseTestFixture fixture)
{
this.fixture = fixture;
var provider = fixture.Services.BuildServiceProvider();
repository = provider.GetService<IRepository>();
unitOfWork = provider.GetService<IUnitOfWork>();
}

[Fact]
Expand All @@ -21,8 +28,8 @@ public void Should_Create_Version()

Assert.Equal(model.Version, 0);

fixture.Repository.Save(model);
fixture.UnitOfWork.Commit();
repository.Save(model);
unitOfWork.Commit();

Assert.Equal(model.Version, 1);
}
Expand All @@ -34,19 +41,19 @@ public void Should_Increase_Version_If_Dirty()

Assert.Equal(model.Version, 0);

fixture.Repository.Save(model);
fixture.UnitOfWork.Commit();
repository.Save(model);
unitOfWork.Commit();

Assert.Equal(model.Version, 1);

fixture.Repository.Save(model);
fixture.UnitOfWork.Commit();
repository.Save(model);
unitOfWork.Commit();

Assert.Equal(model.Version, 1);

model.Name = fixture.TestDataProvider.ProvideRandomString();
fixture.Repository.Save(model);
fixture.UnitOfWork.Commit();
repository.Save(model);
unitOfWork.Commit();

Assert.Equal(model.Version, 2);
}
Expand All @@ -60,14 +67,14 @@ public void Should_Throw_Concurrent_Data_Exception_Saving()

Assert.Equal(model.Version, 0);

fixture.Repository.Save(model);
fixture.UnitOfWork.Commit();
repository.Save(model);
unitOfWork.Commit();

model.Name = fixture.TestDataProvider.ProvideRandomString();
model.Version = 3;

fixture.Repository.Save(model);
fixture.UnitOfWork.Commit();
repository.Save(model);
unitOfWork.Commit();
});
}

Expand All @@ -80,14 +87,14 @@ public void Should_Throw_Concurrent_Data_Exception_Deleting()

Assert.Equal(model.Version, 0);

fixture.Repository.Save(model);
fixture.UnitOfWork.Commit();
repository.Save(model);
unitOfWork.Commit();

model.Name = fixture.TestDataProvider.ProvideRandomString();
model.Version = 3;

fixture.Repository.Delete(model);
fixture.UnitOfWork.Commit();
repository.Delete(model);
unitOfWork.Commit();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using BetterModules.Core.DataAccess.DataContext;
using BetterModules.Core.DataAccess.DataContext.Migrations;
using BetterModules.Core.Environment.FileSystem;
using BetterModules.Core.Modules.Registration;
Expand All @@ -12,10 +13,13 @@ namespace BetterModules.Core.Database.Tests.DataAccess.DataContext.Migrations
[Collection("Database test collection")]
public class DefaultVersionCheckerTests
{
private readonly IUnitOfWork unitOfWork;
private DatabaseTestFixture fixture;

public DefaultVersionCheckerTests(DatabaseTestFixture fixture)
{
var provider = fixture.Services.BuildServiceProvider();
unitOfWork = provider.GetService<IUnitOfWork>();
this.fixture = fixture;
}

Expand Down Expand Up @@ -94,7 +98,7 @@ private DefaultVersionChecker GetVersionCheckerImplementation()
var modulesRegistration = fixture.Provider.GetService<IModulesRegistration>();
var workingDirectory = fixture.Provider.GetService<IWorkingDirectory>();

var versionChecker = new DefaultVersionChecker(fixture.UnitOfWork, modulesRegistration, workingDirectory, new LoggerFactory());
var versionChecker = new DefaultVersionChecker(unitOfWork, modulesRegistration, workingDirectory, new LoggerFactory());

return versionChecker;
}
Expand Down
Loading

0 comments on commit 74aefcf

Please sign in to comment.