This repository was archived by the owner on Aug 1, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTestsHelper.cs
94 lines (80 loc) · 3.5 KB
/
TestsHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) Free Mind Labs, Inc. All rights reserved.
usingSystem.Reflection;
usingElastic.Clients.Elasticsearch;
usingFreeMindLabs.KernelMemory.Elasticsearch;
namespaceUnitTests;
/// <summary>
/// Extension methods for tests on Elasticsearch.
/// </summary>
internalstaticclassTestsHelper
{
/// <summary>
/// Deletes all indices that are created by all test methods of the given class.
/// Indices must have the same name of a test method to be automatically deleted.
/// </summary>
publicstaticasyncTask<IEnumerable<string>>DeleteIndicesOfTestAsync(thisElasticsearchClientclient,TypeunitTestType,IIndexNameHelperindexNameHelper)
{
ArgumentNullException.ThrowIfNull(client);
ArgumentNullException.ThrowIfNull(unitTestType);
ArgumentNullException.ThrowIfNull(indexNameHelper);
// Iterates thru all method names of the test class and deletes the indice with the same name
varmethods=unitTestType.GetMethods(BindingFlags.Public|BindingFlags.Instance)
.Where(m =>
(m.GetCustomAttribute<Xunit.FactAttribute>()!=null)
||
(m.GetCustomAttribute<Xunit.TheoryAttribute>()!=null)
)
.ToArray();
if(methods.Length==0)
{
thrownewArgumentException($"No public test methods found in class '{unitTestType.Name}'.");
}
varresult=newList<string>();
foreach(varmethodinmethods)
{
varindexName=indexNameHelper.Convert(method.Name);
vardelResp=awaitclient.Indices.DeleteAsync(indices:indexName)
.ConfigureAwait(false);
if(delResp.IsSuccess())
{
result.Add(indexName);
}
}
returnresult;
}
///// <summary>
///// Queries the given index for documents until the expected number of documents is found
///// or the max number of retries is reached.
///// It throws an exception if the expected number of documents is not found.
///// </summary>
//public static async Task WaitForDocumentsAsync(this ElasticsearchClient client, string realIndexName, int expectedDocuments, int maxRetries = 3, int msDelay = 500)
//{
// ArgumentNullException.ThrowIfNull(client);
// ArgumentNullException.ThrowIfNull(realIndexName);
// return;
// var foundCount = 0;
// for (int i = 0; i < maxRetries; i++)
// {
// // We search for all documents
// var results = await client
// .SearchAsync<ElasticsearchMemoryRecord>(sr =>
// {
// sr.Index(realIndexName)
// .Query(q => q.MatchAll());
// })
// .ConfigureAwait(false);
// foundCount = results?.HitsMetadata?.Hits?.Count ?? 0;
// // If we found all documents, we can return
// if ((expectedDocuments == 0) && (foundCount == 0))
// {
// return;
// }
// else if (foundCount >= expectedDocuments)
// {
// return;
// }
// await Task.Delay(msDelay).ConfigureAwait(false);
// }
// throw new InvalidOperationException($"It should have inserted {expectedDocuments} documents but only {foundCount}...");
//}
}