improved ef context, ef inmemory

This commit is contained in:
2017-02-03 14:47:10 +02:00
parent 071a2cf0e2
commit 8a2566a2ae
8 changed files with 90 additions and 31 deletions
@@ -227,7 +227,7 @@ export class BackupsComponent implements OnInit {
this.currentPage = page this.currentPage = page
this.totalPages = data.tp * this.pageSize; this.totalPages = data.tp * this.pageSize;
this.isloading = false; this.isloading = false;
}, 300);//TODO - remove - induced delay }, 200);//TODO - remove - induced delay
}, },
err => { err => {
@@ -57,7 +57,7 @@ export class LogsComponent implements OnInit {
this.currentPage = page this.currentPage = page
this.totalPages = data.tp * this.pageSize; this.totalPages = data.tp * this.pageSize;
this.isloading = false; this.isloading = false;
}, 400);//induced delay }, 200);//TODO - remove induced delay
}, },
err => this.log.error(err) err => this.log.error(err)
); );
@@ -10,6 +10,7 @@ using AutoMapper;
using LanBackup.WebApp.Models.DTO; using LanBackup.WebApp.Models.DTO;
using AutoMapper.QueryableExtensions; using AutoMapper.QueryableExtensions;
using LanBackup.WebApp.Models.Telemetry; using LanBackup.WebApp.Models.Telemetry;
using Microsoft.EntityFrameworkCore;
namespace LanBackup.WebApp.Controllers namespace LanBackup.WebApp.Controllers
{ {
@@ -44,9 +45,8 @@ namespace LanBackup.WebApp.Controllers
[ProducesResponseType(typeof(IEnumerable<BackupConfigurationDTO>), 200)] [ProducesResponseType(typeof(IEnumerable<BackupConfigurationDTO>), 200)]
[ProducesResponseType(typeof(PaginatedList<BackupConfiguration, String, BackupConfigurationDTO>), 200)] [ProducesResponseType(typeof(PaginatedList<BackupConfiguration, String, BackupConfigurationDTO>), 200)]
public async Task<IActionResult> Get([FromHeader] string idx, [FromHeader] string siz) public async Task<IActionResult> Get([FromHeader] string idx, [FromHeader] string siz)
//public IEnumerable<BackupConfiguration> Get()
{ {
//return new DatabaseManager(_context).GetBackupConfigs(); _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
if (!string.IsNullOrEmpty(idx) && !string.IsNullOrEmpty(siz)) if (!string.IsNullOrEmpty(idx) && !string.IsNullOrEmpty(siz))
{ {
return new OkObjectResult( return new OkObjectResult(
@@ -67,6 +67,7 @@ namespace LanBackup.WebApp.Controllers
[ProducesResponseType(typeof(BackupConfigurationDTO), 404)] [ProducesResponseType(typeof(BackupConfigurationDTO), 404)]
public IActionResult Get(string id) public IActionResult Get(string id)
{ {
_context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var res = new DatabaseManager(_context).GetBackupConfig(id); var res = new DatabaseManager(_context).GetBackupConfig(id);
if (res == null) if (res == null)
{ {
@@ -87,6 +88,7 @@ namespace LanBackup.WebApp.Controllers
[ProducesResponseType(typeof(IEnumerable<BackupConfigurationDTO>), 404)] [ProducesResponseType(typeof(IEnumerable<BackupConfigurationDTO>), 404)]
public IActionResult GetByCient(string clientid) public IActionResult GetByCient(string clientid)
{ {
_context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var results = new DatabaseManager(_context).GetBackupConfigByClient(clientid); var results = new DatabaseManager(_context).GetBackupConfigByClient(clientid);
if (results == null || results.Count() == 0) if (results == null || results.Count() == 0)
return NotFound(); return NotFound();
@@ -10,6 +10,7 @@ using LanBackup.WebApp.Models.DTO;
using AutoMapper; using AutoMapper;
using AutoMapper.QueryableExtensions; using AutoMapper.QueryableExtensions;
using LanBackup.WebApp.Models.Telemetry; using LanBackup.WebApp.Models.Telemetry;
using Microsoft.EntityFrameworkCore;
namespace LanBackup.WebApp.Controllers namespace LanBackup.WebApp.Controllers
{ {
@@ -35,19 +36,6 @@ namespace LanBackup.WebApp.Controllers
} }
///// <summary>
///// retrieves all logs from DB
///// </summary>
///// <returns></returns>
//[AllowAnonymous]
//[HttpGet]
//[ProducesResponseType(typeof(IEnumerable<BackupLog>), 200)]
//public IActionResult Get()
//{
// return new OkObjectResult(_context.Logs.ToList());
// //return new ObjectResult(_context.Logs.ToList());
//}
/// <summary> /// <summary>
/// retrieves paginated logs from DB /// retrieves paginated logs from DB
@@ -57,14 +45,11 @@ namespace LanBackup.WebApp.Controllers
/// <returns></returns> /// <returns></returns>
[AllowAnonymous] [AllowAnonymous]
[HttpGet] [HttpGet]
//[HttpGet("{pageIndex}/{pageSize}")]
[ProducesResponseType(typeof(IEnumerable<BackupLogDTO>), 200)] [ProducesResponseType(typeof(IEnumerable<BackupLogDTO>), 200)]
[ProducesResponseType(typeof(PaginatedList<BackupLog, DateTime, BackupLogDTO>), 200)] [ProducesResponseType(typeof(PaginatedList<BackupLog, DateTime, BackupLogDTO>), 200)]
public async Task<IActionResult> Get([FromHeader] string idx, [FromHeader] string siz) public async Task<IActionResult> Get([FromHeader] string idx, [FromHeader] string siz)
{ {
//StringValues val1, val2; _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
//HttpContext.Request.Headers.TryGetValue("idx", out val1) &&
//HttpContext.Request.Headers.TryGetValue("siz", out val2)
if (!string.IsNullOrEmpty(idx) && !string.IsNullOrEmpty(siz)) if (!string.IsNullOrEmpty(idx) && !string.IsNullOrEmpty(siz))
{ {
return new OkObjectResult( return new OkObjectResult(
@@ -86,6 +71,7 @@ namespace LanBackup.WebApp.Controllers
[ProducesResponseType(typeof(BackupLogDTO), 404)] [ProducesResponseType(typeof(BackupLogDTO), 404)]
public IActionResult Get(int id) public IActionResult Get(int id)
{ {
_context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var result = _context.Logs.SingleOrDefault(p => p.ID == id); var result = _context.Logs.SingleOrDefault(p => p.ID == id);
if (result == null) if (result == null)
{ {
@@ -107,6 +93,7 @@ namespace LanBackup.WebApp.Controllers
[ProducesResponseType(typeof(IEnumerable<BackupLogDTO>), 404)] [ProducesResponseType(typeof(IEnumerable<BackupLogDTO>), 404)]
public IActionResult GetByCientID(string clientid) public IActionResult GetByCientID(string clientid)
{ {
_context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var results = _context.Logs.Where(p => p.ClientIP == clientid).OrderByDescending(o => o.DateTime); var results = _context.Logs.Where(p => p.ClientIP == clientid).OrderByDescending(o => o.DateTime);
if (results == null || results.Count() == 0) if (results == null || results.Count() == 0)
return NotFound(); return NotFound();
@@ -126,6 +113,7 @@ namespace LanBackup.WebApp.Controllers
[ProducesResponseType(typeof(IEnumerable<BackupLogDTO>), 404)] [ProducesResponseType(typeof(IEnumerable<BackupLogDTO>), 404)]
public IActionResult GetByConfigurationID(string configurationid) public IActionResult GetByConfigurationID(string configurationid)
{ {
_context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var results = _context.Logs.Where(p => p.ConfigurationID == configurationid).OrderByDescending(o => o.DateTime); var results = _context.Logs.Where(p => p.ConfigurationID == configurationid).OrderByDescending(o => o.DateTime);
if (results == null || results.Count() == 0) if (results == null || results.Count() == 0)
return NotFound(); return NotFound();
@@ -141,7 +129,7 @@ namespace LanBackup.WebApp.Controllers
/// </summary> /// </summary>
/// <param name="log"></param> /// <param name="log"></param>
/// <returns></returns> /// <returns></returns>
[AllowAnonymous]//[Authorize(Roles = "Admin")] [AllowAnonymous]//TODO [Authorize(Roles = "Admin")]
[HttpPost] [HttpPost]
[ProducesResponseType(typeof(int), 200)] [ProducesResponseType(typeof(int), 200)]
[ProducesResponseType(typeof(int), 400)] [ProducesResponseType(typeof(int), 400)]
@@ -47,8 +47,8 @@ namespace LanBackup.WebApp.Controllers
var count = await source.CountAsync(); var count = await source.CountAsync();
if (order != null) if (order != null)
{ {
var items = source.OrderByDescending(order).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();// .ToListAsync(); var items = source.OrderByDescending(order).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToAsyncEnumerable();
var mappedItems = mapper.Map<List<T>, List<M>>(items); var mappedItems = mapper.Map<List<T>, List<M>>(await items.ToList());
return new PaginatedList<T, U, M>(mappedItems, count, pageIndex, pageSize); return new PaginatedList<T, U, M>(mappedItems, count, pageIndex, pageSize);
} }
else else
+2 -1
View File
@@ -37,7 +37,8 @@
"AutoMapper": "5.2.0", "AutoMapper": "5.2.0",
"AutoMapper.Extensions.Microsoft.DependencyInjection": "1.2.0", "AutoMapper.Extensions.Microsoft.DependencyInjection": "1.2.0",
"Microsoft.ApplicationInsights.AspNetCore": "2.0.0", "Microsoft.ApplicationInsights.AspNetCore": "2.0.0",
"Microsoft.ApplicationInsights.JavaScript": "1.0.7-build00691" "Microsoft.ApplicationInsights.JavaScript": "1.0.7-build00691",
"Microsoft.EntityFrameworkCore": "1.1.0"
}, },
"tools": { "tools": {
@@ -63,12 +63,76 @@ namespace LanBackup.WebApp.Tests.IntegrationTests
#region EF InMemory DB
[Fact]
public async void WithEfInMemoryTestMethod()
{
using (var mctx = new EfInMemoryContext().GetContext())
{
var service = new LogsController(Mapper.Instance, mctx, _tele);
var res = Assert.IsType<OkObjectResult>(await service.Get(null, null));
var val1 = Assert.IsType<List<BackupLogDTO>>(res.Value);
Assert.Equal(5, val1.Count());
}
}
private class EfInMemoryContext : IDisposable
{
private BackupsContext _context;
private DbContextOptions<BackupsContext> options;
public EfInMemoryContext()
{
options = new DbContextOptionsBuilder<BackupsContext>()
.UseInMemoryDatabase()
.Options;
// Create the schema in the database
using (var context = new BackupsContext(options))
{
context.Database.EnsureCreated();
}
// Insert seed data into the database using one instance of the context
using (var context = new BackupsContext(options))
{
int id = 1;
context.Add(new BackupLog { ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ });
context.Add(new BackupLog { ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ });
context.Add(new BackupLog { ClientIP = "192.168.1.101", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ });
context.Add(new BackupLog { ClientIP = "192.168.1.102", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ });
context.Add(new BackupLog { ClientIP = "192.168.1.103", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ });
context.SaveChanges();
}
_context = new BackupsContext(options);
}
public void Dispose()
{
_context.Dispose();
}
internal BackupsContext GetContext()
{
return _context;
}
}
#endregion EF InMemory DB
#region SqliteInMemory DB tests #region SqliteInMemory DB tests
[Fact] [Fact]
public async void WithSqliteInmemoryTestMethod() public async void WithSqliteInmemoryTestMethod()
{ {
using (var mctx = new GetSqliteMemoryContext().GetContext()) using (var mctx = new SqliteMemoryContext().GetContext())
{ {
var service = new LogsController(Mapper.Instance, mctx, _tele); var service = new LogsController(Mapper.Instance, mctx, _tele);
var res = Assert.IsType<OkObjectResult>(await service.Get(null, null)); var res = Assert.IsType<OkObjectResult>(await service.Get(null, null));
@@ -83,7 +147,7 @@ namespace LanBackup.WebApp.Tests.IntegrationTests
public async void GetByIdWithSqliteInmemoryTestMethod() public async void GetByIdWithSqliteInmemoryTestMethod()
{ {
using (var mctx = new GetSqliteMemoryContext().GetContext()) using (var mctx = new SqliteMemoryContext().GetContext())
{ {
var recId = 1; var recId = 1;
var service = new LogsController(Mapper.Instance, mctx, _tele); var service = new LogsController(Mapper.Instance, mctx, _tele);
@@ -103,7 +167,7 @@ namespace LanBackup.WebApp.Tests.IntegrationTests
public async void GetByClientIdWithSqliteInmemoryTestMethod() public async void GetByClientIdWithSqliteInmemoryTestMethod()
{ {
using (var mctx = new GetSqliteMemoryContext().GetContext()) using (var mctx = new SqliteMemoryContext().GetContext())
{ {
var clientId = "192.168.1.100"; var clientId = "192.168.1.100";
var service = new LogsController(Mapper.Instance, mctx, _tele); var service = new LogsController(Mapper.Instance, mctx, _tele);
@@ -119,13 +183,13 @@ namespace LanBackup.WebApp.Tests.IntegrationTests
private class GetSqliteMemoryContext : IDisposable private class SqliteMemoryContext : IDisposable
{ {
private SqliteConnection connection; private SqliteConnection connection;
private BackupsContext _context; private BackupsContext _context;
private DbContextOptions<BackupsContext> options; private DbContextOptions<BackupsContext> options;
public GetSqliteMemoryContext() public SqliteMemoryContext()
{ {
connection = new SqliteConnection("DataSource=:memory:"); connection = new SqliteConnection("DataSource=:memory:");
connection.Open(); connection.Open();
@@ -169,5 +233,8 @@ namespace LanBackup.WebApp.Tests.IntegrationTests
#endregion #endregion
} }
} }
+2 -1
View File
@@ -21,7 +21,8 @@
"FluentAssertions": "4.18.0", "FluentAssertions": "4.18.0",
"LanBackup.WebApp": "1.0.0-*", "LanBackup.WebApp": "1.0.0-*",
"Moq": "4.6.38-alpha", "Moq": "4.6.38-alpha",
"System.Diagnostics.TraceSource": "4.0.0" "System.Diagnostics.TraceSource": "4.0.0",
"Microsoft.EntityFrameworkCore.InMemory": "1.1.0"
}, },
"testRunner": "xunit", "testRunner": "xunit",
"frameworks": { "frameworks": {