|
| 1 | +//------------------------------------------------------------ |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +//------------------------------------------------------------ |
| 4 | + |
| 5 | +namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Drawing; |
| 9 | + using System.Net; |
| 10 | + using System.Text.Json.Serialization; |
| 11 | + using System.Text.Json; |
| 12 | + using System.Threading.Tasks; |
| 13 | + using Microsoft.Azure.Cosmos; |
| 14 | + using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 15 | + |
| 16 | + [TestClass] |
| 17 | + public class CosmosItemThinClientTests |
| 18 | + { |
| 19 | + private string connectionString; |
| 20 | + private CosmosClient client; |
| 21 | + private Database database; |
| 22 | + private Container container; |
| 23 | + |
| 24 | + [TestInitialize] |
| 25 | + public async Task TestInitAsync() |
| 26 | + { |
| 27 | + this.connectionString = Environment.GetEnvironmentVariable("COSMOSDB_CONNECTION_STRING"); |
| 28 | + Environment.SetEnvironmentVariable(ConfigurationManager.ThinClientModeEnabled, "True"); |
| 29 | + |
| 30 | + if (string.IsNullOrEmpty(this.connectionString)) |
| 31 | + { |
| 32 | + Assert.Fail("Set environment variable COSMOSDB_CONNECTION_STRING to run the tests"); |
| 33 | + } |
| 34 | + |
| 35 | + this.client = new CosmosClient(this.connectionString); |
| 36 | + this.database = await this.client.CreateDatabaseIfNotExistsAsync("TestDatabase"); |
| 37 | + this.container = await this.database.CreateContainerIfNotExistsAsync("TestContainer", "/partitionKey"); |
| 38 | + } |
| 39 | + |
| 40 | + [TestCleanup] |
| 41 | + public async Task TestCleanupAsync() |
| 42 | + { |
| 43 | + Environment.SetEnvironmentVariable(ConfigurationManager.ThinClientModeEnabled, "False"); |
| 44 | + if (this.database != null) |
| 45 | + { |
| 46 | + await this.database.DeleteAsync(); |
| 47 | + } |
| 48 | + |
| 49 | + this.client?.Dispose(); |
| 50 | + } |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + [TestCategory("ThinClient")] |
| 54 | + public async Task CreateItemTest() |
| 55 | + { |
| 56 | + // Arrange |
| 57 | + dynamic testItem = new { id = Guid.NewGuid().ToString(), partitionKey = "pk1", name = "Test Item" }; |
| 58 | + |
| 59 | + // Act |
| 60 | + ItemResponse<dynamic> response = await this.container.CreateItemAsync(testItem, new PartitionKey(testItem.partitionKey)); |
| 61 | + |
| 62 | + // Assert |
| 63 | + Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); |
| 64 | + Assert.AreEqual(testItem.id, response.Resource.id); |
| 65 | + } |
| 66 | + |
| 67 | + [TestMethod] |
| 68 | + [TestCategory("ThinClient")] |
| 69 | + public async Task ReplaceItemTest() |
| 70 | + { |
| 71 | + // Arrange |
| 72 | + dynamic testItem = new { id = Guid.NewGuid().ToString(), partitionKey = "pk1", name = "Test Item" }; |
| 73 | + await this.container.CreateItemAsync(testItem, new PartitionKey(testItem.partitionKey)); |
| 74 | + |
| 75 | + dynamic updatedItem = new { testItem.id, partitionKey = "pk1", name = "Updated Item" }; |
| 76 | + |
| 77 | + // Act |
| 78 | + ItemResponse<dynamic> response = await this.container.ReplaceItemAsync(updatedItem, updatedItem.id, new PartitionKey(updatedItem.partitionKey)); |
| 79 | + |
| 80 | + // Assert |
| 81 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 82 | + Assert.AreEqual(updatedItem.name, response.Resource.name); |
| 83 | + } |
| 84 | + |
| 85 | + [TestMethod] |
| 86 | + [TestCategory("ThinClient")] |
| 87 | + public async Task DeleteItemTest() |
| 88 | + { |
| 89 | + // Arrange |
| 90 | + dynamic testItem = new { id = Guid.NewGuid().ToString(), partitionKey = "pk1", name = "Test Item" }; |
| 91 | + await this.container.CreateItemAsync(testItem, new PartitionKey(testItem.partitionKey)); |
| 92 | + |
| 93 | + // Act |
| 94 | + ItemResponse<dynamic> response = await this.container.DeleteItemAsync<dynamic>(testItem.id, new PartitionKey(testItem.partitionKey)); |
| 95 | + |
| 96 | + // Assert |
| 97 | + Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); |
| 98 | + } |
| 99 | + |
| 100 | + [TestMethod] |
| 101 | + [TestCategory("ThinClient")] |
| 102 | + public async Task ReadItemTest() |
| 103 | + { |
| 104 | + // Arrange |
| 105 | + dynamic testItem = new { id = Guid.NewGuid().ToString(), partitionKey = "pk1", name = "Test Item" }; |
| 106 | + await this.container.CreateItemAsync(testItem, new PartitionKey(testItem.partitionKey)); |
| 107 | + |
| 108 | + // Act |
| 109 | + ItemResponse<dynamic> response = await this.container.ReadItemAsync<dynamic>(testItem.id, new PartitionKey(testItem.partitionKey)); |
| 110 | + |
| 111 | + // Assert |
| 112 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 113 | + Assert.AreEqual(testItem.id, response.Resource.id); |
| 114 | + } |
| 115 | + |
| 116 | + [TestMethod] |
| 117 | + [TestCategory("ThinClient")] |
| 118 | + public async Task UpsertItemTest() |
| 119 | + { |
| 120 | + // Arrange |
| 121 | + dynamic testItem = new { id = Guid.NewGuid().ToString(), partitionKey = "pk1", name = "Test Item" }; |
| 122 | + |
| 123 | + // Act |
| 124 | + ItemResponse<dynamic> response = await this.container.UpsertItemAsync(testItem, new PartitionKey(testItem.partitionKey)); |
| 125 | + |
| 126 | + // Assert |
| 127 | + Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); |
| 128 | + Assert.AreEqual(testItem.id, response.Resource.id); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments