using System; using System.Collections.Generic; using System.Linq; using Gallium; using Nitride.Tests; using Xunit; using Xunit.Abstractions; namespace Nitride.Temporal.Tests; public class CreateDateIndexesTests : TemporalTestBase { public CreateDateIndexesTests(ITestOutputHelper output) : base(output) { } [Fact] public void MonthOnlyIndexes() { using TemporalTestContext context = this.CreateContext(); Timekeeper timekeeper = context.Resolve(); CreateDateIndexes op = context.Resolve() .WithFormats("yyyy-MM") .WithCreateIndex(this.CreateIndex); List input = new() { new Entity().Add("page1").Add(timekeeper.CreateInstant(2021, 1, 2)), new Entity().Add("page2").Add(timekeeper.CreateInstant(2021, 2, 2)), new Entity().Add("page3").Add(timekeeper.CreateInstant(2022, 1, 2)), }; List?, List?>> actual = this.GetActual(op, input); var expected = new List?, List?>> { new("index-2021-01", new List { "page1" }, new List()), new("index-2021-02", new List { "page2" }, new List()), new("index-2022-01", new List { "page3" }, new List()), new("page1", null, null), new("page2", null, null), new("page3", null, null), }; TestHelper.CompareObjects(expected, actual); } [Fact] public void YearMonthDayIndexes() { using TemporalTestContext context = this.CreateContext(); Timekeeper timekeeper = context.Resolve(); CreateDateIndexes op = context.Resolve() .WithFormats("yyyy/MM/dd", "yyyy/MM", "yyyy") .WithCreateIndex(this.CreateIndex); List input = new() { new Entity().Add("page1").Add(timekeeper.CreateInstant(2021, 1, 2)), new Entity().Add("page2").Add(timekeeper.CreateInstant(2021, 2, 2)), new Entity().Add("page3").Add(timekeeper.CreateInstant(2022, 1, 2)), }; List?, List?>> actual = this.GetActual(op, input); var expected = new List?, List?>> { new("index-2021", new List(), new List { "index-2021/01", "index-2021/02" }), new("index-2021/01", new List(), new List { "index-2021/01/02" }), new("index-2021/01/02", new List { "page1" }, new List()), new("index-2021/02", new List(), new List { "index-2021/02/02" }), new("index-2021/02/02", new List { "page2" }, new List()), new("index-2022", new List(), new List { "index-2022/01" }), new("index-2022/01", new List(), new List { "index-2022/01/02" }), new("index-2022/01/02", new List { "page3" }, new List()), new("page1", null, null), new("page2", null, null), new("page3", null, null), }; TestHelper.CompareObjects(expected, actual); } [Fact] public void YearMonthDayIndexesThreshold1() { using TemporalTestContext context = this.CreateContext(); Timekeeper timekeeper = context.Resolve(); CreateDateIndexes op = context.Resolve() .WithFormats("yyyy/MM/dd", "yyyy/MM", "yyyy") .WithCreateIndex(this.CreateIndex) .WithLessThanEqualCollapse(1); List input = new() { new Entity().Add("page1").Add(timekeeper.CreateInstant(2021, 1, 2)), new Entity().Add("page2").Add(timekeeper.CreateInstant(2021, 2, 2)), new Entity().Add("page3").Add(timekeeper.CreateInstant(2022, 1, 2)), }; List?, List?>> actual = this.GetActual(op, input); var expected = new List?, List?>> { new("index-2021", new List(), new List { "index-2021/01", "index-2021/02" }), new("index-2021/01", new List { "page1" }, new List { "index-2021/01/02" }), new("index-2021/01/02", new List { "page1" }, new List()), new("index-2021/02", new List { "page2" }, new List { "index-2021/02/02" }), new("index-2021/02/02", new List { "page2" }, new List()), new("index-2022", new List { "page3" }, new List { "index-2022/01" }), new("index-2022/01", new List { "page3" }, new List { "index-2022/01/02" }), new("index-2022/01/02", new List { "page3" }, new List()), new("page1", null, null), new("page2", null, null), new("page3", null, null), }; TestHelper.CompareObjects(expected, actual); } [Fact] public void YearMonthIndexes() { using TemporalTestContext context = this.CreateContext(); Timekeeper timekeeper = context.Resolve(); CreateDateIndexes op = context.Resolve() .WithFormats("yyyy-MM", "yyyy") .WithCreateIndex(this.CreateIndex); List input = new() { new Entity().Add("page1").Add(timekeeper.CreateInstant(2021, 1, 2)), new Entity().Add("page2").Add(timekeeper.CreateInstant(2021, 2, 2)), new Entity().Add("page3").Add(timekeeper.CreateInstant(2022, 1, 2)), }; List?, List?>> actual = this.GetActual(op, input); var expected = new List?, List?>> { new("index-2021", new List(), new List { "index-2021-01", "index-2021-02" }), new("index-2021-01", new List { "page1" }, new List()), new("index-2021-02", new List { "page2" }, new List()), new("index-2022", new List(), new List { "index-2022-01" }), new("index-2022-01", new List { "page3" }, new List()), new("page1", null, null), new("page2", null, null), new("page3", null, null), }; TestHelper.CompareObjects(expected, actual); } [Fact] public void YearOnlyIndexes() { using TemporalTestContext context = this.CreateContext(); Timekeeper timekeeper = context.Resolve(); CreateDateIndexes op = context.Resolve() .WithFormats("yyyy") .WithCreateIndex(this.CreateIndex); List input = new() { new Entity().Add("page1").Add(timekeeper.CreateInstant(2021, 1, 2)), new Entity().Add("page2").Add(timekeeper.CreateInstant(2021, 2, 2)), new Entity().Add("page3").Add(timekeeper.CreateInstant(2022, 1, 2)), }; List?, List?>> actual = this.GetActual(op, input); var expected = new List?, List?>> { new("index-2021", new List { "page1", "page2" }, new List()), new("index-2022", new List { "page3" }, new List()), new("page1", null, null), new("page2", null, null), new("page3", null, null), }; TestHelper.CompareObjects(expected, actual); } private Entity CreateIndex(DateIndex a) { return new Entity().Add(a).Add($"index-{a.Key}"); } private List?, List?>> GetActual(CreateDateIndexes op, List input) { var actual = op.Run(input) .Select( x => new Tuple?, List?>( x.Get(), x.GetOptional()?.Entries.Select(a => a.Get()).OrderBy(x => x).ToList(), x.GetOptional()?.Indexes.Select(a => a.Get()).OrderBy(x => x).ToList())) .OrderBy(x => x.Item1) .ToList(); return actual; } }