Skip to content

Commit 3d8787b

Browse files
committed
Implemented a SkipVersion Attribute
This is increadibly ugly on .net core though because the semver lib is not compatible with dnxcore50 and will only work with exact matches. This fixes the failing aggregation tests that fail due to two bugs in elasticsearch 5.0 alpha1 itself
1 parent a14dddf commit 3d8787b

14 files changed

+114
-24
lines changed

paket.dependencies

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ nuget Rx-Interfaces
2525
nuget Rx-Linq
2626
nuget Rx-Main
2727
nuget Rx-PlatformServices
28+
nuget SemanticVersioning
2829

2930
source https://api.nuget.org/v3/index.json
3031

paket.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ NUGET
350350
xunit.extensibility.core (2.1) - framework: >= net45, dnx451, dnxcore50, monoandroid, monotouch, xamarinios, winv4.5, wpv8.0, wpav8.1
351351
remote: http://api.nuget.org/v3/index.json
352352
specs:
353+
SemanticVersioning (0.6.17)
353354
ShellProgressBar (3.0)
354355

355356
GROUP build

src/Elasticsearch.Net/Extensions/Extensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static string GetStringValue(this Enum enumValue)
1515
var knownEnum = KnownEnums.Resolve(enumValue);
1616
if (knownEnum != KnownEnums.UnknownEnum) return knownEnum;
1717

