Пока все тестовые контроллеры закомментированы, кроме одного
Some checks failed
Run Tests / run_tests (push) Failing after 2m2s

This commit is contained in:
Olga Nemt 2024-12-18 14:20:05 +05:00
parent 7932177e91
commit 2f77edf7d1
7 changed files with 1133 additions and 1133 deletions

View File

@ -1,343 +1,343 @@
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using DD.Persistence.Database.Model;
using DD.Persistence.Models;
using DD.Persistence.Models.Requests;
using Xunit;
using DD.Persistence.Client.Clients.Interfaces;
using DD.Persistence.Client;
namespace DD.Persistence.IntegrationTests.Controllers;
public class ChangeLogControllerTest : BaseIntegrationTest
{
private readonly IChangeLogClient client;
private static readonly Random generatorRandomDigits = new();
public ChangeLogControllerTest(WebAppFactoryFixture factory) : base(factory)
{
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
client = persistenceClientFactory.GetChangeLogClient();
}
[Fact]
public async Task ClearAndInsertRange_InEmptyDb()
{
// arrange
dbContext.CleanupDbSet<ChangeLog>();
var idDiscriminator = Guid.NewGuid();
var dtos = Generate(2);
// act
var result = await client.ClearAndAddRange(idDiscriminator, dtos, new CancellationToken());
// assert
Assert.Equal(2, result);
}
[Fact]
public async Task ClearAndInsertRange_InNotEmptyDb()
{
// arrange
var insertedCount = 10;
var createdResult = CreateChangeLogItems(insertedCount, (-15, 15));
var idDiscriminator = createdResult.Item1;
var dtos = createdResult.Item2.Select(e => e.Adapt<DataWithWellDepthAndSectionDto>());
// act
var result = await client.ClearAndAddRange(idDiscriminator, dtos, new CancellationToken());
// assert
Assert.Equal(insertedCount*2, result);
}
[Fact]
public async Task Add_returns_success()
{
// arrange
var count = 1;
var idDiscriminator = Guid.NewGuid();
var dtos = Generate(count);
var dto = dtos.FirstOrDefault()!;
// act
var result = await client.Add(idDiscriminator, dto, new CancellationToken());
// assert
Assert.Equal(count, result);
}
[Fact]
public async Task AddRange_returns_success()
{
// arrange
var count = 3;
var idDiscriminator = Guid.NewGuid();
var dtos = Generate(count);
// act
var result = await client.AddRange(idDiscriminator, dtos, new CancellationToken());
// assert
Assert.Equal(count, result);
}
[Fact]
public async Task Update_returns_success()
{
// arrange
dbContext.CleanupDbSet<ChangeLog>();
var idDiscriminator = Guid.NewGuid();
var dtos = Generate(1);
var dto = dtos.FirstOrDefault()!;
var result = await client.Add(idDiscriminator, dto, new CancellationToken());
var entity = dbContext.ChangeLog
.Where(x => x.IdDiscriminator == idDiscriminator)
.FirstOrDefault();
dto = entity.Adapt<DataWithWellDepthAndSectionDto>();
dto.DepthEnd += 10;
// act
result = await client.Update(dto, new CancellationToken());
// assert
Assert.Equal(2, result);
var dateBegin = DateTimeOffset.UtcNow.AddDays(-1);
var dateEnd = DateTimeOffset.UtcNow.AddDays(1);
var changeLogResult = await client.GetChangeLogForInterval(idDiscriminator, dateBegin, dateEnd, new CancellationToken());
Assert.NotNull(changeLogResult);
var obsoleteDto = changeLogResult
.Where(e => e.Obsolete.HasValue)
.FirstOrDefault();
var activeDto = changeLogResult
.Where(e => !e.Obsolete.HasValue)
.FirstOrDefault();
if (obsoleteDto == null || activeDto == null)
{
Assert.Fail();
return;
}
Assert.Equal(activeDto.Id, obsoleteDto.IdNext);
}
[Fact]
public async Task UpdateRange_returns_success()
{
// arrange
var count = 2;
var dtos = Generate(count);
var entities = dtos.Select(d => d.Adapt<ChangeLog>()).ToArray();
dbContext.ChangeLog.AddRange(entities);
dbContext.SaveChanges();
dtos = entities.Select(c => new DataWithWellDepthAndSectionDto()
{
DepthEnd = c.DepthEnd + 10,
DepthStart = c.DepthStart + 10,
Id = c.Id,
IdSection = c.IdSection,
Value = c.Value
}).ToArray();
// act
var result = await client.UpdateRange(dtos, new CancellationToken());
// assert
Assert.Equal(count * 2, result);
}
[Fact]
public async Task Delete_returns_success()
{
// arrange
var dtos = Generate(1);
var dto = dtos.FirstOrDefault()!;
var entity = dto.Adapt<ChangeLog>();
dbContext.ChangeLog.Add(entity);
dbContext.SaveChanges();
// act
var result = await client.Delete(entity.Id, new CancellationToken());
// assert
Assert.Equal(1, result);
}
[Fact]
public async Task DeleteRange_returns_success()
{
// arrange
var count = 10;
var dtos = Generate(count);
var entities = dtos.Select(d => d.Adapt<ChangeLog>()).ToArray();
dbContext.ChangeLog.AddRange(entities);
dbContext.SaveChanges();
// act
var ids = entities.Select(e => e.Id);
var result = await client.DeleteRange(ids, new CancellationToken());
// assert
Assert.Equal(count, result);
}
[Fact]
public async Task GetDatesRange_returns_success()
{
// arrange
var changeLogItems = CreateChangeLogItems(3, (-15, 15));
var idDiscriminator = changeLogItems.Item1;
var entities = changeLogItems.Item2.OrderBy(e => e.Creation);
// act
var result = await client.GetDatesRange(idDiscriminator, new CancellationToken());
// assert
Assert.NotNull(result);
var minDate = entities.First().Creation;
var maxDate = entities.Last().Creation;
var expectedMinDate = minDate.ToUniversalTime().ToString();
var actualMinDate = result.From.ToUniversalTime().ToString();
Assert.Equal(expectedMinDate, actualMinDate);
var expectedMaxDate = maxDate.ToUniversalTime().ToString();
var actualMaxDate = result.To.ToUniversalTime().ToString();
Assert.Equal(expectedMaxDate, actualMaxDate);
}
[Fact]
public async Task GetByDate_returns_success()
{
// arrange
dbContext.CleanupDbSet<ChangeLog>();
//создаем записи
var count = 5;
var changeLogItems = CreateChangeLogItems(count, (-15, 15));
var idDiscriminator = changeLogItems.Item1;
var entities = changeLogItems.Item2;
//удаляем все созданные записи за исключением первой и второй
//даты 2-х оставшихся записей должны вернуться в методе GetByDate
var ids = entities.Select(e => e.Id);
var idsToDelete = ids.Skip(2);
var deletedCount = await client.DeleteRange(idsToDelete, new CancellationToken());
var filterRequest = new SectionPartRequest()
{
DepthStart = 0,
DepthEnd = 1000,
};
var paginationRequest = new PaginationRequest()
{
Skip = 0,
Take = 10,
SortSettings = String.Empty,
};
var moment = DateTimeOffset.UtcNow.AddDays(16);
var result = await client.GetByDate(idDiscriminator, moment, filterRequest, paginationRequest, new CancellationToken());
Assert.NotNull(result);
var restEntities = entities.Where(e => !idsToDelete.Contains(e.Id));
Assert.Equal(restEntities.Count(), result.Count);
var actualIds = restEntities.Select(e => e.Id);
var expectedIds = result.Items.Select(e => e.Id);
Assert.Equivalent(expectedIds, actualIds);
}
[Theory]
[InlineData(5, -15, 15, -20, 20, 10)]
[InlineData(5, -15, -10, -16, -9, 5)]
public async Task GetChangeLogForInterval_returns_success(
int insertedCount,
int daysBeforeNowChangeLog,
int daysAfterNowChangeLog,
int daysBeforeNowFilter,
int daysAfterNowFilter,
int changeLogCount)
{
// arrange
dbContext.CleanupDbSet<ChangeLog>();
//создаем записи
var count = insertedCount;
var daysRange = (daysBeforeNowChangeLog, daysAfterNowChangeLog);
var changeLogItems = CreateChangeLogItems(count, daysRange);
var idDiscriminator = changeLogItems.Item1;
var entities = changeLogItems.Item2;
foreach (var entity in entities)
{
entity.DepthEnd += 10;
}
var dtos = entities.Select(e => e.Adapt<DataWithWellDepthAndSectionDto>()).ToArray();
await client.UpdateRange(dtos, new CancellationToken());
//act
var dateBegin = DateTimeOffset.UtcNow.AddDays(daysBeforeNowFilter);
var dateEnd = DateTimeOffset.UtcNow.AddDays(daysAfterNowFilter);
var result = await client.GetChangeLogForInterval(idDiscriminator, dateBegin, dateEnd, new CancellationToken());
//assert
Assert.NotNull(result);
Assert.Equal(changeLogCount, result.Count());
}
private static IEnumerable<DataWithWellDepthAndSectionDto> Generate(int count)
{
for (int i = 0; i < count; i++)
yield return new DataWithWellDepthAndSectionDto()
{
Value = new Dictionary<string, object>()
{
{ "Key", 1 }
},
DepthStart = generatorRandomDigits.Next(1, 5),
DepthEnd = generatorRandomDigits.Next(5, 15),
Id = Guid.NewGuid(),
IdSection = Guid.NewGuid()
};
}
private (Guid, ChangeLog[]) CreateChangeLogItems(int count, (int, int) daysRange)
{
var minDayCount = daysRange.Item1;
var maxDayCount = daysRange.Item2;
Guid idDiscriminator = Guid.NewGuid();
var dtos = Generate(count);
var entities = dtos.Select(d =>
{
var entity = d.Adapt<ChangeLog>();
entity.IdDiscriminator = idDiscriminator;
entity.Creation = DateTimeOffset.UtcNow.AddDays(generatorRandomDigits.Next(minDayCount, maxDayCount));
return entity;
}).ToArray();
dbContext.ChangeLog.AddRange(entities);
dbContext.SaveChanges();
return (idDiscriminator, entities);
}
}
//using Mapster;
//using Microsoft.EntityFrameworkCore;
//using Microsoft.Extensions.DependencyInjection;
//using DD.Persistence.Database.Model;
//using DD.Persistence.Models;
//using DD.Persistence.Models.Requests;
//using Xunit;
//using DD.Persistence.Client.Clients.Interfaces;
//using DD.Persistence.Client;
//namespace DD.Persistence.IntegrationTests.Controllers;
//public class ChangeLogControllerTest : BaseIntegrationTest
//{
// private readonly IChangeLogClient client;
// private static readonly Random generatorRandomDigits = new();
// public ChangeLogControllerTest(WebAppFactoryFixture factory) : base(factory)
// {
// var persistenceClientFactory = scope.ServiceProvider
// .GetRequiredService<PersistenceClientFactory>();
// client = persistenceClientFactory.GetChangeLogClient();
// }
// [Fact]
// public async Task ClearAndInsertRange_InEmptyDb()
// {
// // arrange
// dbContext.CleanupDbSet<ChangeLog>();
// var idDiscriminator = Guid.NewGuid();
// var dtos = Generate(2);
// // act
// var result = await client.ClearAndAddRange(idDiscriminator, dtos, new CancellationToken());
// // assert
// Assert.Equal(2, result);
// }
// [Fact]
// public async Task ClearAndInsertRange_InNotEmptyDb()
// {
// // arrange
// var insertedCount = 10;
// var createdResult = CreateChangeLogItems(insertedCount, (-15, 15));
// var idDiscriminator = createdResult.Item1;
// var dtos = createdResult.Item2.Select(e => e.Adapt<DataWithWellDepthAndSectionDto>());
// // act
// var result = await client.ClearAndAddRange(idDiscriminator, dtos, new CancellationToken());
// // assert
// Assert.Equal(insertedCount*2, result);
// }
// [Fact]
// public async Task Add_returns_success()
// {
// // arrange
// var count = 1;
// var idDiscriminator = Guid.NewGuid();
// var dtos = Generate(count);
// var dto = dtos.FirstOrDefault()!;
// // act
// var result = await client.Add(idDiscriminator, dto, new CancellationToken());
// // assert
// Assert.Equal(count, result);
// }
// [Fact]
// public async Task AddRange_returns_success()
// {
// // arrange
// var count = 3;
// var idDiscriminator = Guid.NewGuid();
// var dtos = Generate(count);
// // act
// var result = await client.AddRange(idDiscriminator, dtos, new CancellationToken());
// // assert
// Assert.Equal(count, result);
// }
// [Fact]
// public async Task Update_returns_success()
// {
// // arrange
// dbContext.CleanupDbSet<ChangeLog>();
// var idDiscriminator = Guid.NewGuid();
// var dtos = Generate(1);
// var dto = dtos.FirstOrDefault()!;
// var result = await client.Add(idDiscriminator, dto, new CancellationToken());
// var entity = dbContext.ChangeLog
// .Where(x => x.IdDiscriminator == idDiscriminator)
// .FirstOrDefault();
// dto = entity.Adapt<DataWithWellDepthAndSectionDto>();
// dto.DepthEnd += 10;
// // act
// result = await client.Update(dto, new CancellationToken());
// // assert
// Assert.Equal(2, result);
// var dateBegin = DateTimeOffset.UtcNow.AddDays(-1);
// var dateEnd = DateTimeOffset.UtcNow.AddDays(1);
// var changeLogResult = await client.GetChangeLogForInterval(idDiscriminator, dateBegin, dateEnd, new CancellationToken());
// Assert.NotNull(changeLogResult);
// var obsoleteDto = changeLogResult
// .Where(e => e.Obsolete.HasValue)
// .FirstOrDefault();
// var activeDto = changeLogResult
// .Where(e => !e.Obsolete.HasValue)
// .FirstOrDefault();
// if (obsoleteDto == null || activeDto == null)
// {
// Assert.Fail();
// return;
// }
// Assert.Equal(activeDto.Id, obsoleteDto.IdNext);
// }
// [Fact]
// public async Task UpdateRange_returns_success()
// {
// // arrange
// var count = 2;
// var dtos = Generate(count);
// var entities = dtos.Select(d => d.Adapt<ChangeLog>()).ToArray();
// dbContext.ChangeLog.AddRange(entities);
// dbContext.SaveChanges();
// dtos = entities.Select(c => new DataWithWellDepthAndSectionDto()
// {
// DepthEnd = c.DepthEnd + 10,
// DepthStart = c.DepthStart + 10,
// Id = c.Id,
// IdSection = c.IdSection,
// Value = c.Value
// }).ToArray();
// // act
// var result = await client.UpdateRange(dtos, new CancellationToken());
// // assert
// Assert.Equal(count * 2, result);
// }
// [Fact]
// public async Task Delete_returns_success()
// {
// // arrange
// var dtos = Generate(1);
// var dto = dtos.FirstOrDefault()!;
// var entity = dto.Adapt<ChangeLog>();
// dbContext.ChangeLog.Add(entity);
// dbContext.SaveChanges();
// // act
// var result = await client.Delete(entity.Id, new CancellationToken());
// // assert
// Assert.Equal(1, result);
// }
// [Fact]
// public async Task DeleteRange_returns_success()
// {
// // arrange
// var count = 10;
// var dtos = Generate(count);
// var entities = dtos.Select(d => d.Adapt<ChangeLog>()).ToArray();
// dbContext.ChangeLog.AddRange(entities);
// dbContext.SaveChanges();
// // act
// var ids = entities.Select(e => e.Id);
// var result = await client.DeleteRange(ids, new CancellationToken());
// // assert
// Assert.Equal(count, result);
// }
// [Fact]
// public async Task GetDatesRange_returns_success()
// {
// // arrange
// var changeLogItems = CreateChangeLogItems(3, (-15, 15));
// var idDiscriminator = changeLogItems.Item1;
// var entities = changeLogItems.Item2.OrderBy(e => e.Creation);
// // act
// var result = await client.GetDatesRange(idDiscriminator, new CancellationToken());
// // assert
// Assert.NotNull(result);
// var minDate = entities.First().Creation;
// var maxDate = entities.Last().Creation;
// var expectedMinDate = minDate.ToUniversalTime().ToString();
// var actualMinDate = result.From.ToUniversalTime().ToString();
// Assert.Equal(expectedMinDate, actualMinDate);
// var expectedMaxDate = maxDate.ToUniversalTime().ToString();
// var actualMaxDate = result.To.ToUniversalTime().ToString();
// Assert.Equal(expectedMaxDate, actualMaxDate);
// }
// [Fact]
// public async Task GetByDate_returns_success()
// {
// // arrange
// dbContext.CleanupDbSet<ChangeLog>();
// //создаем записи
// var count = 5;
// var changeLogItems = CreateChangeLogItems(count, (-15, 15));
// var idDiscriminator = changeLogItems.Item1;
// var entities = changeLogItems.Item2;
// //удаляем все созданные записи за исключением первой и второй
// //даты 2-х оставшихся записей должны вернуться в методе GetByDate
// var ids = entities.Select(e => e.Id);
// var idsToDelete = ids.Skip(2);
// var deletedCount = await client.DeleteRange(idsToDelete, new CancellationToken());
// var filterRequest = new SectionPartRequest()
// {
// DepthStart = 0,
// DepthEnd = 1000,
// };
// var paginationRequest = new PaginationRequest()
// {
// Skip = 0,
// Take = 10,
// SortSettings = String.Empty,
// };
// var moment = DateTimeOffset.UtcNow.AddDays(16);
// var result = await client.GetByDate(idDiscriminator, moment, filterRequest, paginationRequest, new CancellationToken());
// Assert.NotNull(result);
// var restEntities = entities.Where(e => !idsToDelete.Contains(e.Id));
// Assert.Equal(restEntities.Count(), result.Count);
// var actualIds = restEntities.Select(e => e.Id);
// var expectedIds = result.Items.Select(e => e.Id);
// Assert.Equivalent(expectedIds, actualIds);
// }
// [Theory]
// [InlineData(5, -15, 15, -20, 20, 10)]
// [InlineData(5, -15, -10, -16, -9, 5)]
// public async Task GetChangeLogForInterval_returns_success(
// int insertedCount,
// int daysBeforeNowChangeLog,
// int daysAfterNowChangeLog,
// int daysBeforeNowFilter,
// int daysAfterNowFilter,
// int changeLogCount)
// {
// // arrange
// dbContext.CleanupDbSet<ChangeLog>();
// //создаем записи
// var count = insertedCount;
// var daysRange = (daysBeforeNowChangeLog, daysAfterNowChangeLog);
// var changeLogItems = CreateChangeLogItems(count, daysRange);
// var idDiscriminator = changeLogItems.Item1;
// var entities = changeLogItems.Item2;
// foreach (var entity in entities)
// {
// entity.DepthEnd += 10;
// }
// var dtos = entities.Select(e => e.Adapt<DataWithWellDepthAndSectionDto>()).ToArray();
// await client.UpdateRange(dtos, new CancellationToken());
// //act
// var dateBegin = DateTimeOffset.UtcNow.AddDays(daysBeforeNowFilter);
// var dateEnd = DateTimeOffset.UtcNow.AddDays(daysAfterNowFilter);
// var result = await client.GetChangeLogForInterval(idDiscriminator, dateBegin, dateEnd, new CancellationToken());
// //assert
// Assert.NotNull(result);
// Assert.Equal(changeLogCount, result.Count());
// }
// private static IEnumerable<DataWithWellDepthAndSectionDto> Generate(int count)
// {
// for (int i = 0; i < count; i++)
// yield return new DataWithWellDepthAndSectionDto()
// {
// Value = new Dictionary<string, object>()
// {
// { "Key", 1 }
// },
// DepthStart = generatorRandomDigits.Next(1, 5),
// DepthEnd = generatorRandomDigits.Next(5, 15),
// Id = Guid.NewGuid(),
// IdSection = Guid.NewGuid()
// };
// }
// private (Guid, ChangeLog[]) CreateChangeLogItems(int count, (int, int) daysRange)
// {
// var minDayCount = daysRange.Item1;
// var maxDayCount = daysRange.Item2;
// Guid idDiscriminator = Guid.NewGuid();
// var dtos = Generate(count);
// var entities = dtos.Select(d =>
// {
// var entity = d.Adapt<ChangeLog>();
// entity.IdDiscriminator = idDiscriminator;
// entity.Creation = DateTimeOffset.UtcNow.AddDays(generatorRandomDigits.Next(minDayCount, maxDayCount));
// return entity;
// }).ToArray();
// dbContext.ChangeLog.AddRange(entities);
// dbContext.SaveChanges();
// return (idDiscriminator, entities);
// }
//}

View File

@ -1,85 +1,85 @@
using DD.Persistence.Database.Model;
using DD.Persistence.Models;
using Xunit;
//using DD.Persistence.Database.Model;
//using DD.Persistence.Models;
//using Xunit;
namespace DD.Persistence.IntegrationTests.Controllers;
public class DataSaubControllerTest : TimeSeriesBaseControllerTest<DataSaub, DataSaubDto>
{
private readonly DataSaubDto dto = new()
{
AxialLoad = 1,
BitDepth = 2,
BlockPosition = 3,
BlockSpeed = 4,
Date = DateTimeOffset.UtcNow,
Flow = 5,
HookWeight = 6,
IdFeedRegulator = 8,
Mode = 9,
Mse = 10,
MseState = 11,
Pressure = 12,
Pump0Flow = 13,
Pump1Flow = 14,
Pump2Flow = 15,
RotorSpeed = 16,
RotorTorque = 17,
User = string.Empty,
WellDepth = 18,
};
//namespace DD.Persistence.IntegrationTests.Controllers;
//public class DataSaubControllerTest : TimeSeriesBaseControllerTest<DataSaub, DataSaubDto>
//{
// private readonly DataSaubDto dto = new()
// {
// AxialLoad = 1,
// BitDepth = 2,
// BlockPosition = 3,
// BlockSpeed = 4,
// Date = DateTimeOffset.UtcNow,
// Flow = 5,
// HookWeight = 6,
// IdFeedRegulator = 8,
// Mode = 9,
// Mse = 10,
// MseState = 11,
// Pressure = 12,
// Pump0Flow = 13,
// Pump1Flow = 14,
// Pump2Flow = 15,
// RotorSpeed = 16,
// RotorTorque = 17,
// User = string.Empty,
// WellDepth = 18,
// };
private readonly DataSaub entity = new()
{
AxialLoad = 1,
BitDepth = 2,
BlockPosition = 3,
BlockSpeed = 4,
Date = DateTimeOffset.UtcNow,
Flow = 5,
HookWeight = 6,
IdFeedRegulator = 8,
Mode = 9,
Mse = 10,
MseState = 11,
Pressure = 12,
Pump0Flow = 13,
Pump1Flow = 14,
Pump2Flow = 15,
RotorSpeed = 16,
RotorTorque = 17,
User = string.Empty,
WellDepth = 18,
};
// private readonly DataSaub entity = new()
// {
// AxialLoad = 1,
// BitDepth = 2,
// BlockPosition = 3,
// BlockSpeed = 4,
// Date = DateTimeOffset.UtcNow,
// Flow = 5,
// HookWeight = 6,
// IdFeedRegulator = 8,
// Mode = 9,
// Mse = 10,
// MseState = 11,
// Pressure = 12,
// Pump0Flow = 13,
// Pump1Flow = 14,
// Pump2Flow = 15,
// RotorSpeed = 16,
// RotorTorque = 17,
// User = string.Empty,
// WellDepth = 18,
// };
public DataSaubControllerTest(WebAppFactoryFixture factory) : base(factory)
{
}
// public DataSaubControllerTest(WebAppFactoryFixture factory) : base(factory)
// {
// }
[Fact]
public async Task InsertRange_returns_success()
{
await InsertRangeSuccess(dto);
}
// [Fact]
// public async Task InsertRange_returns_success()
// {
// await InsertRangeSuccess(dto);
// }
[Fact]
public async Task Get_returns_success()
{
var beginDate = DateTimeOffset.UtcNow.AddDays(-1);
var endDate = DateTimeOffset.UtcNow;
await GetSuccess(beginDate, endDate, entity);
}
// [Fact]
// public async Task Get_returns_success()
// {
// var beginDate = DateTimeOffset.UtcNow.AddDays(-1);
// var endDate = DateTimeOffset.UtcNow;
// await GetSuccess(beginDate, endDate, entity);
// }
[Fact]
public async Task GetDatesRange_returns_success()
{
await GetDatesRangeSuccess(entity);
}
// [Fact]
// public async Task GetDatesRange_returns_success()
// {
// await GetDatesRangeSuccess(entity);
// }
[Fact]
public async Task GetResampledData_returns_success()
{
await GetResampledDataSuccess(entity);
}
}
// [Fact]
// public async Task GetResampledData_returns_success()
// {
// await GetResampledDataSuccess(entity);
// }
//}

