From 6f4bf2c2cef7b9500ad92e72065fa00628e6a327 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Thu, 21 Jun 2018 14:25:57 +0300 Subject: [PATCH] Added unit test projects for testbase & ef core. --- modules/blogging/Volo.Blogging.sln | 16 ++++++ .../Volo/Blogging/Blogs/IBlogRepository.cs | 2 - .../Blogging/Blogs/EfCoreBlogRepository.cs | 2 - ....Blogging.EntityFrameworkCore.Tests.csproj | 21 +++++++ .../Blogging/Blogs/BlogRepository_Tests.cs | 9 +++ .../BloggingEntityFrameworkCoreTestModule.cs | 55 +++++++++++++++++++ .../Volo.Blogging.TestBase.csproj | 25 +++++++++ .../Volo/Blogging/BloggingTestBase.cs | 27 +++++++++ .../Volo/Blogging/BloggingTestBaseModule.cs | 35 ++++++++++++ .../Volo/Blogging/BloggingTestDataBuilder.cs | 12 ++++ .../Blogging/Blogs/BlogRepository_Tests.cs | 25 +++++++++ 11 files changed, 225 insertions(+), 4 deletions(-) create mode 100644 modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo.Blogging.EntityFrameworkCore.Tests.csproj create mode 100644 modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo/Blogging/Blogs/BlogRepository_Tests.cs create mode 100644 modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo/Blogging/EntityFrameworkCore/BloggingEntityFrameworkCoreTestModule.cs create mode 100644 modules/blogging/test/Volo.Blogging.TestBase/Volo.Blogging.TestBase.csproj create mode 100644 modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestBase.cs create mode 100644 modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestBaseModule.cs create mode 100644 modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestDataBuilder.cs create mode 100644 modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/Blogs/BlogRepository_Tests.cs diff --git a/modules/blogging/Volo.Blogging.sln b/modules/blogging/Volo.Blogging.sln index 7bc5a0de5f..d50ace19a7 100644 --- a/modules/blogging/Volo.Blogging.sln +++ b/modules/blogging/Volo.Blogging.sln @@ -27,6 +27,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "app", "app", "{EB4FB44A-FE3 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{42BF26EF-B8C7-42DC-9FFB-3653109B7776}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{25B3A516-5C0D-42E3-9294-E8A9346CEE4B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Blogging.TestBase", "test\Volo.Blogging.TestBase\Volo.Blogging.TestBase.csproj", "{BBE8D8BE-1B24-49FE-86EF-3848D4BB6829}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Blogging.EntityFrameworkCore.Tests", "test\Volo.Blogging.EntityFrameworkCore.Tests\Volo.Blogging.EntityFrameworkCore.Tests.csproj", "{0B9AAD44-1FCF-4AF1-838F-A09446E98E37}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -73,6 +79,14 @@ Global {14409939-5A35-4145-A5C4-F3AED1617243}.Debug|Any CPU.Build.0 = Debug|Any CPU {14409939-5A35-4145-A5C4-F3AED1617243}.Release|Any CPU.ActiveCfg = Release|Any CPU {14409939-5A35-4145-A5C4-F3AED1617243}.Release|Any CPU.Build.0 = Release|Any CPU + {BBE8D8BE-1B24-49FE-86EF-3848D4BB6829}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BBE8D8BE-1B24-49FE-86EF-3848D4BB6829}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BBE8D8BE-1B24-49FE-86EF-3848D4BB6829}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BBE8D8BE-1B24-49FE-86EF-3848D4BB6829}.Release|Any CPU.Build.0 = Release|Any CPU + {0B9AAD44-1FCF-4AF1-838F-A09446E98E37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0B9AAD44-1FCF-4AF1-838F-A09446E98E37}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0B9AAD44-1FCF-4AF1-838F-A09446E98E37}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0B9AAD44-1FCF-4AF1-838F-A09446E98E37}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -88,6 +102,8 @@ Global {A4DBA051-7FB9-4AD7-B9B7-6810B2268122} = {42BF26EF-B8C7-42DC-9FFB-3653109B7776} {0D50B2EE-5F62-4C1D-B04D-56BE0CF75967} = {EB4FB44A-FE39-4245-9DAD-D6437BCE3870} {14409939-5A35-4145-A5C4-F3AED1617243} = {EB4FB44A-FE39-4245-9DAD-D6437BCE3870} + {BBE8D8BE-1B24-49FE-86EF-3848D4BB6829} = {25B3A516-5C0D-42E3-9294-E8A9346CEE4B} + {0B9AAD44-1FCF-4AF1-838F-A09446E98E37} = {25B3A516-5C0D-42E3-9294-E8A9346CEE4B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F2BAE819-78D4-407A-9201-22473B2850B0} diff --git a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Blogs/IBlogRepository.cs b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Blogs/IBlogRepository.cs index 335620d4cc..ade8d1be0e 100644 --- a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Blogs/IBlogRepository.cs +++ b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Blogs/IBlogRepository.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; diff --git a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Blogs/EfCoreBlogRepository.cs b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Blogs/EfCoreBlogRepository.cs index e36f2c3224..604f7ca8b5 100644 --- a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Blogs/EfCoreBlogRepository.cs +++ b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Blogs/EfCoreBlogRepository.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; diff --git a/modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo.Blogging.EntityFrameworkCore.Tests.csproj b/modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo.Blogging.EntityFrameworkCore.Tests.csproj new file mode 100644 index 0000000000..8e06e795d5 --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo.Blogging.EntityFrameworkCore.Tests.csproj @@ -0,0 +1,21 @@ + + + + netcoreapp2.1 + true + true + + + + + + + + + + + + + + + diff --git a/modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo/Blogging/Blogs/BlogRepository_Tests.cs b/modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo/Blogging/Blogs/BlogRepository_Tests.cs new file mode 100644 index 0000000000..b86945418c --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo/Blogging/Blogs/BlogRepository_Tests.cs @@ -0,0 +1,9 @@ +using Volo.Blogging.EntityFrameworkCore; + +namespace Volo.Blogging.Blogs +{ + public class BlogRepository_Tests : BlogRepository_Tests + { + + } +} diff --git a/modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo/Blogging/EntityFrameworkCore/BloggingEntityFrameworkCoreTestModule.cs b/modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo/Blogging/EntityFrameworkCore/BloggingEntityFrameworkCoreTestModule.cs new file mode 100644 index 0000000000..9cb4f176b5 --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.EntityFrameworkCore.Tests/Volo/Blogging/EntityFrameworkCore/BloggingEntityFrameworkCoreTestModule.cs @@ -0,0 +1,55 @@ +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Modularity; +using Volo.Blogging.EntityFrameworkCore.Volo.Blogging.EntityFrameworkCore; + +namespace Volo.Blogging.EntityFrameworkCore +{ + [DependsOn( + typeof(BloggingEntityFrameworkCoreModule), + typeof(BloggingTestBaseModule) + )] + public class BloggingEntityFrameworkCoreTestModule : AbpModule + { + private SqliteConnection _sqliteConnection; + + public override void ConfigureServices(IServiceCollection services) + { + _sqliteConnection = CreateDatabaseAndGetConnection(); + + services.Configure(options => + { + options.Configure(context => + { + context.DbContextOptions.UseSqlite(_sqliteConnection); + }); + }); + + services.AddAssemblyOf(); + } + + private static SqliteConnection CreateDatabaseAndGetConnection() + { + var connection = new SqliteConnection("Data Source=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder().UseSqlite(connection).Options; + using (var context = new BloggingDbContext(options)) + { + context.GetService().CreateTables(); + } + + return connection; + } + + public override void OnApplicationShutdown(ApplicationShutdownContext context) + { + _sqliteConnection.Dispose(); + } + } +} diff --git a/modules/blogging/test/Volo.Blogging.TestBase/Volo.Blogging.TestBase.csproj b/modules/blogging/test/Volo.Blogging.TestBase/Volo.Blogging.TestBase.csproj new file mode 100644 index 0000000000..d943e815cc --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.TestBase/Volo.Blogging.TestBase.csproj @@ -0,0 +1,25 @@ + + + + netcoreapp2.1 + true + true + + + + + + + + + + + + + + + + + + + diff --git a/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestBase.cs b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestBase.cs new file mode 100644 index 0000000000..abc5f4153b --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestBase.cs @@ -0,0 +1,27 @@ +using System; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Volo.Abp; +using Volo.Abp.Modularity; +using Volo.Abp.Users; + +namespace Volo.Blogging +{ + public abstract class BloggingTestBase : AbpIntegratedTest + where TStartupModule : IAbpModule + { + protected Guid? CurrentUserId { get; set; } + + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + + protected override void AfterAddApplication(IServiceCollection services) + { + var currentUser = Substitute.For(); + currentUser.Id.Returns(ci => CurrentUserId); + services.AddSingleton(currentUser); + } + } +} diff --git a/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestBaseModule.cs b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestBaseModule.cs new file mode 100644 index 0000000000..2e285e51a9 --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestBaseModule.cs @@ -0,0 +1,35 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; +using Volo.Abp.Autofac; +using Volo.Abp.Modularity; + +namespace Volo.Blogging +{ + [DependsOn( + typeof(BloggingDomainModule), + typeof(AbpTestBaseModule), + typeof(AbpAutofacModule) + )] + public class BloggingTestBaseModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + } + + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + SeedTestData(context); + } + + private static void SeedTestData(ApplicationInitializationContext context) + { + using (var scope = context.ServiceProvider.CreateScope()) + { + scope.ServiceProvider + .GetRequiredService() + .Build(); + } + } + } +} diff --git a/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestDataBuilder.cs b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestDataBuilder.cs new file mode 100644 index 0000000000..6b7a2f7a0d --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/BloggingTestDataBuilder.cs @@ -0,0 +1,12 @@ +using Volo.Abp.DependencyInjection; + +namespace Volo.Blogging +{ + public class BloggingTestDataBuilder : ITransientDependency + { + public void Build() + { + //TODO + } + } +} diff --git a/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/Blogs/BlogRepository_Tests.cs b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/Blogs/BlogRepository_Tests.cs new file mode 100644 index 0000000000..1a94490340 --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.TestBase/Volo/Blogging/Blogs/BlogRepository_Tests.cs @@ -0,0 +1,25 @@ +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Modularity; +using Xunit; + +namespace Volo.Blogging.Blogs +{ + public abstract class BlogRepository_Tests : BloggingTestBase + where TStartupModule : IAbpModule + { + protected IBlogRepository BlogRepository { get; } + + protected BlogRepository_Tests() + { + BlogRepository = GetRequiredService(); + } + + [Fact] + public async Task FindByShortNameAsync_Temp() + { + var blog = await BlogRepository.FindByShortNameAsync("default"); + blog.ShouldBeNull(); + } + } +} \ No newline at end of file