diff --git a/vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_de70e82a-c8cb-4f63-ad06-63af34ad4343.mdf b/vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_215c7893-c5fc-47bc-afdf-f074f29e87b0.mdf similarity index 98% rename from vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_de70e82a-c8cb-4f63-ad06-63af34ad4343.mdf rename to vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_215c7893-c5fc-47bc-afdf-f074f29e87b0.mdf index 24086b8..1299c84 100644 Binary files a/vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_de70e82a-c8cb-4f63-ad06-63af34ad4343.mdf and b/vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_215c7893-c5fc-47bc-afdf-f074f29e87b0.mdf differ diff --git a/vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_de70e82a-c8cb-4f63-ad06-63af34ad4343_log.ldf b/vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_215c7893-c5fc-47bc-afdf-f074f29e87b0_log.ldf similarity index 67% rename from vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_de70e82a-c8cb-4f63-ad06-63af34ad4343_log.ldf rename to vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_215c7893-c5fc-47bc-afdf-f074f29e87b0_log.ldf index b939ae8..a7fee1f 100644 Binary files a/vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_de70e82a-c8cb-4f63-ad06-63af34ad4343_log.ldf and b/vNext/test/BetterModules.Core.Database.Tests/App_Data/Temp/BetterModulesTestsDataSet_215c7893-c5fc-47bc-afdf-f074f29e87b0_log.ldf differ diff --git a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/DefaultUnitOfWorkFactoryIntegrationTests.cs b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/DefaultUnitOfWorkFactoryIntegrationTests.cs index 36313cb..6c774c8 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/DefaultUnitOfWorkFactoryIntegrationTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/DefaultUnitOfWorkFactoryIntegrationTests.cs @@ -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(); this.fixture = fixture; } @@ -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); } } } diff --git a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/DefaultUnitOfWorkTests.cs b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/DefaultUnitOfWorkTests.cs index 1191de8..dba3556 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/DefaultUnitOfWorkTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/DefaultUnitOfWorkTests.cs @@ -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; @@ -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(); + unitOfWork = provider.GetService(); this.fixture = fixture; } @@ -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(model1.Id); - var loadedModel2 = fixture.Repository.FirstOrDefault(model2.Id); + var loadedModel1 = repository.FirstOrDefault(model1.Id); + var loadedModel2 = repository.FirstOrDefault(model2.Id); Assert.Null(loadedModel1); Assert.Null(loadedModel2); @@ -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(model1.Id); - var loadedModel2 = fixture.Repository.FirstOrDefault(model2.Id); + var loadedModel1 = repository.FirstOrDefault(model1.Id); + var loadedModel2 = repository.FirstOrDefault(model2.Id); Assert.NotNull(loadedModel1); Assert.Null(loadedModel2); diff --git a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/EventListeners/DeleteEventListenerIntegrationTests.cs b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/EventListeners/DeleteEventListenerIntegrationTests.cs index 6b3292f..70a0dc9 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/EventListeners/DeleteEventListenerIntegrationTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/EventListeners/DeleteEventListenerIntegrationTests.cs @@ -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; @@ -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(); + unitOfWork = fixture.Provider.GetService(); } [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(); diff --git a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/EventListeners/SaveOrUpdateEventListenerTests.cs b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/EventListeners/SaveOrUpdateEventListenerTests.cs index 901a834..14da90a 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/EventListeners/SaveOrUpdateEventListenerTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/EventListeners/SaveOrUpdateEventListenerTests.cs @@ -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; @@ -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(); + unitOfWork = fixture.Provider.GetService(); } [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(); @@ -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(); @@ -46,10 +53,10 @@ public void Should_Update_Entity_Properties_When_Updating() var modified = entity.ModifiedOn; - var loadedEntity = fixture.Repository.FirstOrDefault(entity.Id); + var loadedEntity = repository.FirstOrDefault(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); diff --git a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/Interceptors/StaleInterceptorIntegrationTests.cs b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/Interceptors/StaleInterceptorIntegrationTests.cs index 1c84500..b8a4dac 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/Interceptors/StaleInterceptorIntegrationTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/Interceptors/StaleInterceptorIntegrationTests.cs @@ -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; @@ -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(); + unitOfWork = provider.GetService(); } [Fact] @@ -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); } @@ -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); } @@ -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(); }); } @@ -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(); }); } } diff --git a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/Migrations/DefaultVersionCheckerIntegrationTests.cs b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/Migrations/DefaultVersionCheckerIntegrationTests.cs index 8aa1159..0f08890 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/Migrations/DefaultVersionCheckerIntegrationTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/Migrations/DefaultVersionCheckerIntegrationTests.cs @@ -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; @@ -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(); this.fixture = fixture; } @@ -94,7 +98,7 @@ private DefaultVersionChecker GetVersionCheckerImplementation() var modulesRegistration = fixture.Provider.GetService(); var workingDirectory = fixture.Provider.GetService(); - var versionChecker = new DefaultVersionChecker(fixture.UnitOfWork, modulesRegistration, workingDirectory, new LoggerFactory()); + var versionChecker = new DefaultVersionChecker(unitOfWork, modulesRegistration, workingDirectory, new LoggerFactory()); return versionChecker; } diff --git a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/QueryOverExtensionsIntegrationTests.cs b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/QueryOverExtensionsIntegrationTests.cs index 060e9c7..a632c7f 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/QueryOverExtensionsIntegrationTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/QueryOverExtensionsIntegrationTests.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using BetterModules.Core.DataAccess; using BetterModules.Core.DataAccess.DataContext; using BetterModules.Core.Exceptions.DataTier; using BetterModules.Sample.Module.Models; @@ -11,7 +12,8 @@ namespace BetterModules.Core.Database.Tests.DataAccess.DataContext [Collection("Database test collection")] public class QueryOverExtensionsIntegrationTests { - private DatabaseTestFixture fixture; + private readonly IRepository repository; + private readonly IUnitOfWork unitOfWork; private TestItemCategory category1; private TestItemModel model1; private TestItemModel model2; @@ -20,7 +22,9 @@ public class QueryOverExtensionsIntegrationTests public QueryOverExtensionsIntegrationTests(DatabaseTestFixture fixture) { - this.fixture = fixture; + var provider = fixture.Services.BuildServiceProvider(); + repository = provider.GetService(); + unitOfWork = provider.GetService(); if (!isSet) { isSet = true; @@ -34,18 +38,18 @@ public QueryOverExtensionsIntegrationTests(DatabaseTestFixture fixture) model3 = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel(category1); model3.Name = "QVO_03"; - fixture.Repository.Save(model3); - fixture.Repository.Save(model2); - fixture.Repository.Save(model1); + repository.Save(model3); + repository.Save(model2); + repository.Save(model1); - fixture.UnitOfWork.Commit(); + unitOfWork.Commit(); } } [Fact] public void Should_Return_First_Element_Successfully() { - var query = fixture.Repository.AsQueryOver().Where(t => t.Id == model1.Id); + var query = repository.AsQueryOver().Where(t => t.Id == model1.Id); var item = query.First(); Assert.NotNull(item); @@ -58,7 +62,7 @@ public void Should_Throw_EntityNotFound_Exception_Retrieving_First() Assert.Throws(() => { var guid = Guid.NewGuid(); - fixture.Repository + repository .AsQueryOver() .Where(t => t.Id == guid) .First(); @@ -68,7 +72,7 @@ public void Should_Throw_EntityNotFound_Exception_Retrieving_First() [Fact] public void Should_Add_Default_Paging() { - var list = fixture.Repository + var list = repository .AsQueryOver() .Where(t => t.Category == category1) .AddPaging(null, null) @@ -85,7 +89,7 @@ public void Should_Add_Default_Paging() [Fact] public void Should_Return_First_Page_Of_Items() { - var list = fixture.Repository + var list = repository .AsQueryOver() .Where(t => t.Category == category1) .AddPaging(1, 2) @@ -101,7 +105,7 @@ public void Should_Return_First_Page_Of_Items() [Fact] public void Should_Return_Second_Page_Of_Items() { - var list = fixture.Repository + var list = repository .AsQueryOver() .Where(t => t.Category == category1) .AddPaging(2, 2) @@ -116,7 +120,7 @@ public void Should_Return_Second_Page_Of_Items() [Fact] public void Should_Apply_Filter_Order_Asc_Paging_Correctly() { - var list = fixture.Repository + var list = repository .AsQueryOver() .ApplyFilters(t => t.Category == category1, t => t.Name, false, 1, 2) .List() @@ -130,7 +134,7 @@ public void Should_Apply_Filter_Order_Asc_Paging_Correctly() [Fact] public void Should_Apply_Filter_Order_Desc_Paging_Correctly() { - var list = fixture.Repository + var list = repository .AsQueryOver() .ApplyFilters(t => t.Category == category1, t => t.Name, true, 1, 2) .List() @@ -144,7 +148,7 @@ public void Should_Apply_Filter_Order_Desc_Paging_Correctly() [Fact] public void Should_Apply_Filter_Order_Desc_No_Paging_Correctly() { - var list = fixture.Repository + var list = repository .AsQueryOver() .ApplyFilters(t => t.Category == category1, t => t.Name, true) .List() @@ -159,7 +163,7 @@ public void Should_Apply_Filter_Order_Desc_No_Paging_Correctly() [Fact] public void Should_Apply_SubQuery_Filter_Order_Desc_Paging_Correctly() { - var list = fixture.Repository + var list = repository .AsQueryOver() .ApplySubQueryFilters(t => t.Category == category1, t => t.Name, true, 1, 2) .List() @@ -173,7 +177,7 @@ public void Should_Apply_SubQuery_Filter_Order_Desc_Paging_Correctly() [Fact] public void Should_Apply_SubQuery_Filter_Order_Desc_No_Paging_Correctly() { - var list = fixture.Repository + var list = repository .AsQueryOver() .ApplySubQueryFilters(t => t.Category == category1, t => t.Name, true) .List() diff --git a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/QueryableExtensionsIntegrationTests.cs b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/QueryableExtensionsIntegrationTests.cs index e701e47..e62a139 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/QueryableExtensionsIntegrationTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/QueryableExtensionsIntegrationTests.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using BetterModules.Core.DataAccess; using BetterModules.Core.DataAccess.DataContext; using BetterModules.Sample.Module.Models; using Microsoft.Framework.DependencyInjection; @@ -12,9 +13,14 @@ namespace BetterModules.Core.Database.Tests.DataAccess.DataContext public class QueryableExtensionsIntegrationTests { private DatabaseTestFixture fixture; + private readonly IRepository repository; + private readonly IUnitOfWork unitOfWork; public QueryableExtensionsIntegrationTests(DatabaseTestFixture fixture) { + var provider = fixture.Services.BuildServiceProvider(); + repository = provider.GetService(); + unitOfWork = provider.GetService(); this.fixture = fixture; } @@ -37,12 +43,12 @@ public void Should_Return_Correct_Items_Future_Count() category2.Name = "QEIT_" + category1.Name.Substring(10); category3.Name = "QEIT_" + category1.Name.Substring(10); - fixture.Repository.Save(category1); - fixture.Repository.Save(category2); - fixture.Repository.Save(category3); - fixture.UnitOfWork.Commit(); + repository.Save(category1); + repository.Save(category2); + repository.Save(category3); + unitOfWork.Commit(); - var query = fixture.Repository.AsQueryable().Where(c => c.Name.StartsWith("QEIT_")); + var query = repository.AsQueryable().Where(c => c.Name.StartsWith("QEIT_")); var countFuture = query.ToRowCountFutureValue(); var future = query.ToFuture(); diff --git a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/RestrictionsExtensionsIntegrationTests.cs b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/RestrictionsExtensionsIntegrationTests.cs index 948e186..0c01085 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/RestrictionsExtensionsIntegrationTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DataContext/RestrictionsExtensionsIntegrationTests.cs @@ -1,4 +1,5 @@ -using BetterModules.Core.DataAccess.DataContext; +using BetterModules.Core.DataAccess; +using BetterModules.Core.DataAccess.DataContext; using BetterModules.Sample.Module.Models; using Microsoft.Framework.DependencyInjection; using NHibernate.Criterion; @@ -9,10 +10,15 @@ namespace BetterModules.Core.Database.Tests.DataAccess.DataContext [Collection("Database test collection")] public class RestrictionsExtensionsIntegrationTests { + private readonly IRepository repository; + private readonly IUnitOfWork unitOfWork; private DatabaseTestFixture fixture; public RestrictionsExtensionsIntegrationTests(DatabaseTestFixture fixture) { + var provider = fixture.Services.BuildServiceProvider(); + repository = provider.GetService(); + unitOfWork = provider.GetService(); this.fixture = fixture; } @@ -22,11 +28,11 @@ public void Should_Filter_Null_Or_Whitespace_Column_Correctly() var category = fixture.DatabaseTestDataProvider.ProvideRandomTestItemCategory(); category.Name = " "; - fixture.Repository.Save(category); - fixture.UnitOfWork.Commit(); + repository.Save(category); + unitOfWork.Commit(); TestItemCategory alias = null; - var loadedCategory = fixture.Repository + var loadedCategory = repository .AsQueryOver(() => alias) .Where(RestrictionsExtensions.IsNullOrWhiteSpace(Projections.Property(() => alias.Name))) .And(() => alias.Id == category.Id) diff --git a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DefaultRepositoryIntegrationTests.cs b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DefaultRepositoryIntegrationTests.cs index 1d094d7..93ff3b6 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DefaultRepositoryIntegrationTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DataAccess/DefaultRepositoryIntegrationTests.cs @@ -23,9 +23,14 @@ public class DefaultRepositoryIntegrationTests private TestItemModel deletedModel; private bool isSet; private DatabaseTestFixture fixture; + private readonly IRepository repository; + private readonly IUnitOfWork unitOfWork; public DefaultRepositoryIntegrationTests(DatabaseTestFixture fixture) { this.fixture = fixture; + var provider = fixture.Services.BuildServiceProvider(); + repository = provider.GetService(); + unitOfWork = provider.GetService(); if (!isSet) { isSet = true; @@ -41,25 +46,25 @@ public DefaultRepositoryIntegrationTests(DatabaseTestFixture fixture) deletedModel = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel(category1); deletedModel.Name = "DRT_04"; - fixture.Repository.Save(model3); - fixture.Repository.Save(deletedModel); - fixture.Repository.Save(model2); - fixture.Repository.Save(model1); - fixture.UnitOfWork.Commit(); + repository.Save(model3); + repository.Save(deletedModel); + repository.Save(model2); + repository.Save(model1); + unitOfWork.Commit(); - fixture.Repository.Delete(deletedModel); - fixture.UnitOfWork.Commit(); + repository.Delete(deletedModel); + unitOfWork.Commit(); } } [Fact] public void Should_UnProxy_Entity() { - var proxy = fixture.Repository.AsProxy(SampleModuleDescriptor.TestItemModelId); + var proxy = repository.AsProxy(SampleModuleDescriptor.TestItemModelId); Assert.NotNull(proxy); Assert.True(proxy is IProxy); - var unproxy = fixture.Repository.UnProxy(proxy); + var unproxy = repository.UnProxy(proxy); Assert.NotNull(unproxy); Assert.Equal(unproxy.Id, SampleModuleDescriptor.TestItemModelId); Assert.False(unproxy is IProxy); @@ -68,10 +73,10 @@ public void Should_UnProxy_Entity() [Fact] public void Should_Return_Same_Entity_When_UnProxying_Entity() { - var proxy = fixture.Repository.AsProxy(model1.Id); + var proxy = repository.AsProxy(model1.Id); Assert.NotNull(proxy); - var unproxy = fixture.Repository.UnProxy(proxy); + var unproxy = repository.UnProxy(proxy); Assert.NotNull(unproxy); Assert.Equal(unproxy, proxy); @@ -80,7 +85,7 @@ public void Should_Return_Same_Entity_When_UnProxying_Entity() [Fact] public void Should_Load_Entity_AsProxy() { - var proxy = fixture.Repository.AsProxy(Guid.NewGuid()); + var proxy = repository.AsProxy(Guid.NewGuid()); Assert.NotNull(proxy); Assert.True(proxy is IProxy); @@ -89,7 +94,7 @@ public void Should_Load_Entity_AsProxy() [Fact] public void Should_Retrieve_First_Entity_By_Id() { - var entity = fixture.Repository.First(model1.Id); + var entity = repository.First(model1.Id); Assert.NotNull(entity); Assert.Equal(entity.Id, model1.Id); @@ -100,7 +105,7 @@ public void Should_Throw_Exception_Retrieving_First_Entity_By_Id() { Assert.Throws(() => { - fixture.Repository.First(Guid.NewGuid()); + repository.First(Guid.NewGuid()); }); } @@ -108,7 +113,7 @@ public void Should_Throw_Exception_Retrieving_First_Entity_By_Id() [Fact] public void Should_Retrieve_First_Entity_By_Filter() { - var entity = fixture.Repository.First(m => m.Id == model1.Id); + var entity = repository.First(m => m.Id == model1.Id); Assert.NotNull(entity); Assert.Equal(entity.Id, model1.Id); @@ -120,14 +125,14 @@ public void Should_Throw_Exception_Retrieving_First_Entity_By_Filter() Assert.Throws(() => { var guid = Guid.NewGuid(); - fixture.Repository.First(m => m.Id == guid); + repository.First(m => m.Id == guid); }); } [Fact] public void Should_Retrieve_FirstOrDefault_Entity_By_Id() { - var entity = fixture.Repository.FirstOrDefault(model1.Id); + var entity = repository.FirstOrDefault(model1.Id); Assert.NotNull(entity); Assert.Equal(entity.Id, model1.Id); @@ -136,7 +141,7 @@ public void Should_Retrieve_FirstOrDefault_Entity_By_Id() [Fact] public void Should_Retrieve_FirstOrDefault_Entity_By_Filter() { - var entity = fixture.Repository.FirstOrDefault(m => m.Id == model1.Id); + var entity = repository.FirstOrDefault(m => m.Id == model1.Id); Assert.NotNull(entity); Assert.Equal(entity.Id, model1.Id); @@ -146,7 +151,7 @@ public void Should_Retrieve_FirstOrDefault_Entity_By_Filter() public void Should_Retrieve_Null_Retrieving_FirstOrDefault_Entity_By_Id() { var guid = Guid.NewGuid(); - var entity = fixture.Repository.FirstOrDefault(guid); + var entity = repository.FirstOrDefault(guid); Assert.Null(entity); } @@ -155,7 +160,7 @@ public void Should_Retrieve_Null_Retrieving_FirstOrDefault_Entity_By_Id() public void Should_Retrieve_Null_Retrieving_FirstOrDefault_Entity_By_Filter() { var guid = Guid.NewGuid(); - var entity = fixture.Repository.FirstOrDefault(m => m.Id == guid); + var entity = repository.FirstOrDefault(m => m.Id == guid); Assert.Null(entity); } @@ -163,7 +168,7 @@ public void Should_Retrieve_Null_Retrieving_FirstOrDefault_Entity_By_Filter() [Fact] public void Should_Return_QueryOver_Without_Deleted_Generic() { - var list = fixture.Repository + var list = repository .AsQueryOver().Where(q => q.Category == category1) .OrderBy(q => q.Name).Asc .List(); @@ -179,7 +184,7 @@ public void Should_Return_QueryOver_Without_Deleted_Generic() public void Should_Return_QueryOver_Without_Deleted_By_Alias() { TestItemModel alias = null; - var list = fixture.Repository + var list = repository .AsQueryOver(() => alias) .Where(() => alias.Category == category1) .OrderBy(q => q.Name).Asc @@ -196,7 +201,7 @@ public void Should_Return_QueryOver_Without_Deleted_By_Alias() public void Should_Return_QueryOver_Without_Deleted_By_Null_Alias() { TestItemModel alias = null; - var list = fixture.Repository + var list = repository .AsQueryOver(null) .Where(t => t.Category == category1) .OrderBy(q => q.Name).Asc @@ -212,7 +217,7 @@ public void Should_Return_QueryOver_Without_Deleted_By_Null_Alias() [Fact] public void Should_Return_Queryable_By_Filter() { - var list = fixture.Repository + var list = repository .AsQueryable(q => q.Category == category1) .OrderBy(q => q.Name) .ToList(); @@ -227,7 +232,7 @@ public void Should_Return_Queryable_By_Filter() [Fact] public void Should_Return_Queryable_Without_Deleted() { - var list = fixture.Repository + var list = repository .AsQueryable() .Where(q => q.Category == category1) .OrderBy(q => q.Name) @@ -243,7 +248,7 @@ public void Should_Return_Queryable_Without_Deleted() [Fact] public void Should_Check_If_Record_Exists() { - var exists = fixture.Repository.Any(q => q.Name == model1.Name); + var exists = repository.Any(q => q.Name == model1.Name); Assert.True(exists); } @@ -251,7 +256,7 @@ public void Should_Check_If_Record_Exists() [Fact] public void Should_Check_If_Deleted_Record_Not_Exists() { - var exists = fixture.Repository.Any(q => q.Name == deletedModel.Name); + var exists = repository.Any(q => q.Name == deletedModel.Name); Assert.False(exists); } @@ -261,8 +266,8 @@ public void Should_Save_Entity() { var model = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel(); - fixture.Repository.Save(model); - fixture.UnitOfWork.Commit(); + repository.Save(model); + unitOfWork.Commit(); Assert.True(model.Id != default(Guid)); } @@ -272,15 +277,15 @@ public void Should_Delete_Entity_By_Id_NotAsProxy() { var model = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel(); - fixture.Repository.Save(model); - fixture.UnitOfWork.Commit(); + repository.Save(model); + unitOfWork.Commit(); Assert.True(model.Id != default(Guid)); - fixture.Repository.Delete(model.Id, model.Version, false); - fixture.UnitOfWork.Commit(); + repository.Delete(model.Id, model.Version, false); + unitOfWork.Commit(); - var exists = fixture.Repository.Any(q => q.Id == model.Id); + var exists = repository.Any(q => q.Id == model.Id); Assert.False(exists); } @@ -289,15 +294,15 @@ public void Should_Delete_Entity_By_Id_AsProxy() { var model = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel(); - fixture.Repository.Save(model); - fixture.UnitOfWork.Commit(); + repository.Save(model); + unitOfWork.Commit(); Assert.True(model.Id != default(Guid)); - fixture.Repository.Delete(model.Id, model.Version, true); - fixture.UnitOfWork.Commit(); + repository.Delete(model.Id, model.Version, true); + unitOfWork.Commit(); - var exists = fixture.Repository.Any(q => q.Id == model.Id); + var exists = repository.Any(q => q.Id == model.Id); Assert.False(exists); } @@ -306,15 +311,15 @@ public void Should_Delete_Entity() { var model = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel(); - fixture.Repository.Save(model); - fixture.UnitOfWork.Commit(); + repository.Save(model); + unitOfWork.Commit(); Assert.True(model.Id != default(Guid)); - fixture.Repository.Delete(model); - fixture.UnitOfWork.Commit(); + repository.Delete(model); + unitOfWork.Commit(); - var exists = fixture.Repository.Any(q => q.Id == model.Id); + var exists = repository.Any(q => q.Id == model.Id); Assert.False(exists); } @@ -323,25 +328,25 @@ public void Should_Attach_Entity() { // Create entity var model = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel(); - fixture.Repository.Save(model); - fixture.UnitOfWork.Commit(); + repository.Save(model); + unitOfWork.Commit(); var version = model.Version; // Load detached version, touch multiple times - var detachedModel = fixture.Repository.First(model.Id); - fixture.Repository.Detach(detachedModel); + var detachedModel = repository.First(model.Id); + repository.Detach(detachedModel); detachedModel.Name = fixture.TestDataProvider.ProvideRandomString(); - fixture.UnitOfWork.Commit(); + unitOfWork.Commit(); detachedModel.Name = fixture.TestDataProvider.ProvideRandomString(); - fixture.UnitOfWork.Commit(); + unitOfWork.Commit(); Assert.Equal(detachedModel.Version, version); // Attach and save again - fixture.Repository.Attach(detachedModel); + repository.Attach(detachedModel); detachedModel.Name = fixture.TestDataProvider.ProvideRandomString(); - fixture.UnitOfWork.Commit(); + unitOfWork.Commit(); Assert.NotEqual(detachedModel.Version, version); } @@ -351,24 +356,24 @@ public void Should_Detach_Entity() { // Create entity var model = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel(); - fixture.Repository.Save(model); - fixture.UnitOfWork.Commit(); + repository.Save(model); + unitOfWork.Commit(); // Touch entity - changes should be saved on flush var version = model.Version; - var attachedModel = fixture.Repository.First(model.Id); + var attachedModel = repository.First(model.Id); attachedModel.Name = fixture.TestDataProvider.ProvideRandomString(); - fixture.UnitOfWork.Commit(); + unitOfWork.Commit(); Assert.True(attachedModel.Version > version); version = attachedModel.Version; // Detach and touch again - changes shouldn't saved on flush - var detachedModel = fixture.Repository.First(model.Id); - fixture.Repository.Detach(detachedModel); + var detachedModel = repository.First(model.Id); + repository.Detach(detachedModel); detachedModel.Name = fixture.TestDataProvider.ProvideRandomString(); - fixture.UnitOfWork.Commit(); + unitOfWork.Commit(); Assert.Equal(detachedModel.Version, version); } @@ -378,18 +383,18 @@ public void Should_Refresh_Entity() { // Create entity var model = fixture.DatabaseTestDataProvider.ProvideRandomTestItemModel(); - fixture.Repository.Save(model); - fixture.UnitOfWork.Commit(); + repository.Save(model); + unitOfWork.Commit(); var version = model.Version; // Load attached and detached version, touch multiple times - var detachedModel = fixture.Repository.First(model.Id); + var detachedModel = repository.First(model.Id); // Open another session var provider = fixture.Services.BuildServiceProvider(); - var configuration = provider.GetService>().Options; - configuration.Database.ConnectionString = fixture.ConnectionString; + //var configuration = provider.GetService>().Options; + //configuration.Database.ConnectionString = fixture.ConnectionString; var repository2 = provider.GetService(); var unitOfWork2 = provider.GetService(); @@ -400,7 +405,7 @@ public void Should_Refresh_Entity() Assert.Equal(detachedModel.Version, version); // Refresh detached entity - version should be updated - fixture.Repository.Refresh(detachedModel); + repository.Refresh(detachedModel); Assert.NotEqual(detachedModel.Version, version); } } diff --git a/vNext/test/BetterModules.Core.Database.Tests/DatabaseTestFixture.cs b/vNext/test/BetterModules.Core.Database.Tests/DatabaseTestFixture.cs index 7cecd3d..d7f995a 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/DatabaseTestFixture.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/DatabaseTestFixture.cs @@ -3,8 +3,6 @@ using System.Data.SqlClient; using System.Reflection; using BetterModules.Core.Configuration; -using BetterModules.Core.DataAccess; -using BetterModules.Core.DataAccess.DataContext; using BetterModules.Core.Database.Tests.TestHelpers; using BetterModules.Core.Database.Tests.TestHelpers.Migrations; using BetterModules.Core.Extensions; @@ -14,7 +12,6 @@ using Microsoft.Framework.Configuration; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Logging; -using Microsoft.Framework.OptionsModel; using Moq; namespace BetterModules.Core.Database.Tests @@ -29,21 +26,15 @@ public class DatabaseTestFixture: IDisposable private bool started; - private IRepository repository; - - private IUnitOfWork unitOfWork; - public IServiceCollection Services { get; set; } public IServiceProvider Provider { get; set; } public SqlConnection SqlConnection => database.SqlConnection; - public IRepository Repository => repository ?? (repository = Provider.GetService()); - - public IUnitOfWork UnitOfWork => unitOfWork ?? (unitOfWork = Provider.GetService()); + public virtual DatabaseRandomTestDataProvider DatabaseTestDataProvider => dbTestDataProvider ?? (dbTestDataProvider = new DatabaseRandomTestDataProvider()); - public string ConnectionString => database?.ConnectionString; + public virtual RandomTestDataProvider TestDataProvider => testDataProvider ?? (testDataProvider = new RandomTestDataProvider()); public DatabaseTestFixture() { @@ -82,20 +73,19 @@ private void InitializeServices() }); services.AddBetterModulesCore(builder.Build()); Services = services; - Provider = services.BuildServiceProvider(); + Provider = Services.BuildServiceProvider(); } private void InitializeDatabase() { database = TestDatabaseInitializer.RunDatabaseMigrationTests(Provider); - - var configuration = Provider.GetService>().Options; - configuration.Database.ConnectionString = database.ConnectionString; + Services.Configure(opt => + { + opt.Database.ConnectionString = database.ConnectionString; + }); + Provider = Services.BuildServiceProvider(); } - public virtual DatabaseRandomTestDataProvider DatabaseTestDataProvider => dbTestDataProvider ?? (dbTestDataProvider = new DatabaseRandomTestDataProvider()); - - public virtual RandomTestDataProvider TestDataProvider => testDataProvider ?? (testDataProvider = new RandomTestDataProvider()); public void Dispose() { database?.Dispose(); diff --git a/vNext/test/BetterModules.Core.Database.Tests/Models/EntityMapBaseIntegrationTests.cs b/vNext/test/BetterModules.Core.Database.Tests/Models/EntityMapBaseIntegrationTests.cs index 3717a72..1f40dea 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/Models/EntityMapBaseIntegrationTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/Models/EntityMapBaseIntegrationTests.cs @@ -1,4 +1,6 @@ using System; +using BetterModules.Core.DataAccess; +using BetterModules.Core.DataAccess.DataContext; using BetterModules.Sample.Module; using BetterModules.Sample.Module.Models; using Microsoft.Framework.DependencyInjection; @@ -9,18 +11,19 @@ namespace BetterModules.Core.Database.Tests.Models [Collection("Database test collection")] public class EntityMapBaseIntegrationTests { - private DatabaseTestFixture fixture; + private readonly IRepository repository; public EntityMapBaseIntegrationTests(DatabaseTestFixture fixture) { - this.fixture = fixture; + var provider = fixture.Services.BuildServiceProvider(); + repository = provider.GetService(); } [Fact] public void Should_Load_And_Map_BaseEntity_Correctly() { - var category = fixture.Repository.FirstOrDefault(c => c.Name == "ItemCategory1"); - var item = fixture.Repository.FirstOrDefault(SampleModuleDescriptor.TestItemModelId); + var category = repository.FirstOrDefault(c => c.Name == "ItemCategory1"); + var item = repository.FirstOrDefault(SampleModuleDescriptor.TestItemModelId); // Base properties Assert.Equal(item.Id, SampleModuleDescriptor.TestItemModelId); diff --git a/vNext/test/BetterModules.Core.Database.Tests/Models/EntitySubClassMapBaseIntergrationTests.cs b/vNext/test/BetterModules.Core.Database.Tests/Models/EntitySubClassMapBaseIntergrationTests.cs index 1c38f8f..a992ea4 100644 --- a/vNext/test/BetterModules.Core.Database.Tests/Models/EntitySubClassMapBaseIntergrationTests.cs +++ b/vNext/test/BetterModules.Core.Database.Tests/Models/EntitySubClassMapBaseIntergrationTests.cs @@ -1,4 +1,5 @@ using System; +using BetterModules.Core.DataAccess; using BetterModules.Sample.Module; using BetterModules.Sample.Module.Models; using Microsoft.Framework.DependencyInjection; @@ -9,18 +10,19 @@ namespace BetterModules.Core.Database.Tests.Models [Collection("Database test collection")] public class EntitySubClassMapBaseIntergrationTests { - private DatabaseTestFixture fixture; + private readonly IRepository repository; public EntitySubClassMapBaseIntergrationTests(DatabaseTestFixture fixture) { - this.fixture = fixture; + var provider = fixture.Services.BuildServiceProvider(); + repository = provider.GetService(); } [Fact] public void Should_Load_And_Map_BaseEntity_Correctly() { - var category = fixture.Repository.FirstOrDefault(c => c.Name == "ItemCategory1"); - var item = fixture.Repository.FirstOrDefault(SampleModuleDescriptor.TestItemModelId); + var category = repository.FirstOrDefault(c => c.Name == "ItemCategory1"); + var item = repository.FirstOrDefault(SampleModuleDescriptor.TestItemModelId); Assert.NotNull(item); Assert.NotNull(category); diff --git a/vNext/test/BetterModules.Core.Tests/project.json b/vNext/test/BetterModules.Core.Tests/project.json index c2a4989..898712f 100644 --- a/vNext/test/BetterModules.Core.Tests/project.json +++ b/vNext/test/BetterModules.Core.Tests/project.json @@ -11,12 +11,11 @@ "xunit.runner.dnx": "2.1.0-beta5a-build188", "BetterModules.Core": "1.0.0-*", "BetterModules.Sample.Module": "1.0.0-*", - "Moq": "4.2.1506.2515", - "Microsoft.Framework.Configuration.EnvironmentVariables": "1.0.0-beta7" + "Moq": "4.2.1506.2515" }, "commands": { - "test": "xunit.runner.dnx --isTestMode=true" + "test": "xunit.runner.dnx" }, "frameworks": {