View File

@ -1,82 +1,82 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using DD.Persistence.Client;
using DD.Persistence.Client.Clients;
using DD.Persistence.Client.Clients.Interfaces;
using DD.Persistence.Database.Entity;
using DD.Persistence.Models;
using Xunit;
//using Microsoft.Extensions.Caching.Memory;
//using Microsoft.Extensions.DependencyInjection;
//using DD.Persistence.Client;
//using DD.Persistence.Client.Clients;
//using DD.Persistence.Client.Clients.Interfaces;
//using DD.Persistence.Database.Entity;
//using DD.Persistence.Models;
//using Xunit;
namespace DD.Persistence.IntegrationTests.Controllers
{
public class DataSourceSystemControllerTest : BaseIntegrationTest
{
private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey";
private readonly IDataSourceSystemClient dataSourceSystemClient;
private readonly IMemoryCache memoryCache;
public DataSourceSystemControllerTest(WebAppFactoryFixture factory) : base(factory)
{
var scope = factory.Services.CreateScope();
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
//namespace DD.Persistence.IntegrationTests.Controllers
//{
// public class DataSourceSystemControllerTest : BaseIntegrationTest
// {
// private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey";
// private readonly IDataSourceSystemClient dataSourceSystemClient;
// private readonly IMemoryCache memoryCache;
// public DataSourceSystemControllerTest(WebAppFactoryFixture factory) : base(factory)
// {
// var scope = factory.Services.CreateScope();
// var persistenceClientFactory = scope.ServiceProvider
// .GetRequiredService<PersistenceClientFactory>();
dataSourceSystemClient = persistenceClientFactory.GetDataSourceSystemClient();
memoryCache = scope.ServiceProvider.GetRequiredService<IMemoryCache>();
}
// dataSourceSystemClient = persistenceClientFactory.GetDataSourceSystemClient();
// memoryCache = scope.ServiceProvider.GetRequiredService<IMemoryCache>();
// }
[Fact]
public async Task Get_returns_success()
{
//arrange
memoryCache.Remove(SystemCacheKey);
dbContext.CleanupDbSet<DataSourceSystem>();
// [Fact]
// public async Task Get_returns_success()
// {
// //arrange
// memoryCache.Remove(SystemCacheKey);
// dbContext.CleanupDbSet<DataSourceSystem>();
//act
var response = await dataSourceSystemClient.Get(CancellationToken.None);
// //act
// var response = await dataSourceSystemClient.Get(CancellationToken.None);
//assert
Assert.NotNull(response);
Assert.Empty(response);
}
// //assert
// Assert.NotNull(response);
// Assert.Empty(response);
// }
[Fact]
public async Task Get_AfterSave_returns_success()
{
//arrange
await Add();
// [Fact]
// public async Task Get_AfterSave_returns_success()
// {
// //arrange
// await Add();
//act
var response = await dataSourceSystemClient.Get(CancellationToken.None);
// //act
// var response = await dataSourceSystemClient.Get(CancellationToken.None);
//assert
Assert.NotNull(response);
// //assert
// Assert.NotNull(response);
var expectedSystemCount = 1;
var actualSystemCount = response!.Count();
Assert.Equal(expectedSystemCount, actualSystemCount);
}
// var expectedSystemCount = 1;
// var actualSystemCount = response!.Count();
// Assert.Equal(expectedSystemCount, actualSystemCount);
// }
[Fact]
public async Task Add_returns_success()
{
await Add();
}
// [Fact]
// public async Task Add_returns_success()
// {
// await Add();
// }
private async Task Add()
{
//arrange
memoryCache.Remove(SystemCacheKey);
dbContext.CleanupDbSet<DataSourceSystem>();
// private async Task Add()
// {
// //arrange
// memoryCache.Remove(SystemCacheKey);
// dbContext.CleanupDbSet<DataSourceSystem>();
var dto = new DataSourceSystemDto()
{
SystemId = Guid.NewGuid(),
Name = "Test",
Description = "Test"
};
// var dto = new DataSourceSystemDto()
// {
// SystemId = Guid.NewGuid(),
// Name = "Test",
// Description = "Test"
// };
//act
await dataSourceSystemClient.Add(dto, CancellationToken.None);
}
}
}
// //act
// await dataSourceSystemClient.Add(dto, CancellationToken.None);
// }
// }
//}

