-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathSmokeTests.cs
More file actions
327 lines (259 loc) · 12.3 KB
/
SmokeTests.cs
File metadata and controls
327 lines (259 loc) · 12.3 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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Linq;
using Microsoft.Azure.Cosmos.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
[TestClass]
[TestCategory("Emulator")]
public class SmokeTests
{
private const string DatabaseName = "netcore_test_db";
private const string CollectionName = "netcore_test_coll";
private const string PartitionedCollectionName = "netcore_test_pcoll";
private const string VSTSContainerHostEnvironmentName = "COSMOSDBEMULATOR_ENDPOINT";
private CosmosClient client;
static SmokeTests()
{
}
[TestCleanup]
public async Task Cleanup()
{
await this.client.GetDatabase(DatabaseName).DeleteStreamAsync();
this.client?.Dispose();
}
/// <summary>
/// Test for the existence of native assembly dependencies
/// </summary>
[Ignore]
[TestMethod]
public void AssembliesExist()
{
Assert.IsTrue(Documents.ServiceInteropWrapper.AssembliesExist.Value);
}
/// <summary>
/// Test if 64-bit and native assembly dependencies exist
/// </summary>
[Ignore]
[TestMethod]
public void ByPassQueryParsing()
{
if (IntPtr.Size == 8)
{
Assert.IsFalse(Query.Core.QueryPlan.QueryPlanRetriever.BypassQueryParsing());
}
else
{
Assert.IsTrue(Query.Core.QueryPlan.QueryPlanRetriever.BypassQueryParsing());
}
}
[TestMethod]
public async Task DocumentInsertsTest_GatewayHttps()
{
this.client = this.GetDocumentClient(ConnectionMode.Gateway, Documents.Client.Protocol.Https);
await this.DocumentInsertsTest();
}
[TestMethod]
public async Task DocumentInsertsTest_DirectTcp()
{
this.client = this.GetDocumentClient(ConnectionMode.Direct, Documents.Client.Protocol.Tcp);
await this.DocumentInsertsTest();
}
private async Task DocumentInsertsTest()
{
Database database = await this.client.CreateDatabaseIfNotExistsAsync(DatabaseName);
Container container = await this.CreatePartitionedCollectionIfNotExists(database, PartitionedCollectionName);
for (int i = 0; i < 2; i++)
{
string id = i.ToString();
await container.CreateItemAsync(new Person() { Id = id, FirstName = "James", LastName = "Smith" });
}
IOrderedQueryable<dynamic> query =
container.GetItemLinqQueryable<dynamic>(allowSynchronousQueryExecution: true);
Assert.AreEqual(query.ToList().Count, 2);
await this.CleanupDocumentCollection(container);
}
[TestMethod]
public async Task QueryWithPaginationTest_GatewayHttps()
{
this.client = this.GetDocumentClient(ConnectionMode.Gateway, Documents.Client.Protocol.Https);
await this.QueryWithPagination();
}
[TestMethod]
public async Task QueryWithPaginationTest_DirectTcp()
{
this.client = this.GetDocumentClient(ConnectionMode.Direct, Documents.Client.Protocol.Tcp);
await this.QueryWithPagination();
}
private async Task QueryWithPagination()
{
Database database = await this.client.CreateDatabaseIfNotExistsAsync(DatabaseName);
Container container = await this.CreatePartitionedCollectionIfNotExists(database, PartitionedCollectionName);
Uri documentCollectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseName, PartitionedCollectionName);
await container.UpsertItemAsync<Person>(new Person() { Id = "1", FirstName = "David", LastName = "Smith" });
await container.UpsertItemAsync<Person>(new Person() { Id = "2", FirstName = "Robert", LastName = "Johnson" });
await container.UpsertItemAsync<Person>(new Person() { Id = "3", FirstName = "William", LastName = "Smith" });
QueryRequestOptions options = new QueryRequestOptions { MaxItemCount = 1 };
List<Person> smithFamily =
container.GetItemLinqQueryable<Person>(true, null, options)
.Where(d => d.LastName == "Smith")
.ToList();
Assert.AreEqual(2, smithFamily.Count);
List<Person> persons =
container.GetItemLinqQueryable<Person>(true, null, options)
.ToList();
Assert.AreEqual(3, persons.Count);
IOrderedQueryable<Person> query = container.GetItemLinqQueryable<Person>(true, null, options);
FeedIterator<Person> documentQuery = query.ToFeedIterator<Person>();
List<Person> personsList = new List<Person>();
while (documentQuery.HasMoreResults)
{
FeedResponse<Person> feedResponse = await documentQuery.ReadNextAsync();
int maxItemCount = options.MaxItemCount ?? default(int);
Assert.IsTrue(feedResponse.Count >= 0 && feedResponse.Count <= maxItemCount);
personsList.AddRange(feedResponse);
}
Assert.AreEqual(3, personsList.Count);
await this.CleanupDocumentCollection(container);
}
[TestMethod]
public async Task CrossPartitionQueries_GatewayHttps()
{
this.client = this.GetDocumentClient(ConnectionMode.Gateway, Documents.Client.Protocol.Https);
await this.CrossPartitionQueries();
}
[TestMethod]
public async Task CrossPartitionQueries_DirectTcp()
{
this.client = this.GetDocumentClient(ConnectionMode.Direct, Documents.Client.Protocol.Tcp);
await this.CrossPartitionQueries();
}
private async Task CrossPartitionQueries()
{
Database db = await this.client.CreateDatabaseIfNotExistsAsync(DatabaseName);
Container container = await this.CreatePartitionedCollectionIfNotExists(db, PartitionedCollectionName);
Uri documentCollectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseName, PartitionedCollectionName);
for (int i = 0; i < 2; i++)
{
string id = i.ToString();
await container.CreateItemAsync<Person>(new Person() { Id = id + Guid.NewGuid().ToString(), FirstName = "James", LastName = "Smith" });
}
List<dynamic> list = new List<dynamic>();
using (FeedIterator<dynamic> query =
container.GetItemQueryIterator<dynamic>("SELECT TOP 10 * FROM coll"))
{
while (query.HasMoreResults)
{
list.AddRange(await query.ReadNextAsync());
}
}
await this.CleanupDocumentCollection(container);
}
[TestMethod]
public async Task CreateDatabaseIfNotExists_GatewayHttps()
{
this.client = this.GetDocumentClient(ConnectionMode.Gateway, Documents.Client.Protocol.Https);
await this.CreateDatabaseIfNotExists(this.client);
}
[TestMethod]
public async Task CreateDatabaseIfNotExists_DirectTcp()
{
this.client = this.GetDocumentClient(ConnectionMode.Direct, Documents.Client.Protocol.Tcp);
await this.CreateDatabaseIfNotExists(this.client);
}
private async Task CreateDatabaseIfNotExists(CosmosClient client)
{
string databaseId = Guid.NewGuid().ToString();
// Create the database with this unique id
Database createdDatabase = await client.CreateDatabaseIfNotExistsAsync(databaseId);
// CreateDatabaseIfNotExistsAsync should create the new database
Assert.AreEqual(databaseId, createdDatabase.Id);
string databaseId2 = Guid.NewGuid().ToString();
// Pre-create the database with this unique id
Database createdDatabase2 = await client.CreateDatabaseAsync(databaseId2);
Database readDatabase = await client.CreateDatabaseIfNotExistsAsync(databaseId2);
// CreateDatabaseIfNotExistsAsync should return the same database
Assert.AreEqual(createdDatabase2.Id, readDatabase.Id);
// cleanup created databases
await createdDatabase.DeleteAsync();
await readDatabase.DeleteAsync();
}
[TestMethod]
public async Task CreateDocumentCollectionIfNotExists_GatewayHttps()
{
this.client = this.GetDocumentClient(ConnectionMode.Gateway, Documents.Client.Protocol.Https);
await this.CreateDocumentCollectionIfNotExists();
}
[TestMethod]
public async Task CreateDocumentCollectionIfNotExists_DirectTcp()
{
this.client = this.GetDocumentClient(ConnectionMode.Direct, Documents.Client.Protocol.Tcp);
await this.CreateDocumentCollectionIfNotExists();
}
private async Task CreateDocumentCollectionIfNotExists()
{
string databaseId = Guid.NewGuid().ToString();
// Create the database with this unique id
Database createdDatabase = await this.client.CreateDatabaseIfNotExistsAsync(databaseId);
string collectionId = Guid.NewGuid().ToString();
ContainerProperties collection = new ContainerProperties(collectionId, "/id");
ContainerProperties createdCollection = await createdDatabase.CreateContainerIfNotExistsAsync(collection);
// CreateDocumentCollectionIfNotExistsAsync should create the new collection
Assert.AreEqual(collectionId, createdCollection.Id);
string collectionId2 = Guid.NewGuid().ToString();
collection = new ContainerProperties(collectionId2, "/id");
// Pre-create the collection with this unique id
createdCollection = await createdDatabase.CreateContainerIfNotExistsAsync(collection);
ContainerProperties readCollection = await createdDatabase.CreateContainerIfNotExistsAsync(collection);
// CreateDocumentCollectionIfNotExistsAsync should return the same collection
Assert.AreEqual(createdCollection.Id, readCollection.Id);
// cleanup created database
await createdDatabase.DeleteAsync();
}
private CosmosClient GetDocumentClient(ConnectionMode connectionMode, Documents.Client.Protocol protocol)
{
CosmosClientOptions connectionPolicy = new CosmosClientOptions()
{
ConnectionMode = connectionMode,
ConnectionProtocol = protocol,
ConsistencyLevel = ConsistencyLevel.Session,
};
return TestCommon.CreateCosmosClient(connectionPolicy);
}
private async Task<Container> CreatePartitionedCollectionIfNotExists(Database database, string collectionName)
{
return await database.CreateContainerIfNotExistsAsync(collectionName, partitionKeyPath: "/id", throughput: 10200);
}
private async Task CleanupDocumentCollection(Container container)
{
FeedIterator<JObject> query = container.GetItemQueryIterator<JObject>();
while (query.HasMoreResults)
{
FeedResponse<JObject> items = await query.ReadNextAsync();
foreach (JObject doc in items)
{
string id = doc["id"].ToString();
await container.DeleteItemAsync<JObject>(id, new PartitionKey(id));
}
}
}
}
internal sealed class Person
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}