Skip to content

Commit 5fe6167

Browse files
committed
Tests.Core, Tests.Configuration
1 parent c7a6ac4 commit 5fe6167

36 files changed

+496
-642
lines changed

src/Tests/Tests.Configuration/ConfigurationLoader.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ private static DirectoryInfo FindTestsConfigurationFolder(DirectoryInfo director
4141
var yamlConfigDir = Path.Combine(directoryInfo.FullName, "Tests.Configuration");
4242
if (directoryInfo.Name == "Tests" && Directory.Exists(yamlConfigDir))
4343
return new DirectoryInfo(yamlConfigDir);
44+
4445
directoryInfo = directoryInfo.Parent;
4546
} while (directoryInfo != null);
4647
return null;

src/Tests/Tests.Configuration/EnvironmentConfiguration.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,44 @@ namespace Tests.Configuration
44
{
55
public class EnvironmentConfiguration : TestConfigurationBase
66
{
7-
public sealed override bool TestAgainstAlreadyRunningElasticsearch { get; protected set; } = false;
8-
public sealed override bool ForceReseed { get; protected set; } = true;
9-
public sealed override string ElasticsearchVersion { get; protected set; }
10-
public sealed override TestMode Mode { get; protected set; } = TestMode.Unit;
11-
public sealed override string ClusterFilter { get; protected set; }
12-
public sealed override string TestFilter { get; protected set; }
13-
public sealed override int Seed { get; protected set; }
14-
public sealed override bool ShowElasticsearchOutputAfterStarted { get; protected set; }
15-
167
public EnvironmentConfiguration()
178
{
189
//if env var NEST_INTEGRATION_VERSION is set assume integration mode
1910
//used by the build script FAKE
2011
var version = Environment.GetEnvironmentVariable("NEST_INTEGRATION_VERSION");
21-
if (!string.IsNullOrEmpty(version)) this.Mode = TestMode.Integration;
12+
if (!string.IsNullOrEmpty(version)) Mode = TestMode.Integration;
2213

23-
this.ElasticsearchVersion = string.IsNullOrWhiteSpace(version) ? DefaultVersion : version;
24-
this.ClusterFilter = Environment.GetEnvironmentVariable("NEST_INTEGRATION_CLUSTER");
25-
this.ShowElasticsearchOutputAfterStarted = Environment.GetEnvironmentVariable("NEST_INTEGRATION_SHOW_OUTPUT_AFTER_START") == "1";
26-
this.TestFilter = Environment.GetEnvironmentVariable("NEST_TEST_FILTER");
14+
ElasticsearchVersion = string.IsNullOrWhiteSpace(version) ? DefaultVersion : version;
15+
ClusterFilter = Environment.GetEnvironmentVariable("NEST_INTEGRATION_CLUSTER");
16+
ShowElasticsearchOutputAfterStarted = Environment.GetEnvironmentVariable("NEST_INTEGRATION_SHOW_OUTPUT_AFTER_START") == "1";
17+
TestFilter = Environment.GetEnvironmentVariable("NEST_TEST_FILTER");
2718

2819
var newRandom = new Random().Next(1, 100000);
2920

30-
this.Seed = TryGetEnv("NEST_TEST_SEED", out var seed) ? int.Parse(seed) : newRandom;
31-
var randomizer = new Random(this.Seed);
21+
Seed = TryGetEnv("NEST_TEST_SEED", out var seed) ? int.Parse(seed) : newRandom;
22+
var randomizer = new Random(Seed);
3223

33-
this.Random = new RandomConfiguration
24+
Random = new RandomConfiguration
3425
{
3526
SourceSerializer = RandomBoolConfig("SOURCESERIALIZER", randomizer),
3627
TypedKeys = RandomBoolConfig("TYPEDKEYS", randomizer),
3728
};
3829
}
3930

31+
public sealed override string ClusterFilter { get; protected set; }
32+
public sealed override string ElasticsearchVersion { get; protected set; }
33+
public sealed override bool ForceReseed { get; protected set; } = true;
34+
public sealed override TestMode Mode { get; protected set; } = TestMode.Unit;
35+
public sealed override int Seed { get; protected set; }
36+
public sealed override bool ShowElasticsearchOutputAfterStarted { get; protected set; }
37+
public sealed override bool TestAgainstAlreadyRunningElasticsearch { get; protected set; } = false;
38+
public sealed override string TestFilter { get; protected set; }
39+
4040
private static bool RandomBoolConfig(string key, Random randomizer)
4141
{
4242
if (TryGetEnv("NEST_RANDOM_" + key, out var source) && bool.TryParse(source, out var b))
4343
return b;
44+
4445
return randomizer.NextDouble() >= 0.5;
4546
}
4647

src/Tests/Tests.Configuration/ITestConfiguration.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
{
33
public interface ITestConfiguration
44
{
5-
TestMode Mode { get; }
6-
string ElasticsearchVersion { get; }
75
string ClusterFilter { get; }
8-
string TestFilter { get; }
6+
string ElasticsearchVersion { get; }
97
bool ForceReseed { get; }
10-
bool TestAgainstAlreadyRunningElasticsearch { get; }
8+
TestMode Mode { get; }
119

12-
int Seed { get; }
10+
RandomConfiguration Random { get; }
1311

1412
bool RunIntegrationTests { get; }
1513
bool RunUnitTests { get; }
1614

17-
RandomConfiguration Random { get; }
15+
int Seed { get; }
1816
bool ShowElasticsearchOutputAfterStarted { get; }
17+
bool TestAgainstAlreadyRunningElasticsearch { get; }
18+
string TestFilter { get; }
1919
}
2020

2121
public class RandomConfiguration

src/Tests/Tests.Configuration/TestConfigurationBase.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
public abstract class TestConfigurationBase : ITestConfiguration
44
{
55
protected const string DefaultVersion = "6.0.0";
6-
public abstract bool TestAgainstAlreadyRunningElasticsearch { get; protected set; }
6+
public abstract string ClusterFilter { get; protected set; }
77
public abstract string ElasticsearchVersion { get; protected set; }
88
public abstract bool ForceReseed { get; protected set; }
99
public abstract TestMode Mode { get; protected set; }
10-
public abstract string ClusterFilter { get; protected set; }
11-
public abstract string TestFilter { get; protected set; }
12-
public abstract bool ShowElasticsearchOutputAfterStarted { get; protected set; }
10+
public RandomConfiguration Random { get; protected set; }
1311

1412

15-
public virtual bool RunIntegrationTests => this.Mode == TestMode.Mixed || this.Mode == TestMode.Integration;
16-
public virtual bool RunUnitTests => this.Mode == TestMode.Mixed || this.Mode == TestMode.Unit;
13+
public virtual bool RunIntegrationTests => Mode == TestMode.Mixed || Mode == TestMode.Integration;
14+
public virtual bool RunUnitTests => Mode == TestMode.Mixed || Mode == TestMode.Unit;
1715

1816
public abstract int Seed { get; protected set; }
19-
public RandomConfiguration Random { get; protected set; }
17+
public abstract bool ShowElasticsearchOutputAfterStarted { get; protected set; }
18+
public abstract bool TestAgainstAlreadyRunningElasticsearch { get; protected set; }
19+
public abstract string TestFilter { get; protected set; }
2020
}
2121
}

src/Tests/Tests.Configuration/YamlConfiguration.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ namespace Tests.Configuration
88
public class YamlConfiguration : TestConfigurationBase
99
{
1010
private readonly Dictionary<string, string> _config;
11-
public sealed override bool TestAgainstAlreadyRunningElasticsearch { get; protected set; } = true;
12-
public sealed override string ElasticsearchVersion { get; protected set; }
13-
public sealed override bool ForceReseed { get; protected set; } = true;
14-
public sealed override TestMode Mode { get; protected set; } = TestMode.Unit;
15-
public sealed override string ClusterFilter { get; protected set; }
16-
public sealed override string TestFilter { get; protected set; }
17-
public sealed override int Seed { get; protected set; }
18-
public sealed override bool ShowElasticsearchOutputAfterStarted { get; protected set; }
1911

2012
public YamlConfiguration(string configurationFile)
2113
{
@@ -25,32 +17,43 @@ public YamlConfiguration(string configurationFile)
2517
.Where(l => !l.Trim().StartsWith("#") && !string.IsNullOrWhiteSpace(l))
2618
.ToDictionary(ConfigName, ConfigValue);
2719

28-
this.Mode = GetTestMode(_config["mode"]);
20+
Mode = GetTestMode(_config["mode"]);
2921
var version = _config["elasticsearch_version"];
30-
this.ElasticsearchVersion = string.IsNullOrWhiteSpace(version) ? DefaultVersion : version;
31-
this.ForceReseed = this.BoolConfig("force_reseed", false);
32-
this.TestAgainstAlreadyRunningElasticsearch = this.BoolConfig("test_against_already_running_elasticsearch", false);
33-
this.ShowElasticsearchOutputAfterStarted = this.BoolConfig("elasticsearch_out_after_started", false);
34-
this.ClusterFilter = _config.ContainsKey("cluster_filter") ? _config["cluster_filter"] : null;
35-
this.TestFilter = _config.ContainsKey("test_filter") ? _config["test_filter"] : null;
22+
ElasticsearchVersion = string.IsNullOrWhiteSpace(version) ? DefaultVersion : version;
23+
ForceReseed = BoolConfig("force_reseed", false);
24+
TestAgainstAlreadyRunningElasticsearch = BoolConfig("test_against_already_running_elasticsearch", false);
25+
ShowElasticsearchOutputAfterStarted = BoolConfig("elasticsearch_out_after_started", false);
26+
ClusterFilter = _config.ContainsKey("cluster_filter") ? _config["cluster_filter"] : null;
27+
TestFilter = _config.ContainsKey("test_filter") ? _config["test_filter"] : null;
3628

3729
var newRandom = new Random().Next(1, 100000);
38-
this.Seed = _config.TryGetValue("seed", out var seed) ? int.Parse(seed) : newRandom;
39-
var randomizer = new Random(this.Seed);
40-
this.Random = new RandomConfiguration
30+
Seed = _config.TryGetValue("seed", out var seed) ? int.Parse(seed) : newRandom;
31+
var randomizer = new Random(Seed);
32+
Random = new RandomConfiguration
4133
{
42-
SourceSerializer = this.RandomBool("source_serializer", randomizer),
43-
TypedKeys = this.RandomBool("typed_keys", randomizer),
34+
SourceSerializer = RandomBool("source_serializer", randomizer),
35+
TypedKeys = RandomBool("typed_keys", randomizer),
4436
};
4537
}
4638

39+
public sealed override string ClusterFilter { get; protected set; }
40+
public sealed override string ElasticsearchVersion { get; protected set; }
41+
public sealed override bool ForceReseed { get; protected set; } = true;
42+
public sealed override TestMode Mode { get; protected set; } = TestMode.Unit;
43+
public sealed override int Seed { get; protected set; }
44+
public sealed override bool ShowElasticsearchOutputAfterStarted { get; protected set; }
45+
public sealed override bool TestAgainstAlreadyRunningElasticsearch { get; protected set; } = true;
46+
public sealed override string TestFilter { get; protected set; }
47+
4748
private bool BoolConfig(string key, bool @default) => _config.TryGetValue(key, out var v) ? bool.Parse(v) : @default;
4849

4950
private bool RandomBool(string key, Random random) =>
5051
_config.TryGetValue($"random_{key}", out var v) ? bool.Parse(v) : random.NextDouble() >= 0.5;
5152

5253
private static string ConfigName(string configLine) => Parse(configLine, 0);
54+
5355
private static string ConfigValue(string configLine) => Parse(configLine, 1);
56+
5457
private static string Parse(string configLine, int index) => configLine.Split(':')[index].Trim(' ');
5558

5659
private static TestMode GetTestMode(string mode)

src/Tests/Tests.Core/Client/FixedResponseClient.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public static IElasticClient Create(
1212
int statusCode = 200,
1313
Func<ConnectionSettings, ConnectionSettings> modifySettings = null,
1414
string contentType = RequestData.MimeType,
15-
Exception exception = null)
15+
Exception exception = null
16+
)
1617
{
1718
var settings = CreateConnectionSettings(response, statusCode, modifySettings, contentType, exception);
1819
return new ElasticClient(settings);
@@ -23,7 +24,8 @@ public static ConnectionSettings CreateConnectionSettings(
2324
int statusCode = 200,
2425
Func<ConnectionSettings, ConnectionSettings> modifySettings = null,
2526
string contentType = RequestData.MimeType,
26-
Exception exception = null)
27+
Exception exception = null
28+
)
2729
{
2830
var serializer = TestClient.Default.Serializer;
2931
byte[] fixedResult = null;
@@ -35,9 +37,8 @@ public static ConnectionSettings CreateConnectionSettings(
3537
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
3638
var defaultSettings = new ConnectionSettings(connectionPool, connection)
3739
.DefaultIndex("default-index");
38-
var settings = (modifySettings != null) ? modifySettings(defaultSettings) : defaultSettings;
40+
var settings = modifySettings != null ? modifySettings(defaultSettings) : defaultSettings;
3941
return settings;
4042
}
41-
4243
}
4344
}

src/Tests/Tests.Core/Client/Settings/AlwaysInMemoryConnectionSettings.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ public AlwaysInMemoryConnectionSettings() : base(forceInMemory: true) { }
1616
public AlwaysInMemoryConnectionSettings(
1717
Func<ICollection<Uri>, IConnectionPool> createPool = null,
1818
ISerializerFactory serializerFactory = null,
19-
int port = 9200)
19+
int port = 9200
20+
)
2021
: base(
2122
createPool,
2223
serializerFactory,
23-
forceInMemory: true,
24-
port: port
24+
true,
25+
port
2526
) { }
2627
}
2728
}

