2022-06-26 02:15:33 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
using MfGames.Gallium;
|
|
|
|
using MfGames.Nitride.Entities;
|
2022-06-26 02:15:33 +00:00
|
|
|
|
|
|
|
using Xunit;
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
namespace MfGames.Nitride.Tests.Entities;
|
2022-06-26 02:15:33 +00:00
|
|
|
|
|
|
|
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>>());
|
|
|
|
|
2022-06-26 02:15:33 +00:00
|
|
|
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()));
|
2022-06-26 02:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[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" }),
|
2022-06-26 02:15:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
List<Tuple<string, List<string>?>> actual = this.GetActual(input);
|
|
|
|
|
|
|
|
Assert.Equal(
|
|
|
|
new[]
|
|
|
|
{
|
2023-01-15 00:19:42 +00:00
|
|
|
new Tuple<string, List<string>?>(
|
|
|
|
"index1",
|
|
|
|
new List<string> { "page1", "page3" }),
|
|
|
|
new Tuple<string, List<string>?>(
|
|
|
|
"index2",
|
|
|
|
new List<string> { "page2" }),
|
2022-06-26 02:15:33 +00:00
|
|
|
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" }),
|
2022-06-26 02:15:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
List<Tuple<string, List<string>?>> actual = this.GetActual(input);
|
|
|
|
|
|
|
|
Assert.Equal(
|
|
|
|
new[]
|
|
|
|
{
|
2023-01-15 00:19:42 +00:00
|
|
|
new Tuple<string, List<string>?>(
|
|
|
|
"index1",
|
|
|
|
new List<string> { "index2", "page3" }),
|
|
|
|
new Tuple<string, List<string>?>(
|
|
|
|
"index2",
|
|
|
|
new List<string> { "page2" }),
|
2022-06-26 02:15:33 +00:00
|
|
|
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" }),
|
2022-06-26 02:15:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
List<Tuple<string, List<string>?>> actual = this.GetActual(input);
|
|
|
|
|
|
|
|
Assert.Equal(
|
|
|
|
new[]
|
|
|
|
{
|
2023-01-15 00:19:42 +00:00
|
|
|
new Tuple<string, List<string>?>(
|
|
|
|
"index1",
|
|
|
|
new List<string> { "page1" }),
|
2022-06-26 02:15:33 +00:00
|
|
|
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-06-26 02:15:33 +00:00
|
|
|
};
|
|
|
|
|
2022-07-09 04:52:10 +00:00
|
|
|
var output = this.scanner.Run(input)
|
|
|
|
.ToList()
|
|
|
|
.Run(this.op)
|
|
|
|
.ToList();
|
|
|
|
|
2022-06-26 02:15:33 +00:00
|
|
|
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()))
|
2022-06-26 02:15:33 +00:00
|
|
|
.OrderBy(x => x.Item1)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
Assert.Equal(
|
|
|
|
new[]
|
|
|
|
{
|
2023-01-15 00:19:42 +00:00
|
|
|
new Tuple<string, List<string>?>(
|
|
|
|
"index1",
|
|
|
|
new List<string> { "page1" }),
|
2022-06-26 02:15:33 +00:00
|
|
|
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();
|
|
|
|
|
2022-06-26 02:15:33 +00:00
|
|
|
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()))
|
2022-06-26 02:15:33 +00:00
|
|
|
.OrderBy(x => x.Item1)
|
|
|
|
.ToList();
|
2022-07-09 04:52:10 +00:00
|
|
|
|
2022-06-26 02:15:33 +00:00
|
|
|
return actual;
|
|
|
|
}
|
|
|
|
}
|