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.Tests/Entities/CreateOrUpdateIndexTests.cs

185 lines
5.3 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.Entities;
using Xunit;
using Xunit.Abstractions;
2022-09-06 05:53:22 +00:00
namespace MfGames.Nitride.Tests.Entities;
public class CreateOrUpdateIndexTests : NitrideTestBase, IDisposable
{
private readonly NitrideTestContext context;
private readonly IOperation op;
private readonly EntityScanner scanner;
public CreateOrUpdateIndexTests(ITestOutputHelper output)
: base(output)
{
this.context = this.CreateContext();
2022-07-09 04:52:10 +00:00
this.scanner = this.context.Resolve<EntityScanner>()
.WithGetKeysFromEntity(e => e.GetOptional<List<string>>());
this.op = this.context.Resolve<CreateOrUpdateIndex>()
.WithScanner(this.scanner)
2022-07-09 04:52:10 +00:00
.WithGetIndexKey(
x => x.Get<string>()
.Contains("index")
? x.Get<string>()
.Replace("index", "cat")
: null)
.WithCreateIndex(
(
key,
list) => new Entity().Add(key.Replace("cat", "index"))
.Add(list.ToList()))
.WithUpdateIndex(
(
index,
key,
list) => index.Add(list.ToList()));
}
[Fact]
public void CreateMultipleIndexes()
{
List<Entity> input = new()
{
2022-07-09 04:52:10 +00:00
new Entity().Add("page1")
.Add(new List<string> { "cat1" }),
new Entity().Add("page2")
.Add(new List<string> { "cat2" }),
new Entity().Add("page3")
.Add(new List<string> { "cat1" }),
};
List<Tuple<string, List<string>?>> actual = this.GetActual(input);
Assert.Equal(
new[]
{
new Tuple<string, List<string>?>("index1", new List<string> { "page1", "page3" }),
new Tuple<string, List<string>?>("index2", new List<string> { "page2" }),
new Tuple<string, List<string>?>("page1", null),
new Tuple<string, List<string>?>("page2", null),
new Tuple<string, List<string>?>("page3", null),
},
actual);
}
[Fact]
public void CreateNestedIndexes()
{
List<Entity> input = new()
{
2022-07-09 04:52:10 +00:00
new Entity().Add("index2")
.Add(new List<string> { "cat1" }),
new Entity().Add("page2")
.Add(new List<string> { "cat2" }),
new Entity().Add("page3")
.Add(new List<string> { "cat1" }),
};
List<Tuple<string, List<string>?>> actual = this.GetActual(input);
Assert.Equal(
new[]
{
new Tuple<string, List<string>?>("index1", new List<string> { "index2", "page3" }),
new Tuple<string, List<string>?>("index2", new List<string> { "page2" }),
new Tuple<string, List<string>?>("page2", null),
new Tuple<string, List<string>?>("page3", null),
},
actual);
}
[Fact]
public void CreateSimpleIndex()
{
List<Entity> input = new()
{
2022-07-09 04:52:10 +00:00
new Entity().Add("page1")
.Add(new List<string> { "cat1" }),
};
List<Tuple<string, List<string>?>> actual = this.GetActual(input);
Assert.Equal(
new[]
{
new Tuple<string, List<string>?>("index1", new List<string> { "page1" }),
new Tuple<string, List<string>?>("page1", null),
},
actual);
}
/// <inheritdoc />
public void Dispose()
{
this.context.Dispose();
}
[Fact]
public void UpdateSimpleIndex()
{
List<Entity> input = new()
{
new Entity().Add("index1"),
2022-07-09 04:52:10 +00:00
new Entity().Add("page1")
.Add(new List<string> { "cat1" }),
};
2022-07-09 04:52:10 +00:00
var output = this.scanner.Run(input)
.ToList()
.Run(this.op)
.ToList();
var actual = output
.Select(
x => new Tuple<string, List<string>?>(
x.Get<string>(),
2022-07-09 04:52:10 +00:00
x.GetOptional<List<Entity>>()
?.Select(y => y.Get<string>())
.OrderBy(y => y)
.ToList()))
.OrderBy(x => x.Item1)
.ToList();
Assert.Equal(
new[]
{
new Tuple<string, List<string>?>("index1", new List<string> { "page1" }),
new Tuple<string, List<string>?>("page1", null),
},
actual);
}
private List<Tuple<string, List<string>?>> GetActual(List<Entity> input)
{
2022-07-09 04:52:10 +00:00
var output = this.scanner.Run(input)
.ToList()
.Run(this.op)
.ToList();
var actual = output
.Select(
x => new Tuple<string, List<string>?>(
x.Get<string>(),
2022-07-09 04:52:10 +00:00
x.GetOptional<List<Entity>>()
?.Select(y => y.Get<string>())
.OrderBy(y => y)
.ToList()))
.OrderBy(x => x.Item1)
.ToList();
2022-07-09 04:52:10 +00:00
return actual;
}
}