before tests pull

This commit is contained in:
Ruidy Nemausat 2020-02-26 14:53:56 +01:00
parent dfbd6632c0
commit a1b7af489b
2 changed files with 78 additions and 0 deletions

View file

@ -0,0 +1,44 @@
using System;
using System.Threading.Tasks;
using TicketManager.Data;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace TicketManager._
{
public class ControllersTests
{
public static void Wrapper(
Func<DbContextOptions<AppDbContext>, Task> Test,
Action<DbContextOptions<AppDbContext>> SeedDb)
{
// Create inMemory Test Database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
try
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseSqlite(connection)
.Options;
// creates DB schema
using (var context = new AppDbContext(options))
{
context.Database.EnsureCreated();
}
// Seed DB usng one context instance
SeedDb(options);
// use another context instance to run the test
Test(options);
}
finally
{
connection.Close();
}
}
}
}

View file

@ -0,0 +1,34 @@
using System;
using TicketManager.Data;
using TicketManager.Models;
using Microsoft.EntityFrameworkCore;
namespace TicketManager._
{
public class SeedDb
{
public static void Projects(DbContextOptions<AppDbContext> options)
// Seed DB usng one context instance
{
using (var context = new AppDbContext(options))
{
context.Projects.AddRange(
new Project()
{
Id = 1,
Title = "Secret Project",
Description = "Shht Don't Ask don't tell",
PlannedEnding = new DateTime(2021, 7, 21)
},
new Project()
{
Id = 2,
Title = "Public Project",
Description = "It's quite obvious, isn't it?!",
PlannedEnding = new DateTime(2036, 6, 16)
});
context.SaveChanges();
}
}
}
}