18-
//TODO measure performance and cache
18+
//TODO measure performance and cache
1919
var type = enumValue.GetType();
2020
#if DOTNETCORE
2121
var info = type.GetTypeInfo().GetDeclaredField(enumValue.ToString());
@@ -32,7 +32,7 @@ internal static string GetStringValue(this Enum enumValue)
3232
#else
3333
internal static string Utf8String(this byte[] bytes) => bytes == null ? null : Encoding.UTF8.GetString(bytes, 0, bytes.Length);
3434
#endif
35-
35+
3636
internal static byte[] Utf8Bytes(this string s)
3737
{
3838
return s.IsNullOrEmpty() ? null : Encoding.UTF8.GetBytes(s);
@@ -81,7 +81,7 @@ internal static bool IsNullOrEmpty(this string value)
8181
{
8282
return string.IsNullOrEmpty(value);
8383
}
84-
84+
8585
internal static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> property)
8686
{
8787
return items.GroupBy(property).Select(x => x.First());

src/Tests/Aggregations/Bucket/Filter/FilterAggregationUsageTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
using System.Linq;
44
using FluentAssertions;
55
using Nest;
6+
using Tests.Framework;
67
using Tests.Framework.Integration;
78
using Tests.Framework.MockData;
89
using static Nest.Infer;
910

1011
namespace Tests.Aggregations.Bucket.Filter
1112
{
1213
/**
13-
* Defines a single bucket of all the documents in the current document set context that match a specified filter.
14+
* Defines a single bucket of all the documents in the current document set context that match a specified filter.
1415
* Often this will be used to narrow down the current aggregation context to a specific set of documents.
1516
*
1617
* Be sure to read the Elasticsearch documentation on {ref_current}/search-aggregations-bucket-filter-aggregation.html[Filter Aggregation]
@@ -87,6 +88,7 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
8788
* When the collection of filters is empty or all are conditionless, NEST will serialize them
8889
* to an empty object.
8990
*/
91+
[SkipVersion("5.0.0-alpha1", "https://github.com/elastic/elasticsearch/issues/17518")]
9092
public class EmptyFilterAggregationUsageTests : AggregationUsageTestBase
9193
{
9294
public EmptyFilterAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage)

src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageEwmaAggregationUsageTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
using System.Linq;
33
using FluentAssertions;
44
using Nest;
5+
using Tests.Framework;
56
using Tests.Framework.Integration;
67
using Tests.Framework.MockData;
78

89
namespace Tests.Aggregations.Pipeline.MovingAverage
910
{
11+
[SkipVersion("5.0.0-alpha1", "https://github.com/elastic/elasticsearch/issues/17516")]
1012
public class MovingAverageEwmaAggregationUsageTests : AggregationUsageTestBase
1113
{
1214
public MovingAverageEwmaAggregationUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
@@ -78,7 +80,7 @@ public MovingAverageEwmaAggregationUsageTests(ReadOnlyCluster cluster, EndpointU
7880
{
7981
Field = "startedOn",
8082
Interval = DateInterval.Month,
81-
Aggregations =
83+
Aggregations =
8284
new SumAggregation("commits", "numberOfCommits") &&
8385
new MovingAverageAggregation("commits_moving_avg", "commits")
8486
{
@@ -98,7 +100,7 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
98100
projectsPerMonth.Should().NotBeNull();
99101
projectsPerMonth.Buckets.Should().NotBeNull();
100102
projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0);
101-
103+
102104
// average not calculated for the first bucket
103105
foreach(var item in projectsPerMonth.Buckets.Skip(1))
104106
{

src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltLinearAggregationUsageTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
using System.Linq;
33
using FluentAssertions;
44
using Nest;
5+
using Tests.Framework;
56
using Tests.Framework.Integration;
67
using Tests.Framework.MockData;
78

89
namespace Tests.Aggregations.Pipeline.MovingAverage
910
{
11+
[SkipVersion("5.0.0-alpha1", "https://github.com/elastic/elasticsearch/issues/17516")]
1012
public class MovingAverageHoltLinearAggregationUsageTests : AggregationUsageTestBase
1113
{
1214
public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
@@ -80,7 +82,7 @@ public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, End
8082
{
8183
Field = "startedOn",
8284
Interval = DateInterval.Month,
83-
Aggregations =
85+
Aggregations =
8486
new SumAggregation("commits", "numberOfCommits") &&
8587
new MovingAverageAggregation("commits_moving_avg", "commits")
8688
{
@@ -101,7 +103,7 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
101103
projectsPerMonth.Should().NotBeNull();
102104
projectsPerMonth.Buckets.Should().NotBeNull();
103105
projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0);
104-
106+
105107
// average not calculated for the first bucket
106108
foreach(var item in projectsPerMonth.Buckets.Skip(1))
107109
{

src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltWintersAggregationUsageTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
22
using FluentAssertions;
33
using Nest;
4+
using Tests.Framework;
45
using Tests.Framework.Integration;
56
using Tests.Framework.MockData;
67

78
namespace Tests.Aggregations.Pipeline.MovingAverage
89
{
10+
[SkipVersion("5.0.0-alpha1", "https://github.com/elastic/elasticsearch/issues/17516")]
911
public class MovingAverageHoltWintersUsageTests : AggregationUsageTestBase
1012
{
1113
public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
@@ -89,7 +91,7 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage
8991
{
9092
Field = "startedOn",
9193
Interval = DateInterval.Month,
92-
Aggregations =
94+
Aggregations =
9395
new SumAggregation("commits", "numberOfCommits") &&
9496
new MovingAverageAggregation("commits_moving_avg", "commits")
9597
{

src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageLinearAggregationUsageTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
using System.Linq;
33
using FluentAssertions;
44
using Nest;
5+
using Tests.Framework;
56
using Tests.Framework.Integration;
67
using Tests.Framework.MockData;
78

89
namespace Tests.Aggregations.Pipeline.MovingAverage
910
{
11+
[SkipVersion("5.0.0-alpha1", "https://github.com/elastic/elasticsearch/issues/17516")]
1012
public class MovingAverageLinearAggregationUsageTests : AggregationUsageTestBase
1113
{
1214
public MovingAverageLinearAggregationUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
@@ -75,7 +77,7 @@ public MovingAverageLinearAggregationUsageTests(ReadOnlyCluster cluster, Endpoin
7577
{
7678
Field = "startedOn",
7779
Interval = DateInterval.Month,
78-
Aggregations =
80+
Aggregations =
7981
new SumAggregation("commits", "numberOfCommits") &&
8082
new MovingAverageAggregation("commits_moving_avg", "commits")
8183
{

src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageSimpleAggregationUsageTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
using System.Linq;
33
using FluentAssertions;
44
using Nest;
5+
using Tests.Framework;
56
using Tests.Framework.Integration;
67
using Tests.Framework.MockData;
78

89
namespace Tests.Aggregations.Pipeline.MovingAverage
910
{
11+
[SkipVersion("5.0.0-alpha1", "https://github.com/elastic/elasticsearch/issues/17516")]
1012
public class MovingAverageSimpleAggregationUsageTests : AggregationUsageTestBase
1113
{
1214
public MovingAverageSimpleAggregationUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
@@ -77,7 +79,7 @@ public MovingAverageSimpleAggregationUsageTests(ReadOnlyCluster cluster, Endpoin
7779
{
7880
Field = "startedOn",
7981
Interval = DateInterval.Month,
80-
Aggregations =
82+
Aggregations =
8183
new SumAggregation("commits", "numberOfCommits") &&
8284
new MovingAverageAggregation("commits_moving_avg", "commits")
8385
{
@@ -96,7 +98,7 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
9698
projectsPerMonth.Should().NotBeNull();
9799
projectsPerMonth.Buckets.Should().NotBeNull();
98100
projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0);
99-
101+
100102
// average not calculated for the first bucket
101103
foreach(var item in projectsPerMonth.Buckets.Skip(1))
102104
{

src/Tests/Framework/TestAttributes.cs

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
4+
#if !DOTNETCORE
5+
using SemVer;
6+
#endif
37
using Xunit;
48
using Xunit.Abstractions;
59
using Xunit.Sdk;
10+
using System.Reflection;
11+
12+
#if DOTNETCORE
13+
namespace SemVer
14+
{
15+
//TODO incredibly ugly but on .net core we only support exact matches
16+
//We'll need to submit a PR to https://github.com/adamreeve/semver.net once .net core is
17+
//in more stable waters
18+
19+
public class Range
20+
{
21+
private string _range;
22+
public Range(string range)
23+
{
24+
this._range = range;
25+
}
26+
public bool IsSatisfied(SemVer.Version version) => version?.Parsed == this._range;
27+
}
28+
public class Version
29+
{
30+
public string Parsed { get; }
31+
public Version(string version)
32+
{
33+
this.Parsed = version;
34+
}
35+
}
36+
}
37+
#endif
638

739
namespace Tests.Framework
840
{
@@ -18,37 +50,68 @@ public class U : FactAttribute
1850

1951
public abstract class NestTestDiscoverer : IXunitTestCaseDiscoverer
2052
{
21-
readonly IMessageSink _diagnosticMessageSink;
53+
protected IMessageSink DiagnosticMessageSink;
2254
private readonly bool _condition;
2355

2456
protected NestTestDiscoverer(IMessageSink diagnosticMessageSink, bool condition)
2557
{
2658
this._condition = condition;
27-
this._diagnosticMessageSink = diagnosticMessageSink;
59+
this.DiagnosticMessageSink = diagnosticMessageSink;
2860
}
2961

30-
public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute) =>
31-
!_condition
32-
? Enumerable.Empty<IXunitTestCase>()
33-
: new[] { new XunitTestCase(_diagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod) };
62+
protected virtual bool SkipMethod(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute) => false;
63+
64+
public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute) =>
65+
!_condition || SkipMethod(discoveryOptions, testMethod, factAttribute)
66+
? Enumerable.Empty<IXunitTestCase>()
67+
: new[] { new XunitTestCase(DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod) };
3468
}
3569

3670
public class IntegrationTestDiscoverer : NestTestDiscoverer
3771
{
38-
public IntegrationTestDiscoverer(IMessageSink diagnosticMessageSink)
72+
public IntegrationTestDiscoverer(IMessageSink diagnosticMessageSink)
3973
: base(diagnosticMessageSink, TestClient.Configuration.RunIntegrationTests)
4074
{
4175
}
76+
77+
protected override bool SkipMethod(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
78+
{
79+
80+
var classOfMethod = Type.GetType(testMethod.TestClass.Class.Name, true, true);
81+
#if !DOTNETCORE
82+
var attributes = Attribute.GetCustomAttributes(classOfMethod, typeof(SkipVersionAttribute));
83+
#else
84+
85+
var attributes = classOfMethod.GetTypeInfo().GetCustomAttributes(typeof(SkipVersionAttribute), false);
86+
#endif
87+
if (!attributes.Any()) return false;
88+
89+
var version = new SemVer.Version(TestClient.Configuration.ElasticsearchVersion);
90+
var ranges = attributes.Cast<SkipVersionAttribute>()
91+
.SelectMany(a=>a.Ranges);
92+
93+
return ranges.Any(range => range.IsSatisfied(version));
94+
}
4295
}
4396

4497

4598
public class UnitTestDiscoverer : NestTestDiscoverer
4699
{
47-
public UnitTestDiscoverer(IMessageSink diagnosticMessageSink)
100+
public UnitTestDiscoverer(IMessageSink diagnosticMessageSink)
48101
: base(diagnosticMessageSink, TestClient.Configuration.RunUnitTests)
49102
{
50103
}
51104
}
52105

106+
public class SkipVersionAttribute : Attribute
107+
{
108+
public IList<SemVer.Range> Ranges { get; }
53109

54-
}
110+
public SkipVersionAttribute(string skipVersionRangesSeparatedByComma, string reason)
111+
{
112+
this.Ranges = skipVersionRangesSeparatedByComma.Split(',')
113+
.Select(r => new SemVer.Range(r))
114+
.ToList();
115+
}
116+
}
117+
}

src/Tests/Search/Percolator/MultiPercolate/MultiPercolateApiTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected override void OnBeforeCall(IElasticClient client)
2626
)
2727
);
2828
this.Client.ClusterHealth(h => h.WaitForStatus(WaitForStatus.Yellow));
29-
this.Client.Index(Project.Instance, s => s.Index(this.Index));
29+
this.Client.Index(Project.Instance, s => s.Index(this.Index).Refresh());
3030
var registerPercolator = this.Client.RegisterPercolator<Project>("match_all", r => r
3131
.Index(this.Index)
3232
.Query(q => q.MatchAll())

src/Tests/Tests.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,17 @@
12161216
</ItemGroup>
12171217
</When>
12181218
</Choose>
1219+
<Choose>
1220+
<When Condition="($(TargetFrameworkIdentifier) == '.NETFramework' And ($(TargetFrameworkVersion) == 'v4.0' Or $(TargetFrameworkVersion) == 'v4.5' Or $(TargetFrameworkVersion) == 'v4.5.1' Or $(TargetFrameworkVersion) == 'v4.5.2' Or $(TargetFrameworkVersion) == 'v4.5.3' Or $(TargetFrameworkVersion) == 'v4.6' Or $(TargetFrameworkVersion) == 'v4.6.1' Or $(TargetFrameworkVersion) == 'v4.6.2')) Or ($(TargetFrameworkIdentifier) == 'Silverlight' And $(TargetFrameworkVersion) == 'v5.0') Or ($(TargetFrameworkIdentifier) == 'MonoAndroid') Or ($(TargetFrameworkIdentifier) == 'MonoTouch') Or ($(TargetFrameworkIdentifier) == 'Xamarin.iOS') Or ($(TargetFrameworkIdentifier) == 'Xamarin.Mac') Or ($(TargetFrameworkProfile) == 'Profile14') Or ($(TargetFrameworkProfile) == 'Profile19') Or ($(TargetFrameworkProfile) == 'Profile24')">
1221+
<ItemGroup>
1222+
<Reference Include="SemVer">
1223+
<HintPath>..\..\packages\SemanticVersioning\lib\portable-net40+sl50+MonoAndroid10+xamarinios10+MonoTouch10\SemVer.dll</HintPath>
1224+
<Private>True</Private>
1225+
<Paket>True</Paket>
1226+
</Reference>
1227+
</ItemGroup>
1228+
</When>
1229+
</Choose>
12191230
<Choose>
12201231
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And ($(TargetFrameworkVersion) == 'v4.5' Or $(TargetFrameworkVersion) == 'v4.5.1' Or $(TargetFrameworkVersion) == 'v4.5.2' Or $(TargetFrameworkVersion) == 'v4.5.3' Or $(TargetFrameworkVersion) == 'v4.6' Or $(TargetFrameworkVersion) == 'v4.6.1' Or $(TargetFrameworkVersion) == 'v4.6.2')">
12211232
<ItemGroup>

src/Tests/paket.references

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Rx-Linq
77
Rx-Main
88
Rx-PlatformServices
99
Bogus
10-
DiffPlex
10+
DiffPlex
11+
SemanticVersioning

src/Tests/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"Nest": { "target": "project" },
1212
"Newtonsoft.Json": "8.0.2",
1313
"Rx-Main": "2.3.0-beta2",
14-
"xunit": "2.1.0-rc1-build3168"
14+
"xunit": "2.1.0-rc1-build3168",
1515
},
1616
"configurations": {
1717
"Debug": {
@@ -40,6 +40,7 @@
4040
"System.Threading.Tasks": ""
4141
},
4242
"dependencies": {
43+
"SemanticVersioning": "0.6.17",
4344
"xunit.runner.dnx": "2.1.0-rc1-build204"
4445
}
4546
},

0 commit comments

Comments
 (0)