-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathIndexFixture.cs
136 lines (110 loc) · 5.28 KB
/
IndexFixture.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Meilisearch.Tests
{
public abstract class IndexFixture : IAsyncLifetime
{
public IndexFixture()
{
DefaultClient = new MeilisearchClient(MeilisearchAddress(), ApiKey);
var httpClient = new HttpClient(new MeilisearchMessageHandler(new HttpClientHandler())) { BaseAddress = new Uri(MeilisearchAddress()) };
ClientWithCustomHttpClient = new MeilisearchClient(httpClient, ApiKey);
}
private const string ApiKey = "masterKey";
public virtual string MeilisearchAddress()
{
throw new InvalidOperationException("Please override the MeilisearchAddress property in inhereted class.");
}
public MeilisearchClient DefaultClient { get; private set; }
public MeilisearchClient ClientWithCustomHttpClient { get; private set; }
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync() => await DeleteAllIndexes(); // Let a clean Meilisearch instance, for maintainers convenience only.
public async Task<Index> SetUpEmptyIndex(string indexUid, string primaryKey = default)
{
var task = await DefaultClient.CreateIndexAsync(indexUid, primaryKey);
// Check the index has been created
var finishedTask = await DefaultClient.WaitForTaskAsync(task.TaskUid);
if (finishedTask.Status != TaskInfoStatus.Succeeded)
{
throw new Exception("The index was not created in SetUpEmptyIndex. Impossible to run the tests.");
}
return DefaultClient.Index(indexUid);
}
public async Task<Index> SetUpBasicIndex(string indexUid)
{
var index = DefaultClient.Index(indexUid);
var movies = await JsonFileReader.ReadAsync<List<Movie>>(Datasets.MoviesWithStringIdJsonPath);
var task = await index.AddDocumentsAsync(movies);
// Check the documents have been added
var finishedTask = await index.WaitForTaskAsync(task.TaskUid);
if (finishedTask.Status != TaskInfoStatus.Succeeded)
{
throw new Exception("The documents were not added during SetUpBasicIndex. Impossible to run the tests.");
}
return index;
}
public async Task<Index> SetUpBasicIndexWithIntId(string indexUid)
{
var index = DefaultClient.Index(indexUid);
var movies = await JsonFileReader.ReadAsync<List<MovieWithIntId>>(Datasets.MoviesWithIntIdJsonPath);
var task = await index.AddDocumentsAsync(movies);
// Check the documents have been added
var finishedTask = await index.WaitForTaskAsync(task.TaskUid);
if (finishedTask.Status != TaskInfoStatus.Succeeded)
{
throw new Exception("The documents were not added during SetUpBasicIndexWithIntId. Impossible to run the tests.");
}
return index;
}
public async Task<Index> SetUpIndexForFaceting(string indexUid)
{
var index = DefaultClient.Index(indexUid);
// Add documents
var movies = await JsonFileReader.ReadAsync<List<Movie>>(Datasets.MoviesForFacetingJsonPath);
var task = await index.AddDocumentsAsync(movies);
// Check the documents have been added
var finishedTask = await index.WaitForTaskAsync(task.TaskUid);
if (finishedTask.Status != TaskInfoStatus.Succeeded)
{
throw new Exception("The documents were not added during SetUpIndexForFaceting. Impossible to run the tests.");
}
// task settings
var settings = new Settings
{
FilterableAttributes = new string[] { "genre" },
};
task = await index.UpdateSettingsAsync(settings);
// Check the settings have been added
finishedTask = await index.WaitForTaskAsync(task.TaskUid);
if (finishedTask.Status != TaskInfoStatus.Succeeded)
{
throw new Exception("The settings were not added during SetUpIndexForFaceting. Impossible to run the tests.");
}
return index;
}
public async Task<Index> SetUpIndexForNestedSearch(string indexUid)
{
var index = DefaultClient.Index(indexUid);
var movies = await JsonFileReader.ReadAsync<List<MovieWithInfo>>(Datasets.MoviesWithInfoJsonPath);
var task = await index.AddDocumentsAsync(movies);
// Check the documents have been added
var finishedTask = await index.WaitForTaskAsync(task.TaskUid);
if (finishedTask.Status != TaskInfoStatus.Succeeded)
{
throw new Exception("The documents were not added during SetUpIndexForNestedSearch. Impossible to run the tests.");
}
return index;
}
public async Task DeleteAllIndexes()
{
var indexes = await DefaultClient.GetAllIndexesAsync();
foreach (var index in indexes.Results)
{
await index.DeleteAsync();
}
}
}
}