mirror of
https://github.com/farcasclaudiu/LanBackup.git
synced 2026-06-28 17:01:02 +03:00
folder rename
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using LanBackup.DataCore;
|
||||
using LanBackup.ModelsCore;
|
||||
using LanBackup.WebApp.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using AutoMapper;
|
||||
using LanBackup.WebApp.Models.Telemetry;
|
||||
using LanBackup.WebApp.Models.DTO;
|
||||
|
||||
namespace LanBackup.WebApp.Tests.IntegrationTests
|
||||
{
|
||||
public class ApiLogsControllerTests : IClassFixture<TestFixture<LanBackup.WebApp.Startup>>
|
||||
{
|
||||
|
||||
private readonly HttpClient _client;
|
||||
private ITestOutputHelper _output;
|
||||
private ITelemetryLogger _tele;
|
||||
|
||||
public ApiLogsControllerTests(TestFixture<LanBackup.WebApp.Startup> fixture, ITestOutputHelper output)
|
||||
{
|
||||
_output = output;
|
||||
_client = fixture.Client;
|
||||
|
||||
Mapper.Initialize(cfg => cfg.AddProfiles(typeof(LanBackup.WebApp.Startup)));
|
||||
|
||||
_tele = new MockTelemetry();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllLogRecords()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
var response = await _client.GetAsync("/api/logs");
|
||||
|
||||
// Assert
|
||||
response.EnsureSuccessStatusCode();
|
||||
var jsonStr = await response.Content.ReadAsStringAsync();
|
||||
var logList = JsonConvert.DeserializeObject<List<LanBackup.ModelsCore.BackupLog>>(jsonStr);
|
||||
|
||||
Assert.NotEmpty(logList);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#region SqliteInMemory DB tests
|
||||
|
||||
[Fact]
|
||||
public async void WithSqliteInmemoryTestMethod()
|
||||
{
|
||||
using (var mctx = new GetSqliteMemoryContext().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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async void GetByIdWithSqliteInmemoryTestMethod()
|
||||
{
|
||||
|
||||
using (var mctx = new GetSqliteMemoryContext().GetContext())
|
||||
{
|
||||
var recId = 1;
|
||||
var service = new LogsController(Mapper.Instance, mctx, _tele);
|
||||
|
||||
var res = Assert.IsType<OkObjectResult>(service.Get(recId));
|
||||
var val1 = Assert.IsType<BackupLogDTO>(res.Value);
|
||||
|
||||
Assert.NotNull(val1);
|
||||
|
||||
var res2 = Assert.IsType<NotFoundResult>(service.Get(10000));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async void GetByClientIdWithSqliteInmemoryTestMethod()
|
||||
{
|
||||
|
||||
using (var mctx = new GetSqliteMemoryContext().GetContext())
|
||||
{
|
||||
var clientId = "192.168.1.100";
|
||||
var service = new LogsController(Mapper.Instance, mctx, _tele);
|
||||
|
||||
var res = Assert.IsType<OkObjectResult>(service.GetByCientID(clientId));
|
||||
var val1 = Assert.IsType<List<BackupLogDTO>>(res.Value);
|
||||
|
||||
Assert.Equal(2, val1.Count());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private class GetSqliteMemoryContext : IDisposable
|
||||
{
|
||||
private SqliteConnection connection;
|
||||
private BackupsContext _context;
|
||||
private DbContextOptions<BackupsContext> options;
|
||||
|
||||
public GetSqliteMemoryContext()
|
||||
{
|
||||
connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
options = new DbContextOptionsBuilder<BackupsContext>()
|
||||
.UseSqlite(connection)
|
||||
.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();
|
||||
connection.Close();
|
||||
connection.Dispose();
|
||||
}
|
||||
|
||||
internal BackupsContext GetContext()
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Net.Http;
|
||||
using Xunit;
|
||||
|
||||
namespace LanBackup.WebApp.Tests.IntegrationTests
|
||||
{
|
||||
public class HomeControllerTests : IClassFixture<TestFixture<LanBackup.WebApp.Startup>>
|
||||
{
|
||||
private readonly HttpClient _client;
|
||||
|
||||
public HomeControllerTests(TestFixture<LanBackup.WebApp.Startup> fixture)
|
||||
{
|
||||
_client = fixture.Client;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using LanBackup.DataCore;
|
||||
using LanBackup.ModelsCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace LanBackup.WebApp.Tests.IntegrationTests
|
||||
{
|
||||
public class MssqlTests : IDisposable
|
||||
{
|
||||
|
||||
|
||||
BackupsContext _context;
|
||||
|
||||
public MssqlTests()
|
||||
{
|
||||
var serviceProvider = new ServiceCollection()
|
||||
.AddEntityFrameworkSqlServer()
|
||||
.BuildServiceProvider();
|
||||
|
||||
var builder = new DbContextOptionsBuilder<BackupsContext>();
|
||||
|
||||
builder.UseSqlServer($"Server=(localdb)\\mssqllocaldb;Database=lanbackup_db_{Guid.NewGuid()};Trusted_Connection=True;MultipleActiveResultSets=true")
|
||||
.UseInternalServiceProvider(serviceProvider);
|
||||
|
||||
_context = new BackupsContext(builder.Options);
|
||||
_context.Database.EnsureCreated();
|
||||
//_context.Database.Migrate();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
_context.Database.EnsureDeleted();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#region MSSQL server integration tests
|
||||
|
||||
[Trait("Category", "DbIntegration")]
|
||||
[Fact]
|
||||
public async void MssqlTestMethod()
|
||||
{
|
||||
|
||||
//Add some monsters before querying
|
||||
_context.Add(new BackupLog { ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"});
|
||||
_context.Add(new BackupLog { ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"});
|
||||
_context.Add(new BackupLog { ClientIP = "192.168.1.101", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"});
|
||||
_context.Add(new BackupLog { ClientIP = "192.168.1.102", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"});
|
||||
_context.Add(new BackupLog { ClientIP = "192.168.1.103", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"});
|
||||
_context.SaveChanges();
|
||||
//Execute the query
|
||||
var res = _context.Logs.FromSql("SELECT ID, Description, ClientIP, ConfigurationID, LogError, Status, DateTime, RowVersion FROM Logs").ToList();
|
||||
//Verify the results
|
||||
Assert.Equal(5, res.Count());
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc.ViewComponents;
|
||||
|
||||
namespace LanBackup.WebApp.Tests
|
||||
{
|
||||
public class TestFixture<TStartup> : IDisposable
|
||||
{
|
||||
|
||||
private const string SolutionName = "LanBackup.sln";
|
||||
|
||||
private readonly TestServer _server;
|
||||
public HttpClient Client;
|
||||
|
||||
public TestFixture() : this(Path.Combine("src"))
|
||||
{
|
||||
}
|
||||
|
||||
protected TestFixture(string solutionRelativeTargetProjectParentDir)
|
||||
{
|
||||
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
|
||||
var contentRoot = GetProjectPath(solutionRelativeTargetProjectParentDir, startupAssembly);
|
||||
|
||||
var builder = new WebHostBuilder()
|
||||
.UseContentRoot(contentRoot)
|
||||
.ConfigureServices(InitializeServices)
|
||||
.UseEnvironment("Development")
|
||||
.UseStartup(typeof(TStartup));
|
||||
|
||||
_server = new TestServer(builder);
|
||||
|
||||
Client = _server.CreateClient();
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Client.Dispose();
|
||||
_server.Dispose();
|
||||
}
|
||||
|
||||
protected virtual void InitializeServices(IServiceCollection services)
|
||||
{
|
||||
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
|
||||
|
||||
// Inject a custom application part manager. Overrides AddMvcCore() because that uses TryAdd().
|
||||
var manager = new ApplicationPartManager();
|
||||
manager.ApplicationParts.Add(new AssemblyPart(startupAssembly));
|
||||
|
||||
manager.FeatureProviders.Add(new ControllerFeatureProvider());
|
||||
manager.FeatureProviders.Add(new ViewComponentFeatureProvider());
|
||||
|
||||
services.AddSingleton(manager);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full path to the target project path that we wish to test
|
||||
/// </summary>
|
||||
/// <param name="solutionRelativePath">
|
||||
/// The parent directory of the target project.
|
||||
/// e.g. src, samples, test, or test/Websites
|
||||
/// </param>
|
||||
/// <param name="startupAssembly">The target project's assembly.</param>
|
||||
/// <returns>The full path to the target project.</returns>
|
||||
private static string GetProjectPath(string solutionRelativePath, Assembly startupAssembly)
|
||||
{
|
||||
// Get name of the target project which we want to test
|
||||
var projectName = startupAssembly.GetName().Name;
|
||||
|
||||
// Get currently executing test project path
|
||||
var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath;
|
||||
|
||||
// Find the folder which contains the solution file. We then use this information to find the target
|
||||
// project which we want to test.
|
||||
var directoryInfo = new DirectoryInfo(applicationBasePath);
|
||||
do
|
||||
{
|
||||
var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, SolutionName));
|
||||
if (solutionFileInfo.Exists)
|
||||
{
|
||||
return Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath, projectName));
|
||||
}
|
||||
|
||||
directoryInfo = directoryInfo.Parent;
|
||||
}
|
||||
while (directoryInfo.Parent != null);
|
||||
|
||||
throw new Exception($"Solution root could not be located using application root {applicationBasePath}.");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user