mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-06 00:36:39 +00:00
before tests pull
This commit is contained in:
parent
dfbd6632c0
commit
a1b7af489b
2 changed files with 78 additions and 0 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue