Creado apartir del commit b888de6c92056de294456f581a56e25e734d4ab7 de develop
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using Serilog;
|
||||
using Serilog.Core;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace adas_core.Test.Models;
|
||||
|
||||
public class FakeLogger : ILogger
|
||||
{
|
||||
public List<string> Messages { get; } = [];
|
||||
|
||||
public ILogger ForContext(IEnumerable<ILogEventEnricher> enrichers)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILogger ForContext(string propertyName, object? value, bool destructureObjects = false)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILogger ForContext<TSource>()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public ILogger ForContext(Type source)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Write(LogEvent logEvent)
|
||||
{
|
||||
Messages.Add(logEvent.RenderMessage());
|
||||
}
|
||||
|
||||
public void Write(LogEventLevel level, string messageTemplate, params object?[]? propertyValues)
|
||||
{
|
||||
// Manejar la situación donde propertyValues es nulo, si es necesario
|
||||
Messages.Add(propertyValues == null
|
||||
? messageTemplate
|
||||
: string.Format(messageTemplate, propertyValues)); // Por ejemplo, solo añadir el mensaje sin formato
|
||||
}
|
||||
|
||||
|
||||
public void Write<T>(LogEventLevel level, string messageTemplate, T propertyValue)
|
||||
{
|
||||
Messages.Add(string.Format(messageTemplate, propertyValue));
|
||||
}
|
||||
|
||||
public void Write(LogEventLevel level, Exception? exception, string messageTemplate,
|
||||
params object?[]? propertyValues)
|
||||
{
|
||||
Messages.Add(propertyValues == null ? messageTemplate : string.Format(messageTemplate, propertyValues));
|
||||
|
||||
if (exception != null) Messages.Add($"Exception: {exception.Message}");
|
||||
}
|
||||
|
||||
|
||||
public bool IsEnabled(LogEventLevel level)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Write<T>(LogEventLevel level, Exception? exception, string messageTemplate, T propertyValue)
|
||||
{
|
||||
Messages.Add(string.Format(messageTemplate, propertyValue));
|
||||
if (exception != null)
|
||||
Messages.Add($"Exception: {exception.Message}"); // You might want to include the exception here.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
using adas_core.Application.Services.Interfaces;
|
||||
using adas_core.Application.Subscriptions;
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models;
|
||||
using adas_core.Domain.Models.GroupedObservations;
|
||||
using MongoDB.Bson;
|
||||
using Moq;
|
||||
using static adas_core.Domain.Models.GroupedObservation;
|
||||
|
||||
namespace adas_core.Test.Models.SignalR;
|
||||
|
||||
[TestFixture]
|
||||
public class SubscriberGroupedTest
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
// Set up GroupedObservation
|
||||
_go = new GroupedObservation
|
||||
{
|
||||
PatientId = _patient1,
|
||||
Name = "GroupedObservationTest1",
|
||||
Group = "GroupedObservationTest1",
|
||||
Observations = []
|
||||
};
|
||||
_go2 = new GroupedObservation
|
||||
{
|
||||
PatientId = _patient2,
|
||||
Name = "GroupedObservationTest2",
|
||||
Group = "GroupedObservationTest2",
|
||||
Observations = []
|
||||
};
|
||||
|
||||
// Set up GroupedField
|
||||
_gf1 = new GroupedField
|
||||
{
|
||||
Name = "GroupedObservationTest1",
|
||||
Group = "GroupedFieldTest1",
|
||||
StartTimeShift = ["00:59", "04:59", "08:59", "12:59", "16:59", "20:59"],
|
||||
Max = 24,
|
||||
Regularity = GroupedObservationEnum.Regularity.Shift,
|
||||
Result = [GroupedObservationEnum.Result.Last]
|
||||
};
|
||||
_gf2 = new GroupedField
|
||||
{
|
||||
Names = ["GroupedObservationTest2", "GroupedObservationTest1"],
|
||||
Group = "GroupedFieldTest2",
|
||||
Max = 24,
|
||||
Regularity = GroupedObservationEnum.Regularity.Hour,
|
||||
Result = [GroupedObservationEnum.Result.Last, GroupedObservationEnum.Result.LastFilled]
|
||||
};
|
||||
|
||||
// Set up WebSocketSubscriber
|
||||
_ws1 = new WsSubscriber("1234")
|
||||
{
|
||||
Id = "1234",
|
||||
Box = "Box1",
|
||||
Section = "Section1",
|
||||
Version = "1.0.0",
|
||||
GroupedFields = [],
|
||||
SubscriptionType = SubscriptionEnum.WsType.Box
|
||||
};
|
||||
_ws2 = new WsSubscriber("1234")
|
||||
{
|
||||
Id = "5321",
|
||||
Box = "Box2",
|
||||
Section = "Section1",
|
||||
Version = "1.0.0",
|
||||
GroupedFields = [],
|
||||
SubscriptionType = SubscriptionEnum.WsType.Box
|
||||
};
|
||||
|
||||
// Set up groupedFields for subscribers
|
||||
_ws1.GroupedFields = [_gf1];
|
||||
_ws2.GroupedFields = [_gf1, _gf2];
|
||||
|
||||
// Set up WsSubscriberGrouped
|
||||
_wsBase = new WsSubscriberGrouped(_patient1, _ws1.Id, "Romance Standard Time", _gf1, _go, delegate { });
|
||||
_wsBase2 = new WsSubscriberGrouped(_patient2, _ws2.Id, "Romance Standard Time", _gf2, _go2, delegate { });
|
||||
}
|
||||
|
||||
private GroupedObservation _go;
|
||||
private GroupedObservation _go2;
|
||||
|
||||
private GroupedField _gf1;
|
||||
private GroupedField _gf2;
|
||||
|
||||
private WsSubscriber _ws1;
|
||||
private WsSubscriber _ws2;
|
||||
|
||||
private WsSubscriberGrouped _wsBase;
|
||||
private WsSubscriberGrouped _wsBase2;
|
||||
|
||||
private readonly ObjectId _patient1 = ObjectId.GenerateNewId();
|
||||
private readonly ObjectId _patient2 = ObjectId.GenerateNewId();
|
||||
|
||||
[Test]
|
||||
public void GetGroupeds_ReturnsExpectedResult()
|
||||
{
|
||||
var mockSubscriberGrouped = new List<WsSubscriberGrouped>
|
||||
{
|
||||
new(_patient1, _ws1.Id, "Romance Standard Time", _gf1, _go, delegate { }),
|
||||
new(_patient2, _ws1.Id, "Romance Standard Time", _gf1, _go, delegate { })
|
||||
};
|
||||
|
||||
var mockSubscriberGroupedService = new Mock<ISubscriberGroupedService>();
|
||||
mockSubscriberGroupedService.Setup(x => x.GetGrouped()).Returns(mockSubscriberGrouped);
|
||||
|
||||
|
||||
var result = mockSubscriberGroupedService.Object.GetGrouped();
|
||||
|
||||
Assert.That(result, Is.EqualTo(mockSubscriberGrouped));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Compare_ExtensionResult()
|
||||
{
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(_wsBase.Compare(_gf1, _go.PatientId), Is.True);
|
||||
Assert.That(_wsBase.Compare(_gf1, _go2.PatientId), Is.False);
|
||||
Assert.That(_wsBase.Compare(_gf2, _go.PatientId), Is.False);
|
||||
Assert.That(_wsBase2.Compare(_gf2, _go2.PatientId), Is.True);
|
||||
};
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Observation_is_rrelevant_because_is_the_first_in_hour()
|
||||
{
|
||||
var obs = new PatientObservation
|
||||
{
|
||||
Time = DateTime.UtcNow,
|
||||
Value = 5
|
||||
};
|
||||
|
||||
|
||||
Assert.That(_wsBase.IsNewObservationRelevantForGroup(obs), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Grouped_Observation_Already_Observations_In_Hour_Should_Be_Relevant_By_Max_Result()
|
||||
{
|
||||
var patientId = ObjectId.GenerateNewId();
|
||||
var grouped = new GroupedObservation
|
||||
{
|
||||
PatientId = patientId,
|
||||
Name = "GroupedObservationTest1",
|
||||
Group = "GroupedObservationTest1",
|
||||
Observations =
|
||||
[
|
||||
new GroupedObservationObs
|
||||
{
|
||||
Max = new GroupedObservationObsValue(5, DateTime.UtcNow)
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var wsSubscriber = new WsSubscriber("1234")
|
||||
{
|
||||
Id = "1234",
|
||||
Box = "Box1",
|
||||
Section = "Section1",
|
||||
Version = "1.0.0",
|
||||
GroupedFields = [],
|
||||
SubscriptionType = SubscriptionEnum.WsType.Box
|
||||
};
|
||||
|
||||
var groupedField = new GroupedField
|
||||
{
|
||||
Names = ["GroupedObservationTest2", "GroupedObservationTest1"],
|
||||
Group = "GroupedFieldTest2",
|
||||
Max = 24,
|
||||
Regularity = GroupedObservationEnum.Regularity.Hour,
|
||||
Result = [GroupedObservationEnum.Result.Max]
|
||||
};
|
||||
|
||||
var newObservation = new PatientObservation
|
||||
{
|
||||
PatientId = patientId,
|
||||
Time = DateTime.UtcNow,
|
||||
Value = 8
|
||||
};
|
||||
|
||||
var wsGrouped = new WsSubscriberGrouped(patientId, wsSubscriber.Id, "", groupedField, grouped, delegate { });
|
||||
//for utc.now two observations, last was 5 and now is 8 result is by Max, should be relevant
|
||||
Assert.That(wsGrouped.IsNewObservationRelevantForGroup(newObservation), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Grouped_Observation_Already_Observations_In_Hour_Should_Be_IRelevant_By_Max_Result()
|
||||
{
|
||||
var patientId = ObjectId.GenerateNewId();
|
||||
var grouped = new GroupedObservation
|
||||
{
|
||||
PatientId = patientId,
|
||||
Name = "GroupedObservationTest1",
|
||||
Group = "GroupedObservationTest1",
|
||||
Observations =
|
||||
[
|
||||
new GroupedObservationObs
|
||||
{
|
||||
Max = new GroupedObservationObsValue(5, DateTime.UtcNow),
|
||||
Time = DateTime.UtcNow
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var wsSubscriber = new WsSubscriber("1234")
|
||||
{
|
||||
Id = "1234",
|
||||
Box = "Box1",
|
||||
Section = "Section1",
|
||||
Version = "1.0.0",
|
||||
GroupedFields = [],
|
||||
SubscriptionType = SubscriptionEnum.WsType.Box
|
||||
};
|
||||
|
||||
var groupedField = new GroupedField
|
||||
{
|
||||
Names = ["GroupedObservationTest2", "GroupedObservationTest1"],
|
||||
Group = "GroupedFieldTest2",
|
||||
Max = 24,
|
||||
Regularity = GroupedObservationEnum.Regularity.Hour,
|
||||
Result = [GroupedObservationEnum.Result.Max]
|
||||
};
|
||||
|
||||
var newObservation = new PatientObservation
|
||||
{
|
||||
PatientId = patientId,
|
||||
Time = DateTime.UtcNow,
|
||||
Value = 4
|
||||
};
|
||||
|
||||
var wsGrouped = new WsSubscriberGrouped(patientId, wsSubscriber.Id, "", groupedField, grouped, delegate { });
|
||||
//for utc.now two observations, last was 5 and now is 4 result is by Max, should be Irelevant
|
||||
Assert.That(wsGrouped.IsNewObservationRelevantForGroup(newObservation), Is.False);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user