src/Tests/Tests.Core/Client/Settings/TestConnectionSettings.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,54 @@
55
using Elasticsearch.Net;
66
using Nest;
77
using Tests.Configuration;
8-
using Tests.Core.Xunit;
98
using Tests.Core.Extensions;
9+
using Tests.Core.Xunit;
1010

1111
namespace Tests.Core.Client.Settings
1212
{
1313
public class TestConnectionSettings : ConnectionSettings
1414
{
1515
public static readonly bool RunningFiddler = Process.GetProcessesByName("fiddler").Any();
1616

17-
private static string LocalHost => "localhost";
18-
public static string LocalOrProxyHost => (RunningFiddler) ? "ipv4.fiddler" : LocalHost;
19-
2017
public TestConnectionSettings(Func<ICollection<Uri>, IConnectionPool> createPool = null,
2118
ISerializerFactory serializerFactory = null,
2219
bool forceInMemory = false,
23-
int port = 9200)
20+
int port = 9200
21+
)
2422
: base(
2523
CreatePool(createPool, port),
2624
TestConfiguration.Instance.CreateConnection(forceInMemory),
2725
serializerFactory
2826
) =>
29-
this.ApplyTestSettings();
27+
ApplyTestSettings();
28+
29+
public static string LocalOrProxyHost => RunningFiddler ? "ipv4.fiddler" : LocalHost;
3030

3131
private static int ConnectionLimitDefault =>
3232
int.TryParse(Environment.GetEnvironmentVariable("NEST_NUMBER_OF_CONNECTIONS"), out var x)
3333
? x
3434
: ConnectionConfiguration.DefaultConnectionLimit;
3535

36-
internal ConnectionSettings ApplyTestSettings() => this
37-
//TODO make this random
38-
//.EnableHttpCompression()
39-
#if DEBUG
40-
.EnableDebugMode()
36+
private static string LocalHost => "localhost";
37+
38+
internal ConnectionSettings ApplyTestSettings() => EnableDebugMode()
4139
#endif
4240
.ConnectionLimit(ConnectionLimitDefault)
4341
.OnRequestCompleted(r =>
4442
{
4543
if (!r.DeprecationWarnings.Any()) return;
44+
4645
var q = r.Uri.Query;
4746
//hack to prevent the deprecation warnings from the deprecation response test to be reported
4847
if (!string.IsNullOrWhiteSpace(q) && q.Contains("routing=ignoredefaultcompletedhandler")) return;
48+
4949
foreach (var d in r.DeprecationWarnings) XunitRunState.SeenDeprecations.Add(d);
5050
});
5151

5252
private static IConnectionPool CreatePool(Func<ICollection<Uri>, IConnectionPool> createPool = null, int port = 9200)
5353
{
5454
createPool = createPool ?? (uris => new StaticConnectionPool(uris));
55-
var connectionPool = createPool(new [] { CreateUri(port) });
55+
var connectionPool = createPool(new[] { CreateUri(port) });
5656
return connectionPool;
5757
}
5858

src/Tests/Tests.Core/Client/TestClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ namespace Tests.Core.Client
77
{
88
public static class TestClient
99
{
10+
public static readonly ITestConfiguration Configuration = TestConfiguration.Instance;
1011
public static readonly IElasticClient Default = new ElasticClient(new TestConnectionSettings().ApplyDomainSettings());
1112
public static readonly IElasticClient DefaultInMemoryClient = new ElasticClient(new AlwaysInMemoryConnectionSettings().ApplyDomainSettings());
12-
public static readonly IElasticClient DisabledStreaming = new ElasticClient(new TestConnectionSettings().ApplyDomainSettings().DisableDirectStreaming());
1313

14-
public static readonly ITestConfiguration Configuration = TestConfiguration.Instance;
14+
public static readonly IElasticClient DisabledStreaming =
15+
new ElasticClient(new TestConnectionSettings().ApplyDomainSettings().DisableDirectStreaming());
1516
}
1617
}

src/Tests/Tests.Core/Extensions/DiffExtensions.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ public static void DeepSort(this JObject jObj)
1515
if (jObj == null) return;
1616

1717
var props = jObj.Properties().ToList();
18-
foreach (var prop in props)
19-
{
20-
prop.Remove();
21-
}
18+
foreach (var prop in props) prop.Remove();
2219

2320
foreach (var prop in props.OrderBy(p => p.Name))
2421
{
@@ -31,8 +28,9 @@ public static void DeepSort(this JObject jObj)
3128
public static string CreateCharacterDifference(this string expected, string actual, string message = null)
3229
{
3330
var d = new Differ();
34-
var result = d.CreateCharacterDiffs(expected.Trim('"'), actual.Trim('"'), ignoreWhitespace: false);
31+
var result = d.CreateCharacterDiffs(expected.Trim('"'), actual.Trim('"'), false);
3532
if (!result.DiffBlocks.Any()) return string.Empty;
33+
3634
var builder = new StringBuilder()
3735
.AppendLine(message)
3836
.AppendLine($"expect: \"{expected}\"")
@@ -44,7 +42,8 @@ public static string CreateCharacterDifference(this string expected, string actu
4442
return builder.ToString();
4543
}
4644

47-
public static string DiffNoApproximation(this string expected, string actual, string message = null) => CreateDiff(expected, actual, message, new Differ());
45+
public static string DiffNoApproximation(this string expected, string actual, string message = null) =>
46+
CreateDiff(expected, actual, message, new Differ());
4847

4948
public static string Diff(this string expected, string actual, string message = null)
5049
{

0 commit comments

Comments
 (0)