|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using Nest; |
| 6 | +using Nest.Tests.MockData; |
| 7 | +using Nest.Tests.MockData.Domain; |
| 8 | +using NUnit.Framework; |
| 9 | + |
| 10 | +namespace Nest.Tests.Integration |
| 11 | +{ |
| 12 | + [TestFixture] |
| 13 | + public class BaseTests |
| 14 | + { |
| 15 | + //static initializer to really run initialization once |
| 16 | + static BaseTests() |
| 17 | + { |
| 18 | + var client = CreateClient(); |
| 19 | + var cloneIndex = Test.Default.DefaultIndex + "_clone"; |
| 20 | + if (client.IsValid) |
| 21 | + { |
| 22 | + var projects = NestTestData.Data; |
| 23 | + var people = NestTestData.People; |
| 24 | + |
| 25 | + client.DeleteMapping<ElasticSearchProject>(); |
| 26 | + client.DeleteMapping<ElasticSearchProject>(cloneIndex); |
| 27 | + client.OpenIndex<ElasticSearchProject>(); |
| 28 | + client.OpenIndex(cloneIndex); |
| 29 | + |
| 30 | + ResetType<ElasticSearchProject>(client, projects); |
| 31 | + ResetType<Person>(client, people); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + [TestFixtureSetUp] |
| 37 | + public virtual void Initialize() |
| 38 | + { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + private static IConnectionSettings _settings; |
| 43 | + protected static IConnectionSettings Settings |
| 44 | + { |
| 45 | + get |
| 46 | + { |
| 47 | + if (_settings != null) |
| 48 | + return _settings; |
| 49 | + |
| 50 | + _settings = new ConnectionSettings(Test.Default.Host, Test.Default.Port) |
| 51 | + .SetDefaultIndex(Test.Default.DefaultIndex) |
| 52 | + .SetMaximumAsyncConnections(Test.Default.MaximumAsyncConnections) |
| 53 | + .UsePrettyResponses(); |
| 54 | + |
| 55 | + return _settings; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private ElasticClient _connectedClient; |
| 60 | + protected ElasticClient _client |
| 61 | + { |
| 62 | + get |
| 63 | + { |
| 64 | + if (this._connectedClient != null) |
| 65 | + return this._connectedClient; |
| 66 | + |
| 67 | + var client = new ElasticClient(Settings); |
| 68 | + if (client.IsValid) |
| 69 | + { |
| 70 | + this._connectedClient = client; |
| 71 | + return this._connectedClient; |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + protected static ElasticClient CreateClient() |
| 78 | + { |
| 79 | + return new ElasticClient(Settings); |
| 80 | + } |
| 81 | + |
| 82 | + protected virtual void ResetIndexes() |
| 83 | + { |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + private static void ResetType<T>(IElasticClient client, IEnumerable<T> objects) where T : class |
| 88 | + { |
| 89 | + var cloneIndex = Test.Default.DefaultIndex + "_clone"; |
| 90 | + var bulkParameters = new SimpleBulkParameters() { Refresh = true }; |
| 91 | + |
| 92 | + client.MapFromAttributes<T>(); |
| 93 | + client.MapFromAttributes<T>(cloneIndex); |
| 94 | + |
| 95 | + client.IndexMany(objects, bulkParameters); |
| 96 | + client.IndexMany(objects, cloneIndex, bulkParameters); |
| 97 | + } |
| 98 | + |
| 99 | + protected void DeleteIndices() |
| 100 | + { |
| 101 | + var client = CreateClient(); |
| 102 | + if (client.IsValid) |
| 103 | + { |
| 104 | + var cloneIndex = Test.Default.DefaultIndex + "_clone"; |
| 105 | + client.DeleteMapping<ElasticSearchProject>(); |
| 106 | + client.DeleteMapping<ElasticSearchProject>(cloneIndex); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + protected void BulkIndexData() |
| 111 | + { |
| 112 | + var client = CreateClient(); |
| 113 | + if (client.IsValid) |
| 114 | + { |
| 115 | + var projects = NestTestData.Data; |
| 116 | + var cloneIndex = Test.Default.DefaultIndex + "_clone"; |
| 117 | + var bulkParameters = new SimpleBulkParameters() { Refresh = true }; |
| 118 | + client.IndexMany(projects, bulkParameters); |
| 119 | + client.IndexMany(projects, cloneIndex, bulkParameters); |
| 120 | + |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Execute a filter test and assert the results. |
| 126 | + /// </summary> |
| 127 | + /// <param name="project">Document to be search</param> |
| 128 | + /// <param name="filter">Filter to be test</param> |
| 129 | + /// <param name="queryMustHaveResults">If the execution of search must return results</param> |
| 130 | + public void DoFilterTest(Func<FilterDescriptor<ElasticSearchProject>, Nest.BaseFilter> filter, ElasticSearchProject project, bool queryMustHaveResults) |
| 131 | + { |
| 132 | + var filterId = Filter<ElasticSearchProject>.Term(e => e.Id, project.Id.ToString()); |
| 133 | + |
| 134 | + var results = this._client.Search<ElasticSearchProject>( |
| 135 | + s => s.Filter(ff => ff.And( |
| 136 | + f => f.Term(e => e.Id, project.Id.ToString()), |
| 137 | + filter |
| 138 | + )) |
| 139 | + ); |
| 140 | + |
| 141 | + Assert.True(results.IsValid, results.ConnectionStatus.Result); |
| 142 | + Assert.True(results.ConnectionStatus.Success, results.ConnectionStatus.Result); |
| 143 | + Assert.AreEqual(queryMustHaveResults ? 1 : 0, results.Total); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments