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

236 lines
8.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2022-09-06 05:53:22 +00:00
using MfGames.Gallium;
2022-09-06 05:53:22 +00:00
using MfGames.Nitride.Tests;
using Xunit;
using Xunit.Abstractions;
2022-09-06 05:53:22 +00:00
namespace MfGames.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<Timekeeper>();
CreateDateIndexes op = context.Resolve<CreateDateIndexes>()
.WithFormats("yyyy-MM")
.WithCreateIndex(this.CreateIndex);
List<Entity> input = new()
{
2022-07-09 04:52:10 +00:00
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<Tuple<string, List<string>?, List<string>?>> actual = this.GetActual(op, input);
2022-07-09 04:52:10 +00:00
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();
Timekeeper timekeeper = context.Resolve<Timekeeper>();
CreateDateIndexes op = context.Resolve<CreateDateIndexes>()
.WithFormats("yyyy/MM/dd", "yyyy/MM", "yyyy")
.WithCreateIndex(this.CreateIndex);
List<Entity> input = new()
{
2022-07-09 04:52:10 +00:00
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<Tuple<string, List<string>?, List<string>?>> actual = this.GetActual(op, input);
2022-07-09 04:52:10 +00:00
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();
Timekeeper timekeeper = context.Resolve<Timekeeper>();
CreateDateIndexes op = context.Resolve<CreateDateIndexes>()
.WithFormats("yyyy/MM/dd", "yyyy/MM", "yyyy")
.WithCreateIndex(this.CreateIndex)
.WithLessThanEqualCollapse(1);
List<Entity> input = new()
{
2022-07-09 04:52:10 +00:00
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<Tuple<string, List<string>?, List<string>?>> actual = this.GetActual(op, input);
2022-07-09 04:52:10 +00:00
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();
Timekeeper timekeeper = context.Resolve<Timekeeper>();
CreateDateIndexes op = context.Resolve<CreateDateIndexes>()
.WithFormats("yyyy-MM", "yyyy")
.WithCreateIndex(this.CreateIndex);
List<Entity> input = new()
{
2022-07-09 04:52:10 +00:00
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<Tuple<string, List<string>?, List<string>?>> actual = this.GetActual(op, input);
2022-07-09 04:52:10 +00:00
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();
Timekeeper timekeeper = context.Resolve<Timekeeper>();
CreateDateIndexes op = context.Resolve<CreateDateIndexes>()
.WithFormats("yyyy")
.WithCreateIndex(this.CreateIndex);
List<Entity> input = new()
{
2022-07-09 04:52:10 +00:00
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<Tuple<string, List<string>?, List<string>?>> actual = this.GetActual(op, input);
2022-07-09 04:52:10 +00:00
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)
{
2022-07-09 04:52:10 +00:00
return new Entity().Add(a)
.Add($"index-{a.Key}");
}
2022-07-09 04:52:10 +00:00
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>(),
2022-07-09 04:52:10 +00:00
x.GetOptional<DateIndex>()
?.Entries.Select(a => a.Get<string>())
.OrderBy(x => x)
.ToList(),
x.GetOptional<DateIndex>()
?.Indexes.Select(a => a.Get<string>())
.OrderBy(x => x)
.ToList()))
.OrderBy(x => x.Item1)
.ToList();
2022-07-09 04:52:10 +00:00
return actual;
}
}