View File

@ -1,227 +1,227 @@
using Microsoft.Extensions.DependencyInjection;
using DD.Persistence.Client;
using DD.Persistence.Client.Clients.Interfaces;
using DD.Persistence.Database.Model;
using System.Net;
using Xunit;
//using Microsoft.Extensions.DependencyInjection;
//using DD.Persistence.Client;
//using DD.Persistence.Client.Clients.Interfaces;
//using DD.Persistence.Database.Model;
//using System.Net;
//using Xunit;
namespace DD.Persistence.IntegrationTests.Controllers
{
public class SetpointControllerTest : BaseIntegrationTest
{
private readonly ISetpointClient setpointClient;
private class TestObject
{
public string? Value1 { get; set; }
public int? Value2 { get; set; }
}
public SetpointControllerTest(WebAppFactoryFixture factory) : base(factory)
{
var scope = factory.Services.CreateScope();
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
//namespace DD.Persistence.IntegrationTests.Controllers
//{
// public class SetpointControllerTest : BaseIntegrationTest
// {
// private readonly ISetpointClient setpointClient;
// private class TestObject
// {
// public string? Value1 { get; set; }
// public int? Value2 { get; set; }
// }
// public SetpointControllerTest(WebAppFactoryFixture factory) : base(factory)
// {
// var scope = factory.Services.CreateScope();
// var persistenceClientFactory = scope.ServiceProvider
// .GetRequiredService<PersistenceClientFactory>();
setpointClient = persistenceClientFactory.GetSetpointClient();
}
// setpointClient = persistenceClientFactory.GetSetpointClient();
// }
[Fact]
public async Task GetCurrent_returns_success()
{
//arrange
var setpointKeys = new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
};
// [Fact]
// public async Task GetCurrent_returns_success()
// {
// //arrange
// var setpointKeys = new List<Guid>()
// {
// Guid.NewGuid(),
// Guid.NewGuid()
// };
//act
var response = await setpointClient.GetCurrent(setpointKeys, new CancellationToken());
// //act
// var response = await setpointClient.GetCurrent(setpointKeys, new CancellationToken());
//assert
Assert.NotNull(response);
Assert.Empty(response);
}
// //assert
// Assert.NotNull(response);
// Assert.Empty(response);
// }
[Fact]
public async Task GetCurrent_AfterSave_returns_success()
{
//arrange
var setpointKey = await Add();
// [Fact]
// public async Task GetCurrent_AfterSave_returns_success()
// {
// //arrange
// var setpointKey = await Add();
//act
var response = await setpointClient.GetCurrent([setpointKey], new CancellationToken());
// //act
// var response = await setpointClient.GetCurrent([setpointKey], new CancellationToken());
//assert
Assert.NotNull(response);
Assert.NotEmpty(response);
Assert.Equal(setpointKey, response.FirstOrDefault()?.Key);
}
// //assert
// Assert.NotNull(response);
// Assert.NotEmpty(response);
// Assert.Equal(setpointKey, response.FirstOrDefault()?.Key);
// }
[Fact]
public async Task GetHistory_returns_success()
{
//arrange
var setpointKeys = new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
};
var historyMoment = DateTimeOffset.UtcNow;
// [Fact]
// public async Task GetHistory_returns_success()
// {
// //arrange
// var setpointKeys = new List<Guid>()
// {
// Guid.NewGuid(),
// Guid.NewGuid()
// };
// var historyMoment = DateTimeOffset.UtcNow;
//act
var response = await setpointClient.GetHistory(setpointKeys, historyMoment, new CancellationToken());
// //act
// var response = await setpointClient.GetHistory(setpointKeys, historyMoment, new CancellationToken());
//assert
Assert.NotNull(response);
Assert.Empty(response);
}
// //assert
// Assert.NotNull(response);
// Assert.Empty(response);
// }
[Fact]
public async Task GetHistory_AfterSave_returns_success()
{
//arrange
var setpointKey = await Add();
var historyMoment = DateTimeOffset.UtcNow;
historyMoment = historyMoment.AddDays(1);
// [Fact]
// public async Task GetHistory_AfterSave_returns_success()
// {
// //arrange
// var setpointKey = await Add();
// var historyMoment = DateTimeOffset.UtcNow;
// historyMoment = historyMoment.AddDays(1);
//act
var response = await setpointClient.GetHistory([setpointKey], historyMoment, new CancellationToken());
// //act
// var response = await setpointClient.GetHistory([setpointKey], historyMoment, new CancellationToken());
//assert
Assert.NotNull(response);
Assert.NotEmpty(response);
Assert.Equal(setpointKey, response.FirstOrDefault()?.Key);
}
// //assert
// Assert.NotNull(response);
// Assert.NotEmpty(response);
// Assert.Equal(setpointKey, response.FirstOrDefault()?.Key);
// }
[Fact]
public async Task GetLog_returns_success()
{
//arrange
var setpointKeys = new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
};
// [Fact]
// public async Task GetLog_returns_success()
// {
// //arrange
// var setpointKeys = new List<Guid>()
// {
// Guid.NewGuid(),
// Guid.NewGuid()
// };
//act
var response = await setpointClient.GetLog(setpointKeys, new CancellationToken());
// //act
// var response = await setpointClient.GetLog(setpointKeys, new CancellationToken());
//assert
Assert.NotNull(response);
Assert.Empty(response);
}
// //assert
// Assert.NotNull(response);
// Assert.Empty(response);
// }
[Fact]
public async Task GetLog_AfterSave_returns_success()
{
//arrange
var setpointKey = await Add();
// [Fact]
// public async Task GetLog_AfterSave_returns_success()
// {
// //arrange
// var setpointKey = await Add();
//act
var response = await setpointClient.GetLog([setpointKey], new CancellationToken());
// //act
// var response = await setpointClient.GetLog([setpointKey], new CancellationToken());
//assert
Assert.NotNull(response);
Assert.NotEmpty(response);
Assert.Equal(setpointKey, response.FirstOrDefault().Key);
}
// //assert
// Assert.NotNull(response);
// Assert.NotEmpty(response);
// Assert.Equal(setpointKey, response.FirstOrDefault().Key);
// }
[Fact]
public async Task GetDatesRange_returns_success()
{
//arrange
dbContext.CleanupDbSet<Setpoint>();
// [Fact]
// public async Task GetDatesRange_returns_success()
// {
// //arrange
// dbContext.CleanupDbSet<Setpoint>();
//act
var response = await setpointClient.GetDatesRangeAsync(CancellationToken.None);
// //act
// var response = await setpointClient.GetDatesRangeAsync(CancellationToken.None);
//assert
Assert.NotNull(response);
Assert.Equal(DateTimeOffset.MinValue, response!.From);
Assert.Equal(DateTimeOffset.MaxValue, response!.To);
}
// //assert
// Assert.NotNull(response);
// Assert.Equal(DateTimeOffset.MinValue, response!.From);
// Assert.Equal(DateTimeOffset.MaxValue, response!.To);
// }
[Fact]
public async Task GetDatesRange_AfterSave_returns_success()
{
//arrange
dbContext.CleanupDbSet<Setpoint>();
// [Fact]
// public async Task GetDatesRange_AfterSave_returns_success()
// {
// //arrange
// dbContext.CleanupDbSet<Setpoint>();
await Add();
// await Add();
var dateBegin = DateTimeOffset.MinValue;
var take = 1;
var part = await setpointClient.GetPart(dateBegin, take, CancellationToken.None);
// var dateBegin = DateTimeOffset.MinValue;
// var take = 1;
// var part = await setpointClient.GetPart(dateBegin, take, CancellationToken.None);
//act
var response = await setpointClient.GetDatesRangeAsync(CancellationToken.None);
// //act
// var response = await setpointClient.GetDatesRangeAsync(CancellationToken.None);
//assert
Assert.NotNull(response);
// //assert
// Assert.NotNull(response);
var expectedValue = part!
.FirstOrDefault()!.Created
.ToString("dd.MM.yyyy-HH:mm:ss");
var actualValueFrom = response.From
.ToString("dd.MM.yyyy-HH:mm:ss");
Assert.Equal(expectedValue, actualValueFrom);
// var expectedValue = part!
// .FirstOrDefault()!.Created
// .ToString("dd.MM.yyyy-HH:mm:ss");
// var actualValueFrom = response.From
// .ToString("dd.MM.yyyy-HH:mm:ss");
// Assert.Equal(expectedValue, actualValueFrom);
var actualValueTo = response.To
.ToString("dd.MM.yyyy-HH:mm:ss");
Assert.Equal(expectedValue, actualValueTo);
}
// var actualValueTo = response.To
// .ToString("dd.MM.yyyy-HH:mm:ss");
// Assert.Equal(expectedValue, actualValueTo);
// }
[Fact]
public async Task GetPart_returns_success()
{
//arrange
var dateBegin = DateTimeOffset.UtcNow;
var take = 2;
// [Fact]
// public async Task GetPart_returns_success()
// {
// //arrange
// var dateBegin = DateTimeOffset.UtcNow;
// var take = 2;
//act
var response = await setpointClient.GetPart(dateBegin, take, CancellationToken.None);
// //act
// var response = await setpointClient.GetPart(dateBegin, take, CancellationToken.None);
//assert
Assert.NotNull(response);
Assert.Empty(response);
}
// //assert
// Assert.NotNull(response);
// Assert.Empty(response);
// }
[Fact]
public async Task GetPart_AfterSave_returns_success()
{
//arrange
var dateBegin = DateTimeOffset.UtcNow;
var take = 1;
await Add();
// [Fact]
// public async Task GetPart_AfterSave_returns_success()
// {
// //arrange
// var dateBegin = DateTimeOffset.UtcNow;
// var take = 1;
// await Add();
//act
var response = await setpointClient.GetPart(dateBegin, take, CancellationToken.None);
// //act
// var response = await setpointClient.GetPart(dateBegin, take, CancellationToken.None);
//assert
Assert.NotNull(response);
Assert.NotEmpty(response);
}
// //assert
// Assert.NotNull(response);
// Assert.NotEmpty(response);
// }
[Fact]
public async Task Save_returns_success()
{
await Add();
}
// [Fact]
// public async Task Save_returns_success()
// {
// await Add();
// }
private async Task<Guid> Add()
{
//arrange
var setpointKey = Guid.NewGuid();
var setpointValue = new TestObject()
{
Value1 = "1",
Value2 = 2
};
// private async Task<Guid> Add()
// {
// //arrange
// var setpointKey = Guid.NewGuid();
// var setpointValue = new TestObject()
// {
// Value1 = "1",
// Value2 = 2
// };
//act
await setpointClient.Add(setpointKey, setpointValue, new CancellationToken());
// //act
// await setpointClient.Add(setpointKey, setpointValue, new CancellationToken());
return setpointKey;
}
}
}
// return setpointKey;
// }
// }
//}

