This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-nitride-cil/tests/MfGames.Nitride.Temporal.Schedules.Tests/IndexedPathRegexScheduleTest.cs

261 lines
8.3 KiB
C#
Raw Normal View History

2023-01-17 04:09:06 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using MfGames.Gallium;
using MfGames.Nitride.Tests;
using NodaTime;
using NodaTime.Testing;
2023-01-17 04:09:06 +00:00
using Xunit;
using Xunit.Abstractions;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
2023-01-17 04:09:06 +00:00
using Zio;
2023-01-17 04:09:06 +00:00
namespace MfGames.Nitride.Temporal.Schedules.Tests;
2023-01-17 04:09:06 +00:00
public class IndexedPathRegexScheduleTest : TemporalSchedulesTestBase
2023-01-17 04:09:06 +00:00
{
public IndexedPathRegexScheduleTest(ITestOutputHelper output)
2023-01-17 04:09:06 +00:00
: base(output)
{
}
[Fact]
public void DeserializedSetupWorks()
2023-01-17 04:09:06 +00:00
{
using TemporalSchedulesTestContext context = this.CreateContext();
// Create a numerical series of entities.
var input = new List<Entity>
2023-01-17 04:09:06 +00:00
{
new Entity().SetAll((UPath)"/chapter-01.md", new TestModel()),
new Entity().SetAll((UPath)"/chapter-02.md", new TestModel()),
new Entity().SetAll((UPath)"/chapter-03.md", new TestModel()),
2023-01-17 04:09:06 +00:00
};
TestModel model = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build()
.Deserialize<TestModel>(
string.Join(
"\n",
"---",
"access: custom",
"schedules:",
" pathRegex: chapter-(\\d+)",
" indexes:",
" 1:",
" scheduleStart: 2020-01-01",
" schedulePeriod: instant",
" access: t-1",
" 2:",
" scheduleStart: 2023-01-02",
" schedulePeriod: 1 week",
" access: t-2",
""));
var schedules = model.Schedules!;
// Create the operation and run it, but treat it as being set after the
// second but before the third item.
TimeService time = context.Resolve<TimeService>();
ApplySchedules op = context.Resolve<ApplySchedules>()
.WithGetSchedules(_ => new ISchedule[] { schedules });
var now = Instant.FromUtc(2023, 1, 3, 0, 0);
time.Clock = new FakeClock(now);
var actual = op
.Run(input)
.Select(
a => string.Format(
"{0} -- {1} -- {2}",
a.Get<UPath>().ToString(),
a.Has<Instant>()
? time
.ToDateTime(a.Get<Instant>())
.ToString("yyyy-MM-dd")
: "none",
a.Get<TestModel>().Access))
.ToList();
2023-01-17 04:09:06 +00:00
var expected = new List<string>
2023-01-17 04:09:06 +00:00
{
"/chapter-01.md -- 2020-01-01 -- t-1",
"/chapter-02.md -- 2023-01-02 -- t-2",
"/chapter-03.md -- none -- private",
2023-01-17 04:09:06 +00:00
};
TestHelper.CompareObjects(expected, actual);
}
[Fact]
public void ManualSetupWorks()
2023-01-17 04:09:06 +00:00
{
using TemporalSchedulesTestContext context = this.CreateContext();
// Create a numerical series of entities.
var input = new List<Entity>
2023-01-17 04:09:06 +00:00
{
new Entity().SetAll((UPath)"/chapter-01.md", new TestModel()),
new Entity().SetAll((UPath)"/chapter-02.md", new TestModel()),
new Entity().SetAll((UPath)"/chapter-03.md", new TestModel()),
2023-01-17 04:09:06 +00:00
};
var schedules = new TestRegexSchedule
2023-01-17 04:09:06 +00:00
{
Indexes = new Dictionary<int, TestSchedule>
{
[1] = new TestSchedule
{
ScheduleStart = DateTime.Parse("2023-01-01"),
Access = "public",
},
},
2023-01-17 04:09:06 +00:00
};
// Create the operation and run it, but treat it as being set after the
// second but before the third item.
TimeService time = context.Resolve<TimeService>();
ApplySchedules op = context.Resolve<ApplySchedules>()
.WithGetSchedules(_ => new ISchedule[] { schedules });
var now = Instant.FromUtc(2023, 1, 9, 0, 0);
2023-01-17 04:09:06 +00:00
time.Clock = new FakeClock(now);
2023-01-17 04:09:06 +00:00
var actual = op
.Run(input)
.Select(
a => string.Format(
"{0} -- {1} -- {2}",
a.Get<UPath>().ToString(),
a.Has<Instant>()
? time
.ToDateTime(a.Get<Instant>())
.ToString("yyyy-MM-dd")
: "none",
a.Get<TestModel>().Access))
.ToList();
2023-01-17 04:09:06 +00:00
var expected = new List<string>
2023-01-17 04:09:06 +00:00
{
"/chapter-01.md -- 2023-01-01 -- public",
"/chapter-02.md -- 2023-01-08 -- public",
"/chapter-03.md -- none -- private",
2023-01-17 04:09:06 +00:00
};
TestHelper.CompareObjects(expected, actual);
}
[Fact]
public void SequencedScheduleWorks()
2023-01-17 04:09:06 +00:00
{
using TemporalSchedulesTestContext context = this.CreateContext();
// Create a numerical series of entities.
var input = new List<Entity>
2023-01-17 04:09:06 +00:00
{
new Entity().SetAll((UPath)"/chapter-01.md", new TestModel()),
new Entity().SetAll((UPath)"/chapter-02.md", new TestModel()),
new Entity().SetAll((UPath)"/chapter-03.md", new TestModel()),
new Entity().SetAll((UPath)"/chapter-04.md", new TestModel()),
new Entity().SetAll((UPath)"/chapter-05.md", new TestModel()),
new Entity().SetAll((UPath)"/chapter-06.md", new TestModel()),
2023-01-17 04:09:06 +00:00
};
var schedules = new TestRegexSchedule()
2023-01-17 04:09:06 +00:00
{
Indexes = new Dictionary<int, TestSchedule>
{
[1] = new()
{
ScheduleStart = DateTime.Parse("2020-01-01"),
Access = "subscriber",
SchedulePeriodTimeSpan = TimeSpan.FromDays(7),
},
[3] = new()
{
ScheduleStart = DateTime.Parse("2023-01-07"),
Access = "public",
SchedulePeriodTimeSpan = TimeSpan.Zero,
},
[5] = new()
{
SchedulePeriod = "never",
},
},
2023-01-17 04:09:06 +00:00
};
// Create the operation and run it, but treat it as being set after the
// second but before the third item.
TimeService time = context.Resolve<TimeService>();
ApplySchedules op = context.Resolve<ApplySchedules>()
.WithGetSchedules(_ => new ISchedule[] { schedules });
var now = Instant.FromUtc(2023, 1, 9, 0, 0);
2023-01-17 04:09:06 +00:00
time.Clock = new FakeClock(now);
2023-01-17 04:09:06 +00:00
var actual = op
.Run(input)
.Select(
a => string.Format(
"{0} -- {1} -- {2}",
a.Get<UPath>().ToString(),
a.Has<Instant>()
? time
.ToDateTime(a.Get<Instant>())
.ToString("yyyy-MM-dd")
: "none",
a.Get<TestModel>().Access))
.ToList();
2023-01-17 04:09:06 +00:00
var expected = new List<string>
2023-01-17 04:09:06 +00:00
{
"/chapter-01.md -- 2020-01-01 -- subscriber",
"/chapter-02.md -- 2020-01-08 -- subscriber",
"/chapter-03.md -- 2023-01-07 -- public",
"/chapter-04.md -- 2023-01-07 -- public",
"/chapter-05.md -- none -- private",
"/chapter-06.md -- none -- private",
2023-01-17 04:09:06 +00:00
};
TestHelper.CompareObjects(expected, actual);
}
public class TestModel
2023-01-17 04:09:06 +00:00
{
public string? Access { get; set; } = "private";
public TestRegexSchedule? Schedules { get; set; }
}
public class TestRegexSchedule : IndexedPathRegexSchedule<TestSchedule>
{
2023-01-17 04:09:06 +00:00
}
public class TestSchedule : IndexedSchedule
2023-01-17 04:09:06 +00:00
{
public TestSchedule()
{
this.SchedulePeriod = "1 week";
}
2023-01-17 04:09:06 +00:00
public string? Access { get; set; }
/// <inheritdoc />
protected override Entity Apply(
Entity entity,
int number,
Instant instant)
{
TestModel model = entity.Get<TestModel>();
model.Access = this.Access;
return entity.SetAll(instant, model);
}
2023-01-17 04:09:06 +00:00
}
}