-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathCosmosItemThinClientTests.cs
More file actions
371 lines (318 loc) · 13.1 KB
/
CosmosItemThinClientTests.cs
File metadata and controls
371 lines (318 loc) · 13.1 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static Microsoft.Azure.Cosmos.SDK.EmulatorTests.MultiRegionSetupHelpers;
using TestObject = MultiRegionSetupHelpers.CosmosIntegrationTestObject;
[TestClass]
public class CosmosItemThinClientTests
{
private string connectionString;
private CosmosClient client;
private Database database;
private Container container;
private CosmosSystemTextJsonSerializer cosmosSystemTextJsonSerializer;
private const int ItemCount = 1000;
[TestInitialize]
public async Task TestInitAsync()
{
this.connectionString = Environment.GetEnvironmentVariable("COSMOSDB_THINCLIENT");
Environment.SetEnvironmentVariable(ConfigurationManager.ThinClientModeEnabled, "True");
if (string.IsNullOrEmpty(this.connectionString))
{
Assert.Fail("Set environment variable COSMOSDB_THINCLIENT to run the tests");
}
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = null,
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
this.cosmosSystemTextJsonSerializer = new MultiRegionSetupHelpers.CosmosSystemTextJsonSerializer(jsonSerializerOptions);
this.client = new CosmosClient(
this.connectionString,
new CosmosClientOptions()
{
ConnectionMode = ConnectionMode.Gateway,
Serializer = this.cosmosSystemTextJsonSerializer,
});
string uniqueDbName = "TestDb_" + Guid.NewGuid().ToString();
this.database = await this.client.CreateDatabaseIfNotExistsAsync(uniqueDbName);
string uniqueContainerName = "TestContainer_" + Guid.NewGuid().ToString();
this.container = await this.database.CreateContainerIfNotExistsAsync(uniqueContainerName, "/pk");
}
[TestCleanup]
public async Task TestCleanupAsync()
{
Environment.SetEnvironmentVariable(ConfigurationManager.ThinClientModeEnabled, "False");
if (this.database != null)
{
await this.database.DeleteAsync();
}
this.client?.Dispose();
}
private IEnumerable<TestObject> GenerateItems(string partitionKey)
{
List<TestObject> items = new List<TestObject>();
for (int i = 0; i < ItemCount; i++)
{
items.Add(new TestObject
{
Id = Guid.NewGuid().ToString(),
Pk = partitionKey,
Other = "Test Item " + i
});
}
return items;
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task CreateItemsTest()
{
string pk = "pk_create";
IEnumerable<TestObject> items = this.GenerateItems(pk);
foreach (TestObject item in items)
{
ItemResponse<TestObject> response = await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
}
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task ReadItemsTest()
{
string pk = "pk_read";
List<TestObject> items = this.GenerateItems(pk).ToList();
foreach (TestObject item in items)
{
await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
}
foreach (TestObject item in items)
{
ItemResponse<TestObject> response = await this.container.ReadItemAsync<TestObject>(item.Id, new PartitionKey(item.Pk));
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual(item.Id, response.Resource.Id);
}
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task ReplaceItemsTest()
{
string pk = "pk_replace";
List<TestObject> items = this.GenerateItems(pk).ToList();
foreach (TestObject item in items)
{
await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
}
foreach (TestObject item in items)
{
TestObject updatedItem = new TestObject
{
Id = item.Id,
Pk = item.Pk,
Other = "Updated " + item.Other
};
ItemResponse<TestObject> response = await this.container.ReplaceItemAsync(updatedItem, updatedItem.Id, new PartitionKey(updatedItem.Pk));
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual("Updated " + item.Other, response.Resource.Other);
}
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task UpsertItemsTest()
{
string pk = "pk_upsert";
IEnumerable<TestObject> items = this.GenerateItems(pk);
foreach (TestObject item in items)
{
ItemResponse<TestObject> response = await this.container.UpsertItemAsync(item, new PartitionKey(item.Pk));
Assert.IsTrue(response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK);
}
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task DeleteItemsTest()
{
string pk = "pk_delete";
List<TestObject> items = this.GenerateItems(pk).ToList();
foreach (TestObject item in items)
{
await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
}
foreach (TestObject item in items)
{
ItemResponse<TestObject> response = await this.container.DeleteItemAsync<TestObject>(item.Id, new PartitionKey(item.Pk));
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
}
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task CreateItemStreamTest()
{
string pk = "pk_create_stream";
IEnumerable<TestObject> items = this.GenerateItems(pk);
foreach (TestObject item in items)
{
using (Stream stream = this.cosmosSystemTextJsonSerializer.ToStream(item))
{
using (ResponseMessage response = await this.container.CreateItemStreamAsync(stream, new PartitionKey(item.Pk)))
{
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
}
}
}
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task ReadItemStreamTest()
{
string pk = "pk_read_stream";
List<TestObject> items = this.GenerateItems(pk).ToList();
foreach (TestObject item in items)
{
await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
}
foreach (TestObject item in items)
{
using (ResponseMessage response = await this.container.ReadItemStreamAsync(item.Id, new PartitionKey(item.Pk)))
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task ReplaceItemStreamTest()
{
string pk = "pk_replace_stream";
List<TestObject> items = this.GenerateItems(pk).ToList();
foreach (TestObject item in items)
{
await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
}
foreach (TestObject item in items)
{
TestObject updatedItem = new TestObject
{
Id = item.Id,
Pk = item.Pk,
Other = "Updated " + item.Other
};
using (Stream stream = this.cosmosSystemTextJsonSerializer.ToStream(updatedItem))
{
using (ResponseMessage response = await this.container.ReplaceItemStreamAsync(stream, updatedItem.Id, new PartitionKey(updatedItem.Pk)))
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
}
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task UpsertItemStreamTest()
{
string pk = "pk_upsert_stream";
IEnumerable<TestObject> items = this.GenerateItems(pk);
foreach (TestObject item in items)
{
using (Stream stream = this.cosmosSystemTextJsonSerializer.ToStream(item))
{
using (ResponseMessage response = await this.container.UpsertItemStreamAsync(stream, new PartitionKey(item.Pk)))
{
Assert.IsTrue(response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK);
}
}
}
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task DeleteItemStreamTest()
{
string pk = "pk_delete_stream";
List<TestObject> items = this.GenerateItems(pk).ToList();
foreach (TestObject item in items)
{
await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
}
foreach (TestObject item in items)
{
using (ResponseMessage response = await this.container.DeleteItemStreamAsync(item.Id, new PartitionKey(item.Pk)))
{
Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
}
}
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task QueryItemsTest()
{
string pk = "pk_query";
List<TestObject> items = this.GenerateItems(pk).ToList();
foreach (TestObject item in items)
{
await this.container.CreateItemAsync(item, new PartitionKey(item.Pk));
}
string query = $"SELECT * FROM c WHERE c.partitionKey = '{pk}'";
FeedIterator<TestObject> iterator = this.container.GetItemQueryIterator<TestObject>(query);
int count = 0;
while (iterator.HasMoreResults)
{
FeedResponse<TestObject> response = await iterator.ReadNextAsync();
count += response.Count;
}
Assert.AreEqual(ItemCount, count);
}
[TestMethod]
[TestCategory("ThinClient")]
[Timeout(70000)]
public async Task QueryItemsStreamTest()
{
string pk = "pk_query_stream";
List<TestObject> items = this.GenerateItems(pk).ToList();
foreach (dynamic item in items)
{
await this.container.CreateItemAsync(item, new PartitionKey(item.partitionKey));
}
QueryDefinition query = new QueryDefinition("SELECT * FROM c WHERE c.partitionKey = @pk").WithParameter("@pk", pk);
FeedIterator iterator = this.container.GetItemQueryStreamIterator(query);
int count = 0;
while (iterator.HasMoreResults)
{
using (ResponseMessage response = await iterator.ReadNextAsync())
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
using (StreamReader reader = new StreamReader(response.Content))
{
string json = await reader.ReadToEndAsync();
using (JsonDocument doc = JsonDocument.Parse(json))
{
count += doc.RootElement.GetProperty("Documents").GetArrayLength();
}
}
}
}
Assert.AreEqual(ItemCount, count);
}
}
}