View File

@ -1,118 +1,118 @@
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using DD.Persistence.Client;
using DD.Persistence.Client.Clients.Interfaces;
using DD.Persistence.Database.Model;
using System.Net;
using Xunit;
//using Mapster;
//using Microsoft.Extensions.DependencyInjection;
//using DD.Persistence.Client;
//using DD.Persistence.Client.Clients.Interfaces;
//using DD.Persistence.Database.Model;
//using System.Net;
//using Xunit;
namespace DD.Persistence.IntegrationTests.Controllers;
//namespace DD.Persistence.IntegrationTests.Controllers;
public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrationTest
where TEntity : class, ITimestampedData, new()
where TDto : class, new()
{
private readonly ITimeSeriesClient<TDto> timeSeriesClient;
//public abstract class TimeSeriesBaseControllerTest<TEntity, TDto> : BaseIntegrationTest
// where TEntity : class, ITimestampedData, new()
// where TDto : class, new()
//{
// private readonly ITimeSeriesClient<TDto> timeSeriesClient;
public TimeSeriesBaseControllerTest(WebAppFactoryFixture factory) : base(factory)
{
dbContext.CleanupDbSet<TEntity>();
// public TimeSeriesBaseControllerTest(WebAppFactoryFixture factory) : base(factory)
// {
// dbContext.CleanupDbSet<TEntity>();
var scope = factory.Services.CreateScope();
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
// var scope = factory.Services.CreateScope();
// var persistenceClientFactory = scope.ServiceProvider
// .GetRequiredService<PersistenceClientFactory>();
timeSeriesClient = persistenceClientFactory.GetTimeSeriesClient<TDto>();
}
// timeSeriesClient = persistenceClientFactory.GetTimeSeriesClient<TDto>();
// }
public async Task InsertRangeSuccess(TDto dto)
{
//arrange
var expected = dto.Adapt<TDto>();
// public async Task InsertRangeSuccess(TDto dto)
// {
// //arrange
// var expected = dto.Adapt<TDto>();
//act
var response = await timeSeriesClient.AddRange(new TDto[] { expected }, new CancellationToken());
// //act
// var response = await timeSeriesClient.AddRange(new TDto[] { expected }, new CancellationToken());
//assert
Assert.Equal(1, response);
}
// //assert
// Assert.Equal(1, response);
// }
public async Task GetSuccess(DateTimeOffset beginDate, DateTimeOffset endDate, TEntity entity)
{
//arrange
var dbset = dbContext.Set<TEntity>();
// public async Task GetSuccess(DateTimeOffset beginDate, DateTimeOffset endDate, TEntity entity)
// {
// //arrange
// var dbset = dbContext.Set<TEntity>();
dbset.Add(entity);
// dbset.Add(entity);
dbContext.SaveChanges();
// dbContext.SaveChanges();
var response = await timeSeriesClient.Get(beginDate, endDate, new CancellationToken());
// var response = await timeSeriesClient.Get(beginDate, endDate, new CancellationToken());
//assert
Assert.NotNull(response);
Assert.Single(response);
}
// //assert
// Assert.NotNull(response);
// Assert.Single(response);
// }
public async Task GetDatesRangeSuccess(TEntity entity)
{
//arrange
var datesRangeExpected = 30;
// public async Task GetDatesRangeSuccess(TEntity entity)
// {
// //arrange
// var datesRangeExpected = 30;
var entity2 = entity.Adapt<TEntity>();
entity2.Date = entity.Date.AddDays(datesRangeExpected);
// var entity2 = entity.Adapt<TEntity>();
// entity2.Date = entity.Date.AddDays(datesRangeExpected);
var dbset = dbContext.Set<TEntity>();
dbset.Add(entity);
dbset.Add(entity2);
// var dbset = dbContext.Set<TEntity>();
// dbset.Add(entity);
// dbset.Add(entity2);
dbContext.SaveChanges();
// dbContext.SaveChanges();
var response = await timeSeriesClient.GetDatesRange(new CancellationToken());
// var response = await timeSeriesClient.GetDatesRange(new CancellationToken());
//assert
Assert.NotNull(response);
// //assert
// Assert.NotNull(response);
var datesRangeActual = (response.To - response.From).Days;
Assert.Equal(datesRangeExpected, datesRangeActual);
}
// var datesRangeActual = (response.To - response.From).Days;
// Assert.Equal(datesRangeExpected, datesRangeActual);
// }
public async Task GetResampledDataSuccess(TEntity entity)
{
//arrange
var approxPointsCount = 10;
var differenceBetweenStartAndEndDays = 50;
// public async Task GetResampledDataSuccess(TEntity entity)
// {
// //arrange
// var approxPointsCount = 10;
// var differenceBetweenStartAndEndDays = 50;
var entities = new List<TEntity>();
for (var i = 1; i <= differenceBetweenStartAndEndDays; i++)
{
var entity2 = entity.Adapt<TEntity>();
entity2.Date = entity.Date.AddDays(i - 1);
// var entities = new List<TEntity>();
// for (var i = 1; i <= differenceBetweenStartAndEndDays; i++)
// {
// var entity2 = entity.Adapt<TEntity>();
// entity2.Date = entity.Date.AddDays(i - 1);
entities.Add(entity2);
}
// entities.Add(entity2);
// }
var dbset = dbContext.Set<TEntity>();
dbset.AddRange(entities);
// var dbset = dbContext.Set<TEntity>();
// dbset.AddRange(entities);
dbContext.SaveChanges();
// dbContext.SaveChanges();
var response = await timeSeriesClient.GetResampledData(entity.Date.AddMinutes(-1), differenceBetweenStartAndEndDays * 24 * 60 * 60 + 60, approxPointsCount, new CancellationToken());
// var response = await timeSeriesClient.GetResampledData(entity.Date.AddMinutes(-1), differenceBetweenStartAndEndDays * 24 * 60 * 60 + 60, approxPointsCount, new CancellationToken());
//assert
Assert.NotNull(response);
// //assert
// Assert.NotNull(response);
var ratio = entities.Count / approxPointsCount;
if (ratio > 1)
{
var expectedResampledCount = entities
.Where((_, index) => index % ratio == 0)
.Count();
// var ratio = entities.Count / approxPointsCount;
// if (ratio > 1)
// {
// var expectedResampledCount = entities
// .Where((_, index) => index % ratio == 0)
// .Count();
Assert.Equal(expectedResampledCount, response.Count());
}
else
{
Assert.Equal(entities.Count(), response.Count());
}
}
}
// Assert.Equal(expectedResampledCount, response.Count());
// }
// else
// {
// Assert.Equal(entities.Count(), response.Count());
// }
// }
//}

