229 lines
7.9 KiB
C#
229 lines
7.9 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
||
using DD.Persistence.Client;
|
||
using DD.Persistence.Client.Clients.Interfaces;
|
||
using DD.Persistence.Models;
|
||
using Xunit;
|
||
using DD.Persistence.Client.Clients.Interfaces.Refit;
|
||
using DD.Persistence.Client.Clients;
|
||
using Microsoft.Extensions.Logging;
|
||
using System.Text.Json;
|
||
using System.Text;
|
||
using Microsoft.Extensions.Primitives;
|
||
using System.Dynamic;
|
||
using Newtonsoft.Json.Linq;
|
||
|
||
namespace DD.Persistence.IntegrationTests.Controllers;
|
||
public class TimestampedSetControllerTest : BaseIntegrationTest
|
||
{
|
||
private readonly ITimestampedValuesClient client;
|
||
|
||
public TimestampedSetControllerTest(WebAppFactoryFixture factory) : base(factory)
|
||
{
|
||
var refitClientFactory = scope.ServiceProvider
|
||
.GetRequiredService<IRefitClientFactory<IRefitTimestampedValuesClient>>();
|
||
var logger = scope.ServiceProvider.GetRequiredService<ILogger<TimestampedValuesClient>>();
|
||
|
||
client = scope.ServiceProvider
|
||
.GetRequiredService<ITimestampedValuesClient>();
|
||
}
|
||
|
||
[Fact]
|
||
public async Task InsertRange()
|
||
{
|
||
// arrange
|
||
Guid idDiscriminator = Guid.NewGuid();
|
||
IEnumerable<TimestampedValuesDto> testSets = Generate(10, DateTimeOffset.Now.ToOffset(TimeSpan.FromHours(7)));
|
||
|
||
// act
|
||
var response = await client.AddRange(idDiscriminator, testSets, CancellationToken.None);
|
||
|
||
// assert
|
||
Assert.Equal(testSets.Count(), response);
|
||
}
|
||
|
||
[Fact]
|
||
public async Task Get_without_filter()
|
||
{
|
||
// arrange
|
||
Guid idDiscriminator = Guid.NewGuid();
|
||
int count = 10;
|
||
IEnumerable<TimestampedValuesDto> 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);
|
||
|
||
// 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<TimestampedValuesDto> 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());
|
||
|
||
// assert
|
||
Assert.NotNull(response);
|
||
Assert.Equal(count, response.Count());
|
||
foreach (var item in response)
|
||
{
|
||
Assert.Single(item.Values!);
|
||
var kv = item.Values!.First();
|
||
Assert.Equal("A", ((KeyValuePair<string, object>) 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<TimestampedValuesDto> 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);
|
||
|
||
// 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<TimestampedValuesDto> 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());
|
||
|
||
// 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<TimestampedValuesDto> 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());
|
||
|
||
// assert
|
||
Assert.NotNull(response);
|
||
Assert.Equal(expectedCount, response.Count());
|
||
}
|
||
|
||
[Fact]
|
||
public async Task GetLast()
|
||
{
|
||
// arrange
|
||
Guid idDiscriminator = Guid.NewGuid();
|
||
int count = 10;
|
||
IEnumerable<TimestampedValuesDto> 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());
|
||
|
||
// 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<TimestampedValuesDto> 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());
|
||
|
||
// 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<TimestampedValuesDto> 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());
|
||
|
||
// assert
|
||
Assert.Equal(count, response);
|
||
}
|
||
|
||
private static IEnumerable<TimestampedValuesDto> Generate(int n, DateTimeOffset from)
|
||
{
|
||
var result = new List<TimestampedValuesDto>();
|
||
for (int i = 0; i < n; i++)
|
||
{
|
||
var t = new object[] {
|
||
new { A = i },
|
||
new { B = i * 1.1 },
|
||
new { C = $"Any{i}" },
|
||
new { D = DateTimeOffset.Now }
|
||
};
|
||
string jsonString = JsonSerializer.Serialize(t);
|
||
var values = JsonSerializer.Deserialize<object[]>(jsonString);
|
||
//// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
//dynamic obj = new ExpandoObject();
|
||
|
||
//// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> "FieldName" <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 123
|
||
//obj.FieldName = 123;
|
||
|
||
yield return new TimestampedValuesDto()
|
||
{
|
||
Timestamp = from.AddSeconds(i),
|
||
Values = values
|
||
};
|
||
}
|
||
}
|
||
}
|