-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathClient_Tests.cs
287 lines (235 loc) · 10.4 KB
/
Client_Tests.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
285
286
287
using CouchDB.Driver.Types;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Threading;
using System.Threading.Tasks;
using CouchDB.Driver.E2ETests;
using CouchDB.Driver.E2ETests.Models;
using CouchDB.Driver.Extensions;
using CouchDB.Driver.Local;
using CouchDB.Driver.Query.Extensions;
using Xunit;
namespace CouchDB.Driver.E2E
{
[Trait("Category", "Integration")]
public class ClientTests : IAsyncLifetime
{
private ICouchClient _client;
private ICouchDatabase<Rebel> _rebels;
public async Task InitializeAsync()
{
_client = new CouchClient("http://localhost:5984", c =>
c.UseBasicAuthentication("admin", "admin"));
// ensure the _users database exists to prevent couchdb from
// generating tons of errors in the logs
await _client.GetOrCreateUsersDatabaseAsync();
_rebels = await _client.GetOrCreateDatabaseAsync<Rebel>();
}
public async Task DisposeAsync()
{
await _client.DeleteDatabaseAsync<Rebel>();
await _client.DisposeAsync();
}
[Fact]
public async Task ChangesFeed()
{
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_1", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_2", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_3", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_4", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_5", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_6", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_7", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_8", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_9", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_10", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_11", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_12", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_13", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_14", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_15", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_16", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_17", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_18", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_19", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_20", Age = 19 });
var lineCount = 0;
var tokenSource = new CancellationTokenSource();
await foreach (var l in _rebels.GetContinuousChangesAsync(null, null, tokenSource.Token))
{
lineCount++;
if (lineCount == 20)
{
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_11", Age = 19 });
_ = await _rebels.AddAsync(new Rebel { Name = "Luke_12", Age = 19 });
}
if (lineCount == 22)
{
tokenSource.Cancel();
}
}
}
[Fact]
public async Task Crud()
{
Rebel luke = await _rebels.AddAsync(new Rebel { Name = "Luke", Age = 19 });
Assert.Equal("Luke", luke.Name);
luke.Surname = "Skywalker";
luke = await _rebels.AddOrUpdateAsync(luke);
Assert.Equal("Skywalker", luke.Surname);
luke = await _rebels.FindAsync(luke.Id);
Assert.Equal(19, luke.Age);
await _rebels.RemoveAsync(luke);
luke = await _rebels.FindAsync(luke.Id);
Assert.Null(luke);
}
[Fact]
public async Task Crud_Context()
{
await using var context = new MyDeathStarContext();
var luke = await context.Rebels.AddAsync(new Rebel { Name = "Luke", Age = 19 });
Assert.Equal("Luke", luke.Name);
var result = await context.Rebels.ToListAsync();
Assert.NotEmpty(result);
}
[Fact]
public async Task Crud_Index_Context()
{
// Create index
await using var context = new MyDeathStarContext();
// Override index
await using var newContext = new MyDeathStarContext2();
var indexes = await context.Rebels.GetIndexesAsync();
Assert.Equal(2, indexes.Count);
await context.Rebels.AddAsync(new Rebel { Name = "Han", Age = 30, Surname = "Solo" });
await context.Rebels.AddAsync(new Rebel { Name = "Leia", Age = 19, Surname = "Skywalker" });
await context.Rebels.AddAsync(new Rebel { Name = "Luke", Age = 19, Surname = "Skywalker" });
var rebels = await context.Rebels
.OrderBy(r => r.Surname)
.ThenBy(r => r.Name)
.ToListAsync();
Assert.NotEmpty(rebels);
}
[Fact]
public async Task Crud_SpecialCharacters()
{
const string databaseName = "rebel0_$()+/-";
var rebels = await _client.GetOrCreateDatabaseAsync<Rebel>(databaseName);
Rebel luke = await rebels.AddAsync(new Rebel { Name = "Luke", Age = 19 });
Assert.Equal("Luke", luke.Name);
luke.Surname = "Skywalker";
luke = await rebels.AddOrUpdateAsync(luke);
Assert.Equal("Skywalker", luke.Surname);
luke = await rebels.FindAsync(luke.Id);
Assert.Equal(19, luke.Age);
await rebels.RemoveAsync(luke);
luke = await rebels.FindAsync(luke.Id);
Assert.Null(luke);
await _client.DeleteDatabaseAsync(databaseName);
}
[Fact]
public async Task Users()
{
var users = await _client.GetOrCreateUsersDatabaseAsync();
CouchUser luke = await users.AddAsync(new CouchUser(name: "luke", password: "lasersword"));
Assert.Equal("luke", luke.Name);
luke = await users.FindAsync(luke.Id);
Assert.Equal("luke", luke.Name);
luke = await users.ChangeUserPassword(luke, "r2d2");
await users.RemoveAsync(luke);
luke = await users.FindAsync(luke.Id);
Assert.Null(luke);
await _client.DeleteDatabaseAsync<CouchUser>();
}
[Fact]
public async Task Attachment()
{
var luke = new Rebel { Name = "Luke", Age = 19 };
var runningPath = Directory.GetCurrentDirectory();
// Create
luke.Attachments.AddOrUpdate($@"{runningPath}\Assets\luke.txt", MediaTypeNames.Text.Plain);
luke = await _rebels.AddAsync(luke);
Assert.Equal("Luke", luke.Name);
Assert.NotEmpty(luke.Attachments);
CouchAttachment attachment = luke.Attachments.First();
Assert.NotNull(attachment);
Assert.NotNull(attachment.Uri);
// Download
var downloadFilePath = await _rebels.DownloadAttachmentAsync(attachment, $@"{runningPath}\Assets", "luke-downloaded.txt");
Assert.True(File.Exists(downloadFilePath));
File.Delete(downloadFilePath);
// Find
luke = await _rebels.FindAsync(luke.Id);
Assert.Equal(19, luke.Age);
attachment = luke.Attachments.First();
Assert.NotNull(attachment);
Assert.NotNull(attachment.Uri);
Assert.NotNull(attachment.Digest);
Assert.NotNull(attachment.Length);
// Update
luke.Surname = "Skywalker";
luke = await _rebels.AddOrUpdateAsync(luke);
Assert.Equal("Skywalker", luke.Surname);
}
[Fact]
public async Task AttachmentAsStream()
{
var luke = new Rebel { Name = "Luke", Age = 19 };
var runningPath = Directory.GetCurrentDirectory();
var fileOnDisk = File.ReadAllBytes($@"{runningPath}\Assets\luke.txt");
// Create
luke.Attachments.AddOrUpdate($@"{runningPath}\Assets\luke.txt", MediaTypeNames.Text.Plain);
luke = await _rebels.AddAsync(luke);
Assert.Equal("Luke", luke.Name);
Assert.NotEmpty(luke.Attachments);
CouchAttachment attachment = luke.Attachments.First();
Assert.NotNull(attachment);
Assert.NotNull(attachment.Uri);
// Download
var responseStream = await _rebels.DownloadAttachmentAsStreamAsync(attachment);
var memStream = new MemoryStream();
responseStream.CopyTo(memStream);
var fileFromDb = memStream.ToArray();
var areEqual = fileOnDisk.SequenceEqual(fileFromDb);
Assert.True(areEqual);
}
[Fact]
public async Task LocalDocuments()
{
var local = _rebels.LocalDocuments;
var docId = "传";
var settings = new RebelSettings
{
Id = docId,
IsActive = true
};
await local.CreateOrUpdateAsync(settings);
settings = await local.GetAsync<RebelSettings>(docId);
Assert.True(settings.IsActive);
settings.IsActive = false;
await local.CreateOrUpdateAsync(settings);
settings = await local.GetAsync<RebelSettings>(docId);
Assert.False(settings.IsActive);
var searchOpt = new LocalDocumentsOptions
{
Descending = true,
Limit = 10,
Conflicts = true
};
var docs = await local.GetAsync(searchOpt);
var containsId = docs.Select(d => d.Id).Contains("_local/" + docId);
Assert.True(containsId);
}
[Fact]
public async Task ExecutionStats()
{
await using var context = new MyDeathStarContext();
var rebels = await context.Rebels
.Where(r => r.Age == 19)
.IncludeExecutionStats()
.ToCouchListAsync();
Assert.True(rebels.ExecutionStats.ExecutionTimeMs > 0);
}
}
}