Skip to content

Commit 3a01b50

Browse files
committed
set Randomizer seed in Test Framework
Sets the seed for all mock data before running all tests Refactor usage of Project.Projects.First() to a static property on Project. Refactor usage of Uri.EscapeDataString to UrlEncode method on ApiTestBase fix Terms aggregation partitioning test to use 10 partitions of size 5 so that partititon 0 has buckets (cherry picked from commit d8273d0)
1 parent de391e9 commit 3a01b50

File tree

24 files changed

+76
-62
lines changed

24 files changed

+76
-62
lines changed

docs/aggregations/writing-aggregations.asciidoc

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ return s => s
144144
);
145145
----
146146
<1> a list of aggregation functions to apply
147+
147148
<2> Using LINQ's `Aggregate()` function to accumulate/apply all of the aggregation functions
148149

149150
Combining multipe `AggregationDescriptor`'s is also possible using the bitwise `&` operator
@@ -211,5 +212,6 @@ var maxPerChild = childAggregation.Max("max_per_child");
211212
maxPerChild.Should().NotBeNull(); <2>
212213
----
213214
<1> Do something with the average per child. Here we just assert it's not null
215+
214216
<2> Do something with the max per child. Here we just assert it's not null
215217

docs/client-concepts/connection-pooling/request-overrides/disable-sniff-ping-per-request.asciidoc

+4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ new ClientCall()
6464
);
6565
----
6666
<1> disable sniffing
67+
6768
<2> first call is a successful ping
69+
6870
<3> sniff on startup call happens here, on the second call
6971

7072
Now, let's disable pinging on the request
@@ -87,6 +89,7 @@ audit = await audit.TraceCall(
8789
);
8890
----
8991
<1> disable ping
92+
9093
<2> No ping after sniffing
9194

9295
Finally, let's demonstrate disabling both sniff and ping on the request
@@ -108,5 +111,6 @@ audit = await audit.TraceCall(
108111
);
109112
----
110113
<1> diable ping and sniff
114+
111115
<2> no ping or sniff before the call
112116

docs/client-concepts/high-level/inference/field-inference.asciidoc

+4
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,13 @@ class Precedence
523523
}
524524
----
525525
<1> Even though this property has a NEST property mapping _and_ a `JsonProperty` attribute, We are going to provide a hard rename for it on ConnectionSettings later that should win.
526+
526527
<2> This property has both a NEST attribute and a `JsonProperty`, NEST should win.
528+
527529
<3> We should take the json property into account by itself
530+
528531
<4> This property we are going to special case in our custom serializer to resolve to ask
532+
529533
<5> We are going to register a DefaultFieldNameInferrer on ConnectionSettings that will uppercase all properties.
530534

531535
Here we create a custom serializer that renames any property named `AskSerializer` to `ask`

docs/client-concepts/high-level/inference/indices-paths.asciidoc

