SQLite Nunit Tests is showing me data seeded on Application DB Context

21 Views Asked by At

So I am Doing my unit tests with an in memory sql lite database obviously I cant see all the data at all not as if it was a .db file

However, when I am looking through my inspector on the local, I only see the expected data. But in the results view tag, I am seeing what can only be described as my test data being seeded on OnModelCreating.

So obviously, I have my DbContext set up like this, which would normally interact with SQL Server and Entity Framework.

 public class ApplicationDBContext : 
 IdentityDbContext<IdentityUser>
 {
 
  public ApplicationDBContext(DbContextOptions options) : 
  base(options)
  {
  }
   public DbSet<Ref_Category> Ref_Categorys { get; set; }
   public DbSet<Location> Locations { get; set; }
   public DbSet<Customer> Customers { get; set; }

   protected override void OnModelCreating(ModelBuilder 
   modelBuilder)
   {
     base.OnModelCreating(modelBuilder);

     modelBuilder.Entity<Customer>().HasData(new Customer { Id = 
     10, Forename = "The", Surname = "Doctor" 
    ,IsActive=true,IsDeleted=false});
    modelBuilder.Entity<Customer>().HasData(new Customer { Id = 
    11, Forename = "Martha", Surname = "Jones", IsActive = true, 
    IsDeleted = false });         
 }
}

Then My Unit test is as such

public class BookingServicesTests : IDisposable 
{
    private bool _useSqlite;
    private SqliteConnection _connection;
    private DbContextOptions _options;
    public BookingServicesTests()
    {
        _connection = new 
        SqliteConnection("datasource=:memory:");
        _connection.Open();
        _options = new DbContextOptionsBuilder()
            .UseSqlite(_connection)
            .Options;
        using (var context = new ApplicationDBContext(_options))
            context.Database.EnsureCreated();
    }

/// <summary>
/// Books the check out should decrease inventory of the same book.
/// </summary>
/// <param name="expectedCount">The expected count.</param>
[TestCase(9)]
public async Task Book_CheckOut_ShouldDecreaseByNumber(int expectedCount)
{

    using (var context = new ApplicationDBContext(_options))
    {
        context.BookInventories.Add(new BookInventory
        {

            BookId = 4,
            BarCode = "1111",
            InventoryCount = 10,
            IsActive = true,
            IsDeleted = false,

        });
        await context.SaveChangesAsync();

        using (var context2 = new ApplicationDBContext(_options))
        {
            var service = new BookManagerServices(context2);
            int bookId = 4;
            int customerId = 10;
            string barCode = "1111";

            // ACT
            var act = service.CheckOut(customerId, bookId, barCode, 7);


            // Assert
            Assert.AreEqual(act.Data, expectedCount);
        }
    }

}

My Main question is should the sql lite not just be seeing the record I created ie this one. Instead of all the test data?

context.BookInventories.Add(new BookInventory
{
  BookId = 4,
  BarCode = "1111",
  InventoryCount = 10,
  IsActive = true,
  IsDeleted = false,

});

Is it possible to switch to a file db just for local testing to see what's going on in the tables?

0

There are 0 best solutions below