mirror of https://github.com/abpframework/abp
				
				
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							80 lines
						
					
					
						
							2.5 KiB
						
					
					
				
			
		
		
	
	
							80 lines
						
					
					
						
							2.5 KiB
						
					
					
				| using Microsoft.Data.Sqlite;
 | |
| using Microsoft.EntityFrameworkCore;
 | |
| using Microsoft.EntityFrameworkCore.Infrastructure;
 | |
| using Microsoft.EntityFrameworkCore.Storage;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using Acme.BookStore.EntityFrameworkCore;
 | |
| using Volo.Abp;
 | |
| using Volo.Abp.Autofac;
 | |
| using Volo.Abp.EntityFrameworkCore;
 | |
| using Volo.Abp.Modularity;
 | |
| 
 | |
| namespace Acme.BookStore
 | |
| {
 | |
|     [DependsOn(
 | |
|         typeof(BookStoreApplicationModule),
 | |
|         typeof(BookStoreEntityFrameworkCoreModule),
 | |
|         typeof(AbpAutofacModule),
 | |
|         typeof(AbpTestBaseModule)
 | |
|         )]
 | |
|     public class BookStoreApplicationTestModule : AbpModule
 | |
|     {
 | |
|         private SqliteConnection _sqliteConnection;
 | |
| 
 | |
|         public override void ConfigureServices(ServiceConfigurationContext context)
 | |
|         {
 | |
|             context.Services.AddAlwaysAllowAuthorization();
 | |
| 
 | |
|             ConfigureInMemorySqlite(context.Services);
 | |
| 
 | |
|             context.Services.AddAssemblyOf<BookStoreApplicationTestModule>();
 | |
|         }
 | |
| 
 | |
|         private void ConfigureInMemorySqlite(IServiceCollection services)
 | |
|         {
 | |
|             _sqliteConnection = CreateDatabaseAndGetConnection();
 | |
| 
 | |
|             services.Configure<AbpDbContextOptions>(options =>
 | |
|             {
 | |
|                 options.Configure(context => { context.DbContextOptions.UseSqlite(_sqliteConnection); });
 | |
|             });
 | |
|         }
 | |
| 
 | |
|         public override void OnApplicationInitialization(ApplicationInitializationContext context)
 | |
|         {
 | |
|             SeedTestData(context);
 | |
|         }
 | |
| 
 | |
|         public override void OnApplicationShutdown(ApplicationShutdownContext context)
 | |
|         {
 | |
|             _sqliteConnection.Dispose();
 | |
|         }
 | |
| 
 | |
|         private static SqliteConnection CreateDatabaseAndGetConnection()
 | |
|         {
 | |
|             var connection = new SqliteConnection("Data Source=:memory:");
 | |
|             connection.Open();
 | |
| 
 | |
|             var options = new DbContextOptionsBuilder<BookStoreDbContext>()
 | |
|                 .UseSqlite(connection)
 | |
|                 .Options;
 | |
| 
 | |
|             using (var context = new BookStoreDbContext(options))
 | |
|             {
 | |
|                 context.GetService<IRelationalDatabaseCreator>().CreateTables();
 | |
|             }
 | |
| 
 | |
|             return connection;
 | |
|         }
 | |
| 
 | |
|         private static void SeedTestData(ApplicationInitializationContext context)
 | |
|         {
 | |
|             using (var scope = context.ServiceProvider.CreateScope())
 | |
|             {
 | |
|                 scope.ServiceProvider
 | |
|                     .GetRequiredService<BookStoreTestDataBuilder>()
 | |
|                     .Build();
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| } |