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.Tests/CreateDateIndexesTests.cs
D. Moonfire 07eb12414a
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
refactor(temporal)!: renamed Timekeeper to TimeService
2023-01-21 01:52:52 -06:00

314 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using MfGames.Gallium;
using MfGames.Nitride.Tests;
using Xunit;
using Xunit.Abstractions;
namespace MfGames.Nitride.Temporal.Tests;
public class CreateDateIndexesTests : TemporalTestBase
{
public CreateDateIndexesTests(ITestOutputHelper output)
: base(output)
{
}
[Fact]
public void MonthOnlyIndexes()
{
using TemporalTestContext context = this.CreateContext();
TimeService timeService = context.Resolve<TimeService>();
CreateDateIndexes op = context.Resolve<CreateDateIndexes>()
.WithFormats("yyyy-MM")
.WithCreateIndex(this.CreateIndex);
List<Entity> input = new()
{
new Entity().Add("page1")
.Add(timeService.CreateInstant(2021, 1, 2)),
new Entity().Add("page2")
.Add(timeService.CreateInstant(2021, 2, 2)),
new Entity().Add("page3")
.Add(timeService.CreateInstant(2022, 1, 2)),
};
List<Tuple<string, List<string>?, List<string>?>> actual =
this.GetActual(op, input);
var expected = new List<Tuple<string, List<string>?, List<string>?>>
{
new(
"index-2021-01",
new List<string> { "page1" },
new List<string>()),
new(
"index-2021-02",
new List<string> { "page2" },
new List<string>()),
new(
"index-2022-01",
new List<string> { "page3" },
new List<string>()),
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();
TimeService timeService = context.Resolve<TimeService>();
CreateDateIndexes op = context.Resolve<CreateDateIndexes>()
.WithFormats("yyyy/MM/dd", "yyyy/MM", "yyyy")
.WithCreateIndex(this.CreateIndex);
List<Entity> input = new()
{
new Entity().Add("page1")
.Add(timeService.CreateInstant(2021, 1, 2)),
new Entity().Add("page2")
.Add(timeService.CreateInstant(2021, 2, 2)),
new Entity().Add("page3")
.Add(timeService.CreateInstant(2022, 1, 2)),
};
List<Tuple<string, List<string>?, List<string>?>> actual =
this.GetActual(op, input);
var expected = new List<Tuple<string, List<string>?, List<string>?>>
{
new(
"index-2021",
new List<string>(),
new List<string> { "index-2021/01", "index-2021/02" }),
new(
"index-2021/01",
new List<string>(),
new List<string> { "index-2021/01/02" }),
new(
"index-2021/01/02",
new List<string> { "page1" },
new List<string>()),
new(
"index-2021/02",
new List<string>(),
new List<string> { "index-2021/02/02" }),
new(
"index-2021/02/02",
new List<string> { "page2" },
new List<string>()),
new(
"index-2022",
new List<string>(),
new List<string> { "index-2022/01" }),
new(
"index-2022/01",
new List<string>(),
new List<string> { "index-2022/01/02" }),
new(
"index-2022/01/02",
new List<string> { "page3" },
new List<string>()),
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();
TimeService timeService = context.Resolve<TimeService>();
CreateDateIndexes op = context.Resolve<CreateDateIndexes>()
.WithFormats("yyyy/MM/dd", "yyyy/MM", "yyyy")
.WithCreateIndex(this.CreateIndex)
.WithLessThanEqualCollapse(1);
List<Entity> input = new()
{
new Entity().Add("page1")
.Add(timeService.CreateInstant(2021, 1, 2)),
new Entity().Add("page2")
.Add(timeService.CreateInstant(2021, 2, 2)),
new Entity().Add("page3")
.Add(timeService.CreateInstant(2022, 1, 2)),
};
List<Tuple<string, List<string>?, List<string>?>> actual =
this.GetActual(op, input);
var expected = new List<Tuple<string, List<string>?, List<string>?>>
{
new(
"index-2021",
new List<string>(),
new List<string> { "index-2021/01", "index-2021/02" }),
new(
"index-2021/01",
new List<string> { "page1" },
new List<string> { "index-2021/01/02" }),
new(
"index-2021/01/02",
new List<string> { "page1" },
new List<string>()),
new(
"index-2021/02",
new List<string> { "page2" },
new List<string> { "index-2021/02/02" }),
new(
"index-2021/02/02",
new List<string> { "page2" },
new List<string>()),
new(
"index-2022",
new List<string> { "page3" },
new List<string> { "index-2022/01" }),
new(
"index-2022/01",
new List<string> { "page3" },
new List<string> { "index-2022/01/02" }),
new(
"index-2022/01/02",
new List<string> { "page3" },
new List<string>()),
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();
TimeService timeService = context.Resolve<TimeService>();
CreateDateIndexes op = context.Resolve<CreateDateIndexes>()
.WithFormats("yyyy-MM", "yyyy")
.WithCreateIndex(this.CreateIndex);
List<Entity> input = new()
{
new Entity().Add("page1")
.Add(timeService.CreateInstant(2021, 1, 2)),
new Entity().Add("page2")
.Add(timeService.CreateInstant(2021, 2, 2)),
new Entity().Add("page3")
.Add(timeService.CreateInstant(2022, 1, 2)),
};
List<Tuple<string, List<string>?, List<string>?>> actual =
this.GetActual(op, input);
var expected = new List<Tuple<string, List<string>?, List<string>?>>
{
new(
"index-2021",
new List<string>(),
new List<string> { "index-2021-01", "index-2021-02" }),
new(
"index-2021-01",
new List<string> { "page1" },
new List<string>()),
new(
"index-2021-02",
new List<string> { "page2" },
new List<string>()),
new(
"index-2022",
new List<string>(),
new List<string> { "index-2022-01" }),
new(
"index-2022-01",
new List<string> { "page3" },
new List<string>()),
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();
TimeService timeService = context.Resolve<TimeService>();
CreateDateIndexes op = context.Resolve<CreateDateIndexes>()
.WithFormats("yyyy")
.WithCreateIndex(this.CreateIndex);
List<Entity> input = new()
{
new Entity().Add("page1")
.Add(timeService.CreateInstant(2021, 1, 2)),
new Entity().Add("page2")
.Add(timeService.CreateInstant(2021, 2, 2)),
new Entity().Add("page3")
.Add(timeService.CreateInstant(2022, 1, 2)),
};
List<Tuple<string, List<string>?, List<string>?>> actual =
this.GetActual(op, input);
var expected = new List<Tuple<string, List<string>?, List<string>?>>
{
new(
"index-2021",
new List<string> { "page1", "page2" },
new List<string>()),
new("index-2022", new List<string> { "page3" }, new List<string>()),
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<Tuple<string, List<string>?, List<string>?>> GetActual(
CreateDateIndexes op,
List<Entity> input)
{
var actual = op.Run(input)
.Select(
x => new Tuple<string, List<string>?, List<string>?>(
x.Get<string>(),
x.GetOptional<DateIndex>()
?.Entries.Select(a => a.Get<string>())
.OrderBy(b => b)
.ToList(),
x.GetOptional<DateIndex>()
?.Indexes.Select(a => a.Get<string>())
.OrderBy(b => b)
.ToList()))
.OrderBy(x => x.Item1)
.ToList();
return actual;
}
}