-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit refactors the way we do analysis tests by making it easier for test classes to do full blown endpoint testing (cherry picked from commit d6f1ae5) * continuing to port analysis over to more isolated chunks (cherry picked from commit f5f0c437871589b1fb90b6c4c6f09f0dfc296d7e) * Ported normalizers over to new test format (cherry picked from commit c74ed51e2c30804ffc1d50f95a17893a93bfa6ea) * clean up namespaces (cherry picked from commit f2da9f51b43b188cc1b2d09f616fbf87ca268344) * All analysis unit/integ tests pass again (cherry picked from commit 7ecbee5435df02810ede7f07985e7bb13f66b6f3) * abstracted analysis base classes even further to all share a base which implements the bulk of the setup and tests (cherry picked from commit 8a6e99493a4174a87cc8680609afd0c482cf10d7) (cherry picked from commit 6461c8f)
- Loading branch information
Showing
28 changed files
with
2,003 additions
and
629 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/Tests/Tests.Core/ManagedElasticsearch/Clusters/ReadOnlyCluster.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 8 additions & 3 deletions
11
src/Tests/Tests.Core/ManagedElasticsearch/Clusters/WritableCluster.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Elastic.Xunit; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using FluentAssertions; | ||
using Nest; | ||
using Tests.Core.Client; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Core.Serialization; | ||
using Tests.Framework.Integration; | ||
|
||
namespace Tests.Analysis | ||
{ | ||
public interface IAnalysisAssertion | ||
{ | ||
string Name { get; } | ||
object Json { get; } | ||
} | ||
public interface IAnalysisAssertion<out TComponent, out TContainer, in TDescriptor> : IAnalysisAssertion | ||
where TContainer : class | ||
{ | ||
TComponent Initializer { get; } | ||
Func<string, TDescriptor, IPromise<TContainer>> Fluent { get; } | ||
} | ||
|
||
[IntegrationTestCluster(typeof(WritableCluster))] | ||
public abstract class AnalysisComponentTestBase<TAssertion, TComponent, TContainer, TDescriptor> | ||
: IAnalysisAssertion<TComponent, TContainer, TDescriptor> | ||
where TAssertion : AnalysisComponentTestBase<TAssertion, TComponent, TContainer, TDescriptor>, new() | ||
where TContainer : class | ||
{ | ||
private static readonly SingleEndpointUsage<ICreateIndexResponse> Usage = new SingleEndpointUsage<ICreateIndexResponse> | ||
( | ||
fluent: (s, c) => c.CreateIndex(s, AssertionSetup.FluentCall), | ||
fluentAsync: (s, c) => c.CreateIndexAsync(s, AssertionSetup.FluentCall), | ||
request: (s, c) => c.CreateIndex(AssertionSetup.InitializerCall(s)), | ||
requestAsync: (s, c) => c.CreateIndexAsync(AssertionSetup.InitializerCall(s)), | ||
valuePrefix: $"test-{typeof(TAssertion).Name.ToLowerInvariant()}" | ||
) | ||
{ | ||
OnAfterCall = c=> c.DeleteIndex(Usage.CallUniqueValues.Value) | ||
}; | ||
protected static TAssertion AssertionSetup { get; } = new TAssertion(); | ||
|
||
protected AnalysisComponentTestBase() | ||
{ | ||
this.Client = (ElasticXunitRunner.CurrentCluster as INestTestCluster)?.Client ?? TestClient.DefaultInMemoryClient; | ||
Usage.KickOffOnce(this.Client, oneRandomCall: true); | ||
} | ||
|
||
private IElasticClient Client { get; } | ||
|
||
public abstract string Name { get; } | ||
public abstract TComponent Initializer { get; } | ||
public abstract Func<string, TDescriptor, IPromise<TContainer>> Fluent { get; } | ||
public abstract object Json { get; } | ||
|
||
private Func<CreateIndexDescriptor, ICreateIndexRequest> FluentCall => i =>i.Settings(s => s.Analysis(this.FluentAnalysis)); | ||
protected abstract IAnalysis FluentAnalysis(AnalysisDescriptor an); | ||
|
||
private CreateIndexRequest InitializerCall(string index) => new CreateIndexRequest(index) | ||
{ | ||
Settings = new IndexSettings { Analysis = this.InitializerAnalysis() } | ||
}; | ||
protected abstract Nest.Analysis InitializerAnalysis(); | ||
|
||
[U] public virtual async Task TestPutSettingsRequest() => await Usage.AssertOnAllResponses(r => | ||
{ | ||
var json = new { settings = new { analysis = this.AnalysisJson } }; | ||
SerializationTestHelper.Expect(json).FromRequest(r); | ||
}); | ||
|
||
protected abstract object AnalysisJson { get; } | ||
|
||
[I] public virtual async Task TestPutSettingsResponse() => await Usage.AssertOnAllResponses(r => | ||
{ | ||
r.ApiCall.HttpStatusCode.Should().Be(200); | ||
}); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using FluentAssertions; | ||
using Nest; | ||
using Tests.Analysis.Analyzers; | ||
using Tests.Analysis.CharFilters; | ||
using Tests.Analysis.Normalizers; | ||
using Tests.Analysis.TokenFilters; | ||
using Tests.Analysis.Tokenizers; | ||
using Tests.Core.Client; | ||
|
||
namespace Tests.Analysis | ||
{ | ||
public class AnalysisUsageTestsTests | ||
{ | ||
[U] public static void CollectionsShouldNotBeEmpty() | ||
{ | ||
var analyzers = AnalysisUsageTests.AnalyzersInitializer.Analysis.Analyzers; | ||
var charFilters = AnalysisUsageTests.CharFiltersInitializer.Analysis.CharFilters; | ||
var tokenizers = AnalysisUsageTests.TokenizersInitializer.Analysis.Tokenizers; | ||
var tokenFilters = AnalysisUsageTests.TokenFiltersInitializer.Analysis.TokenFilters; | ||
|
||
analyzers.Should().NotBeNull().And.NotBeEmpty(); | ||
charFilters.Should().NotBeNull().And.NotBeEmpty(); | ||
tokenizers.Should().NotBeNull().And.NotBeEmpty(); | ||
tokenFilters.Should().NotBeNull().And.NotBeEmpty(); | ||
} | ||
} | ||
|
||
public static class AnalysisUsageTests | ||
{ | ||
|
||
public static IndexSettings NormalizersFluent => Fluent<NormalizersDescriptor, INormalizerAssertion, INormalizers>(i => i.Fluent, (a, v) => a.Normalizers = v.Value); | ||
|
||
public static IndexSettings AnalyzersFluent => Fluent<AnalyzersDescriptor, IAnalyzerAssertion, IAnalyzers>(i => i.Fluent, (a, v) => a.Analyzers = v.Value); | ||
|
||
public static IndexSettings TokenizersFluent => Fluent<TokenizersDescriptor, ITokenizerAssertion, ITokenizers>(i => i.Fluent, (a, v) => a.Tokenizers = v.Value); | ||
|
||
public static IndexSettings TokenFiltersFluent => Fluent<TokenFiltersDescriptor, ITokenFilterAssertion, ITokenFilters>(i => i.Fluent, (a, v) => a.TokenFilters = v.Value); | ||
|
||
public static IndexSettings CharFiltersFluent => Fluent<CharFiltersDescriptor, ICharFilterAssertion, ICharFilters>(i => i.Fluent, (a, v) => a.CharFilters = v.Value); | ||
|
||
public static IndexSettings NormalizersInitializer => Init<Nest.Normalizers, INormalizerAssertion, INormalizer>(i => i.Initializer, (a, v) => a.Normalizers = v); | ||
|
||
public static IndexSettings AnalyzersInitializer => Init<Nest.Analyzers, IAnalyzerAssertion, IAnalyzer>(i => i.Initializer, (a, v) => a.Analyzers = v); | ||
|
||
public static IndexSettings TokenizersInitializer => Init<Nest.Tokenizers, ITokenizerAssertion, ITokenizer>(i => i.Initializer, (a, v) => a.Tokenizers = v); | ||
|
||
public static IndexSettings TokenFiltersInitializer => Init<Nest.TokenFilters, ITokenFilterAssertion, ITokenFilter>(i => i.Initializer, (a, v) => a.TokenFilters = v); | ||
|
||
public static IndexSettings CharFiltersInitializer => Init<Nest.CharFilters, ICharFilterAssertion, ICharFilter>(i => i.Initializer, (a, v) => a.CharFilters = v); | ||
|
||
private static IndexSettings Fluent<TContainer, TAssertion, TValue>(Func<TAssertion, Func<string, TContainer, IPromise<TValue>>> fluent, Action<Nest.Analysis, IPromise<TValue>> set) | ||
where TAssertion : IAnalysisAssertion | ||
where TContainer : IPromise<TValue>, new() | ||
where TValue : class => Wrap(an => set(an, Apply<TContainer, TAssertion>((t, a) => fluent(a)(a.Name, t)))); | ||
|
||
private static IndexSettings Init<TContainer, TAssertion, TInitializer>(Func<TAssertion, TInitializer> value, Action<Nest.Analysis, TContainer> set) | ||
where TAssertion : IAnalysisAssertion | ||
where TContainer : IDictionary<string, TInitializer>, new() => Wrap(an => set(an, Apply<TContainer, TAssertion>((t, a) => t[a.Name] = value(a)))); | ||
|
||
private static TContainer Apply<TContainer, TAssertion>(Action<TContainer, TAssertion> act) | ||
where TAssertion : IAnalysisAssertion | ||
where TContainer : new() => All<TAssertion>().Aggregate(new TContainer() , (t,a) => { act(t,a); return t; }, t=>t); | ||
|
||
private static IndexSettings Wrap(Action<Nest.Analysis> set) | ||
{ | ||
var a = new Nest.Analysis(); | ||
var s =new IndexSettings { Analysis = a }; | ||
set(a); | ||
return s; | ||
} | ||
|
||
private static List<TAssertion> All<TAssertion>() | ||
where TAssertion : IAnalysisAssertion | ||
{ | ||
var assertions = typeof(TokenizerTests).GetNestedTypes() | ||
.Union(typeof(TokenFilterTests).GetNestedTypes()) | ||
.Union(typeof(NormalizerTests).GetNestedTypes()) | ||
.Union(typeof(AnalyzerTests).GetNestedTypes()) | ||
.Union(typeof(CharFilterTests).GetNestedTypes()) | ||
.ToList(); | ||
|
||
var nestedTypes = assertions | ||
.Where(t => typeof(TAssertion).IsAssignableFrom(t) && t.IsClass) | ||
.ToList(); | ||
|
||
var types = nestedTypes | ||
.Select(t => new | ||
{ | ||
t, | ||
a = t.GetCustomAttributes(typeof(SkipVersionAttribute)).FirstOrDefault() as SkipVersionAttribute | ||
}) | ||
.Where(@t1 => @t1.a == null || !@t1.a.Ranges.Any(r => r.IsSatisfied(TestClient.Configuration.ElasticsearchVersion))) | ||
.Select(@t1 => (TAssertion) Activator.CreateInstance(@t1.t)); | ||
return types.ToList(); | ||
} | ||
|
||
|
||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Tests/Tests/Analysis/AnalysisWithNormalizerCrudTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Nest; | ||
using Tests.Analysis.Tokenizers; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Framework.Integration; | ||
using static Tests.Framework.Promisify; | ||
|
||
namespace Tests.Analysis | ||
{ | ||
public class AnalysisWithNormalizerCrudTests : AnalysisCrudTests | ||
{ | ||
public AnalysisWithNormalizerCrudTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { } | ||
|
||
protected override CreateIndexRequest CreateInitializer(string indexName) => new CreateIndexRequest(indexName) | ||
{ | ||
Settings = new IndexSettings | ||
{ | ||
Analysis = new Nest.Analysis | ||
{ | ||
Analyzers = AnalysisUsageTests.AnalyzersInitializer.Analysis.Analyzers, | ||
CharFilters = AnalysisUsageTests.CharFiltersInitializer.Analysis.CharFilters, | ||
Tokenizers = AnalysisUsageTests.TokenizersInitializer.Analysis.Tokenizers, | ||
TokenFilters = AnalysisUsageTests.TokenFiltersInitializer.Analysis.TokenFilters, | ||
Normalizers = AnalysisUsageTests.NormalizersInitializer.Analysis.Normalizers, | ||
} | ||
} | ||
}; | ||
|
||
protected override ICreateIndexRequest CreateFluent(string indexName, CreateIndexDescriptor c) => | ||
c.Settings(s => s | ||
.Analysis(a => a | ||
.Analyzers(t => Promise(AnalysisUsageTests.AnalyzersFluent.Analysis.Analyzers)) | ||
.CharFilters(t => Promise(AnalysisUsageTests.CharFiltersFluent.Analysis.CharFilters)) | ||
.Tokenizers(t => Promise(AnalysisUsageTests.TokenizersFluent.Analysis.Tokenizers)) | ||
.TokenFilters(t => Promise(AnalysisUsageTests.TokenFiltersFluent.Analysis.TokenFilters)) | ||
.Normalizers(t => Promise(AnalysisUsageTests.NormalizersFluent.Analysis.Normalizers)) | ||
) | ||
); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Tests/Tests/Analysis/Analyzers/AnalyzerAssertionBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using Nest; | ||
|
||
namespace Tests.Analysis.Analyzers | ||
{ | ||
public interface IAnalyzerAssertion : IAnalysisAssertion<IAnalyzer, IAnalyzers, AnalyzersDescriptor> { } | ||
|
||
public abstract class AnalyzerAssertionBase<TAssertion> | ||
: AnalysisComponentTestBase<TAssertion, IAnalyzer, IAnalyzers, AnalyzersDescriptor> | ||
, IAnalyzerAssertion | ||
where TAssertion : AnalyzerAssertionBase<TAssertion>, new() | ||
{ | ||
protected override IAnalysis FluentAnalysis(AnalysisDescriptor an) => | ||
an.Analyzers(d => AssertionSetup.Fluent(AssertionSetup.Name, d)); | ||
|
||
protected override Nest.Analysis InitializerAnalysis() => | ||
new Nest.Analysis {Analyzers = new Nest.Analyzers {{AssertionSetup.Name, AssertionSetup.Initializer}}}; | ||
|
||
protected override object AnalysisJson => new | ||
{ | ||
analyzer = new Dictionary<string, object> { {AssertionSetup.Name, AssertionSetup.Json} } | ||
}; | ||
// https://youtrack.jetbrains.com/issue/RIDER-19912 | ||
[U] public override Task TestPutSettingsRequest() => base.TestPutSettingsRequest(); | ||
[I] public override Task TestPutSettingsResponse() => base.TestPutSettingsResponse(); | ||
} | ||
} |
Oops, something went wrong.