Unit Testing With InMemory Provider And SQLite In-Memory Database In EFCore

Writing Unit Test Cases is an essential aspect for any software development which helps us to test small building block an application. While doing development, we write test cases side by side, just parallel to development code. To test the Unit Test cases, we generally try to recreate the similar environment as we have in the development phase. So, that testing the code becomes easy. But every time, this is not feasible.

Writing Unit Test Cases is an essential aspect of any software development which helps us to test small building blocks of an application. While doing development, we write test cases side by side, just parallel to development code. To test the Unit Test cases, we generally try to recreate a similar environment as we have in the development phase so that testing the code becomes easy. But every time, this is not feasible. Let's say one example is if our application is using the database and we would like to test our application then we need a database similar to the dev database with all tables, views, and other components available inside that database at the time of testing. It is a very challenging task to maintain multiple databases for a single project, one for development, one for testing, and one for production. 

Download Code from here.

The question arises here, can we reduce the number of databases, at least testing databases, without affecting the work and generate the database on runtime and destroy it once the task is completed?

The answer to the above question is 'YES'. Yes, it is possible in Entity Framework Core to generate the database on runtime and use it, execute test cases, and destroy it. If you are working with Asp.Net Core and Entity Framework Core then Unit Testing is very easy. In Entity Framework Core, we have two packages which help us to create the database on runtime as similar to the actual database and its tables as well. It provides the similar database functionality without over heading the actual database. An objective of this article is to provide step by step information on how to write Unit Test Case for Entity Framework Core without the actual database being ready. We will see how to generate the database on runtime in memory with the help of two packages available in EF Core.

Here in Entity Framework Core, we have two different ways or we can say, we have available two packages, which help for generating the in-memory database on runtime as follows.

  1. In-Memory Provider
  2. SQLite In-Memory Database

Both In-Memory Provider and SQLite In-Memory Database create the database on runtime in memory space but the difference is that In-Memory does not support a Relational Database but SQLite supports it. Both are useful as per the scenario which best fits your requirement. So, let's see how we can implement In-Memory and SQLite in Entity Framework Core and use it while writing Unit Test Cases.

So, let's move to the practical demonstration without wasting much time. Just open Visual Studio 2017 or higher and create an ASP.NET Core Web API application project and add a xUnit Testing project in the same solution. The final solution structure will be similar to the below image. To know more about how to create ASP.NET Core Web API application and how to perform CRUD operations in ASP.NET Core Web API or how to write Unit Test Cases for ASP.NET Core Web API CURD operations, you can follow these articles.

Unit Testing With InMemory Provider And SQLite In-Memory Database In EFCore 

We hope you have created a similar solution structure as above. So, let's move and create one folder as "Model" inside the "InMemoryVsSQLiteDemo" project and create two model classes such as "Category" and "Post" along with one Db Context class as "BlogDBContext".

"Post" class will be similar to the following. Here you can see, we have multiple properties and we are marking "CategoryId" properties as Foreign Key for "Id" field which is pointing to "Category" model Id property.

  1. using System;  
  2. using System.ComponentModel.DataAnnotations.Schema;  
  3.   
  4. namespace InMemoryVsSQLiteDemo.Model  
  5. {  
  6.     public partial class Post  
  7.     {  
  8.         public int PostId { get; set; }  
  9.         public string Title { get; set; }  
  10.         public string Description { get; set; }  
  11.   
  12.         [ForeignKey("Id")]  
  13.         public int CategoryId { get; set; }  
  14.         public DateTime? CreatedDate { get; set; }          
  15.         public virtual Category Category { get; set; }  
  16.     }  
  17. }  

Following is the "Category" model class which has Id property as key, which shows that it is the primary key for the "Category" table and some other properties as well.

  1. using System.ComponentModel.DataAnnotations;  
  2.   
  3. namespace InMemoryVsSQLiteDemo.Model  
  4. {  
  5.     public partial class Category  
  6.     {  
  7.         [Key]  
  8.         public int Id { get; set; }  
  9.         public string Name { get; set; }  
  10.         public string Slug { get; set; }          
  11.     }  
  12. }  

Now, let's move to BlogDBContext class which is inheriting the DbContext class. Here, we have two DbSet properties for Category and Post models which will work as tables when we will perform a query to get data from runtime database.

  1. using System;  
  2. using Microsoft.EntityFrameworkCore;  
  3.   
  4. namespace InMemoryVsSQLiteDemo.Model  
  5. {  
  6.     public partial class BlogDBContext : DbContext  
  7.     {  
  8.         public BlogDBContext(){}  
  9.   
  10.         public BlogDBContext(DbContextOptions<BlogDBContext> options)  
  11.             : base(options){}  
  12.   
  13.         public virtual DbSet<Category> Category { get; set; }  
  14.         public virtual DbSet<Post> Post { get; set; }  
  15.     }  
  16. }  

So, we are not going to do much more inside the main project. Now, it's time to prepare the test project and install the required components to use In-Memory Provider and SQLite In-Memory Database. So, let's move to "InMemoryVsSQLiteDemo.Test" project and open NuGet Package Manager and install the following required packages.

  1. EntityFrameworkCore
  2. EntityFrameworkCore.InMemory
  3. EntityFrameworkCore.Sqlite

The first package will provide all the required classes for using Entity Framework Core functionality within the xUnit project and the rest are for In-Memory Provider and SQLite In-Memory Database respectively. These packages will help to create a database on runtime.

The first question which comes to mind is how will we get the instance of the BlogDBContext class, so that we can access the model classes. So to do that, right click on "Dependencies" of the "InMemoryVsSQLiteDemo.Test" project and add the main project reference.

Now, let's create one class as "ConnectionFactory" which checks the connection, generates the runtime database and returns the context for BlogDBContext. 

In the following class, we have two methods as "CreateContextForMemory" which will create a database on runtime with the name "Test_Database" and before returning the object of BlogDBContext, it ensures that database has been deleted from the memory with the similar name if available and regenerated again. We have another method as "CreateContextForSQLite" for SQLite which also returns the object of BlogDBContext after validating the connection string and ensuring that the database has deleted and been re-created in memory.

  1. using InMemoryVsSQLiteDemo.Model;  
  2. using Microsoft.Data.SQLite;  
  3. using Microsoft.EntityFrameworkCore;  
  4. using System;  
  5. using System.IO;  
  6.   
  7. namespace InMemoryVsSQLiteDemo.Test  
  8. {  
  9.     public class ConnectionFactory : IDisposable  
  10.     {  
  11.  
  12.         #region IDisposable Support  
  13.         private bool disposedValue = false// To detect redundant calls  
  14.   
  15.         public BlogDBContext CreateContextForInMemory()  
  16.         {  
  17.             var option = new DbContextOptionsBuilder<BlogDBContext>().UseInMemoryDatabase(databaseName: "Test_Database").Options;  
  18.   
  19.             var context = new BlogDBContext(option);  
  20.             if (context != null)  
  21.             {  
  22.                 context.Database.EnsureDeleted();  
  23.                 context.Database.EnsureCreated();  
  24.             }  
  25.   
  26.             return context;  
  27.         }  
  28.   
  29.         public BlogDBContext CreateContextForSQLite()  
  30.         {  
  31.             var connection = new SqliteConnection("DataSource=:memory:");  
  32.             connection.Open();  
  33.   
  34.             var option = new DbContextOptionsBuilder<BlogDBContext>().UseSqlite(connection).Options;  
  35.   
  36.             var context = new BlogDBContext(option);  
  37.               
  38.             if (context != null)  
  39.             {  
  40.                 context.Database.EnsureDeleted();  
  41.                 context.Database.EnsureCreated();  
  42.             }  
  43.   
  44.             return context;  
  45.         }  
  46.   
  47.   
  48.         protected virtual void Dispose(bool disposing)  
  49.         {  
  50.             if (!disposedValue)  
  51.             {  
  52.                 if (disposing)  
  53.                 {  
  54.                 }  
  55.   
  56.                 disposedValue = true;  
  57.             }  
  58.         }  
  59.   
  60.         public void Dispose()  
  61.         {  
  62.             Dispose(true);  
  63.         }  
  64.         #endregion  
  65.     }  
  66. }  

As the above Connection Factory class is returning the BlogDBContect class object for In-Memory Provider and SQLite In-Memory Database,  it's time to write some Unit Test Cases. Let's create a new class as "InMemoryDataProviderTest" where we will write the Unit Test Cases for In-Memory Provider. Here we are writing three Unit Test Case as follows.

Every test case has three portions as "Arrange", "Act" and "Assert". In the "Arrange" section, we are preparing the object of BlogDBContext from the ConnectionFactory class, in the "Act" section, we are executing the action to the database and in the "Assert" section, we are just checking the data and validating it. So, let's understand about these test case one by one as follows.

  1. Task_Add_Without_Relation: We are using the Relational Database, but in this test case, we are not passing data along with relational mapping and seeing data get saved into the database and test case pass successfully. It is because In-Memory does not support Relational Database.
  2. Task_Add_With_Relation: Here in this test case, we are passing data with relational mapping and seeing data get saved in the database again and test case passes successfully without any error. It is because In-Memory does not support Relational Database and it does not affect if Primary and Foreign key relationship data are not valid.
  3. Task_Add_Time_Test: We have written this test case to test the time taken by In-Memory Provider while adding 1000 records in one go. You see the time comparison between In-Memory and SQLite for executing the 1000 records in one go in the last image.

You can find the whole code as follows for In-Memory Provider.

  1. using InMemoryVsSQLiteDemo.Model;  
  2. using System;  
  3. using System.Linq;  
  4. using Xunit;  
  5.   
  6. namespace InMemoryVsSQLiteDemo.Test  
  7. {  
  8.     public class InMemoryDataProviderTest  
  9.     {  
  10.         [Fact]  
  11.         public void Task_Add_Without_Relation()  
  12.         {  
  13.             //Arrange    
  14.             var factory = new ConnectionFactory();  
  15.   
  16.             //Get the instance of BlogDBContext  
  17.             var context = factory.CreateContextForInMemory();  
  18.                           
  19.             var post = new Post() { Title = "Test Title 3", Description = "Test Description 3", CreatedDate = DateTime.Now };  
  20.   
  21.             //Act    
  22.             var data = context.Post.Add(post);  
  23.             context.SaveChanges();  
  24.   
  25.             //Assert    
  26.             //Get the post count  
  27.             var postCount = context.Post.Count();  
  28.             if (postCount != 0)  
  29.             {  
  30.                 Assert.Equal(1, postCount);  
  31.             }  
  32.   
  33.             //Get single post detail  
  34.             var singlePost = context.Post.FirstOrDefault();  
  35.             if (singlePost != null)  
  36.             {  
  37.                 Assert.Equal("Test Title 3", singlePost.Title);  
  38.             }  
  39.         }  
  40.   
  41.         [Fact]  
  42.         public void Task_Add_With_Relation()  
  43.         {  
  44.             //Arrange    
  45.             var factory = new ConnectionFactory();  
  46.   
  47.             //Get the instance of BlogDBContext  
  48.             var context = factory.CreateContextForInMemory();  
  49.   
  50.             var post = new Post() { Title = "Test Title 3", Description = "Test Description 3", CategoryId = 2, CreatedDate = DateTime.Now };  
  51.   
  52.             //Act    
  53.             var data = context.Post.Add(post);  
  54.             context.SaveChanges();  
  55.   
  56.             //Assert    
  57.             //Get the post count  
  58.             var postCount = context.Post.Count();  
  59.             if (postCount != 0)  
  60.             {  
  61.                 Assert.Equal(1, postCount);  
  62.             }  
  63.   
  64.             //Get single post detail  
  65.             var singlePost = context.Post.FirstOrDefault();  
  66.             if (singlePost != null)  
  67.             {  
  68.                 Assert.Equal("Test Title 3", singlePost.Title);  
  69.             }  
  70.         }  
  71.   
  72.         [Fact]  
  73.         public void Task_Add_Time_Test()  
  74.         {  
  75.             //Arrange    
  76.             var factory = new ConnectionFactory();  
  77.   
  78.             //Get the instance of BlogDBContext  
  79.             var context = factory.CreateContextForInMemory();  
  80.   
  81.             //Act   
  82.             for (int i =1; i<=1000; i++)  
  83.             {  
  84.                 var post = new Post() { Title = "Test Title "+i, Description = "Test Description "+i, CategoryId = 2, CreatedDate = DateTime.Now };                   
  85.                 context.Post.Add(post);                  
  86.             }  
  87.   
  88.             context.SaveChanges();  
  89.   
  90.   
  91.             //Assert    
  92.             //Get the post count  
  93.             var postCount = context.Post.Count();  
  94.             if (postCount != 0)  
  95.             {  
  96.                 Assert.Equal(1000, postCount);  
  97.             }  
  98.   
  99.             //Get single post detail  
  100.             var singlePost = context.Post.Where(x=>x.PostId == 1).FirstOrDefault();  
  101.             if (singlePost != null)  
  102.             {  
  103.                 Assert.Equal("Test Title 1", singlePost.Title);  
  104.             }  
  105.         }  
  106.     }  
  107. }  

Now, let's write the Unit Test Case for SQLite and see what are the differences between In-Memory Provider and SQLite. So, let's create a class as "SQLiteTest". Here we also follow the same rule while writing the test case as above. Every test case has three portions as "Arrange", "Act" and "Assert". In the "Arrange" section, we are preparing the object for context from the ConnectionFactory class, in the "Act" section, executing the action to the database, and in the "Assert" section, just checking the data and validating it. 

Here, we are writing the four different test cases as follows for different purposes.

  1. Task_Add_Without_Relation: We are using the Relational Database, but in this test case, we are not passing data with relational mapping and seeing it while saving data to the database, we get the DbUpdateException and we need to handle it to pass the Test Case successfully. It means if the database is a relational database and we are using SQLite and we are not passing the appropriate data then it will fail.
  2. Task_Add_With_Relation_Return_Exception: Here, we are passing the data for a relational database but as you can see that, we are passing CategoryId =2 which should exist in the Category table. But for now "Category" is empty. So, here Primary Key and Foreign Key relationship failed and returned DbUpdateException, which needs to be handled.
  3. Task_Add_With_Relation_Return_No_Exception: This test case will run successfully because first, we are preparing the data for the "Category" table and then trying to push the data into "Post" table along with category id which already exists inside the Category table. So, here Primary Key and Foreign Key relationship get passed.
  4. Task_Add_Time_Test: We have written this test case to test the time taken by SQLite while adding 1000 records in one go. See the time comparison between In-Memory and SQLite for executing the 1000 records in one go in the last image.

You can find whole code as follows for SQLite In-Memory Database.

  1. using InMemoryVsSQLiteDemo.Model;  
  2. using Microsoft.EntityFrameworkCore;  
  3. using System;  
  4. using System.Linq;  
  5. using Xunit;  
  6.   
  7. namespace InMemoryVsSQLiteDemo.Test  
  8. {  
  9.     public class SQLiteTest  
  10.     {  
  11.         [Fact]  
  12.         public void Task_Add_Without_Relation()  
  13.         {  
  14.             //Arrange    
  15.             var factory = new ConnectionFactory();  
  16.   
  17.             //Get the instance of BlogDBContext  
  18.             var context = factory.CreateContextForSQLite();  
  19.   
  20.             var post = new Post() { Title = "Test Title 3", Description = "Test Description 3", CreatedDate = DateTime.Now };  
  21.   
  22.             //Act    
  23.             var data = context.Post.Add(post);  
  24.               
  25.             //Assert   
  26.             Assert.Throws<DbUpdateException>(() => context.SaveChanges());  
  27.             Assert.Empty(context.Post.ToList());  
  28.         }  
  29.   
  30.         [Fact]  
  31.         public void Task_Add_With_Relation_Return_Exception()  
  32.         {  
  33.             //Arrange    
  34.             var factory = new ConnectionFactory();  
  35.   
  36.             //Get the instance of BlogDBContext  
  37.             var context = factory.CreateContextForSQLite();  
  38.   
  39.             var post = new Post() { Title = "Test Title 3", Description = "Test Description 3", CategoryId = 2, CreatedDate = DateTime.Now };  
  40.   
  41.             //Act    
  42.             var data = context.Post.Add(post);  
  43.               
  44.             //Assert   
  45.             Assert.Throws<DbUpdateException>(() => context.SaveChanges());  
  46.             Assert.Empty(context.Post.ToList());  
  47.         }  
  48.   
  49.         [Fact]  
  50.         public void Task_Add_With_Relation_Return_No_Exception()  
  51.         {  
  52.             //Arrange    
  53.             var factory = new ConnectionFactory();  
  54.   
  55.             //Get the instance of BlogDBContext  
  56.             var context = factory.CreateContextForSQLite();  
  57.             var post = new Post() { Title = "Test Title 3", Description = "Test Description 3", CategoryId = 2, CreatedDate = DateTime.Now };  
  58.   
  59.             //Act    
  60.             for (int i = 1; i < 4; i++){  
  61.                 var category = new Category() { Id = i, Name = "Category " + i, Slug = "slug" + i };  
  62.                 context.Category.Add(category);                  
  63.             }  
  64.             context.SaveChanges();  
  65.   
  66.             var data = context.Post.Add(post);  
  67.             context.SaveChanges();  
  68.   
  69.             //Assert             
  70.   
  71.             //Get the post count  
  72.             var postCount = context.Post.Count();  
  73.             if (postCount != 0)  
  74.             {  
  75.                 Assert.Equal(1, postCount);  
  76.             }  
  77.   
  78.             //Get single post detail  
  79.             var singlePost = context.Post.FirstOrDefault();  
  80.             if (singlePost != null)  
  81.             {  
  82.                 Assert.Equal("Test Title 3", singlePost.Title);  
  83.             }  
  84.         }  
  85.   
  86.         [Fact]  
  87.         public void Task_Add_Time_Test()  
  88.         {  
  89.             //Arrange    
  90.             var factory = new ConnectionFactory();  
  91.   
  92.             //Get the instance of BlogDBContext  
  93.             var context = factory.CreateContextForInMemory();  
  94.   
  95.             //Act   
  96.             for (int i = 1; i < 4; i++)  
  97.             {  
  98.                 var category = new Category() { Id = i, Name = "Category " + i, Slug = "slug" + i };  
  99.                 context.Category.Add(category);                  
  100.             }  
  101.   
  102.             context.SaveChanges();  
  103.   
  104.             for (int i = 1; i <= 1000; i++)  
  105.             {  
  106.                 var post = new Post() { Title = "Test Title " + i, Description = "Test Description " + i, CategoryId = 2, CreatedDate = DateTime.Now };  
  107.                 context.Post.Add(post);                  
  108.             }  
  109.               
  110.             context.SaveChanges();  
  111.   
  112.             //Assert    
  113.             //Get the post count  
  114.             var postCount = context.Post.Count();  
  115.             if (postCount != 0)  
  116.             {  
  117.                 Assert.Equal(1000, postCount);  
  118.             }  
  119.   
  120.             //Get single post detail  
  121.             var singlePost = context.Post.Where(x => x.PostId == 1).FirstOrDefault();  
  122.             if (singlePost != null)  
  123.             {  
  124.                 Assert.Equal("Test Title 1", singlePost.Title);  
  125.             }  
  126.         }  
  127.     }  
  128. }  

Now, we have written seven Unit Test Cases, with three for In-Memory Provider and four for SQLite In-Memory database. To run the Test Cases, open Test Explorer and click to Run All Buttons and you will find that all test cases pass successfully as shown in the below image. Here you can also find the comparison for executing 1000 records in one go between both.

Unit Testing With InMemory Provider And SQLite In-Memory Database In EFCore 

Conclusion

So, today we have learned how to write Unit Test Cases with help of In-Memory Provider and SQLite In-Memory Database in Entity Framework Core.

I hope this post will help you. Please put your feedback using comments which helps me to improve myself for next post. If you have any doubts please ask your doubts or queries in the comment section and if you like this post, please share it with your friends. Thanks.