0

I would like to perform integration testing on my API which connects to an oracle database via EF Core (database-first). Initially, I went with Testcontainers.Oracle but it was running too slow on my machine. Hence, I decided to perform the test using a separate schema. I do not wish to use in-memory database to perform my integration tests.

I have two schemas:

  1. Schema1 (the original schema which the context was scaffolded from)
  2. Schema2 (I would like to create the tables in schema1 here and run my tests)

How do I override the default schema in my web application factory? Currently I have tried to check whether unit test is running in my source project but I would prefer do it inside the test project in case it is overwritten by a new scaffold.

public class MyApiFactory : WebApplicationFactory<IApiMarker>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureTestServices(services =>
        {
            services.RemoveAll(typeof(ApplicationDbContext));
            services.AddDbContext<ApplicationDbContext>(optionsBuilder =>
                optionsBuilder.UseOracle(""));

            // is it possible to change default schema of the model builder?
        });
    }
}

public partial class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<MyTable> MyTable{ get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("Schema1");

#if DEBUG
        if (UnitTestDetector.IsRunningFromXUnit)
        {
            modelBuilder.HasDefaultSchema("Schema2");
        }
#endif

        modelBuilder.Entity<MyTable1>(entity =>
        {
            // some table configuration
        });
    }
}
4
  • May I ask why not just run with actual oracle instance and rollback the transactions once the tests completed? Commented Oct 13, 2023 at 6:32
  • @XiangWeiHuang You are not wrong, that would work but I would personally prefer if there is a clear separation, similar like how we would spin up docker containers and dispose them at the end of the tests. Commented Oct 13, 2023 at 6:40
  • 1
    Change the current schema after connection instead, with ALTER SESSION SET CURRENT_SCHEMA=schema_other;. What you ask isn't clean to begin with - you're trying to use a schema, a logical namespace in the database, as if it was a different database. That's not its job though, which is why the schema is part of the model, not the configuration. I understand why you want to do that: in Oracle a database is equivalent to a different server instance, with different binaries and files. An Oracle schema is closer to a SQL Server database though. Commented Oct 13, 2023 at 12:05
  • @PanagiotisKanavos thank you for the explanation! I should look into setting up another instance for testing Commented Oct 13, 2023 at 15:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.