In order to understand completly this presentantion it's necesary be familiar with
Testing Pyramid
@jecaestevez
Why integration test are important ?
Ado.net appear in 2005 and Entity Framework in 2007
Ado.Net
Smart people
Entity Framework
Dapper
2. EF Migration for Database first
1. Invent a Custom tool to apply
sql changes to db
public class EfDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//TODO Extract connection string to a secret
optionsBuilder.UseSqlServer(@"Server=.\;Database=EFDatabaseFirstDB;
Trusted_Connection=True;MultipleActiveResultSets=true");
}
}
public partial class CreateDatabase : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'Items')
BEGIN
DROP TABLE [dbo].[Items]
END
CREATE TABLE [dbo].[Items](
[id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL,
[Description] [nvarchar](max) NULL,
[Expiration] [datetime2](7) NOT NULL,
CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"DROP TABLE [dbo].[Items]);
}
}
add migration
PM > add-migration CreateDatabase
PM> update-database –verbose
update Database
dotnet ef database update
or
public class EfDbContext : DbContext
{
public DbSet<Item> Items { get; set; }
public EfDbContext(DbContextOptions<EfDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
//TODO Extract connection string to a secret
optionsBuilder.UseSqlServer(@"Server=.\;Database=EFDatabaseFirstDB;
Trusted_Connection=True;MultipleActiveResultSets=true");
}
}
}
Items
ID|Name|Description
Table
var itemSaved = new Item();
//Arrange
var expirationDay = DateTime.Now.AddYears(1);
//Act
using (var context = new EfDbContext())
{
var newItem = new Item()
{
Name = "Ron Palido",
Description = "Drink",
Expiration = expirationDay
};
context.Add(newItem);
context.SaveChanges();
itemSaved = context.Items.Find(1);
}
//Assert
Assert.IsNotNull(itemSaved, "Failed -Item not saved");
Saving in
Sql server
var options = new DbContextOptionsBuilder<EfDbContext>()
.UseInMemoryDatabase(databaseName: "InMemory_EFDatabaseFirstDB")
.Options;
var itemSaved = new Item();
//Arrange
var expirationDay = DateTime.Now.AddYears(1);
//Act
using (var context = new EfDbContext(options))
{
var newItem = new Item()
{
Name = "Ron Palido",
Description = "Drink",
Expiration = expirationDay
};
context.Add(newItem);
context.SaveChanges();
itemSaved = context.Items.Find(1);
}
//Assert
Assert.IsNotNull(itemSaved, "Failed -Item not saved");
In Memory
Saving
created automatic
DB
Sql server feature used will
in Memory
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<EfDbContext>()
.UseSqlite(connection)
.Options;
var itemSaved = new Item();
//Arrange
var expirationDay = DateTime.Now.AddYears(1);
//Act
using (var context = new EfDbContext(options))
{
var newItem = new Item()
{
Name = "Ron Palido", Description = "Drink", Expiration = expirationDay
};
context.Add(newItem);
context.SaveChanges();
itemSaved = context.Items.Find(1);
}
//Assert
Assert.IsNotNull(itemSaved, "Failed -Item not saved");
Saving
Sql server feature used will
in Memory
from scratch
[TestClass]
public class IntegrationTestSqlServerDal
{
private DbContextOptions<EfDbContext> _options;
[TestInitialize]
public void Init()
{
string randomDBName = "EFDatabaseFirstDB" + Guid.NewGuid();
_options = new DbContextOptionsBuilder<EfDbContext>()
.UseSqlServer($"Server=.;Database={randomDBName};
Trusted_Connection=True;MultipleActiveResultSets=true")
.Options;
using (var context = new EfDbContext(_options))
{
context.Database.Migrate();
};
}
[TestClass]
public class IntegrationTestSqlServerDal
{
private DbContextOptions<EfDbContext> _options;
[TestMethod]
public void Given_NoItems_Them_AddNewItem()
{
var itemSaved = new Item();
//Arrange
var expirationDay = DateTime.Now.AddYears(1);
//Act
using (var context = new EfDbContext(_options))
{
var newItem = new Item()
{
Name = "Ron Palido",
Description = "Drink",
Expiration = expirationDay
};
context.Add(newItem);
context.SaveChanges();
Saving in
Sql server
Random
new
Let's do a workshop!