-
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.
re-arch the way we do anlysis tests by making it easier for test clas…
…ses to do full blown endpoint testing
- Loading branch information
Showing
19 changed files
with
1,500 additions
and
856 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
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
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,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using Nest; | ||
using Tests.Analysis.TokenFilters; | ||
using Tests.Core.Client; | ||
using Tests.Search; | ||
|
||
namespace Tests.Analysis.Tokenizers | ||
{ | ||
public static class AnalysisUsageTests | ||
{ | ||
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 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); | ||
|
||
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 types = | ||
from t in typeof(TokenizerTests).GetNestedTypes() | ||
where typeof(TAssertion).IsAssignableFrom(t) && t.IsClass | ||
let a = t.GetCustomAttributes(typeof(SkipVersionAttribute)).FirstOrDefault() as SkipVersionAttribute | ||
where a != null && !a.Ranges.Any(r=>r.IsSatisfied(TestClient.Configuration.ElasticsearchVersion)) | ||
select (TAssertion) Activator.CreateInstance(t); | ||
return types.ToList(); | ||
} | ||
|
||
|
||
} | ||
} |
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,8 @@ | ||
namespace Tests.Analysis.Tokenizers | ||
{ | ||
public interface IAnalysisAssertion | ||
{ | ||
string Name { get; } | ||
object Json { get; } | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/Tests/Tests/Analysis/TokenFilters/ITokenFilterAssertion.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,12 @@ | ||
using System; | ||
using Nest; | ||
|
||
namespace Tests.Analysis.Tokenizers | ||
{ | ||
|
||
public interface ITokenFilterAssertion : IAnalysisAssertion | ||
{ | ||
ITokenFilter Initializer { get; } | ||
Func<string, TokenFiltersDescriptor, IPromise<ITokenFilters>> Fluent { get; } | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/Tests/Tests/Analysis/TokenFilters/TokenFilterAssertionBase.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,88 @@ | ||
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.TokenFilters | ||
{ | ||
|
||
[IntegrationTestCluster(typeof(ReadOnlyCluster))] | ||
public abstract class TokenFilterAssertionBase<TAssertion> where TAssertion : TokenFilterAssertionBase<TAssertion>, new() | ||
{ | ||
private static readonly SingleEndpointUsage<ICreateIndexResponse> Usage = new SingleEndpointUsage<ICreateIndexResponse> | ||
( | ||
fluent: (s, c) => c.CreateIndex(s, FluentCall), | ||
fluentAsync: (s, c) => c.CreateIndexAsync(s, FluentCall), | ||
request: (s, c) => c.CreateIndex(InitializerCall(s)), | ||
requestAsync: (s, c) => c.CreateIndexAsync(InitializerCall(s)), | ||
valuePrefix: $"test-{typeof(TAssertion).Name.ToLowerInvariant()}" | ||
) | ||
{ | ||
OnAfterCall = c=> c.DeleteIndex(Usage.CallUniqueValues.Value) | ||
}; | ||
private static TAssertion AssertionSetup { get; } = new TAssertion(); | ||
|
||
protected TokenFilterAssertionBase() | ||
{ | ||
this.Client = (ElasticXunitRunner.CurrentCluster as ReadOnlyCluster)?.Client ?? TestClient.DefaultInMemoryClient; | ||
Usage.KickOffOnce(this.Client); | ||
} | ||
|
||
private IElasticClient Client { get; } | ||
|
||
protected abstract string Name { get; } | ||
protected abstract ITokenFilter Initializer { get; } | ||
protected abstract Func<string, TokenFiltersDescriptor, IPromise<ITokenFilters>> Fluent { get; } | ||
protected abstract object Json { get; } | ||
|
||
[U] public async Task TestPutSettingsRequest() => await Usage.AssertOnAllResponses(r => | ||
{ | ||
var json = new | ||
{ | ||
settings = new | ||
{ | ||
analysis = new | ||
{ | ||
tokenizer = new Dictionary<string, object> | ||
{ | ||
{ AssertionSetup.Name, AssertionSetup.Json} | ||
} | ||
} | ||
} | ||
}; | ||
SerializationTestHelper.Expect(json).FromRequest(r); | ||
}); | ||
|
||
[I] public async Task TestPutSettingsResponse() => await Usage.AssertOnAllResponses(r => | ||
{ | ||
r.ApiCall.HttpStatusCode.Should().Be(200); | ||
}); | ||
|
||
private static CreateIndexRequest InitializerCall(string index) => new CreateIndexRequest(index) | ||
{ | ||
Settings = new IndexSettings | ||
{ | ||
Analysis = new Nest.Analysis | ||
{ | ||
TokenFilters = new Nest.TokenFilters { { AssertionSetup.Name, AssertionSetup.Initializer } } | ||
|
||
} | ||
} | ||
}; | ||
|
||
private static Func<CreateIndexDescriptor, ICreateIndexRequest> FluentCall => i => i | ||
.Settings(s => s | ||
.Analysis(a => a | ||
.TokenFilters(d => AssertionSetup.Fluent(AssertionSetup.Name, d)) | ||
) | ||
); | ||
|
||
} | ||
} |
Oops, something went wrong.