forked from meilisearch/meilisearch-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeilisearchClientTests.cs
192 lines (161 loc) · 7.25 KB
/
MeilisearchClientTests.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using FluentAssertions;
using Meilisearch.Extensions;
using Meilisearch.QueryParameters;
using Xunit;
namespace Meilisearch.Tests
{
public abstract class MeilisearchClientTests<TFixture> : IAsyncLifetime where TFixture : IndexFixture
{
private readonly MeilisearchClient _defaultClient;
private readonly string _defaultPrimaryKey;
private readonly TFixture _fixture;
public MeilisearchClientTests(TFixture fixture)
{
_fixture = fixture;
_defaultClient = 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 GetVersionWithCustomClient()
{
var meilisearchversion = await _fixture.ClientWithCustomHttpClient.GetVersionAsync();
meilisearchversion.Version.Should().NotBeNullOrEmpty();
}
[Fact]
public async Task GetVersionWithDefaultClient()
{
var meilisearchversion = await _defaultClient.GetVersionAsync();
meilisearchversion.Version.Should().NotBeNullOrEmpty();
}
[Fact]
public async Task BasicUsageOfCustomClient()
{
var indexUid = "BasicUsageOfCustomClientTest";
var task = await _fixture.ClientWithCustomHttpClient.CreateIndexAsync(indexUid);
task.TaskUid.Should().BeGreaterOrEqualTo(0);
await _defaultClient.Index(indexUid).WaitForTaskAsync(task.TaskUid);
var index = _defaultClient.Index(indexUid);
task = await index.AddDocumentsAsync(new[] { new Movie { Id = "1", Name = "Batman" } });
task.TaskUid.Should().BeGreaterOrEqualTo(0);
await index.WaitForTaskAsync(task.TaskUid);
// Check the JSON has been well serialized and the primary key is equal to "id"
Assert.Equal("id", await index.FetchPrimaryKey());
}
[Fact]
public async Task ErrorHandlerOfCustomClient()
{
var indexUid = "wrong UID";
var ex = await Assert.ThrowsAsync<MeilisearchApiError>(() => _fixture.ClientWithCustomHttpClient.CreateIndexAsync(indexUid, _defaultPrimaryKey));
Assert.Equal("invalid_index_uid", ex.Code);
}
[Fact]
public async Task GetStats()
{
var stats = await _defaultClient.GetStatsAsync();
stats.Should().NotBeNull();
}
[Fact]
public async Task CreateAndGetDumps()
{
var dumpResponse = await _defaultClient.CreateDumpAsync();
Assert.NotNull(dumpResponse);
dumpResponse.Status.Should().Be(TaskInfoStatus.Enqueued);
var dumpTask = await _defaultClient.GetTaskAsync(dumpResponse.TaskUid);
dumpTask.Status.Should().BeOneOf(TaskInfoStatus.Succeeded, TaskInfoStatus.Processing, TaskInfoStatus.Enqueued);
Assert.Equal(dumpResponse.TaskUid, dumpTask.Uid);
}
[Fact]
public async Task CancelTasks()
{
var date = DateTime.Now;
var formattedDate = Uri.EscapeDataString(((DateTime)date).ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz"));
var response = await _defaultClient.CancelTasksAsync(new CancelTasksQuery
{
Uids = new List<int> { 1, 4 },
AfterStartedAt = date
});
var task = await _defaultClient.WaitForTaskAsync(response.TaskUid);
response.TaskUid.Should().Be(task.Uid);
response.Type.Should().Be(TaskInfoType.TaskCancelation);
task.Status.Should().Be(TaskInfoStatus.Succeeded);
Assert.Equal($"?uids=1,4&afterStartedAt={formattedDate}", task.Details["originalFilter"].ToString());
}
[Fact]
public async Task DeleteTasks()
{
var date = DateTime.Now;
var formattedDate = Uri.EscapeDataString(((DateTime)date).ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz"));
var response = await _defaultClient.DeleteTasksAsync(new DeleteTasksQuery
{
Uids = new List<int> { 1, 4 },
AfterStartedAt = date
});
var task = await _defaultClient.WaitForTaskAsync(response.TaskUid);
response.TaskUid.Should().Be(task.Uid);
response.Type.Should().Be(TaskInfoType.TaskDeletion);
task.Status.Should().Be(TaskInfoStatus.Succeeded);
Assert.Equal($"?uids=1,4&afterStartedAt={formattedDate}", task.Details["originalFilter"].ToString());
}
[Fact]
public async Task SwapIndexes()
{
var swaps = new List<IndexSwap> { new IndexSwap("indexA", "indexB") };
var response = await _defaultClient.SwapIndexesAsync(swaps);
var task = await _defaultClient.WaitForTaskAsync(response.TaskUid);
response.TaskUid.Should().Be(task.Uid);
response.Type.Should().Be(TaskInfoType.IndexSwap);
task.Status.Should().Be(TaskInfoStatus.Failed);
Assert.Equal(task.Details["swaps"].ToString(), JsonSerializer.Serialize(swaps).ToString());
}
[Fact]
public async Task Health()
{
var health = await _defaultClient.HealthAsync();
health.Status.Should().Be("available");
}
[Fact]
public async Task HealthWithBadUrl()
{
var client = new MeilisearchClient("http://badhost:7700", "masterKey");
var ex = await Assert.ThrowsAsync<MeilisearchCommunicationError>(() => client.HealthAsync());
Assert.Equal("CommunicationError", ex.Message);
}
[Fact]
public async Task IsHealthy()
{
var health = await _defaultClient.IsHealthyAsync();
health.Should().BeTrue();
}
[Fact]
public async Task IsHealthyWithBadUrl()
{
var client = new MeilisearchClient("http://wrongurl:1234", "masterKey");
var health = await client.IsHealthyAsync();
health.Should().BeFalse();
}
[Fact]
public async Task ExceptionWithBadPath()
{
var client = new HttpClient(new MeilisearchMessageHandler(new HttpClientHandler())) { BaseAddress = _fixture.MeilisearchAddress().ToSafeUri() };
var ex = await Assert.ThrowsAsync<MeilisearchApiError>(() => client.GetAsync("wrong-path"));
Assert.Equal("MeilisearchApiError, Message: Not Found, Code: 404", ex.Message);
}
[Fact]
public async Task DeleteIndex()
{
var indexUid = "DeleteIndexTest";
await _fixture.ClientWithCustomHttpClient.CreateIndexAsync(indexUid, _defaultPrimaryKey);
var task = await _fixture.ClientWithCustomHttpClient.DeleteIndexAsync(indexUid);
task.TaskUid.Should().BeGreaterOrEqualTo(0);
var finishedTask = await _defaultClient.Index(indexUid).WaitForTaskAsync(task.TaskUid);
Assert.Equal(TaskInfoStatus.Succeeded, finishedTask.Status);
}
}
}