forked from ddrilling/AsbCloudServer
draft unit tests
This commit is contained in:
parent
e9d980438b
commit
28c35b5487
@ -24,15 +24,20 @@ namespace AsbCloudInfrastructure
|
||||
return context;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
|
||||
public static void MapsterSetup()
|
||||
{
|
||||
TypeAdapterConfig.GlobalSettings.Default.Config
|
||||
.ForType<DateTimeOffset, DateTime>()
|
||||
.MapWith((source) => source.DateTime);
|
||||
TypeAdapterConfig.GlobalSettings.Default.Config
|
||||
.ForType<DateTime, DateTimeOffset>()
|
||||
.MapWith((source) => source == default ? new DateTime(0,DateTimeKind.Utc) : source );
|
||||
.MapWith((source) => source == default ? new DateTime(0, DateTimeKind.Utc) : source);
|
||||
|
||||
}
|
||||
|
||||
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
MapsterSetup();
|
||||
services.AddDbContext<AsbCloudDbContext>(options =>
|
||||
options.UseNpgsql(configuration.GetConnectionString("DefaultConnection")));
|
||||
|
||||
|
@ -45,15 +45,7 @@ namespace AsbCloudWebApi.Tests.ControllersTests
|
||||
controller = new TelemetryAnalyticsController(analyticsService.Object,
|
||||
wellService.Object);
|
||||
|
||||
var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
|
||||
{
|
||||
new Claim("idCompany", "1"),
|
||||
}, "mock"));
|
||||
|
||||
controller.ControllerContext = new ControllerContext()
|
||||
{
|
||||
HttpContext = new DefaultHttpContext() { User = user }
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -0,0 +1,31 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudWebApi.Tests.ControllersTests
|
||||
{
|
||||
static class ControllerExtentions
|
||||
{
|
||||
public static void AddUser(this ControllerBase controller)
|
||||
{
|
||||
var claims = new Claim[] { new Claim("idCompany", "1") };
|
||||
controller.AddUser(claims);
|
||||
}
|
||||
|
||||
public static void AddUser(this ControllerBase controller, Claim[] claims)
|
||||
{
|
||||
var identity = new ClaimsIdentity(claims, "mock");
|
||||
var user = new ClaimsPrincipal(identity);
|
||||
|
||||
controller.ControllerContext = new ControllerContext()
|
||||
{
|
||||
HttpContext = new DefaultHttpContext() { User = user }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudWebApi.Controllers;
|
||||
using AsbCloudWebApi.SignalR;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace AsbCloudWebApi.Tests.ControllersTests
|
||||
{
|
||||
public class TelemetryDataSaubControllerTests
|
||||
{
|
||||
private readonly Mock<ITelemetryService> telemetryService;
|
||||
private readonly Mock<ITelemetryDataService<TelemetryDataSaubDto>> telemetryDataService;
|
||||
private readonly Mock<IWellService> wellService;
|
||||
private readonly Mock<IHubContext<TelemetryHub>> telemetryHubContext;
|
||||
|
||||
public TelemetryDataSaubControllerTests()
|
||||
{
|
||||
telemetryService = new Mock<ITelemetryService>();
|
||||
telemetryDataService = new Mock<ITelemetryDataService<TelemetryDataSaubDto>>();
|
||||
wellService = new Mock<IWellService>();
|
||||
telemetryHubContext = new Mock<IHubContext<TelemetryHub>>();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure.Services;
|
||||
using AsbCloudInfrastructure.Services.Cache;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
{
|
||||
public class TelemetryDataSaubServiceTest
|
||||
{
|
||||
private readonly Mock<ITelemetryTracker> telemetryTracker;
|
||||
private readonly Mock<ITimezoneService> timezoneService;
|
||||
private readonly SimpleTimezoneDto timezone;
|
||||
private readonly AsbCloudDbContext context;
|
||||
private readonly CacheDb cacheDb;
|
||||
private readonly TelemetryService telemetryService;
|
||||
|
||||
private readonly DateTime drillingStartDate;
|
||||
private readonly string uuid;
|
||||
public TelemetryDataSaubServiceTest()
|
||||
{
|
||||
timezone = new() { Hours = 7 };
|
||||
drillingStartDate = new DateTime(2022, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);
|
||||
uuid = drillingStartDate.ToString("yyyyMMdd_HHmmssfff");
|
||||
|
||||
AsbCloudInfrastructure.DependencyInjection.MapsterSetup();
|
||||
|
||||
telemetryTracker = new Mock<ITelemetryTracker>();
|
||||
timezoneService = new Mock<ITimezoneService>();
|
||||
timezoneService.Setup(s => s.GetByCoordinatesAsync(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(Task.FromResult(timezone));
|
||||
timezoneService.Setup(s => s.GetByCoordinates(It.IsAny<double>(), It.IsAny<double>()))
|
||||
.Returns(timezone);
|
||||
|
||||
context = TestHelpter.MakeTestContext();
|
||||
cacheDb = new CacheDb();
|
||||
telemetryService = new TelemetryService(context, telemetryTracker.Object, timezoneService.Object, cacheDb);
|
||||
|
||||
var info = new TelemetryInfoDto
|
||||
{
|
||||
TimeZoneOffsetTotalHours = timezone.Hours,
|
||||
DrillingStartDate = drillingStartDate,
|
||||
};
|
||||
telemetryService.UpdateInfoAsync(uuid, info, CancellationToken.None).Wait();
|
||||
|
||||
}
|
||||
~TelemetryDataSaubServiceTest()
|
||||
{
|
||||
var ts = context.Telemetries.Where(t => t.RemoteUid == uuid);
|
||||
context.Telemetries.RemoveRange(ts);
|
||||
context.SaveChanges();
|
||||
context?.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateDataAsync()
|
||||
{
|
||||
// Arrange
|
||||
var telemetryDataSaubService = new TelemetryDataSaubService(context, telemetryService, cacheDb);
|
||||
|
||||
var now = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(timezone.Hours)).DateTime;
|
||||
var tuser = "Завулон";
|
||||
var newData = new List<TelemetryDataSaubDto>
|
||||
{
|
||||
new TelemetryDataSaubDto{
|
||||
Date = now,
|
||||
AxialLoad = 1,
|
||||
MseState = 1,
|
||||
User = tuser,
|
||||
}
|
||||
};
|
||||
|
||||
// act
|
||||
var affected = await telemetryDataSaubService.UpdateDataAsync(uuid, newData, CancellationToken.None);
|
||||
|
||||
// assert
|
||||
Assert.Equal(1, affected);
|
||||
}
|
||||
}
|
||||
}
|
22
AsbCloudWebApi.Tests/TestHelpter.cs
Normal file
22
AsbCloudWebApi.Tests/TestHelpter.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using AsbCloudDb.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudWebApi.Tests
|
||||
{
|
||||
internal static class TestHelpter
|
||||
{
|
||||
public static AsbCloudDbContext MakeTestContext()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
||||
.UseNpgsql("Host=localhost;Database=tests;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True")
|
||||
.Options;
|
||||
var context = new AsbCloudDbContext(options);
|
||||
return context;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user