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..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,7 +27,6 @@ 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; @@ -3006,115 +3005,6 @@ public async Task HaLayerDoesNotThrowNullOnGoneExceptionTest() } } -#if PREVIEW - [TestMethod] - public async Task VerifyDocumentCrudWithMultiHashKind() - { - 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. - ItemResponse[] documents = new ItemResponse[3]; - Document doc1 = new Document { Id = "document1" }; - doc1.SetValue("ZipCode", "500026"); - doc1.SetValue("Address", "Secunderabad"); - documents[0] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document2" }; - doc1.SetValue("ZipCode", "15232"); - doc1.SetValue("Address", "Pittsburgh"); - documents[1] = await container.CreateItemAsync(doc1); - - doc1 = new Document { Id = "document3" }; - doc1.SetValue("ZipCode", "11790"); - doc1.SetValue("Address", "Stonybrook"); - documents[2] = await container.CreateItemAsync(doc1); - - Assert.AreEqual(3, documents.Select(document => ((Document)document).SelfLink).Distinct().Count()); - - //Negative test - { - 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); - } - - //Document Read. - foreach (Document document in documents) - { - Cosmos.PartitionKey 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()); - } - - //Document Update. - foreach (ItemResponse obj in documents) - { - Cosmos.PartitionKey pKey = new PartitionKeyBuilder() - .Add(obj.Resource.GetValue("ZipCode")) - .Add(obj.Resource.GetPropertyValue("Address")) - .Build(); - - Document document = (await container.ReadItemAsync(obj.Resource.Id, pKey)).Resource; - document.SetPropertyValue("Name", document.Id); - - Document readDocument = (await container.ReplaceItemAsync(document, document.Id, pKey)).Resource; - Assert.AreEqual(readDocument.GetValue("Name"), document.GetValue("Name")); - } - - //Document Delete. - foreach (Document document in documents) - { - Cosmos.PartitionKey pKey = new PartitionKeyBuilder() - .Add(document.GetPropertyValue("ZipCode")) - .Add(document.GetPropertyValue("Address")) - .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); - } - } - - } - 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..d069e87935 --- /dev/null +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosMultiHashTest.cs @@ -0,0 +1,445 @@ +#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); + + FeedResponse queryDoc = await feedIterator.ReadNextAsync(); + } + + } + } + + } +} +#endif