View File

@ -1,206 +1,206 @@
using Microsoft.Extensions.DependencyInjection;
using DD.Persistence.Client;
using DD.Persistence.Client.Clients.Interfaces;
using DD.Persistence.Models;
using Xunit;
//using Microsoft.Extensions.DependencyInjection;
//using DD.Persistence.Client;
//using DD.Persistence.Client.Clients.Interfaces;
//using DD.Persistence.Models;
//using Xunit;
namespace DD.Persistence.IntegrationTests.Controllers;
public class TimestampedSetControllerTest : BaseIntegrationTest
{
private readonly ITimestampedSetClient client;
//namespace DD.Persistence.IntegrationTests.Controllers;
//public class TimestampedSetControllerTest : BaseIntegrationTest
//{
// private readonly ITimestampedSetClient client;
public TimestampedSetControllerTest(WebAppFactoryFixture factory) : base(factory)
{
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
// public TimestampedSetControllerTest(WebAppFactoryFixture factory) : base(factory)
// {
// var persistenceClientFactory = scope.ServiceProvider
// .GetRequiredService<PersistenceClientFactory>();
client = persistenceClientFactory.GetTimestampedSetClient();
}
// client = persistenceClientFactory.GetTimestampedSetClient();
// }
[Fact]
public async Task InsertRange()
{
// arrange
Guid idDiscriminator = Guid.NewGuid();
IEnumerable<TimestampedSetDto> testSets = Generate(10, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
// [Fact]
// public async Task InsertRange()
// {
// // arrange
// Guid idDiscriminator = Guid.NewGuid();
// IEnumerable<TimestampedSetDto> testSets = Generate(10, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
// act
var response = await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// // act
// var response = await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// assert
Assert.Equal(testSets.Count(), response);
}
// // assert
// Assert.Equal(testSets.Count(), response);
// }
[Fact]
public async Task Get_without_filter()
{
// arrange
Guid idDiscriminator = Guid.NewGuid();
int count = 10;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// [Fact]
// public async Task Get_without_filter()
// {
// // arrange
// Guid idDiscriminator = Guid.NewGuid();
// int count = 10;
// IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
// await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// act
var response = await client.Get(idDiscriminator, null, null, 0, int.MaxValue, CancellationToken.None);
// // act
// var response = await client.Get(idDiscriminator, null, null, 0, int.MaxValue, CancellationToken.None);
// assert
Assert.NotNull(response);
Assert.Equal(count, response.Count());
}
// // assert
// Assert.NotNull(response);
// Assert.Equal(count, response.Count());
// }
[Fact]
public async Task Get_with_filter_props()
{
// arrange
Guid idDiscriminator = Guid.NewGuid();
int count = 10;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
string[] props = ["A"];
// [Fact]
// public async Task Get_with_filter_props()
// {
// // arrange
// Guid idDiscriminator = Guid.NewGuid();
// int count = 10;
// IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
// await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// string[] props = ["A"];
// act
var response = await client.Get(idDiscriminator, null, props, 0, int.MaxValue, new CancellationToken());
// // act
// var response = await client.Get(idDiscriminator, null, props, 0, int.MaxValue, new CancellationToken());
// assert
Assert.NotNull(response);
Assert.Equal(count, response.Count());
foreach (var item in response)
{
Assert.Single(item.Set);
var kv = item.Set.First();
Assert.Equal("A", kv.Key);
}
}
// // assert
// Assert.NotNull(response);
// Assert.Equal(count, response.Count());
// foreach (var item in response)
// {
// Assert.Single(item.Set);
// var kv = item.Set.First();
// Assert.Equal("A", kv.Key);
// }
// }
[Fact]
public async Task Get_geDate()
{
// arrange
Guid idDiscriminator = Guid.NewGuid();
int count = 10;
var dateMin = DateTimeOffset.Now;
var dateMax = DateTimeOffset.Now.AddSeconds(count);
IEnumerable<TimestampedSetDto> testSets = Generate(count, dateMin.ToOffset(TimeSpan.FromHours(7)));
var insertResponse = await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
var tail = testSets.OrderBy(t => t.Timestamp).Skip(count / 2).Take(int.MaxValue);
var geDate = tail.First().Timestamp;
var tolerance = TimeSpan.FromSeconds(1);
var expectedCount = tail.Count();
// [Fact]
// public async Task Get_geDate()
// {
// // arrange
// Guid idDiscriminator = Guid.NewGuid();
// int count = 10;
// var dateMin = DateTimeOffset.Now;
// var dateMax = DateTimeOffset.Now.AddSeconds(count);
// IEnumerable<TimestampedSetDto> testSets = Generate(count, dateMin.ToOffset(TimeSpan.FromHours(7)));
// var insertResponse = await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// var tail = testSets.OrderBy(t => t.Timestamp).Skip(count / 2).Take(int.MaxValue);
// var geDate = tail.First().Timestamp;
// var tolerance = TimeSpan.FromSeconds(1);
// var expectedCount = tail.Count();
// act
var response = await client.Get(idDiscriminator, geDate, null, 0, int.MaxValue, CancellationToken.None);
// // act
// var response = await client.Get(idDiscriminator, geDate, null, 0, int.MaxValue, CancellationToken.None);
// assert
Assert.NotNull(response);
Assert.Equal(expectedCount, response.Count());
var minDate = response.Min(t => t.Timestamp);
Assert.Equal(geDate, geDate, tolerance);
}
// // assert
// Assert.NotNull(response);
// Assert.Equal(expectedCount, response.Count());
// var minDate = response.Min(t => t.Timestamp);
// Assert.Equal(geDate, geDate, tolerance);
// }
[Fact]
public async Task Get_with_skip_take()
{
// arrange
Guid idDiscriminator = Guid.NewGuid();
int count = 10;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
var expectedCount = count / 2;
// [Fact]
// public async Task Get_with_skip_take()
// {
// // arrange
// Guid idDiscriminator = Guid.NewGuid();
// int count = 10;
// IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
// await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// var expectedCount = count / 2;
// act
var response = await client.Get(idDiscriminator, null, null, 2, expectedCount, new CancellationToken());
// // act
// var response = await client.Get(idDiscriminator, null, null, 2, expectedCount, new CancellationToken());
// assert
Assert.NotNull(response);
Assert.Equal(expectedCount, response.Count());
}
// // assert
// Assert.NotNull(response);
// Assert.Equal(expectedCount, response.Count());
// }
[Fact]
public async Task Get_with_big_skip_take()
{
// arrange
Guid idDiscriminator = Guid.NewGuid();
var expectedCount = 1;
int count = 10 + expectedCount;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// [Fact]
// public async Task Get_with_big_skip_take()
// {
// // arrange
// Guid idDiscriminator = Guid.NewGuid();
// var expectedCount = 1;
// int count = 10 + expectedCount;
// IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
// await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// act
var response = await client.Get(idDiscriminator, null, null, count - expectedCount, count, new CancellationToken());
// // act
// var response = await client.Get(idDiscriminator, null, null, count - expectedCount, count, new CancellationToken());
// assert
Assert.NotNull(response);
Assert.Equal(expectedCount, response.Count());
}
// // assert
// Assert.NotNull(response);
// Assert.Equal(expectedCount, response.Count());
// }
[Fact]
public async Task GetLast()
{
// arrange
Guid idDiscriminator = Guid.NewGuid();
int count = 10;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
var expectedCount = 8;
// [Fact]
// public async Task GetLast()
// {
// // arrange
// Guid idDiscriminator = Guid.NewGuid();
// int count = 10;
// IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
// await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// var expectedCount = 8;
// act
var response = await client.GetLast(idDiscriminator, null, expectedCount, new CancellationToken());
// // act
// var response = await client.GetLast(idDiscriminator, null, expectedCount, new CancellationToken());
// assert
Assert.NotNull(response);
Assert.Equal(expectedCount, response.Count());
}
// // assert
// Assert.NotNull(response);
// Assert.Equal(expectedCount, response.Count());
// }
[Fact]
public async Task GetDatesRange()
{
// arrange
Guid idDiscriminator = Guid.NewGuid();
int count = 10;
var dateMin = DateTimeOffset.Now;
var dateMax = DateTimeOffset.Now.AddSeconds(count - 1);
IEnumerable<TimestampedSetDto> testSets = Generate(count, dateMin.ToOffset(TimeSpan.FromHours(7)));
await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
var tolerance = TimeSpan.FromSeconds(1);
// [Fact]
// public async Task GetDatesRange()
// {
// // arrange
// Guid idDiscriminator = Guid.NewGuid();
// int count = 10;
// var dateMin = DateTimeOffset.Now;
// var dateMax = DateTimeOffset.Now.AddSeconds(count - 1);
// IEnumerable<TimestampedSetDto> testSets = Generate(count, dateMin.ToOffset(TimeSpan.FromHours(7)));
// await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// var tolerance = TimeSpan.FromSeconds(1);
// act
var response = await client.GetDatesRange(idDiscriminator, new CancellationToken());
// // act
// var response = await client.GetDatesRange(idDiscriminator, new CancellationToken());
// assert
Assert.NotNull(response);
Assert.Equal(dateMin, response.From, tolerance);
Assert.Equal(dateMax, response.To, tolerance);
}
// // assert
// Assert.NotNull(response);
// Assert.Equal(dateMin, response.From, tolerance);
// Assert.Equal(dateMax, response.To, tolerance);
// }
[Fact]
public async Task Count()
{
// arrange
Guid idDiscriminator = Guid.NewGuid();
int count = 144;
IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// [Fact]
// public async Task Count()
// {
// // arrange
// Guid idDiscriminator = Guid.NewGuid();
// int count = 144;
// IEnumerable<TimestampedSetDto> testSets = Generate(count, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
// await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
// act
var response = await client.Count(idDiscriminator, new CancellationToken());
// // act
// var response = await client.Count(idDiscriminator, new CancellationToken());
// assert
Assert.Equal(count, response);
}
// // assert
// Assert.Equal(count, response);
// }
private static IEnumerable<TimestampedSetDto> Generate(int n, DateTimeOffset from)
{
for (int i = 0; i < n; i++)
yield return new TimestampedSetDto
(
from.AddSeconds(i),
new Dictionary<string, object>{
{"A", i },
{"B", i * 1.1 },
{"C", $"Any{i}" },
{"D", DateTimeOffset.Now},
}
);
}
}
// private static IEnumerable<TimestampedSetDto> Generate(int n, DateTimeOffset from)
// {
// for (int i = 0; i < n; i++)
// yield return new TimestampedSetDto
// (
// from.AddSeconds(i),
// new Dictionary<string, object>{
// {"A", i },
// {"B", i * 1.1 },
// {"C", $"Any{i}" },
// {"D", DateTimeOffset.Now},
// }
// );
// }
//}

View File

@ -1,234 +1,234 @@
using Microsoft.Extensions.DependencyInjection;
using DD.Persistence.Database.Entity;
using DD.Persistence.Models;
using System.Net;
using Xunit;
using DD.Persistence.Client.Clients.Interfaces;
using DD.Persistence.Client;
//using Microsoft.Extensions.DependencyInjection;
//using DD.Persistence.Database.Entity;
//using DD.Persistence.Models;
//using System.Net;
//using Xunit;
//using DD.Persistence.Client.Clients.Interfaces;
//using DD.Persistence.Client;
namespace DD.Persistence.IntegrationTests.Controllers;
public class WitsDataControllerTest : BaseIntegrationTest
{
private IWitsDataClient witsDataClient;
//namespace DD.Persistence.IntegrationTests.Controllers;
//public class WitsDataControllerTest : BaseIntegrationTest
//{
// private IWitsDataClient witsDataClient;
public WitsDataControllerTest(WebAppFactoryFixture factory) : base(factory)
{
var scope = factory.Services.CreateScope();
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
// public WitsDataControllerTest(WebAppFactoryFixture factory) : base(factory)
// {
// var scope = factory.Services.CreateScope();
// var persistenceClientFactory = scope.ServiceProvider
// .GetRequiredService<PersistenceClientFactory>();
witsDataClient = persistenceClientFactory.GetWitsDataClient();
}
// witsDataClient = persistenceClientFactory.GetWitsDataClient();
// }
[Fact]
public async Task GetDatesRangeAsync_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
// [Fact]
// public async Task GetDatesRangeAsync_returns_success()
// {
// //arrange
// dbContext.CleanupDbSet<ParameterData>();
var discriminatorId = Guid.NewGuid();
// var discriminatorId = Guid.NewGuid();
//act
var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, CancellationToken.None);
// //act
// var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, CancellationToken.None);
//assert
Assert.NotNull(response);
}
// //assert
// Assert.NotNull(response);
// }
[Fact]
public async Task GetPart_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
// [Fact]
// public async Task GetPart_returns_success()
// {
// //arrange
// dbContext.CleanupDbSet<ParameterData>();
var discriminatorId = Guid.NewGuid();
var dateBegin = DateTimeOffset.UtcNow;
var take = 1;
// var discriminatorId = Guid.NewGuid();
// var dateBegin = DateTimeOffset.UtcNow;
// var take = 1;
//act
var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, CancellationToken.None);
// //act
// var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, CancellationToken.None);
//assert
Assert.NotNull(response);
Assert.Empty(response);
}
// //assert
// Assert.NotNull(response);
// Assert.Empty(response);
// }
[Fact]
public async Task InsertRange_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
// [Fact]
// public async Task InsertRange_returns_success()
// {
// //arrange
// dbContext.CleanupDbSet<ParameterData>();
//act
await AddRange();
}
// //act
// await AddRange();
// }
[Fact]
public async Task GetValuesForGraph_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
// [Fact]
// public async Task GetValuesForGraph_returns_success()
// {
// //arrange
// dbContext.CleanupDbSet<ParameterData>();
var discriminatorId = Guid.NewGuid();
var dateFrom = DateTimeOffset.UtcNow;
var dateTo = DateTimeOffset.UtcNow;
var approxPointCount = 12;
// var discriminatorId = Guid.NewGuid();
// var dateFrom = DateTimeOffset.UtcNow;
// var dateTo = DateTimeOffset.UtcNow;
// var approxPointCount = 12;
//act
var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, CancellationToken.None);
// //act
// var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, CancellationToken.None);
//assert
Assert.NotNull(response);
Assert.Empty(response);
}
// //assert
// Assert.NotNull(response);
// Assert.Empty(response);
// }
[Fact]
public async Task GetDatesRangeAsync_AfterSave_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
// [Fact]
// public async Task GetDatesRangeAsync_AfterSave_returns_success()
// {
// //arrange
// dbContext.CleanupDbSet<ParameterData>();
var dtos = await AddRange();
var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId;
// var dtos = await AddRange();
// var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId;
//act
var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, CancellationToken.None);
// //act
// var response = await witsDataClient.GetDatesRangeAsync(discriminatorId, CancellationToken.None);
//assert
Assert.NotNull(response);
// //assert
// Assert.NotNull(response);
var expectedDateFrom = dtos
.Select(e => e.Timestamped)
.Min()
.ToString("dd.MM.yyyy-HH:mm:ss");
var actualDateFrom = response.From.DateTime
.ToString("dd.MM.yyyy-HH:mm:ss");
Assert.Equal(expectedDateFrom, actualDateFrom);
// var expectedDateFrom = dtos
// .Select(e => e.Timestamped)
// .Min()
// .ToString("dd.MM.yyyy-HH:mm:ss");
// var actualDateFrom = response.From.DateTime
// .ToString("dd.MM.yyyy-HH:mm:ss");
// Assert.Equal(expectedDateFrom, actualDateFrom);
var expectedDateTo = dtos
.Select(e => e.Timestamped)
.Max()
.ToString("dd.MM.yyyy-HH:mm:ss");
var actualDateTo = response.To.DateTime
.ToString("dd.MM.yyyy-HH:mm:ss");
Assert.Equal(expectedDateTo, actualDateTo);
}
// var expectedDateTo = dtos
// .Select(e => e.Timestamped)
// .Max()
// .ToString("dd.MM.yyyy-HH:mm:ss");
// var actualDateTo = response.To.DateTime
// .ToString("dd.MM.yyyy-HH:mm:ss");
// Assert.Equal(expectedDateTo, actualDateTo);
// }
[Fact]
public async Task GetPart_AfterSave_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
// [Fact]
// public async Task GetPart_AfterSave_returns_success()
// {
// //arrange
// dbContext.CleanupDbSet<ParameterData>();
var dtos = await AddRange();
var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId;
var dateBegin = dtos.FirstOrDefault()!.Timestamped;
var take = 1;
// var dtos = await AddRange();
// var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId;
// var dateBegin = dtos.FirstOrDefault()!.Timestamped;
// var take = 1;
//act
var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, CancellationToken.None);
// //act
// var response = await witsDataClient.GetPart(discriminatorId, dateBegin, take, CancellationToken.None);
//assert
Assert.NotNull(response);
Assert.NotEmpty(response);
Assert.Equal(take, response.Count());
// //assert
// Assert.NotNull(response);
// Assert.NotEmpty(response);
// Assert.Equal(take, response.Count());
var expectedDto = dtos.FirstOrDefault();
var actualDto = response.FirstOrDefault();
Assert.Equal(expectedDto?.DiscriminatorId, actualDto?.DiscriminatorId);
// var expectedDto = dtos.FirstOrDefault();
// var actualDto = response.FirstOrDefault();
// Assert.Equal(expectedDto?.DiscriminatorId, actualDto?.DiscriminatorId);
var expectedValueDto = expectedDto?.Values.FirstOrDefault();
var actualValueDto = actualDto?.Values.FirstOrDefault();
Assert.Equal(expectedValueDto?.ItemId, actualValueDto?.ItemId);
Assert.Equal(expectedValueDto?.RecordId, actualValueDto?.RecordId);
}
// var expectedValueDto = expectedDto?.Values.FirstOrDefault();
// var actualValueDto = actualDto?.Values.FirstOrDefault();
// Assert.Equal(expectedValueDto?.ItemId, actualValueDto?.ItemId);
// Assert.Equal(expectedValueDto?.RecordId, actualValueDto?.RecordId);
// }
[Fact]
public async Task GetValuesForGraph_AfterSave_returns_success()
{
//arrange
dbContext.CleanupDbSet<ParameterData>();
// [Fact]
// public async Task GetValuesForGraph_AfterSave_returns_success()
// {
// //arrange
// dbContext.CleanupDbSet<ParameterData>();
var dtos = await AddRange(37);
var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId;
var dateFrom = dtos.Select(e => e.Timestamped).Min();
var dateTo = dtos.Select(e => e.Timestamped).Max();
var approxPointCount = 12;
// var dtos = await AddRange(37);
// var discriminatorId = dtos.FirstOrDefault()!.DiscriminatorId;
// var dateFrom = dtos.Select(e => e.Timestamped).Min();
// var dateTo = dtos.Select(e => e.Timestamped).Max();
// var approxPointCount = 12;
//act
var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, CancellationToken.None);
// //act
// var response = await witsDataClient.GetValuesForGraph(discriminatorId, dateFrom, dateTo, approxPointCount, CancellationToken.None);
//assert
Assert.NotNull(response);
Assert.Equal(approxPointCount, response.Count());
}
// //assert
// Assert.NotNull(response);
// Assert.Equal(approxPointCount, response.Count());
// }
[Fact]
public async Task AddRange_returns_BadRequest()
{
//arrange
const string exceptionMessage = "Ошибка валидации, формата или маршрутизации запроса";
var dtos = new List<WitsDataDto>()
{
new WitsDataDto()
{
DiscriminatorId = Guid.NewGuid(),
Timestamped = DateTimeOffset.UtcNow,
Values = new List<WitsValueDto>()
{
new WitsValueDto()
{
RecordId = -1, // < 0
ItemId = 101, // > 100
Value = string.Empty
}
}
}
};
// [Fact]
// public async Task AddRange_returns_BadRequest()
// {
// //arrange
// const string exceptionMessage = "Ошибка валидации, формата или маршрутизации запроса";
// var dtos = new List<WitsDataDto>()
// {
// new WitsDataDto()
// {
// DiscriminatorId = Guid.NewGuid(),
// Timestamped = DateTimeOffset.UtcNow,
// Values = new List<WitsValueDto>()
// {
// new WitsValueDto()
// {
// RecordId = -1, // < 0
// ItemId = 101, // > 100
// Value = string.Empty
// }
// }
// }
// };
try
{
//act
var response = await witsDataClient.AddRange(dtos, CancellationToken.None);
}
catch (Exception ex)
{
//assert
Assert.Equal(exceptionMessage, ex.Message);
}
}
// try
// {
// //act
// var response = await witsDataClient.AddRange(dtos, CancellationToken.None);
// }
// catch (Exception ex)
// {
// //assert
// Assert.Equal(exceptionMessage, ex.Message);
// }
// }
private async Task<IEnumerable<WitsDataDto>> AddRange(int countToCreate = 10)
{
var dtos = new List<WitsDataDto>();
var discriminatorId = Guid.NewGuid();
var timestamped = DateTimeOffset.UtcNow;
for (var i = 0; i < countToCreate; i++)
{
var random = new Random();
dtos.Add(new WitsDataDto()
{
DiscriminatorId = discriminatorId,
Timestamped = timestamped.AddSeconds(i),
Values = new List<WitsValueDto>()
{
new WitsValueDto()
{
RecordId = i + 1,
ItemId = i + 1,
Value = random.Next(1, 100)
}
}
});
}
// private async Task<IEnumerable<WitsDataDto>> AddRange(int countToCreate = 10)
// {
// var dtos = new List<WitsDataDto>();
// var discriminatorId = Guid.NewGuid();
// var timestamped = DateTimeOffset.UtcNow;
// for (var i = 0; i < countToCreate; i++)
// {
// var random = new Random();
// dtos.Add(new WitsDataDto()
// {
// DiscriminatorId = discriminatorId,
// Timestamped = timestamped.AddSeconds(i),
// Values = new List<WitsValueDto>()
// {
// new WitsValueDto()
// {
// RecordId = i + 1,
// ItemId = i + 1,
// Value = random.Next(1, 100)
// }
// }
// });
// }
//act
var response = await witsDataClient.AddRange(dtos, CancellationToken.None);
// //act
// var response = await witsDataClient.AddRange(dtos, CancellationToken.None);
//assert
var count = dtos.SelectMany(e => e.Values).Count();
Assert.Equal(count, response);
// //assert
// var count = dtos.SelectMany(e => e.Values).Count();
// Assert.Equal(count, response);
return dtos;
}
}
// return dtos;
// }
//}