+4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ ISearchRequest singleTypedRequest = new SearchDescriptor<Project>().Index(single
7979
var invalidSingleString = Index("name1, name2"); <3>
8080
----
8181
<1> specifying a single index using a string
82+
8283
<2> specifying a single index using a type
84+
8385
<3> an **invalid** single index name
8486

8587
==== Specifying multiple indices
@@ -111,7 +113,9 @@ manyStringRequest = new SearchDescriptor<Project>().Type(new[] { "name1", "name2
111113
((IUrlParameter)manyStringRequest.Type).GetString(this.Client.ConnectionSettings).Should().Be("name1,name2");
112114
----
113115
<1> specifying multiple indices using strings
116+
114117
<2> specifying multiple indices using types
118+
115119
<3> The index names here come from the Connection Settings passed to `TestClient`. See the documentation on <<index-name-inference, Index Name Inference>> for more details.
116120

117121
==== Specifying All Indices

docs/client-concepts/low-level/connecting.asciidoc

+8
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ var client = new ElasticLowLevelClient(config);
9191
var result = client.Search<SearchResponse<object>>(new { size = 12 });
9292
----
9393
<1> Disable automatic proxy detection. When called, defaults to `true`.
94+
9495
<2> Enable compressed request and responses from Elasticsearch (Note that nodes need to be configured to allow this. See the {ref_current}/modules-http.html[http module settings] for more info).
96+
9597
<3> By default responses are deserialized directly from the response stream to the object you tell it to. For debugging purposes, it can be very useful to keep a copy of the raw response on the result object, which is what calling this method will do.
9698

9799
`.ResponseBodyInBytes` will only have a value if the client configuration has `DisableDirectStreaming` set
@@ -129,9 +131,13 @@ config = config
129131
.BasicAuthentication("username", "password");
130132
----
131133
<1> Allows you to set querystring parameters that have to be added to every request. For instance, if you use a hosted elasticserch provider, and you need need to pass an `apiKey` parameter onto every request.
134+
132135
<2> Sets proxy information on the connection.
136+
133137
<3> [[request-timeout]] Sets the global maximum time a connection may take. Please note that this is the request timeout, the builtin .NET `WebRequest` has no way to set connection timeouts (see http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout(v=vs.110).aspx[the MSDN documentation on `HttpWebRequest.Timeout` Property]).
138+
134139
<4> As an alternative to the C/go like error checking on `response.IsValid`, you can instead tell the client to <<thrown-exceptions, throw exceptions>>.
140+
135141
<5> forces all serialization to be indented and appends `pretty=true` to all the requests so that the responses are indented as well
136142

137143
NOTE: Basic authentication credentials can alternatively be specified on the node URI directly:
@@ -353,6 +359,8 @@ public class MyJsonNetSerializer : JsonNetSerializer
353359
}
354360
----
355361
<1> Call this constructor if you only need access to `JsonSerializerSettings` without local state (properties on MyJsonNetSerializer)
362+
356363
<2> Call OverwriteDefaultSerializers if you need access to `JsonSerializerSettings` with local state
364+
357365
<3> You can inject contract resolved converters by implementing the ContractConverters property. This can be much faster then registering them on `JsonSerializerSettings.Converters`
358366

src/CodeGeneration/DocGenerator/StringExtensions.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ public static string[] SplitOnNewLines(this string input, StringSplitOptions opt
117117
{
118118
{ "FixedDate", "new DateTime(2015, 06, 06, 12, 01, 02, 123)" },
119119
{ "FirstNameToFind", "\"pierce\"" },
120-
{ "Project.Projects.First().Suggest.Context.Values.SelectMany(v => v).First()", "\"red\"" },
121-
{ "Project.Projects.First().Suggest.Contexts.Values.SelectMany(v => v).First()", "\"red\"" },
120+
{ "Project.First.Suggest.Context.Values.SelectMany(v => v).First()", "\"red\"" },
121+
{ "Project.First.Suggest.Contexts.Values.SelectMany(v => v).First()", "\"red\"" },
122122
{ "Project.Instance.Name", "\"Durgan LLC\"" },
123123
{ "Project.InstanceAnonymous", "new {name = \"Koch, Collier and Mohr\", state = \"BellyUp\",startedOn = " +
124124
"\"2015-01-01T00:00:00\",lastActivity = \"0001-01-01T00:00:00\",leadDeveloper = " +
@@ -128,9 +128,8 @@ public static string[] SplitOnNewLines(this string input, StringSplitOptions opt
128128
{ "base.QueryJson", "new{ @bool = new { must = new[] { new { match_all = new { } } }, must_not = new[] { new { match_all = new { } } }, should = new[] { new { match_all = new { } } }, filter = new[] { new { match_all = new { } } }, minimum_should_match = 1, boost = 2.0, } }" },
129129
{ "ExpectedTerms", "new [] { \"term1\", \"term2\" }" },
130130
{ "_ctxNumberofCommits", "\"_source.numberOfCommits > 0\"" },
131-
{ "Project.Projects.First().Name", "\"Lesch Group\"" },
132-
{ "Project.Projects.FirstOrDefault().NumberOfCommits", "775" },
133-
{ "Project.Projects.FirstOrDefault().Name", "\"Dickinson - Beier\"" },
131+
{ "Project.First.Name", "\"Lesch Group\"" },
132+
{ "Project.First.NumberOfCommits", "775" },
134133
{ "LastNameSearch", "\"Stokes\"" }
135134
};
136135

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public FilterAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : bas
2323
{
2424
}
2525

26-
public static string FirstNameToFind = Project.Projects.First().LeadDeveloper.FirstName.ToLowerInvariant();
26+
public static string FirstNameToFind = Project.First.LeadDeveloper.FirstName.ToLowerInvariant();
2727

2828
protected override object ExpectJson => new
2929
{

src/Tests/Aggregations/Bucket/Terms/TermsAggregationUsageTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,11 @@ public PartitionTermsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usag
353353
terms = new
354354
{
355355
field = "numberOfCommits",
356-
size = 2,
356+
size = 5,
357357
include = new
358358
{
359359
partition = 0,
360-
num_partitions = 50
360+
num_partitions = 10
361361
}
362362
}
363363
}
@@ -369,8 +369,8 @@ public PartitionTermsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usag
369369
.Aggregations(a => a
370370
.Terms("commits", st => st
371371
.Field(p => p.NumberOfCommits)
372-
.Include(partition: 0, numberOfPartitions: 50)
373-
.Size(2)
372+
.Include(partition: 0, numberOfPartitions: 10)
373+
.Size(5)
374374
)
375375
);
376376

@@ -381,8 +381,8 @@ public PartitionTermsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usag
381381
Aggregations = new TermsAggregation("commits")
382382
{
383383
Field = Infer.Field<Project>(p => p.NumberOfCommits),
384-
Include = new TermsInclude(0, 50),
385-
Size = 2
384+
Include = new TermsInclude(0, 10),
385+
Size = 5
386386
}
387387
};
388388

src/Tests/ClientConcepts/Certificates/WorkingWithCertificates.doc.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ protected override ConnectionSettings ConnectionSettings(ConnectionSettings s) =
6060
}
6161
//hide
6262
[IntegrationOnly]
63-
public class AllowAllSllCertificatesApiTests : CanConnectTestBase<AllowAllCertificatesCluster>
63+
public class AllowAllSslCertificatesApiTests : CanConnectTestBase<AllowAllCertificatesCluster>
6464
{
65-
public AllowAllSllCertificatesApiTests(AllowAllCertificatesCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
65+
public AllowAllSslCertificatesApiTests(AllowAllCertificatesCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
6666
[I] public async Task UsedHttps() => await AssertOnAllResponses(r => r.ApiCall.Uri.Scheme.Should().Be("https"));
6767
}
6868

src/Tests/Cluster/TaskManagement/TasksCancel/TasksCancelApiTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected override LazyResponses ClientUsage() => Calls(
5959
protected override bool ExpectIsValid => true;
6060
protected override int ExpectStatusCode => 200;
6161
protected override HttpMethod HttpMethod => HttpMethod.POST;
62-
protected override string UrlPath => $"/_tasks/{Uri.EscapeDataString(this.TaskId.ToString())}/_cancel";
62+
protected override string UrlPath => $"/_tasks/{UrlEncode(this.TaskId.ToString())}/_cancel";
6363
protected override bool SupportsDeserialization => false;
6464

6565

src/Tests/Document/Multiple/DeleteByQuery/DeleteByQueryApiTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected override LazyResponses ClientUsage() => Calls(
5454
ids = new
5555
{
5656
types = new[] { "project" },
57-
values = new [] { Project.Projects.First().Name, "x" }
57+
values = new [] { Project.First.Name, "x" }
5858
}
5959
}
6060
};
@@ -67,7 +67,7 @@ protected override LazyResponses ClientUsage() => Calls(
6767
.Query(q=>q
6868
.Ids(ids=>ids
6969
.Types(typeof(Project))
70-
.Values(Project.Projects.First().Name, "x")
70+
.Values(Project.First.Name, "x")
7171
)
7272
);
7373

@@ -77,7 +77,7 @@ protected override LazyResponses ClientUsage() => Calls(
7777
Query = new IdsQuery
7878
{
7979
Types = Types.Type<Project>(),
80-
Values = new Id[] { Project.Projects.First().Name, "x" }
80+
Values = new Id[] { Project.First.Name, "x" }
8181
}
8282
};
8383

src/Tests/Document/Single/Get/GetApiTests.cs

+8-16
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ namespace Tests.Document.Single.Get
1313
{
1414
public class GetApiTests : ApiIntegrationTestBase<ReadOnlyCluster, IGetResponse<Project>, IGetRequest, GetDescriptor<Project>, GetRequest<Project>>
1515
{
16-
protected string ProjectId => Project.Projects.First().Name;
16+
protected string ProjectId => Project.First.Name;
1717

18-
protected string ProjectIdForUrl => Uri.EscapeDataString(this.ProjectId);
19-
20-
public GetApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
18+
public GetApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
2119
protected override LazyResponses ClientUsage() => Calls(
2220
fluent: (client, f) => client.Get<Project>(this.ProjectId, f),
2321
fluentAsync: (client, f) => client.GetAsync<Project>(this.ProjectId, f),
@@ -28,7 +26,7 @@ protected override LazyResponses ClientUsage() => Calls(
2826
protected override bool ExpectIsValid => true;
2927
protected override int ExpectStatusCode => 200;
3028
protected override HttpMethod HttpMethod => HttpMethod.GET;
31-
protected override string UrlPath => $"/project/project/{ProjectIdForUrl}";
29+
protected override string UrlPath => $"/project/project/{UrlEncode(this.ProjectId)}";
3230

3331
protected override bool SupportsDeserialization => false;
3432

@@ -47,9 +45,7 @@ public class GetNonExistentDocumentApiTests : ApiIntegrationTestBase<ReadOnlyClu
4745
{
4846
protected string ProjectId => this.CallIsolatedValue;
4947

50-
protected string ProjectIdForUrl => Uri.EscapeDataString(this.ProjectId);
51-
52-
public GetNonExistentDocumentApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
48+
public GetNonExistentDocumentApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
5349
protected override LazyResponses ClientUsage() => Calls(
5450
fluent: (client, f) => client.Get<Project>(this.ProjectId, f),
5551
fluentAsync: (client, f) => client.GetAsync<Project>(this.ProjectId, f),
@@ -60,7 +56,7 @@ protected override LazyResponses ClientUsage() => Calls(
6056
protected override bool ExpectIsValid => true;
6157
protected override int ExpectStatusCode => 404;
6258
protected override HttpMethod HttpMethod => HttpMethod.GET;
63-
protected override string UrlPath => $"/project/project/{ProjectIdForUrl}";
59+
protected override string UrlPath => $"/project/project/{UrlEncode(this.ProjectId)}";
6460

6561
protected override bool SupportsDeserialization => false;
6662

@@ -83,11 +79,7 @@ public class GetApiParentTests : ApiIntegrationTestBase<ReadOnlyCluster, IGetRes
8379

8480
protected string CommitActivityId => CommitActivity.Id;
8581

86-
protected string ParentIdForUrl => Uri.EscapeDataString(this.CommitActivity.ProjectName);
87-
88-
protected string CommitActivityIdForUrl => Uri.EscapeDataString(this.CommitActivityId);
89-
90-
public GetApiParentTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
82+
public GetApiParentTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
9183
protected override LazyResponses ClientUsage() => Calls(
9284
fluent: (client, f) => client.Get<CommitActivity>(this.CommitActivityId, f),
9385
fluentAsync: (client, f) => client.GetAsync<CommitActivity>(this.CommitActivityId, f),
@@ -98,7 +90,7 @@ protected override LazyResponses ClientUsage() => Calls(
9890
protected override bool ExpectIsValid => true;
9991
protected override int ExpectStatusCode => 200;
10092
protected override HttpMethod HttpMethod => HttpMethod.GET;
101-
protected override string UrlPath => $"/project/commits/{CommitActivityIdForUrl}?parent={ParentIdForUrl}";
93+
protected override string UrlPath => $"/project/commits/{UrlEncode(this.CommitActivityId)}?parent={UrlEncode(this.CommitActivity.ProjectName)}";
10294

10395
protected override bool SupportsDeserialization => false;
10496

@@ -126,7 +118,7 @@ public class GetApiFieldsTests : GetApiTests
126118
{
127119
public GetApiFieldsTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
128120

129-
protected override string UrlPath => $"/project/project/{ProjectIdForUrl}?stored_fields=name%2CnumberOfCommits";
121+
protected override string UrlPath => $"/project/project/{UrlEncode(this.ProjectId)}?stored_fields=name%2CnumberOfCommits";
130122

131123
protected override Func<GetDescriptor<Project>, IGetRequest> Fluent => g => g
132124
.StoredFields(

src/Tests/Document/Single/TermVectors/TermVectorsApiTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected override LazyResponses ClientUsage() => Calls(
2424
protected override bool ExpectIsValid => true;
2525
protected override int ExpectStatusCode => 200;
2626
protected override HttpMethod HttpMethod => HttpMethod.POST;
27-
protected override string UrlPath => $"/project/project/{Uri.EscapeDataString(Project.Instance.Name)}/_termvectors?offsets=true";
27+
protected override string UrlPath => $"/project/project/{UrlEncode(Project.Instance.Name)}/_termvectors?offsets=true";
2828

2929
protected override bool SupportsDeserialization => false;
3030

src/Tests/Document/Single/Update/UpdateWithSourceApiTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ [I] public Task ReturnsSourceAndFields() => this.AssertOnAllResponses(r =>
6363
r.Get.Should().NotBeNull();
6464
r.Get.Found.Should().BeTrue();
6565
r.Get.Source.Should().NotBeNull();
66-
var name = Project.Projects.First().Name;
66+
var name = Project.First.Name;
6767
r.Get.Source.Name.Should().Be(name);
6868
r.Get.Fields.Should().NotBeEmpty().And.ContainKey("name");
6969
r.Get.Fields.Value<string>("name").Should().Be(name);

src/Tests/Framework/EndpointTests/ApiTestBase.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public abstract class ApiTestBase<TCluster, TResponse, TInterface, TDescriptor,
2626

2727
protected static string RandomString() => Guid.NewGuid().ToString("N").Substring(0, 8);
2828
protected bool RanIntegrationSetup => this._usage?.CalledSetup ?? false;
29+
protected string UrlEncode(string s) => Uri.EscapeDataString(s);
2930

30-
protected ClusterBase Cluster { get; }
31+
protected ClusterBase Cluster { get; }
3132

3233
protected string CallIsolatedValue => _uniqueValues.Value;
3334
protected T ExtendedValue<T>(string key) where T : class => this._uniqueValues.ExtendedValue<T>(key);

src/Tests/Framework/MockData/Gimme.cs

-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ namespace Tests.Framework.MockData
55
{
66
internal static class Gimme
77
{
8-
static Gimme()
9-
{
10-
Randomizer.Seed = new Random(1337);
11-
}
12-
138
public static Randomizer Random = new Randomizer();
14-
159
}
1610
}

src/Tests/Framework/MockData/Project.cs

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class Project
5555

5656
public static IList<Project> Projects { get; } = Project.Generator.Generate(100).ToList();
5757

58+
public static Project First { get; } = Projects.First();
59+
5860
public static Project Instance = new Project
5961
{
6062
Name = Projects.First().Name,

src/Tests/Framework/Xunit/TestFramework.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Reflection;
3+
using Bogus;
34
using Tests.Framework;
45
using Tests.Framework.Configuration;
56
using Xunit.Abstractions;
@@ -16,7 +17,10 @@ public TestFramework(IMessageSink messageSink)
1617
protected override ITestFrameworkExecutor CreateExecutor(AssemblyName assemblyName)
1718
{
1819
var config = TestClient.Configuration;
19-
Console.WriteLine("Starting tests using config:");
20+
21+
Randomizer.Seed = new Random(1337);
22+
23+
Console.WriteLine("Starting tests using config:");
2024
Console.WriteLine($" - {nameof(config.TestAgainstAlreadyRunningElasticsearch)}: {config.TestAgainstAlreadyRunningElasticsearch}");
2125
Console.WriteLine($" - {nameof(config.ElasticsearchVersion)}: {config.ElasticsearchVersion}");
2226
Console.WriteLine($" - {nameof(config.ForceReseed)}: {config.ForceReseed}");

src/Tests/QueryDsl/Joining/ParentId/ParentIdUsageTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ public ParentIdUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usag
2121
{
2222
_name = "named_query",
2323
type = "developer",
24-
id = Project.Projects.First().Name
24+
id = Project.First.Name
2525
}
2626
};
2727

2828
protected override QueryContainer QueryInitializer => new ParentIdQuery
2929
{
3030
Name = "named_query",
3131
Type = Infer.Type<Developer>(),
32-
Id = Project.Projects.First().Name
32+
Id = Project.First.Name
3333
};
3434

3535
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
3636
.ParentId(p => p
3737
.Name("named_query")
3838
.Type<Developer>()
39-
.Id(Project.Projects.First().Name)
39+
.Id(Project.First.Name)
4040
);
4141

4242
protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<IParentIdQuery>(a => a.ParentId)

0 commit comments

Comments
 (0)