first commit
This commit is contained in:
@ -0,0 +1,9 @@
|
||||
using Xunit;
|
||||
|
||||
namespace KonSoft.Admin.EntityFrameworkCore;
|
||||
|
||||
[CollectionDefinition(AdminTestConsts.CollectionDefinitionName)]
|
||||
public class AdminEntityFrameworkCoreCollection : ICollectionFixture<AdminEntityFrameworkCoreFixture>
|
||||
{
|
||||
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
using KonSoft.Admin.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
namespace KonSoft.Admin.EntityFrameworkCore;
|
||||
|
||||
public class AdminEntityFrameworkCoreCollectionFixtureBase : ICollectionFixture<AdminEntityFrameworkCoreFixture>
|
||||
{
|
||||
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace KonSoft.Admin.EntityFrameworkCore;
|
||||
|
||||
public class AdminEntityFrameworkCoreFixture : IDisposable
|
||||
{
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
using Volo.Abp;
|
||||
|
||||
namespace KonSoft.Admin.EntityFrameworkCore;
|
||||
|
||||
public abstract class AdminEntityFrameworkCoreTestBase : AdminTestBase<AdminEntityFrameworkCoreTestModule>
|
||||
{
|
||||
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
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.EntityFrameworkCore.Sqlite;
|
||||
using Volo.Abp.FeatureManagement;
|
||||
using Volo.Abp.Modularity;
|
||||
using Volo.Abp.PermissionManagement;
|
||||
using Volo.Abp.SettingManagement;
|
||||
using Volo.Abp.Uow;
|
||||
|
||||
namespace KonSoft.Admin.EntityFrameworkCore;
|
||||
|
||||
[DependsOn(
|
||||
typeof(AdminApplicationTestModule),
|
||||
typeof(AdminEntityFrameworkCoreModule),
|
||||
typeof(AbpEntityFrameworkCoreSqliteModule)
|
||||
)]
|
||||
public class AdminEntityFrameworkCoreTestModule : AbpModule
|
||||
{
|
||||
private SqliteConnection? _sqliteConnection;
|
||||
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
Configure<FeatureManagementOptions>(options =>
|
||||
{
|
||||
options.SaveStaticFeaturesToDatabase = false;
|
||||
options.IsDynamicFeatureStoreEnabled = false;
|
||||
});
|
||||
Configure<PermissionManagementOptions>(options =>
|
||||
{
|
||||
options.SaveStaticPermissionsToDatabase = false;
|
||||
options.IsDynamicPermissionStoreEnabled = false;
|
||||
});
|
||||
Configure<SettingManagementOptions>(options =>
|
||||
{
|
||||
options.SaveStaticSettingsToDatabase = false;
|
||||
options.IsDynamicSettingStoreEnabled = false;
|
||||
});
|
||||
context.Services.AddAlwaysDisableUnitOfWorkTransaction();
|
||||
|
||||
ConfigureInMemorySqlite(context.Services);
|
||||
}
|
||||
|
||||
private void ConfigureInMemorySqlite(IServiceCollection services)
|
||||
{
|
||||
_sqliteConnection = CreateDatabaseAndGetConnection();
|
||||
|
||||
services.Configure<AbpDbContextOptions>(options =>
|
||||
{
|
||||
options.Configure(context =>
|
||||
{
|
||||
context.DbContextOptions.UseSqlite(_sqliteConnection);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public override void OnApplicationShutdown(ApplicationShutdownContext context)
|
||||
{
|
||||
_sqliteConnection?.Dispose();
|
||||
}
|
||||
|
||||
private static SqliteConnection CreateDatabaseAndGetConnection()
|
||||
{
|
||||
var connection = new AbpUnitTestSqliteConnection("Data Source=:memory:");
|
||||
connection.Open();
|
||||
|
||||
var options = new DbContextOptionsBuilder<AdminDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
using (var context = new AdminDbContext(options))
|
||||
{
|
||||
context.GetService<IRelationalDatabaseCreator>().CreateTables();
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
using KonSoft.Admin.Samples;
|
||||
using Xunit;
|
||||
|
||||
namespace KonSoft.Admin.EntityFrameworkCore.Applications;
|
||||
|
||||
[Collection(AdminTestConsts.CollectionDefinitionName)]
|
||||
public class EfCoreSampleAppServiceTests : SampleAppServiceTests<AdminEntityFrameworkCoreTestModule>
|
||||
{
|
||||
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
using KonSoft.Admin.Samples;
|
||||
using Xunit;
|
||||
|
||||
namespace KonSoft.Admin.EntityFrameworkCore.Domains;
|
||||
|
||||
[Collection(AdminTestConsts.CollectionDefinitionName)]
|
||||
public class EfCoreSampleDomainTests : SampleDomainTests<AdminEntityFrameworkCoreTestModule>
|
||||
{
|
||||
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Shouldly;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Volo.Abp.Identity;
|
||||
using Xunit;
|
||||
|
||||
namespace KonSoft.Admin.EntityFrameworkCore.Samples;
|
||||
|
||||
/* This is just an example test class.
|
||||
* Normally, you don't test ABP framework code
|
||||
* (like default AppUser repository IRepository<AppUser, Guid> here).
|
||||
* Only test your custom repository methods.
|
||||
*/
|
||||
[Collection(AdminTestConsts.CollectionDefinitionName)]
|
||||
public class SampleRepositoryTests : AdminEntityFrameworkCoreTestBase
|
||||
{
|
||||
private readonly IRepository<IdentityUser, Guid> _appUserRepository;
|
||||
|
||||
public SampleRepositoryTests()
|
||||
{
|
||||
_appUserRepository = GetRequiredService<IRepository<IdentityUser, Guid>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Query_AppUser()
|
||||
{
|
||||
/* Need to manually start Unit Of Work because
|
||||
* FirstOrDefaultAsync should be executed while db connection / context is available.
|
||||
*/
|
||||
await WithUnitOfWorkAsync(async () =>
|
||||
{
|
||||
//Act
|
||||
var adminUser = await (await _appUserRepository.GetQueryableAsync())
|
||||
.Where(u => u.UserName == "admin")
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
//Assert
|
||||
adminUser.ShouldNotBeNull();
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<Import Project="..\..\common.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>KonSoft.Admin</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\KonSoft.Admin.EntityFrameworkCore\KonSoft.Admin.EntityFrameworkCore.csproj" />
|
||||
<ProjectReference Include="..\KonSoft.Admin.Application.Tests\KonSoft.Admin.Application.Tests.csproj" />
|
||||
<PackageReference Include="Volo.Abp.EntityFrameworkCore.Sqlite" Version="8.3.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user