Open Closed

Tests-in-a-layered-solution #8238


User avatar
0
dmeagor created

We have a project with a parent later and multiple sub-modules.

The tests in the sub-modules run just OK using the supplied sqlite seeder.

When I try to run the tests in the parent application layer I'm getting issues though as the underlying database schema has not been created.

To fix this I added a DependsOn --- typeof(MySubModuleTestBaseModule) and referenced the sub-test project. Unfortunatle whilst the sub-module seeder runs it errors due to the tables not exsiting despote being entities.

Please explain how theses tests are supposed to be linked up and how I correct this.

There is no documentation on this at all, only for the tests in a module.


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share a minimal project to show your problem?

    liming.ma@volosoft.com

    Thanks.

  • User Avatar
    0
    dmeagor created

    Sorry I didn't explain the issue very well. I've managed to figure out what was happening. Basicially we had two separate databases Shout (top layer) and Survey (module). Sqlite was destroying /overwriting the module later in this use case.

    Can you please confirm that this is the correct way to resolve the issue? If it is then it should probably be documented in your Integration Test setup section.

    #ShoutEntityFrameworkCoreTestModule.cs
    
    namespace Shout.EntityFrameworkCore;
    
    [DependsOn(
        typeof(ShoutEntityFrameworkCoreDbMigrationsModule),
        typeof(ShoutTestBaseModule),
        typeof(AbpEntityFrameworkCoreSqliteModule)
    )]
    public class ShoutEntityFrameworkCoreTestModule : AbpModule
    {
        private SqliteConnection _sqliteConnection;
    
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            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 SqliteConnection("Data Source=:memory:");
            connection.Open();
    
            // Configure logging for SurveyDbContext
            var surveyDbOptions = new DbContextOptionsBuilder<SurveyDbContext>()
                .UseSqlite(connection)
                .EnableSensitiveDataLogging() // Include parameter values in logs
                .LogTo(Console.WriteLine, LogLevel.Information) // Log SQL queries to console
                .Options;
    
            // Configure logging for ShoutMigrationsDbContext
            var shoutMigrationsDbOptions = new DbContextOptionsBuilder<ShoutMigrationsDbContext>()
                .UseSqlite(connection)
                .EnableSensitiveDataLogging()
                .LogTo(Console.WriteLine, LogLevel.Information)
                .Options;
    
            // Create tables with logging enabled
            new SurveyDbContext(surveyDbOptions).GetService<IRelationalDatabaseCreator>().CreateTables();
            new ShoutMigrationsDbContext(shoutMigrationsDbOptions).GetService<IRelationalDatabaseCreator>().CreateTables();
    
            return connection;
        }
    }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    If you have two DbContext in the unit test, the code is no problem

    new SurveyDbContext(surveyDbOptions).GetService<IRelationalDatabaseCreator>().CreateTables();
    new ShoutMigrationsDbContext(shoutMigrationsDbOptions).GetService<IRelationalDatabaseCreator>().CreateTables();
    

    https://github.com/abpframework/abp/blob/dev/modules/openiddict/test/Volo.Abp.OpenIddict.EntityFrameworkCore.Tests/Volo/Abp/OpenIddict/EntityFrameworkCore/OpenIddictEntityFrameworkCoreTestModule.cs#L38-L56

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09