From bafb71c93cb8a32c40100302a1e537fb654cfe62 Mon Sep 17 00:00:00 2001 From: Nalu Tripician Date: Wed, 21 Dec 2022 11:48:04 -0500 Subject: [PATCH 1/6] updates to test coverage for subpartitioning --- .../CosmosContainerTests.cs | 1 - .../CosmosItemTests.cs | 215 +++++++++++++++++- 2 files changed, 203 insertions(+), 13 deletions(-) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs index 208b08ec11..0151cd424d 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs @@ -795,7 +795,6 @@ public async Task CreateContainerIfNotExistsAsyncForMultiHashCollectionsTest() containerResponse = await containerResponse.Container.DeleteContainerAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); - } #endif diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs index da6d79c333..6b692a2d0d 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs @@ -34,6 +34,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests using System.Reflection; using System.Text.RegularExpressions; using Microsoft.Azure.Cosmos.Diagnostics; + using static System.Net.Mime.MediaTypeNames; [TestClass] public class CosmosItemTests : BaseCosmosClientHelper @@ -3019,22 +3020,27 @@ public async Task VerifyDocumentCrudWithMultiHashKind() { ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); Container container = await database.CreateContainerAsync(containerProperties); - + Cosmos.PartitionKey pKey; + Cosmos.PartitionKey badPKey; + //Document create. ItemResponse[] documents = new ItemResponse[3]; Document doc1 = new Document { Id = "document1" }; doc1.SetValue("ZipCode", "500026"); doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); documents[0] = await container.CreateItemAsync(doc1); doc1 = new Document { Id = "document2" }; doc1.SetValue("ZipCode", "15232"); doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); documents[1] = await container.CreateItemAsync(doc1); doc1 = new Document { Id = "document3" }; doc1.SetValue("ZipCode", "11790"); doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); documents[2] = await container.CreateItemAsync(doc1); Assert.AreEqual(3, documents.Select(document => ((Document)document).SelfLink).Distinct().Count()); @@ -3058,34 +3064,219 @@ public async Task VerifyDocumentCrudWithMultiHashKind() //Document Read. foreach (Document document in documents) { - Cosmos.PartitionKey pKey = new PartitionKeyBuilder() + pKey = new PartitionKeyBuilder() .Add(document.GetPropertyValue("ZipCode")) .Add(document.GetPropertyValue("Address")) .Build(); Document readDocument = (await container.ReadItemAsync(document.Id, pKey)).Resource; Assert.AreEqual(document.ToString(), readDocument.ToString()); + + //Negative test - using incomplete partition key + Boolean failedCorrextly = false; + try + { + badPKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("Address")) + .Build(); + + Document badReadDocument = (await container.ReadItemAsync(document.Id, pKey)).Resource; + } + catch (Exception ex) + { + Assert.AreEqual("PartitionKey value must be supplied for this operation.", + ex.Message); + failedCorrextly = true; + } + Assert.IsTrue(failedCorrextly); } - //Document Update. - foreach (ItemResponse obj in documents) + //Read Many + List<(string, Cosmos.PartitionKey)> itemList = new List<(string, Cosmos.PartitionKey)>(); + foreach (Document document in documents) { - Cosmos.PartitionKey pKey = new PartitionKeyBuilder() - .Add(obj.Resource.GetValue("ZipCode")) - .Add(obj.Resource.GetPropertyValue("Address")) + pKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("ZipCode")) + .Add(document.GetPropertyValue("Address")) .Build(); - Document document = (await container.ReadItemAsync(obj.Resource.Id, pKey)).Resource; - document.SetPropertyValue("Name", document.Id); + itemList.Add((document.Id, pKey)); + } + + FeedResponse feedResponse = await container.ReadManyItemsAsync(itemList); + + Assert.IsNotNull(feedResponse); + Assert.AreEqual(feedResponse.Count, 3); + Assert.IsTrue(feedResponse.Headers.RequestCharge > 0); + Assert.IsNotNull(feedResponse.Diagnostics); + + int count = 0; + foreach (ToDoActivity item in feedResponse) + { + count++; + Assert.IsNotNull(item); + Assert.IsNotNull(item.pk); + } + Assert.AreEqual(count, 3); + + //Document Upsert + doc1 = new Document { Id = "document4" }; + doc1.SetValue("ZipCode", "97756"); + doc1.SetValue("Address", "Redmond"); + doc1.SetValue("Type", "Residence"); + + pKey = new PartitionKeyBuilder() + .Add(doc1.GetPropertyValue("ZipCode")) + .Add(doc1.GetPropertyValue("Address")) + .Build(); - Document readDocument = (await container.ReplaceItemAsync(document, document.Id, pKey)).Resource; - Assert.AreEqual(readDocument.GetValue("Name"), document.GetValue("Name")); + //insert check + await container.UpsertItemAsync(doc1, pKey); + + Document readCheck = (await container.ReadItemAsync(doc1.Id, pKey)).Resource; + + Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); + Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); + Assert.AreEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); + + doc1 = new Document { Id = "document4" }; + doc1.SetValue("ZipCode", "97756"); + doc1.SetValue("Address", "Redmond"); + doc1.SetValue("Type", "Business"); + + //update check + documents.Append>(await container.UpsertItemAsync(doc1)); + + readCheck = (await container.ReadItemAsync(doc1.Id, pKey)).Resource; + + Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); + Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); + Assert.AreEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); + + count = 0; + + foreach (Document doc in container.GetItemLinqQueryable(true)) + { + count++; + } + Assert.AreEqual(4, count); + + //Negative test - using incomplete partition key + doc1 = new Document { Id = "document4" }; + doc1.SetValue("ZipCode", "97756"); + doc1.SetValue("Address", "Redmond"); + doc1.SetValue("Type", "Residence"); + + badPKey = new PartitionKeyBuilder() + .Add(doc1.GetPropertyValue("ZipCode")) + .Build(); + + Boolean failedCorrectly = false; + try + { + await container.UpsertItemAsync(doc1); + } + catch (Exception ex) + { + Assert.AreEqual("PartitionKey value must be supplied for this operation.", + ex.Message); + failedCorrectly = true; + } + Assert.IsTrue(failedCorrectly); + + readCheck = (await container.ReadItemAsync(doc1.Id, pKey)).Resource; + + Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); + Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); + Assert.AreNotEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); + + //Query + foreach (Document document in documents) + { + pKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("ZipCode")) + .Add(document.GetPropertyValue("Address")) + .Build(); + + String query = $"SELECT * from c where c.id = {document.GetPropertyValue("Id")}"; + + using (FeedIterator feedIterator = this.Container.GetItemQueryIterator( + query, + null, + new QueryRequestOptions() { PartitionKey = pKey })) + { + Assert.IsTrue(feedIterator.HasMoreResults); + + try + { + var queryDoc = await feedIterator.ReadNextAsync(); + } + catch (Exception) + { + Assert.Fail("Should succeed"); + } + } + + } + + //Document Replace + foreach (Document document in documents) + { + pKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("ZipCode")) + .Add(document.GetPropertyValue("Address")) + .Build(); + + + Document readDocument = (await container.ReadItemAsync(document.Id, pKey)).Resource; + readDocument.SetValue("Type", "Park"); + + ItemResponse item = await container.ReplaceItemAsync(readDocument, readDocument.Id, pKey); + + Document checkDocument = (await container.ReadItemAsync(document.Id, pKey)).Resource; + Assert.AreEqual(checkDocument.GetPropertyValue("Type"), readDocument.GetPropertyValue("Type")); + + //Negative test - using incomplete partition key + Boolean failedCorrectly = false; + try + { + badPKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("Address")) + .Build(); + + readDocument.SetValue("Type", "Goverment"); + ItemResponse badItem = await container.ReplaceItemAsync(document, document.Id, partitionKey: badPKey); + } + catch (Exception ex) + { + Assert.AreEqual("PartitionKey value must be supplied for this operation.", + ex.Message); + failedCorrectly = true; + } + Assert.IsTrue(failedCorrectly); } //Document Delete. foreach (Document document in documents) { - Cosmos.PartitionKey pKey = new PartitionKeyBuilder() + //Negative test - using incomplete partition key + Boolean failedCorrectly = false; + try + { + badPKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("Address")) + .Build(); + + Document badReadDocument = (await container.DeleteItemAsync(document.Id, badPKey)).Resource; + } + catch (Exception ex) + { + Assert.IsTrue(ex.Message.Contains("Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document")); + failedCorrectly = true; + } + Assert.IsTrue(failedCorrectly); + + pKey = new PartitionKeyBuilder() .Add(document.GetPropertyValue("ZipCode")) .Add(document.GetPropertyValue("Address")) .Build(); From 611a5f2e3c03cf00db2b5869c63eaa31e8a20381 Mon Sep 17 00:00:00 2001 From: Nalu Tripician Date: Wed, 21 Dec 2022 14:01:33 -0500 Subject: [PATCH 2/6] bug fixes --- .../CosmosItemTests.cs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs index 6b692a2d0d..7becb4561e 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs @@ -3073,7 +3073,6 @@ public async Task VerifyDocumentCrudWithMultiHashKind() Assert.AreEqual(document.ToString(), readDocument.ToString()); //Negative test - using incomplete partition key - Boolean failedCorrextly = false; try { badPKey = new PartitionKeyBuilder() @@ -3086,9 +3085,7 @@ public async Task VerifyDocumentCrudWithMultiHashKind() { Assert.AreEqual("PartitionKey value must be supplied for this operation.", ex.Message); - failedCorrextly = true; } - Assert.IsTrue(failedCorrextly); } //Read Many @@ -3171,7 +3168,6 @@ public async Task VerifyDocumentCrudWithMultiHashKind() .Add(doc1.GetPropertyValue("ZipCode")) .Build(); - Boolean failedCorrectly = false; try { await container.UpsertItemAsync(doc1); @@ -3180,9 +3176,7 @@ public async Task VerifyDocumentCrudWithMultiHashKind() { Assert.AreEqual("PartitionKey value must be supplied for this operation.", ex.Message); - failedCorrectly = true; } - Assert.IsTrue(failedCorrectly); readCheck = (await container.ReadItemAsync(doc1.Id, pKey)).Resource; @@ -3237,7 +3231,6 @@ public async Task VerifyDocumentCrudWithMultiHashKind() Assert.AreEqual(checkDocument.GetPropertyValue("Type"), readDocument.GetPropertyValue("Type")); //Negative test - using incomplete partition key - Boolean failedCorrectly = false; try { badPKey = new PartitionKeyBuilder() @@ -3250,17 +3243,14 @@ public async Task VerifyDocumentCrudWithMultiHashKind() catch (Exception ex) { Assert.AreEqual("PartitionKey value must be supplied for this operation.", - ex.Message); - failedCorrectly = true; + ex.Message); } - Assert.IsTrue(failedCorrectly); } //Document Delete. foreach (Document document in documents) { - //Negative test - using incomplete partition key - Boolean failedCorrectly = false; + //Negative test - using incomplete partition key try { badPKey = new PartitionKeyBuilder() @@ -3271,10 +3261,8 @@ public async Task VerifyDocumentCrudWithMultiHashKind() } catch (Exception ex) { - Assert.IsTrue(ex.Message.Contains("Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document")); - failedCorrectly = true; + Assert.IsTrue(ex.Message.Contains("Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document")); } - Assert.IsTrue(failedCorrectly); pKey = new PartitionKeyBuilder() .Add(document.GetPropertyValue("ZipCode")) From 4fa81717b2d4105c33a1e11303fe197ce43ceb85 Mon Sep 17 00:00:00 2001 From: Nalu Tripician Date: Thu, 5 Jan 2023 19:05:51 -0500 Subject: [PATCH 3/6] now useses Assert.ThrowsException --- .../CosmosItemTests.cs | 83 ++++++++----------- 1 file changed, 33 insertions(+), 50 deletions(-) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs index 7becb4561e..e3926cc86b 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs @@ -3073,19 +3073,15 @@ public async Task VerifyDocumentCrudWithMultiHashKind() Assert.AreEqual(document.ToString(), readDocument.ToString()); //Negative test - using incomplete partition key - try - { - badPKey = new PartitionKeyBuilder() + badPKey = new PartitionKeyBuilder() .Add(document.GetPropertyValue("Address")) .Build(); - Document badReadDocument = (await container.ReadItemAsync(document.Id, pKey)).Resource; - } - catch (Exception ex) - { - Assert.AreEqual("PartitionKey value must be supplied for this operation.", - ex.Message); - } + CosmosException clientException = await Assert.ThrowsExceptionAsync(() => + container.ReadItemAsync(document.Id, badPKey) + ); + + Assert.AreEqual(clientException.StatusCode, HttpStatusCode.BadRequest); } //Read Many @@ -3142,7 +3138,12 @@ public async Task VerifyDocumentCrudWithMultiHashKind() doc1.SetValue("Type", "Business"); //update check - documents.Append>(await container.UpsertItemAsync(doc1)); + pKey = new PartitionKeyBuilder() + .Add(doc1.GetPropertyValue("ZipCode")) + .Add(doc1.GetPropertyValue("Address")) + .Build(); + + documents.Append>(await container.UpsertItemAsync(doc1, pKey)); readCheck = (await container.ReadItemAsync(doc1.Id, pKey)).Resource; @@ -3168,15 +3169,9 @@ public async Task VerifyDocumentCrudWithMultiHashKind() .Add(doc1.GetPropertyValue("ZipCode")) .Build(); - try - { - await container.UpsertItemAsync(doc1); - } - catch (Exception ex) - { - Assert.AreEqual("PartitionKey value must be supplied for this operation.", - ex.Message); - } + await Assert.ThrowsExceptionAsync(() => + container.UpsertItemAsync(doc1, badPKey) + ); readCheck = (await container.ReadItemAsync(doc1.Id, pKey)).Resource; @@ -3203,7 +3198,7 @@ public async Task VerifyDocumentCrudWithMultiHashKind() try { - var queryDoc = await feedIterator.ReadNextAsync(); + FeedResponse queryDoc = await feedIterator.ReadNextAsync(); } catch (Exception) { @@ -3231,38 +3226,28 @@ public async Task VerifyDocumentCrudWithMultiHashKind() Assert.AreEqual(checkDocument.GetPropertyValue("Type"), readDocument.GetPropertyValue("Type")); //Negative test - using incomplete partition key - try - { - badPKey = new PartitionKeyBuilder() + badPKey = new PartitionKeyBuilder() .Add(document.GetPropertyValue("Address")) .Build(); - readDocument.SetValue("Type", "Goverment"); - ItemResponse badItem = await container.ReplaceItemAsync(document, document.Id, partitionKey: badPKey); - } - catch (Exception ex) - { - Assert.AreEqual("PartitionKey value must be supplied for this operation.", - ex.Message); - } + readDocument.SetValue("Type", "Goverment"); + + await Assert.ThrowsExceptionAsync(() => + container.ReplaceItemAsync(document, document.Id, partitionKey: badPKey) + ); } //Document Delete. foreach (Document document in documents) { - //Negative test - using incomplete partition key - try - { - badPKey = new PartitionKeyBuilder() + //Negative test - using incomplete partition key + badPKey = new PartitionKeyBuilder() .Add(document.GetPropertyValue("Address")) .Build(); - Document badReadDocument = (await container.DeleteItemAsync(document.Id, badPKey)).Resource; - } - catch (Exception ex) - { - Assert.IsTrue(ex.Message.Contains("Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document")); - } + await Assert.ThrowsExceptionAsync(() => + container.DeleteItemAsync(document.Id, badPKey) + ); pKey = new PartitionKeyBuilder() .Add(document.GetPropertyValue("ZipCode")) @@ -3270,14 +3255,12 @@ public async Task VerifyDocumentCrudWithMultiHashKind() .Build(); Document readDocument = (await container.DeleteItemAsync(document.Id, pKey)).Resource; - try - { - readDocument = await container.ReadItemAsync(document.Id, pKey); - } - catch (CosmosException clientException) - { - Assert.AreEqual(clientException.StatusCode, HttpStatusCode.NotFound); - } + + CosmosException clientException = await Assert.ThrowsExceptionAsync(() => + container.ReadItemAsync(document.Id, pKey) + ); + + Assert.AreEqual(clientException.StatusCode, HttpStatusCode.NotFound); } } From 86e3a940ff16d777eac5404c4888ec7fa308bf99 Mon Sep 17 00:00:00 2001 From: Nalu Tripician Date: Fri, 6 Jan 2023 14:50:51 -0500 Subject: [PATCH 4/6] Seperated into multiple tests for clarity --- .../CosmosItemTests.cs | 392 +++++++++++++++--- 1 file changed, 333 insertions(+), 59 deletions(-) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs index e3926cc86b..fd3ad30b06 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs @@ -3009,7 +3009,7 @@ public async Task HaLayerDoesNotThrowNullOnGoneExceptionTest() #if PREVIEW [TestMethod] - public async Task VerifyDocumentCrudWithMultiHashKind() + public async Task SubpartitioningCreateDocumentTest() { string currentVersion = HttpConstants.Versions.CurrentVersion; HttpConstants.Versions.CurrentVersion = "2020-07-15"; @@ -3020,10 +3020,8 @@ public async Task VerifyDocumentCrudWithMultiHashKind() { ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); Container container = await database.CreateContainerAsync(containerProperties); - Cosmos.PartitionKey pKey; - Cosmos.PartitionKey badPKey; - //Document create. + //Document create test ItemResponse[] documents = new ItemResponse[3]; Document doc1 = new Document { Id = "document1" }; doc1.SetValue("ZipCode", "500026"); @@ -3045,7 +3043,7 @@ public async Task VerifyDocumentCrudWithMultiHashKind() Assert.AreEqual(3, documents.Select(document => ((Document)document).SelfLink).Distinct().Count()); - //Negative test + //Negative test - using incomplete partition key { doc1 = new Document { Id = "doc1" }; doc1.SetValue("Zipcode", 11790); @@ -3054,14 +3052,136 @@ public async Task VerifyDocumentCrudWithMultiHashKind() pKValueList.Add(doc1.GetPropertyValue("ZipCode")); Cosmos.PartitionKey pKeyErr = pKValueList.Build(); - ResponseMessage response = await this.Container.CreateItemStreamAsync(streamPayload: TestCommon.SerializerCore.ToStream(doc1), partitionKey: pKeyErr); + ResponseMessage response = await this.Container.CreateItemStreamAsync(streamPayload: TestCommon.SerializerCore.ToStream(doc1), partitionKey: pKeyErr); Assert.IsNotNull(response); Assert.IsNull(response.Content); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); } - - //Document Read. + } + catch (Exception) + { + Assert.Fail(); + } + finally + { + await database.DeleteAsync(); + HttpConstants.Versions.CurrentVersion = currentVersion; + } + } + + [TestMethod] + public async Task SubpartitioningDeleteDocumentTest() + { + string currentVersion = HttpConstants.Versions.CurrentVersion; + HttpConstants.Versions.CurrentVersion = "2020-07-15"; + CosmosClient client = TestCommon.CreateCosmosClient(true); + Cosmos.Database database = null; + database = await client.CreateDatabaseIfNotExistsAsync("mydb"); + try + { + ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); + Container container = await database.CreateContainerAsync(containerProperties); + Cosmos.PartitionKey pKey; + Cosmos.PartitionKey badPKey; + + //Create Items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await container.CreateItemAsync(doc1); + + //Document Delete Test + foreach (Document document in documents) + { + //Negative test - using incomplete partition key + badPKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("Address")) + .Build(); + + CosmosException deleteException = await Assert.ThrowsExceptionAsync(() => + container.DeleteItemAsync(document.Id, badPKey) + ); + + Assert.AreEqual(deleteException.StatusCode, HttpStatusCode.BadRequest); + + //Positive test + pKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("ZipCode")) + .Add(document.GetPropertyValue("Address")) + .Build(); + + Document readDocument = (await container.DeleteItemAsync(document.Id, pKey)).Resource; + + CosmosException clientException = await Assert.ThrowsExceptionAsync(() => + container.ReadItemAsync(document.Id, pKey) + ); + + Assert.AreEqual(clientException.StatusCode, HttpStatusCode.NotFound); + } + + } + catch (Exception) + { + Assert.Fail(); + } + finally + { + await database.DeleteAsync(); + HttpConstants.Versions.CurrentVersion = currentVersion; + } + } + + [TestMethod] + public async Task SubpartitioningReadItemTest() + { + string currentVersion = HttpConstants.Versions.CurrentVersion; + HttpConstants.Versions.CurrentVersion = "2020-07-15"; + CosmosClient client = TestCommon.CreateCosmosClient(true); + Cosmos.Database database = null; + database = await client.CreateDatabaseIfNotExistsAsync("mydb"); + try + { + ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); + Container container = await database.CreateContainerAsync(containerProperties); + Cosmos.PartitionKey pKey; + Cosmos.PartitionKey badPKey; + + //Create Items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await container.CreateItemAsync(doc1); + + //Document Read Test foreach (Document document in documents) { pKey = new PartitionKeyBuilder() @@ -3083,8 +3203,53 @@ public async Task VerifyDocumentCrudWithMultiHashKind() Assert.AreEqual(clientException.StatusCode, HttpStatusCode.BadRequest); } + } + catch (Exception) + { + Assert.Fail(); + } + finally + { + await database.DeleteAsync(); + HttpConstants.Versions.CurrentVersion = currentVersion; + } + } - //Read Many + [TestMethod] + public async Task SubpartitioningReadManyTest() + { + string currentVersion = HttpConstants.Versions.CurrentVersion; + HttpConstants.Versions.CurrentVersion = "2020-07-15"; + CosmosClient client = TestCommon.CreateCosmosClient(true); + Cosmos.Database database = null; + database = await client.CreateDatabaseIfNotExistsAsync("mydb"); + try + { + ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); + Container container = await database.CreateContainerAsync(containerProperties); + Cosmos.PartitionKey pKey; + + //Create Items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await container.CreateItemAsync(doc1); + + //Read Many Test List<(string, Cosmos.PartitionKey)> itemList = new List<(string, Cosmos.PartitionKey)>(); foreach (Document document in documents) { @@ -3111,8 +3276,55 @@ public async Task VerifyDocumentCrudWithMultiHashKind() Assert.IsNotNull(item.pk); } Assert.AreEqual(count, 3); + } + catch (Exception) + { + Assert.Fail(); + } + finally + { + await database.DeleteAsync(); + HttpConstants.Versions.CurrentVersion = currentVersion; + } + } - //Document Upsert + [TestMethod] + public async Task SubpartitioningUpsetItemTest() + { + string currentVersion = HttpConstants.Versions.CurrentVersion; + HttpConstants.Versions.CurrentVersion = "2020-07-15"; + CosmosClient client = TestCommon.CreateCosmosClient(true); + Cosmos.Database database = null; + database = await client.CreateDatabaseIfNotExistsAsync("mydb"); + try + { + ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); + Container container = await database.CreateContainerAsync(containerProperties); + Cosmos.PartitionKey pKey; + Cosmos.PartitionKey badPKey; + int count; + + //Create Items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await container.CreateItemAsync(doc1); + + //Document Upsert Test doc1 = new Document { Id = "document4" }; doc1.SetValue("ZipCode", "97756"); doc1.SetValue("Address", "Redmond"); @@ -3125,9 +3337,9 @@ public async Task VerifyDocumentCrudWithMultiHashKind() //insert check await container.UpsertItemAsync(doc1, pKey); - + Document readCheck = (await container.ReadItemAsync(doc1.Id, pKey)).Resource; - + Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); Assert.AreEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); @@ -3178,37 +3390,54 @@ await Assert.ThrowsExceptionAsync(() => Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); Assert.AreNotEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); + } + catch (Exception) + { + Assert.Fail(); + } + finally + { + await database.DeleteAsync(); + HttpConstants.Versions.CurrentVersion = currentVersion; + } + } - //Query - foreach (Document document in documents) - { - pKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("ZipCode")) - .Add(document.GetPropertyValue("Address")) - .Build(); - - String query = $"SELECT * from c where c.id = {document.GetPropertyValue("Id")}"; + [TestMethod] + public async Task SubpartitioningReplaceItemTest() + { + string currentVersion = HttpConstants.Versions.CurrentVersion; + HttpConstants.Versions.CurrentVersion = "2020-07-15"; + CosmosClient client = TestCommon.CreateCosmosClient(true); + Cosmos.Database database = null; + database = await client.CreateDatabaseIfNotExistsAsync("mydb"); + try + { + ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); + Container container = await database.CreateContainerAsync(containerProperties); + Cosmos.PartitionKey pKey; + Cosmos.PartitionKey badPKey; - using (FeedIterator feedIterator = this.Container.GetItemQueryIterator( - query, - null, - new QueryRequestOptions() { PartitionKey = pKey })) - { - Assert.IsTrue(feedIterator.HasMoreResults); + //Create items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await container.CreateItemAsync(doc1); - try - { - FeedResponse queryDoc = await feedIterator.ReadNextAsync(); - } - catch (Exception) - { - Assert.Fail("Should succeed"); - } - } + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await container.CreateItemAsync(doc1); - } + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await container.CreateItemAsync(doc1); - //Document Replace + //Document Replace Test foreach (Document document in documents) { pKey = new PartitionKeyBuilder() @@ -3231,38 +3460,85 @@ await Assert.ThrowsExceptionAsync(() => .Build(); readDocument.SetValue("Type", "Goverment"); - + await Assert.ThrowsExceptionAsync(() => container.ReplaceItemAsync(document, document.Id, partitionKey: badPKey) ); } + } + catch (Exception) + { + Assert.Fail(); + } + finally + { + await database.DeleteAsync(); + HttpConstants.Versions.CurrentVersion = currentVersion; + } + } - //Document Delete. - foreach (Document document in documents) - { - //Negative test - using incomplete partition key - badPKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("Address")) - .Build(); + [TestMethod] + public async Task SubpartitioningQueryItemTest() + { + string currentVersion = HttpConstants.Versions.CurrentVersion; + HttpConstants.Versions.CurrentVersion = "2020-07-15"; + CosmosClient client = TestCommon.CreateCosmosClient(true); + Cosmos.Database database = null; + database = await client.CreateDatabaseIfNotExistsAsync("mydb"); + try + { + ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); + Container container = await database.CreateContainerAsync(containerProperties); + Cosmos.PartitionKey pKey; - await Assert.ThrowsExceptionAsync(() => - container.DeleteItemAsync(document.Id, badPKey) - ); + //Create items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await container.CreateItemAsync(doc1); + //Query + foreach (Document document in documents) + { pKey = new PartitionKeyBuilder() .Add(document.GetPropertyValue("ZipCode")) .Add(document.GetPropertyValue("Address")) - .Build(); + .Build(); - Document readDocument = (await container.DeleteItemAsync(document.Id, pKey)).Resource; - - CosmosException clientException = await Assert.ThrowsExceptionAsync(() => - container.ReadItemAsync(document.Id, pKey) - ); + String query = $"SELECT * from c where c.id = {document.GetPropertyValue("Id")}"; - Assert.AreEqual(clientException.StatusCode, HttpStatusCode.NotFound); - } + using (FeedIterator feedIterator = this.Container.GetItemQueryIterator( + query, + null, + new QueryRequestOptions() { PartitionKey = pKey })) + { + Assert.IsTrue(feedIterator.HasMoreResults); + + try + { + FeedResponse queryDoc = await feedIterator.ReadNextAsync(); + } + catch (Exception) + { + Assert.Fail("Should succeed"); + } + } + } } catch (Exception) { @@ -3273,9 +3549,7 @@ await Assert.ThrowsExceptionAsync(() => await database.DeleteAsync(); HttpConstants.Versions.CurrentVersion = currentVersion; } - } - #endif private async Task AutoGenerateIdPatternTest(Cosmos.PartitionKey pk, T itemWithoutId) { From 020639350e184a6d15e0771973343697ed30c824 Mon Sep 17 00:00:00 2001 From: Nalu Tripician Date: Fri, 6 Jan 2023 17:18:28 -0500 Subject: [PATCH 5/6] Put MultiHash test into seperate test file --- .../CosmosContainerTests.cs | 1 + .../CosmosItemTests.cs | 546 ------------------ .../CosmosMultiHashTest.cs | 452 +++++++++++++++ 3 files changed, 453 insertions(+), 546 deletions(-) create mode 100644 Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosMultiHashTest.cs diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs index 0151cd424d..208b08ec11 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosContainerTests.cs @@ -795,6 +795,7 @@ public async Task CreateContainerIfNotExistsAsyncForMultiHashCollectionsTest() containerResponse = await containerResponse.Container.DeleteContainerAsync(); Assert.AreEqual(HttpStatusCode.NoContent, containerResponse.StatusCode); + } #endif diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs index fd3ad30b06..50bc71f276 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs @@ -27,14 +27,12 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests using Newtonsoft.Json; using Newtonsoft.Json.Linq; using JsonReader = Json.JsonReader; - using JsonSerializer = Json.JsonSerializer; using JsonWriter = Json.JsonWriter; using PartitionKey = Documents.PartitionKey; using static Microsoft.Azure.Cosmos.SDK.EmulatorTests.TransportClientHelper; using System.Reflection; using System.Text.RegularExpressions; using Microsoft.Azure.Cosmos.Diagnostics; - using static System.Net.Mime.MediaTypeNames; [TestClass] public class CosmosItemTests : BaseCosmosClientHelper @@ -3007,550 +3005,6 @@ public async Task HaLayerDoesNotThrowNullOnGoneExceptionTest() } } -#if PREVIEW - [TestMethod] - public async Task SubpartitioningCreateDocumentTest() - { - string currentVersion = HttpConstants.Versions.CurrentVersion; - HttpConstants.Versions.CurrentVersion = "2020-07-15"; - CosmosClient client = TestCommon.CreateCosmosClient(true); - Cosmos.Database database = null; - database = await client.CreateDatabaseIfNotExistsAsync("mydb"); - try - { - ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); - Container container = await database.CreateContainerAsync(containerProperties); - - //Document create test - ItemResponse[] documents = new ItemResponse[3]; - Document doc1 = new Document { Id = "document1" }; - doc1.SetValue("ZipCode", "500026"); - doc1.SetValue("Address", "Secunderabad"); - doc1.SetValue("Type", "Residence"); - documents[0] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document2" }; - doc1.SetValue("ZipCode", "15232"); - doc1.SetValue("Address", "Pittsburgh"); - doc1.SetValue("Type", "Business"); - documents[1] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document3" }; - doc1.SetValue("ZipCode", "11790"); - doc1.SetValue("Address", "Stonybrook"); - doc1.SetValue("Type", "Goverment"); - documents[2] = await container.CreateItemAsync(doc1); - - Assert.AreEqual(3, documents.Select(document => ((Document)document).SelfLink).Distinct().Count()); - - //Negative test - using incomplete partition key - { - doc1 = new Document { Id = "doc1" }; - doc1.SetValue("Zipcode", 11790); - - PartitionKeyBuilder pKValueList = new PartitionKeyBuilder(); - pKValueList.Add(doc1.GetPropertyValue("ZipCode")); - - Cosmos.PartitionKey pKeyErr = pKValueList.Build(); - ResponseMessage response = await this.Container.CreateItemStreamAsync(streamPayload: TestCommon.SerializerCore.ToStream(doc1), partitionKey: pKeyErr); - - Assert.IsNotNull(response); - Assert.IsNull(response.Content); - Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); - } - } - catch (Exception) - { - Assert.Fail(); - } - finally - { - await database.DeleteAsync(); - HttpConstants.Versions.CurrentVersion = currentVersion; - } - } - - [TestMethod] - public async Task SubpartitioningDeleteDocumentTest() - { - string currentVersion = HttpConstants.Versions.CurrentVersion; - HttpConstants.Versions.CurrentVersion = "2020-07-15"; - CosmosClient client = TestCommon.CreateCosmosClient(true); - Cosmos.Database database = null; - database = await client.CreateDatabaseIfNotExistsAsync("mydb"); - try - { - ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); - Container container = await database.CreateContainerAsync(containerProperties); - Cosmos.PartitionKey pKey; - Cosmos.PartitionKey badPKey; - - //Create Items for test - ItemResponse[] documents = new ItemResponse[3]; - Document doc1 = new Document { Id = "document1" }; - doc1.SetValue("ZipCode", "500026"); - doc1.SetValue("Address", "Secunderabad"); - doc1.SetValue("Type", "Residence"); - documents[0] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document2" }; - doc1.SetValue("ZipCode", "15232"); - doc1.SetValue("Address", "Pittsburgh"); - doc1.SetValue("Type", "Business"); - documents[1] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document3" }; - doc1.SetValue("ZipCode", "11790"); - doc1.SetValue("Address", "Stonybrook"); - doc1.SetValue("Type", "Goverment"); - documents[2] = await container.CreateItemAsync(doc1); - - //Document Delete Test - foreach (Document document in documents) - { - //Negative test - using incomplete partition key - badPKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("Address")) - .Build(); - - CosmosException deleteException = await Assert.ThrowsExceptionAsync(() => - container.DeleteItemAsync(document.Id, badPKey) - ); - - Assert.AreEqual(deleteException.StatusCode, HttpStatusCode.BadRequest); - - //Positive test - pKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("ZipCode")) - .Add(document.GetPropertyValue("Address")) - .Build(); - - Document readDocument = (await container.DeleteItemAsync(document.Id, pKey)).Resource; - - CosmosException clientException = await Assert.ThrowsExceptionAsync(() => - container.ReadItemAsync(document.Id, pKey) - ); - - Assert.AreEqual(clientException.StatusCode, HttpStatusCode.NotFound); - } - - } - catch (Exception) - { - Assert.Fail(); - } - finally - { - await database.DeleteAsync(); - HttpConstants.Versions.CurrentVersion = currentVersion; - } - } - - [TestMethod] - public async Task SubpartitioningReadItemTest() - { - string currentVersion = HttpConstants.Versions.CurrentVersion; - HttpConstants.Versions.CurrentVersion = "2020-07-15"; - CosmosClient client = TestCommon.CreateCosmosClient(true); - Cosmos.Database database = null; - database = await client.CreateDatabaseIfNotExistsAsync("mydb"); - try - { - ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); - Container container = await database.CreateContainerAsync(containerProperties); - Cosmos.PartitionKey pKey; - Cosmos.PartitionKey badPKey; - - //Create Items for test - ItemResponse[] documents = new ItemResponse[3]; - Document doc1 = new Document { Id = "document1" }; - doc1.SetValue("ZipCode", "500026"); - doc1.SetValue("Address", "Secunderabad"); - doc1.SetValue("Type", "Residence"); - documents[0] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document2" }; - doc1.SetValue("ZipCode", "15232"); - doc1.SetValue("Address", "Pittsburgh"); - doc1.SetValue("Type", "Business"); - documents[1] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document3" }; - doc1.SetValue("ZipCode", "11790"); - doc1.SetValue("Address", "Stonybrook"); - doc1.SetValue("Type", "Goverment"); - documents[2] = await container.CreateItemAsync(doc1); - - //Document Read Test - foreach (Document document in documents) - { - pKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("ZipCode")) - .Add(document.GetPropertyValue("Address")) - .Build(); - - Document readDocument = (await container.ReadItemAsync(document.Id, pKey)).Resource; - Assert.AreEqual(document.ToString(), readDocument.ToString()); - - //Negative test - using incomplete partition key - badPKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("Address")) - .Build(); - - CosmosException clientException = await Assert.ThrowsExceptionAsync(() => - container.ReadItemAsync(document.Id, badPKey) - ); - - Assert.AreEqual(clientException.StatusCode, HttpStatusCode.BadRequest); - } - } - catch (Exception) - { - Assert.Fail(); - } - finally - { - await database.DeleteAsync(); - HttpConstants.Versions.CurrentVersion = currentVersion; - } - } - - [TestMethod] - public async Task SubpartitioningReadManyTest() - { - string currentVersion = HttpConstants.Versions.CurrentVersion; - HttpConstants.Versions.CurrentVersion = "2020-07-15"; - CosmosClient client = TestCommon.CreateCosmosClient(true); - Cosmos.Database database = null; - database = await client.CreateDatabaseIfNotExistsAsync("mydb"); - try - { - ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); - Container container = await database.CreateContainerAsync(containerProperties); - Cosmos.PartitionKey pKey; - - //Create Items for test - ItemResponse[] documents = new ItemResponse[3]; - Document doc1 = new Document { Id = "document1" }; - doc1.SetValue("ZipCode", "500026"); - doc1.SetValue("Address", "Secunderabad"); - doc1.SetValue("Type", "Residence"); - documents[0] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document2" }; - doc1.SetValue("ZipCode", "15232"); - doc1.SetValue("Address", "Pittsburgh"); - doc1.SetValue("Type", "Business"); - documents[1] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document3" }; - doc1.SetValue("ZipCode", "11790"); - doc1.SetValue("Address", "Stonybrook"); - doc1.SetValue("Type", "Goverment"); - documents[2] = await container.CreateItemAsync(doc1); - - //Read Many Test - List<(string, Cosmos.PartitionKey)> itemList = new List<(string, Cosmos.PartitionKey)>(); - foreach (Document document in documents) - { - pKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("ZipCode")) - .Add(document.GetPropertyValue("Address")) - .Build(); - - itemList.Add((document.Id, pKey)); - } - - FeedResponse feedResponse = await container.ReadManyItemsAsync(itemList); - - Assert.IsNotNull(feedResponse); - Assert.AreEqual(feedResponse.Count, 3); - Assert.IsTrue(feedResponse.Headers.RequestCharge > 0); - Assert.IsNotNull(feedResponse.Diagnostics); - - int count = 0; - foreach (ToDoActivity item in feedResponse) - { - count++; - Assert.IsNotNull(item); - Assert.IsNotNull(item.pk); - } - Assert.AreEqual(count, 3); - } - catch (Exception) - { - Assert.Fail(); - } - finally - { - await database.DeleteAsync(); - HttpConstants.Versions.CurrentVersion = currentVersion; - } - } - - [TestMethod] - public async Task SubpartitioningUpsetItemTest() - { - string currentVersion = HttpConstants.Versions.CurrentVersion; - HttpConstants.Versions.CurrentVersion = "2020-07-15"; - CosmosClient client = TestCommon.CreateCosmosClient(true); - Cosmos.Database database = null; - database = await client.CreateDatabaseIfNotExistsAsync("mydb"); - try - { - ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); - Container container = await database.CreateContainerAsync(containerProperties); - Cosmos.PartitionKey pKey; - Cosmos.PartitionKey badPKey; - int count; - - //Create Items for test - ItemResponse[] documents = new ItemResponse[3]; - Document doc1 = new Document { Id = "document1" }; - doc1.SetValue("ZipCode", "500026"); - doc1.SetValue("Address", "Secunderabad"); - doc1.SetValue("Type", "Residence"); - documents[0] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document2" }; - doc1.SetValue("ZipCode", "15232"); - doc1.SetValue("Address", "Pittsburgh"); - doc1.SetValue("Type", "Business"); - documents[1] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document3" }; - doc1.SetValue("ZipCode", "11790"); - doc1.SetValue("Address", "Stonybrook"); - doc1.SetValue("Type", "Goverment"); - documents[2] = await container.CreateItemAsync(doc1); - - //Document Upsert Test - doc1 = new Document { Id = "document4" }; - doc1.SetValue("ZipCode", "97756"); - doc1.SetValue("Address", "Redmond"); - doc1.SetValue("Type", "Residence"); - - pKey = new PartitionKeyBuilder() - .Add(doc1.GetPropertyValue("ZipCode")) - .Add(doc1.GetPropertyValue("Address")) - .Build(); - - //insert check - await container.UpsertItemAsync(doc1, pKey); - - Document readCheck = (await container.ReadItemAsync(doc1.Id, pKey)).Resource; - - Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); - Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); - Assert.AreEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); - - doc1 = new Document { Id = "document4" }; - doc1.SetValue("ZipCode", "97756"); - doc1.SetValue("Address", "Redmond"); - doc1.SetValue("Type", "Business"); - - //update check - pKey = new PartitionKeyBuilder() - .Add(doc1.GetPropertyValue("ZipCode")) - .Add(doc1.GetPropertyValue("Address")) - .Build(); - - documents.Append>(await container.UpsertItemAsync(doc1, pKey)); - - readCheck = (await container.ReadItemAsync(doc1.Id, pKey)).Resource; - - Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); - Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); - Assert.AreEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); - - count = 0; - - foreach (Document doc in container.GetItemLinqQueryable(true)) - { - count++; - } - Assert.AreEqual(4, count); - - //Negative test - using incomplete partition key - doc1 = new Document { Id = "document4" }; - doc1.SetValue("ZipCode", "97756"); - doc1.SetValue("Address", "Redmond"); - doc1.SetValue("Type", "Residence"); - - badPKey = new PartitionKeyBuilder() - .Add(doc1.GetPropertyValue("ZipCode")) - .Build(); - - await Assert.ThrowsExceptionAsync(() => - container.UpsertItemAsync(doc1, badPKey) - ); - - readCheck = (await container.ReadItemAsync(doc1.Id, pKey)).Resource; - - Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); - Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); - Assert.AreNotEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); - } - catch (Exception) - { - Assert.Fail(); - } - finally - { - await database.DeleteAsync(); - HttpConstants.Versions.CurrentVersion = currentVersion; - } - } - - [TestMethod] - public async Task SubpartitioningReplaceItemTest() - { - string currentVersion = HttpConstants.Versions.CurrentVersion; - HttpConstants.Versions.CurrentVersion = "2020-07-15"; - CosmosClient client = TestCommon.CreateCosmosClient(true); - Cosmos.Database database = null; - database = await client.CreateDatabaseIfNotExistsAsync("mydb"); - try - { - ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); - Container container = await database.CreateContainerAsync(containerProperties); - Cosmos.PartitionKey pKey; - Cosmos.PartitionKey badPKey; - - //Create items for test - ItemResponse[] documents = new ItemResponse[3]; - Document doc1 = new Document { Id = "document1" }; - doc1.SetValue("ZipCode", "500026"); - doc1.SetValue("Address", "Secunderabad"); - doc1.SetValue("Type", "Residence"); - documents[0] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document2" }; - doc1.SetValue("ZipCode", "15232"); - doc1.SetValue("Address", "Pittsburgh"); - doc1.SetValue("Type", "Business"); - documents[1] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document3" }; - doc1.SetValue("ZipCode", "11790"); - doc1.SetValue("Address", "Stonybrook"); - doc1.SetValue("Type", "Goverment"); - documents[2] = await container.CreateItemAsync(doc1); - - //Document Replace Test - foreach (Document document in documents) - { - pKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("ZipCode")) - .Add(document.GetPropertyValue("Address")) - .Build(); - - - Document readDocument = (await container.ReadItemAsync(document.Id, pKey)).Resource; - readDocument.SetValue("Type", "Park"); - - ItemResponse item = await container.ReplaceItemAsync(readDocument, readDocument.Id, pKey); - - Document checkDocument = (await container.ReadItemAsync(document.Id, pKey)).Resource; - Assert.AreEqual(checkDocument.GetPropertyValue("Type"), readDocument.GetPropertyValue("Type")); - - //Negative test - using incomplete partition key - badPKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("Address")) - .Build(); - - readDocument.SetValue("Type", "Goverment"); - - await Assert.ThrowsExceptionAsync(() => - container.ReplaceItemAsync(document, document.Id, partitionKey: badPKey) - ); - } - } - catch (Exception) - { - Assert.Fail(); - } - finally - { - await database.DeleteAsync(); - HttpConstants.Versions.CurrentVersion = currentVersion; - } - } - - [TestMethod] - public async Task SubpartitioningQueryItemTest() - { - string currentVersion = HttpConstants.Versions.CurrentVersion; - HttpConstants.Versions.CurrentVersion = "2020-07-15"; - CosmosClient client = TestCommon.CreateCosmosClient(true); - Cosmos.Database database = null; - database = await client.CreateDatabaseIfNotExistsAsync("mydb"); - try - { - ContainerProperties containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); - Container container = await database.CreateContainerAsync(containerProperties); - Cosmos.PartitionKey pKey; - - //Create items for test - ItemResponse[] documents = new ItemResponse[3]; - Document doc1 = new Document { Id = "document1" }; - doc1.SetValue("ZipCode", "500026"); - doc1.SetValue("Address", "Secunderabad"); - doc1.SetValue("Type", "Residence"); - documents[0] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document2" }; - doc1.SetValue("ZipCode", "15232"); - doc1.SetValue("Address", "Pittsburgh"); - doc1.SetValue("Type", "Business"); - documents[1] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document3" }; - doc1.SetValue("ZipCode", "11790"); - doc1.SetValue("Address", "Stonybrook"); - doc1.SetValue("Type", "Goverment"); - documents[2] = await container.CreateItemAsync(doc1); - - //Query - foreach (Document document in documents) - { - pKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("ZipCode")) - .Add(document.GetPropertyValue("Address")) - .Build(); - - String query = $"SELECT * from c where c.id = {document.GetPropertyValue("Id")}"; - - using (FeedIterator feedIterator = this.Container.GetItemQueryIterator( - query, - null, - new QueryRequestOptions() { PartitionKey = pKey })) - { - Assert.IsTrue(feedIterator.HasMoreResults); - - try - { - FeedResponse queryDoc = await feedIterator.ReadNextAsync(); - } - catch (Exception) - { - Assert.Fail("Should succeed"); - } - } - - } - } - catch (Exception) - { - Assert.Fail(); - } - finally - { - await database.DeleteAsync(); - HttpConstants.Versions.CurrentVersion = currentVersion; - } - } -#endif private async Task AutoGenerateIdPatternTest(Cosmos.PartitionKey pk, T itemWithoutId) { string autoId = Guid.NewGuid().ToString(); diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosMultiHashTest.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosMultiHashTest.cs new file mode 100644 index 0000000000..e25d65b527 --- /dev/null +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosMultiHashTest.cs @@ -0,0 +1,452 @@ +#if PREVIEW +namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net; + using System.Threading.Tasks; + using Microsoft.Azure.Documents; + using Microsoft.Azure.Cosmos; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class CosmosMultiHashTest + { + private Cosmos.Database database = null; + + private Container container = null; + private ContainerProperties containerProperties = null; + + private readonly string currentVersion = HttpConstants.Versions.CurrentVersion; + + + [TestInitialize] + public async Task TestInitialize() + { + HttpConstants.Versions.CurrentVersion = "2020-07-15"; + CosmosClient client = TestCommon.CreateCosmosClient(true); + this.database = await client.CreateDatabaseIfNotExistsAsync("mydb"); + + this.containerProperties = new ContainerProperties("mycoll", new List { "/ZipCode", "/Address" }); + this.container = await this.database.CreateContainerAsync(this.containerProperties); + } + + [TestCleanup] + public async Task Cleanup() + { + await this.database.DeleteAsync(); + HttpConstants.Versions.CurrentVersion = this.currentVersion; + } + + [TestMethod] + public async Task MultiHashCreateDocumentTest() + { + //Document create test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await this.container.CreateItemAsync(doc1); + + Assert.AreEqual(3, documents.Select(document => ((Document)document).SelfLink).Distinct().Count()); + + //Negative test - using incomplete partition key + Cosmos.PartitionKey badPKey; + + foreach (Document document in documents) + { + badPKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("Address")) + .Build(); + + document.Id += "Bad"; + + ArgumentException createException = await Assert.ThrowsExceptionAsync(() => + this.container.CreateItemAsync(document, badPKey) + ); + } + } + + [TestMethod] + public async Task MultiHashDeleteDocumentTest() + { + Cosmos.PartitionKey pKey; + Cosmos.PartitionKey badPKey; + + //Create Items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await this.container.CreateItemAsync(doc1); + + //Document Delete Test + foreach (Document document in documents) + { + //Negative test - using incomplete partition key + badPKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("Address")) + .Build(); + + CosmosException deleteException = await Assert.ThrowsExceptionAsync(() => + this.container.DeleteItemAsync(document.Id, badPKey) + ); + + Assert.AreEqual(deleteException.StatusCode, HttpStatusCode.BadRequest); + + //Positive test + pKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("ZipCode")) + .Add(document.GetPropertyValue("Address")) + .Build(); + + Document readDocument = (await this.container.DeleteItemAsync(document.Id, pKey)).Resource; + + CosmosException clientException = await Assert.ThrowsExceptionAsync(() => + this.container.ReadItemAsync(document.Id, pKey) + ); + + Assert.AreEqual(clientException.StatusCode, HttpStatusCode.NotFound); + } + } + + [TestMethod] + public async Task MultiHashReadItemTest() + { + Cosmos.PartitionKey pKey; + Cosmos.PartitionKey badPKey; + + //Create Items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await this.container.CreateItemAsync(doc1); + + //Document Read Test + foreach (Document document in documents) + { + pKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("ZipCode")) + .Add(document.GetPropertyValue("Address")) + .Build(); + + Document readDocument = (await this.container.ReadItemAsync(document.Id, pKey)).Resource; + Assert.AreEqual(document.ToString(), readDocument.ToString()); + + //Negative test - using incomplete partition key + badPKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("Address")) + .Build(); + + CosmosException clientException = await Assert.ThrowsExceptionAsync(() => + this.container.ReadItemAsync(document.Id, badPKey) + ); + + Assert.AreEqual(clientException.StatusCode, HttpStatusCode.BadRequest); + } + } + + [TestMethod] + public async Task MultiHashReadManyTest() + { + Cosmos.PartitionKey pKey; + + //Create Items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await this.container.CreateItemAsync(doc1); + + //Read Many Test + List<(string, Cosmos.PartitionKey)> itemList = new List<(string, Cosmos.PartitionKey)>(); + foreach (Document document in documents) + { + pKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("ZipCode")) + .Add(document.GetPropertyValue("Address")) + .Build(); + + itemList.Add((document.Id, pKey)); + } + + FeedResponse feedResponse = await this.container.ReadManyItemsAsync(itemList); + + Assert.IsNotNull(feedResponse); + Assert.AreEqual(feedResponse.Count, 3); + Assert.IsTrue(feedResponse.Headers.RequestCharge > 0); + Assert.IsNotNull(feedResponse.Diagnostics); + + int count = 0; + foreach (ToDoActivity item in feedResponse) + { + count++; + Assert.IsNotNull(item); + Assert.IsNotNull(item.pk); + } + Assert.AreEqual(count, 3); + } + + [TestMethod] + public async Task MultiHashUpsetItemTest() + { + Cosmos.PartitionKey pKey; + Cosmos.PartitionKey badPKey; + int count; + + //Create Items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await this.container.CreateItemAsync(doc1); + + //Document Upsert Test + doc1 = new Document { Id = "document4" }; + doc1.SetValue("ZipCode", "97756"); + doc1.SetValue("Address", "Redmond"); + doc1.SetValue("Type", "Residence"); + + pKey = new PartitionKeyBuilder() + .Add(doc1.GetPropertyValue("ZipCode")) + .Add(doc1.GetPropertyValue("Address")) + .Build(); + + //insert check + await this.container.UpsertItemAsync(doc1, pKey); + + Document readCheck = (await this.container.ReadItemAsync(doc1.Id, pKey)).Resource; + + Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); + Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); + Assert.AreEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); + + doc1 = new Document { Id = "document4" }; + doc1.SetValue("ZipCode", "97756"); + doc1.SetValue("Address", "Redmond"); + doc1.SetValue("Type", "Business"); + + //update check + pKey = new PartitionKeyBuilder() + .Add(doc1.GetPropertyValue("ZipCode")) + .Add(doc1.GetPropertyValue("Address")) + .Build(); + + documents.Append>(await this.container.UpsertItemAsync(doc1, pKey)); + + readCheck = (await this.container.ReadItemAsync(doc1.Id, pKey)).Resource; + + Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); + Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); + Assert.AreEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); + + count = 0; + + foreach (Document doc in this.container.GetItemLinqQueryable(true)) + { + count++; + } + Assert.AreEqual(4, count); + + //Negative test - using incomplete partition key + doc1 = new Document { Id = "document4" }; + doc1.SetValue("ZipCode", "97756"); + doc1.SetValue("Address", "Redmond"); + doc1.SetValue("Type", "Residence"); + + badPKey = new PartitionKeyBuilder() + .Add(doc1.GetPropertyValue("ZipCode")) + .Build(); + + await Assert.ThrowsExceptionAsync(() => + this.container.UpsertItemAsync(doc1, badPKey) + ); + + readCheck = (await this.container.ReadItemAsync(doc1.Id, pKey)).Resource; + + Assert.AreEqual(doc1.GetPropertyValue("ZipCode"), readCheck.GetPropertyValue("ZipCode")); + Assert.AreEqual(doc1.GetPropertyValue("Address"), readCheck.GetPropertyValue("Address")); + Assert.AreNotEqual(doc1.GetPropertyValue("Type"), readCheck.GetPropertyValue("Type")); + } + + [TestMethod] + public async Task MultiHashReplaceItemTest() + { + Cosmos.PartitionKey pKey; + Cosmos.PartitionKey badPKey; + + //Create items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await this.container.CreateItemAsync(doc1); + + //Document Replace Test + foreach (Document document in documents) + { + pKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("ZipCode")) + .Add(document.GetPropertyValue("Address")) + .Build(); + + + Document readDocument = (await this.container.ReadItemAsync(document.Id, pKey)).Resource; + readDocument.SetValue("Type", "Park"); + + ItemResponse item = await this.container.ReplaceItemAsync(readDocument, readDocument.Id, pKey); + + Document checkDocument = (await this.container.ReadItemAsync(document.Id, pKey)).Resource; + Assert.AreEqual(checkDocument.GetPropertyValue("Type"), readDocument.GetPropertyValue("Type")); + + //Negative test - using incomplete partition key + badPKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("Address")) + .Build(); + + readDocument.SetValue("Type", "Goverment"); + + await Assert.ThrowsExceptionAsync(() => + this.container.ReplaceItemAsync(document, document.Id, partitionKey: badPKey) + ); + } + } + + [TestMethod] + public async Task MultiHashQueryItemTest() + { + Cosmos.PartitionKey pKey; + + //Create items for test + ItemResponse[] documents = new ItemResponse[3]; + Document doc1 = new Document { Id = "document1" }; + doc1.SetValue("ZipCode", "500026"); + doc1.SetValue("Address", "Secunderabad"); + doc1.SetValue("Type", "Residence"); + documents[0] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document2" }; + doc1.SetValue("ZipCode", "15232"); + doc1.SetValue("Address", "Pittsburgh"); + doc1.SetValue("Type", "Business"); + documents[1] = await this.container.CreateItemAsync(doc1); + + doc1 = new Document { Id = "document3" }; + doc1.SetValue("ZipCode", "11790"); + doc1.SetValue("Address", "Stonybrook"); + doc1.SetValue("Type", "Goverment"); + documents[2] = await this.container.CreateItemAsync(doc1); + + //Query + foreach (Document document in documents) + { + pKey = new PartitionKeyBuilder() + .Add(document.GetPropertyValue("ZipCode")) + .Add(document.GetPropertyValue("Address")) + .Build(); + + String query = $"SELECT * from c where c.id = {document.GetPropertyValue("Id")}"; + + using (FeedIterator feedIterator = this.container.GetItemQueryIterator( + query, + null, + new QueryRequestOptions() { PartitionKey = pKey })) + { + Assert.IsTrue(feedIterator.HasMoreResults); + + try + { + FeedResponse queryDoc = await feedIterator.ReadNextAsync(); + } + catch (Exception) + { + Assert.Fail("Should succeed"); + } + } + + } + } + + } +} +#endif From bd66debaacd3a12ab205dd0ae43d6f292023dc99 Mon Sep 17 00:00:00 2001 From: Nalu Tripician Date: Mon, 9 Jan 2023 12:49:47 -0500 Subject: [PATCH 6/6] nit --- .../CosmosMultiHashTest.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosMultiHashTest.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosMultiHashTest.cs index e25d65b527..d069e87935 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosMultiHashTest.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosMultiHashTest.cs @@ -434,14 +434,7 @@ public async Task MultiHashQueryItemTest() { Assert.IsTrue(feedIterator.HasMoreResults); - try - { - FeedResponse queryDoc = await feedIterator.ReadNextAsync(); - } - catch (Exception) - { - Assert.Fail("Should succeed"); - } + FeedResponse queryDoc = await feedIterator.ReadNextAsync(); } }