|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Elastic.Elasticsearch.Xunit.XunitPlumbing; |
| 8 | +using FluentAssertions; |
| 9 | +using Nest; |
| 10 | +using Tests.Core.Extensions; |
| 11 | +using Tests.Core.ManagedElasticsearch.Clusters; |
| 12 | +using Tests.Domain; |
| 13 | +using Tests.Domain.Helpers; |
| 14 | +using Tests.Framework.EndpointTests; |
| 15 | +using Tests.Framework.EndpointTests.TestState; |
| 16 | + |
| 17 | +namespace Tests.XPack.Sql |
| 18 | +{ |
| 19 | + [SkipVersion("<7.14.0", "All endpoints GA in 7.14.0")] |
| 20 | + public class SqlSearchApiCoordinatedTests : CoordinatedIntegrationTestBase<XPackCluster> |
| 21 | + { |
| 22 | + private const string DeleteStep = nameof(DeleteStep); |
| 23 | + private const string GetStep = nameof(GetStep); |
| 24 | + private const string StatusStep = nameof(StatusStep); |
| 25 | + private const string SubmitStep = nameof(SubmitStep); |
| 26 | + private const string WaitStep = nameof(WaitStep); |
| 27 | + |
| 28 | + private static readonly string SqlQuery = |
| 29 | + $@"SELECT type, name, startedOn, numberOfCommits |
| 30 | +FROM {TestValueHelper.ProjectsIndex} |
| 31 | +WHERE type = '{Project.TypeName}' |
| 32 | +ORDER BY numberOfContributors DESC"; |
| 33 | + |
| 34 | + public SqlSearchApiCoordinatedTests(XPackCluster cluster, EndpointUsage usage) : base(new CoordinatedUsage(cluster, usage, testOnlyOne: true) |
| 35 | + { |
| 36 | + { |
| 37 | + SubmitStep, u => |
| 38 | + u.Calls<QuerySqlDescriptor, QuerySqlRequest, IQuerySqlRequest, QuerySqlResponse>( |
| 39 | + _ => new QuerySqlRequest { Query = SqlQuery, FetchSize = 5, WaitForCompletionTimeout = "0s" }, |
| 40 | + (_, d) => d |
| 41 | + .Query(SqlQuery) |
| 42 | + .FetchSize(5) |
| 43 | + .WaitForCompletionTimeout("0s"), |
| 44 | + (_, c, f) => c.Sql.Query(f), |
| 45 | + (_, c, f) => c.Sql.QueryAsync(f), |
| 46 | + (_, c, r) => c.Sql.Query(r), |
| 47 | + (_, c, r) => c.Sql.QueryAsync(r), |
| 48 | + (r, values) => values.ExtendedValue("id", r.Id) |
| 49 | + ) |
| 50 | + }, |
| 51 | + { |
| 52 | + StatusStep, u => |
| 53 | + u.Calls<SqlSearchStatusDescriptor, SqlSearchStatusRequest, ISqlSearchStatusRequest, SqlSearchStatusResponse>( |
| 54 | + v => new SqlSearchStatusRequest(v), |
| 55 | + (v, d) => d, |
| 56 | + (v, c, f) => c.Sql.SearchStatus(v, f), |
| 57 | + (v, c, f) => c.Sql.SearchStatusAsync(v, f), |
| 58 | + (v, c, r) => c.Sql.SearchStatus(r), |
| 59 | + (v, c, r) => c.Sql.SearchStatusAsync(r), |
| 60 | + uniqueValueSelector: values => values.ExtendedValue<string>("id") |
| 61 | + ) |
| 62 | + }, |
| 63 | + { |
| 64 | + // allows the search to complete |
| 65 | + WaitStep, u => u.Call(async (_, c) => |
| 66 | + { |
| 67 | + // wait for the search to complete |
| 68 | + var complete = false; |
| 69 | + var count = 0; |
| 70 | + |
| 71 | + while (!complete && count++ < 10) |
| 72 | + { |
| 73 | + await Task.Delay(100); |
| 74 | + var status = await c.Sql.SearchStatusAsync(u.Usage.CallUniqueValues.ExtendedValue<string>("id")); |
| 75 | + complete = !status.IsRunning && status.CompletionStatus.HasValue; |
| 76 | + } |
| 77 | + }) |
| 78 | + }, |
| 79 | + { |
| 80 | + GetStep, u => |
| 81 | + u.Calls<SqlGetDescriptor, SqlGetRequest, ISqlGetRequest, SqlGetResponse>( |
| 82 | + v => new SqlGetRequest(v), |
| 83 | + (_, d) => d, |
| 84 | + (v, c, f) => c.Sql.Get(v, f), |
| 85 | + (v, c, f) => c.Sql.GetAsync(v, f), |
| 86 | + (_, c, r) => c.Sql.Get(r), |
| 87 | + (_, c, r) => c.Sql.GetAsync(r), |
| 88 | + uniqueValueSelector: values => values.ExtendedValue<string>("id") |
| 89 | + ) |
| 90 | + }, |
| 91 | + { |
| 92 | + DeleteStep, u => |
| 93 | + u.Calls<SqlDeleteDescriptor, SqlDeleteRequest, ISqlDeleteRequest, SqlDeleteResponse>( |
| 94 | + v => new SqlDeleteRequest(v), |
| 95 | + (_, d) => d, |
| 96 | + (v, c, f) => c.Sql.Delete(v, f), |
| 97 | + (v, c, f) => c.Sql.DeleteAsync(v, f), |
| 98 | + (_, c, r) => c.Sql.Delete(r), |
| 99 | + (_, c, r) => c.Sql.DeleteAsync(r), |
| 100 | + uniqueValueSelector: values => values.ExtendedValue<string>("id") |
| 101 | + ) |
| 102 | + } |
| 103 | + }) { } |
| 104 | + |
| 105 | + [I] public async Task SqlSearchResponse() => await Assert<QuerySqlResponse>(SubmitStep, r => |
| 106 | + { |
| 107 | + r.ShouldBeValid(); |
| 108 | + r.Id.Should().NotBeNullOrEmpty(); |
| 109 | + r.IsPartial.Should().BeTrue(); |
| 110 | + r.IsRunning.Should().BeTrue(); |
| 111 | + }); |
| 112 | + |
| 113 | + [I] public async Task SqlSearchStatusResponse() => await Assert<SqlSearchStatusResponse>(StatusStep, r => |
| 114 | + { |
| 115 | + r.ShouldBeValid(); |
| 116 | + r.Id.Should().NotBeNullOrEmpty(); |
| 117 | + r.IsPartial.Should().BeTrue(); |
| 118 | + r.IsRunning.Should().BeTrue(); |
| 119 | + r.ExpirationTimeInMillis.Should().BeGreaterThan(0); |
| 120 | + r.StartTimeInMillis.Should().BeGreaterThan(0); |
| 121 | + }); |
| 122 | + |
| 123 | + [I] public async Task SqlGetResponse() => await Assert<SqlGetResponse>(GetStep, r => |
| 124 | + { |
| 125 | + r.ShouldBeValid(); |
| 126 | + r.IsPartial.Should().BeFalse(); |
| 127 | + r.IsRunning.Should().BeFalse(); |
| 128 | + |
| 129 | + r.Cursor.Should().NotBeNullOrWhiteSpace("response cursor"); |
| 130 | + r.Rows.Should().NotBeNullOrEmpty(); |
| 131 | + r.Columns.Should().NotBeNullOrEmpty().And.HaveCount(4); |
| 132 | + foreach (var c in r.Columns) |
| 133 | + { |
| 134 | + c.Name.Should().NotBeNullOrWhiteSpace("column name"); |
| 135 | + c.Type.Should().NotBeNullOrWhiteSpace("column type"); |
| 136 | + } |
| 137 | + foreach (var row in r.Rows) |
| 138 | + { |
| 139 | + row.Should().NotBeNull().And.HaveCount(4); |
| 140 | + var type = row[0].As<string>().Should().NotBeNullOrWhiteSpace("a type returned null"); |
| 141 | + var name = row[1].As<string>().Should().NotBeNullOrWhiteSpace("a name returned null"); |
| 142 | + var date = row[2].As<DateTime>().Should().BeAfter(default); |
| 143 | + var numberOfCommits = row[3].As<int?>(); |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + [I] public async Task SqlDeleteResponse() => await Assert<SqlDeleteResponse>(DeleteStep, r => |
| 148 | + { |
| 149 | + r.ShouldBeValid(); |
| 150 | + r.Acknowledged.Should().BeTrue(); |
| 151 | + }); |
| 152 | + } |
| 153 | +} |
0 commit comments