-
Notifications
You must be signed in to change notification settings - Fork 55
/
IndexTests.cs
284 lines (233 loc) · 10.8 KB
/
IndexTests.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Meilisearch.QueryParameters;
using Xunit;
namespace Meilisearch.Tests
{
public abstract class IndexTests<TFixture> : IAsyncLifetime where TFixture : IndexFixture
{
private readonly MeilisearchClient _client;
private readonly string _defaultPrimaryKey;
private readonly TFixture _fixture;
public IndexTests(TFixture fixture)
{
_fixture = fixture;
_client = fixture.DefaultClient;
_defaultPrimaryKey = "movieId";
}
public async Task InitializeAsync() => await _fixture.DeleteAllIndexes(); // Test context cleaned for each [Fact]
public Task DisposeAsync() => Task.CompletedTask;
[Fact]
public async Task BasicIndexCreation()
{
var indexUid = "BasicIndexCreationTest";
await _fixture.SetUpEmptyIndex(indexUid);
var index = await _client.GetIndexAsync(indexUid);
index.Uid.Should().Be(indexUid);
index.PrimaryKey.Should().BeNull();
}
[Fact]
public async Task IndexCreationWithPrimaryKey()
{
var indexUid = "IndexCreationWithPrimaryKeyTest";
await _fixture.SetUpEmptyIndex(indexUid, _defaultPrimaryKey);
var index = await _client.GetIndexAsync(indexUid);
index.Uid.Should().Be(indexUid);
index.PrimaryKey.Should().Be(_defaultPrimaryKey);
}
[Fact]
public async Task BasicUsageOfIndexMethod()
{
var indexUid = "BasicUsageOfIndexMethodTest";
var index = _client.Index(indexUid);
index.Uid.Should().Be(indexUid);
index.PrimaryKey.Should().BeNull();
var ex = await Assert.ThrowsAsync<MeilisearchApiError>(() => _client.GetIndexAsync(indexUid));
Assert.Equal("index_not_found", ex.Code);
}
[Fact]
public async Task IndexMethodUsageOnExistingIndex()
{
var indexUid = "IndexMethodUsageOnExistingIndexTest";
var index = await _fixture.SetUpEmptyIndex(indexUid);
index.Uid.Should().Be(indexUid);
index.PrimaryKey.Should().BeNull();
var document = await index.AddDocumentsAsync(new[] { new Movie { Id = "1", Name = "Batman" } });
document.TaskUid.Should().BeGreaterOrEqualTo(0);
}
[Fact]
public async Task IndexFetchInfoPrimaryKey()
{
var indexUid = "IndexFetchInfoPrimaryKeyTest";
var index = await _fixture.SetUpEmptyIndex(indexUid, _defaultPrimaryKey);
await index.FetchInfoAsync();
Assert.Equal(index.Uid, indexUid);
index.PrimaryKey.Should().Be(_defaultPrimaryKey);
var primaryKey = await index.FetchPrimaryKey();
Assert.Equal(index.PrimaryKey, primaryKey);
}
[Fact]
public async Task IndexAlreadyExistsError()
{
var indexUid = "IndexAlreadyExistsErrorTest";
await _client.CreateIndexAsync(indexUid, _defaultPrimaryKey);
var task = await _client.CreateIndexAsync(indexUid, _defaultPrimaryKey);
task.TaskUid.Should().BeGreaterOrEqualTo(0);
var finishedTask = await _client.Index(indexUid).WaitForTaskAsync(task.TaskUid);
Assert.Equal(task.TaskUid, finishedTask.Uid);
Assert.Equal(indexUid, finishedTask.IndexUid);
Assert.Equal(TaskInfoStatus.Failed, finishedTask.Status);
var error = finishedTask.Error;
error.Should().NotBeNull();
Assert.Equal("index_already_exists", error["code"]);
}
[Fact]
public async Task UpdateIndex()
{
var indexUid = "UpdateIndexTest";
var primarykey = "MovieId" + new Random().Next();
await _fixture.SetUpEmptyIndex(indexUid);
var task = await _client.UpdateIndexAsync(indexUid, primarykey);
task.TaskUid.Should().BeGreaterOrEqualTo(0);
await _client.Index(indexUid).WaitForTaskAsync(task.TaskUid);
var index = await _client.GetIndexAsync(indexUid);
index.PrimaryKey.Should().Be(primarykey);
}
[Fact]
public async Task IndexNameWrongFormattedError()
{
var indexUid = "Wrong UID";
var ex = await Assert.ThrowsAsync<MeilisearchApiError>(() => _client.CreateIndexAsync(indexUid));
Assert.Equal("invalid_index_uid", ex.Code);
}
[Fact]
public async Task GetAllRawIndexes()
{
var indexUid = "GetAllRawIndexesTest";
await _fixture.SetUpEmptyIndex(indexUid, _defaultPrimaryKey);
var indexes = await _client.GetAllRawIndexesAsync();
var results = indexes.RootElement.GetProperty("results");
var index = results[0];
Assert.Equal(index.GetProperty("uid").GetString(), indexUid);
Assert.Equal(index.GetProperty("primaryKey").GetString(), _defaultPrimaryKey);
}
[Fact]
public async Task GetMultipleRawExistingIndexesWithLimit()
{
var indexUid1 = "GetMultipleRawExistingIndexesWithLimit1";
var indexUid2 = "GetMultipleRawExistingIndexesWithLimit2";
await _fixture.SetUpEmptyIndex(indexUid1, _defaultPrimaryKey);
await _fixture.SetUpEmptyIndex(indexUid2, _defaultPrimaryKey);
var indexes = await _client.GetAllRawIndexesAsync(new IndexesQuery() { Limit = 1 });
var results = indexes.RootElement.GetProperty("results");
var limit = indexes.RootElement.GetProperty("limit").GetInt32();
results.GetArrayLength().Should().BeGreaterThanOrEqualTo(1);
Assert.Equal(1, limit);
}
[Fact]
public async Task GetMultipleRawExistingIndexesWithOffset()
{
var indexUid1 = "GetMultipleRawExistingIndexesWithOffset1";
var indexUid2 = "GetMultipleRawExistingIndexesWithOffset2";
await _fixture.SetUpEmptyIndex(indexUid1, _defaultPrimaryKey);
await _fixture.SetUpEmptyIndex(indexUid2, _defaultPrimaryKey);
var indexes = await _client.GetAllRawIndexesAsync(new IndexesQuery() { Offset = 1 });
var results = indexes.RootElement.GetProperty("results");
var offset = indexes.RootElement.GetProperty("offset").GetInt32();
results.GetArrayLength().Should().BeGreaterThanOrEqualTo(1);
Assert.Equal(1, offset);
}
[Fact]
public async Task GetMultipleExistingIndexes()
{
var indexUid1 = "GetMultipleExistingIndexesTest1";
var indexUid2 = "GetMultipleExistingIndexesTest2";
await _fixture.SetUpEmptyIndex(indexUid1, _defaultPrimaryKey);
await _fixture.SetUpEmptyIndex(indexUid2, _defaultPrimaryKey);
var indexes = await _client.GetAllIndexesAsync();
indexes.Results.Count().Should().BeGreaterOrEqualTo(2);
}
[Fact]
public async Task GetMultipleExistingIndexesWithLimit()
{
var indexUid1 = "GetMultipleExistingIndexesWithLimit1";
var indexUid2 = "GetMultipleExistingIndexesWithLimit2";
await _fixture.SetUpEmptyIndex(indexUid1, _defaultPrimaryKey);
await _fixture.SetUpEmptyIndex(indexUid2, _defaultPrimaryKey);
var indexes = await _client.GetAllIndexesAsync(new IndexesQuery() { Limit = 1 });
indexes.Results.Count().Should().BeGreaterOrEqualTo(1);
indexes.Limit.Should().BeGreaterOrEqualTo(1);
}
[Fact]
public async Task GetMultipleExistingIndexesWithOffset()
{
var indexUid1 = "GetMultipleExistingIndexesWithOffset1";
var indexUid2 = "GetMultipleExistingIndexesWithOffset2";
await _fixture.SetUpEmptyIndex(indexUid1, _defaultPrimaryKey);
await _fixture.SetUpEmptyIndex(indexUid2, _defaultPrimaryKey);
var indexes = await _client.GetAllIndexesAsync(new IndexesQuery() { Offset = 1 });
indexes.Results.Count().Should().BeGreaterOrEqualTo(1);
Assert.Equal(1, indexes.Offset);
}
[Fact]
public async Task GetOneExistingIndex()
{
var indexUid = "GetOneExistingIndexTest";
await _fixture.SetUpEmptyIndex(indexUid, _defaultPrimaryKey);
var index = await _client.GetIndexAsync(indexUid);
index.Uid.Should().Be(indexUid);
index.PrimaryKey.Should().Be(_defaultPrimaryKey);
index.CreatedAt.Should().BeCloseTo(DateTimeOffset.Now, TimeSpan.FromSeconds(10));
index.UpdatedAt.Should().BeCloseTo(DateTimeOffset.Now, TimeSpan.FromSeconds(10));
}
[Fact]
public async Task GetAnNonExistingIndex()
{
var indexUid = "GetAnNonExistingIndexTest";
var ex = await Assert.ThrowsAsync<MeilisearchApiError>(() => _client.GetIndexAsync(indexUid));
Assert.Equal("index_not_found", ex.Code);
}
[Fact]
public async Task FetchPrimaryKey()
{
var indexUid = "FetchPrimaryKeyTest";
var index = await _fixture.SetUpEmptyIndex(indexUid, _defaultPrimaryKey);
index.Uid.Should().Be(indexUid);
index.PrimaryKey.Should().BeNull();
await index.FetchPrimaryKey();
Assert.Equal(_defaultPrimaryKey, index.PrimaryKey);
}
[Fact]
public async Task UpdateIndexMethod()
{
var indexUid = "UpdateIndexMethodTest";
var primarykey = "MovieId" + new Random().Next();
await _fixture.SetUpEmptyIndex(indexUid);
var task = await _client.Index(indexUid).UpdateAsync(primarykey);
task.TaskUid.Should().BeGreaterOrEqualTo(0);
await _client.Index(indexUid).WaitForTaskAsync(task.TaskUid);
var index = await _client.GetIndexAsync(indexUid);
index.PrimaryKey.Should().Be(primarykey);
index.CreatedAt.Should().BeCloseTo(DateTimeOffset.Now, TimeSpan.FromSeconds(10));
index.UpdatedAt.Should().BeCloseTo(DateTimeOffset.Now, TimeSpan.FromSeconds(10));
}
[Fact]
public async Task GetStats()
{
var indexUid = "GetStatsTests";
await _fixture.SetUpEmptyIndex(indexUid);
var stats = await _client.Index(indexUid).GetStatsAsync();
stats.Should().NotBeNull();
}
[Fact]
public async Task GetRawIndex()
{
var indexUid = "GetRawIndex";
await _fixture.SetUpEmptyIndex(indexUid);
var rawIndex = await _client.GetRawIndexAsync(indexUid);
rawIndex.GetProperty("uid").GetString().Should().Be(indexUid);
}
}
}