Skip to content

Commit

Permalink
test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NaluTripician committed Feb 3, 2023
1 parent ff71ffe commit 9b531c6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task TestInitialize()
CosmosClient client = TestCommon.CreateCosmosClient(true);
this.database = await client.CreateDatabaseIfNotExistsAsync("mydb");

this.containerProperties = new ContainerProperties("mycoll", new List<string> { "/ZipCode", "/Address" });
this.containerProperties = new ContainerProperties("mycoll", new List<string> { "/ZipCode", "/City" });
this.container = await this.database.CreateContainerAsync(this.containerProperties);
}

Expand All @@ -42,24 +42,37 @@ public async Task Cleanup()
[TestMethod]
public async Task MultiHashCreateDocumentTest()
{
Cosmos.PartitionKey pKey;
//Document create test
ItemResponse<Document>[] documents = new ItemResponse<Document>[3];
Document doc = new Document { Id = "document1" };
doc.SetValue("ZipCode", "500026");
doc.SetValue("Address", "Secunderabad");
doc.SetValue("City", "Secunderabad");
doc.SetValue("Type", "Residence");
documents[0] = await this.container.CreateItemAsync<Document>(doc);
pKey= new PartitionKeyBuilder()
.Add(doc.GetPropertyValue<string>("ZipCode"))
.Add(doc.GetPropertyValue<string>("City"))
.Build();
documents[0] = await this.container.CreateItemAsync<Document>(doc, pKey);

doc = new Document { Id = "document2" };
doc.SetValue("ZipCode", "15232");
doc.SetValue("Address", "Pittsburgh");
doc.SetValue("City", "Pittsburgh");
doc.SetValue("Type", "Business");
pKey = new PartitionKeyBuilder()
.Add(doc.GetPropertyValue<string>("ZipCode"))
.Add(doc.GetPropertyValue<string>("City"))
.Build();
documents[1] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document3" };
doc.SetValue("ZipCode", "11790");
doc.SetValue("Address", "Stonybrook");
doc.SetValue("City", "Stonybrook");
doc.SetValue("Type", "Goverment");
pKey = new PartitionKeyBuilder()
.Add(doc.GetPropertyValue<string>("ZipCode"))
.Add(doc.GetPropertyValue<string>("City"))
.Build();
documents[2] = await this.container.CreateItemAsync<Document>(doc);

Assert.AreEqual(3, documents.Select(document => ((Document)document).SelfLink).Distinct().Count());
Expand Down Expand Up @@ -93,19 +106,19 @@ public async Task MultiHashDeleteDocumentTest()
ItemResponse<Document>[] documents = new ItemResponse<Document>[3];
Document doc = new Document { Id = "document1" };
doc.SetValue("ZipCode", "500026");
doc.SetValue("Address", "Secunderabad");
doc.SetValue("City", "Secunderabad");
doc.SetValue("Type", "Residence");
documents[0] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document2" };
doc.SetValue("ZipCode", "15232");
doc.SetValue("Address", "Pittsburgh");
doc.SetValue("City", "Pittsburgh");
doc.SetValue("Type", "Business");
documents[1] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document3" };
doc.SetValue("ZipCode", "11790");
doc.SetValue("Address", "Stonybrook");
doc.SetValue("City", "Stonybrook");
doc.SetValue("Type", "Goverment");
documents[2] = await this.container.CreateItemAsync<Document>(doc);

Expand All @@ -126,7 +139,7 @@ public async Task MultiHashDeleteDocumentTest()
//Positive test
pKey = new PartitionKeyBuilder()
.Add(document.GetPropertyValue<string>("ZipCode"))
.Add(document.GetPropertyValue<string>("Address"))
.Add(document.GetPropertyValue<string>("City"))
.Build();

Document deleteDocument = (await this.container.DeleteItemAsync<Document>(document.Id, pKey)).Resource;
Expand All @@ -149,19 +162,19 @@ public async Task MultiHashReadItemTest()
ItemResponse<Document>[] documents = new ItemResponse<Document>[3];
Document doc = new Document { Id = "document1" };
doc.SetValue("ZipCode", "500026");
doc.SetValue("Address", "Secunderabad");
doc.SetValue("City", "Secunderabad");
doc.SetValue("Type", "Residence");
documents[0] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document2" };
doc.SetValue("ZipCode", "15232");
doc.SetValue("Address", "Pittsburgh");
doc.SetValue("City", "Pittsburgh");
doc.SetValue("Type", "Business");
documents[1] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document3" };
doc.SetValue("ZipCode", "11790");
doc.SetValue("Address", "Stonybrook");
doc.SetValue("City", "Stonybrook");
doc.SetValue("Type", "Goverment");
documents[2] = await this.container.CreateItemAsync<Document>(doc);

Expand All @@ -170,7 +183,7 @@ public async Task MultiHashReadItemTest()
{
pKey = new PartitionKeyBuilder()
.Add(document.GetPropertyValue<string>("ZipCode"))
.Add(document.GetPropertyValue<string>("Address"))
.Add(document.GetPropertyValue<string>("City"))
.Build();

Document readDocument = (await this.container.ReadItemAsync<Document>(document.Id, pKey)).Resource;
Expand Down Expand Up @@ -199,19 +212,19 @@ public async Task MultiHashReadManyTest()
ItemResponse<Document>[] documents = new ItemResponse<Document>[3];
Document doc = new Document { Id = "document1" };
doc.SetValue("ZipCode", "500026");
doc.SetValue("Address", "Secunderabad");
doc.SetValue("City", "Secunderabad");
doc.SetValue("Type", "Residence");
documents[0] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document2" };
doc.SetValue("ZipCode", "15232");
doc.SetValue("Address", "Pittsburgh");
doc.SetValue("City", "Pittsburgh");
doc.SetValue("Type", "Business");
documents[1] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document3" };
doc.SetValue("ZipCode", "11790");
doc.SetValue("Address", "Stonybrook");
doc.SetValue("City", "Stonybrook");
doc.SetValue("Type", "Goverment");
documents[2] = await this.container.CreateItemAsync<Document>(doc);

Expand All @@ -222,7 +235,7 @@ public async Task MultiHashReadManyTest()
{
pKey = new PartitionKeyBuilder()
.Add(document.GetPropertyValue<string>("ZipCode"))
.Add(document.GetPropertyValue<string>("Address"))
.Add(document.GetPropertyValue<string>("City"))
.Build();

badPKey = new PartitionKeyBuilder()
Expand Down Expand Up @@ -269,31 +282,31 @@ public async Task MultiHashUpsertItemTest()
ItemResponse<Document>[] documents = new ItemResponse<Document>[3];
Document doc = new Document { Id = "document1" };
doc.SetValue("ZipCode", "500026");
doc.SetValue("Address", "Secunderabad");
doc.SetValue("City", "Secunderabad");
doc.SetValue("Type", "Residence");
documents[0] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document2" };
doc.SetValue("ZipCode", "15232");
doc.SetValue("Address", "Pittsburgh");
doc.SetValue("City", "Pittsburgh");
doc.SetValue("Type", "Business");
documents[1] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document3" };
doc.SetValue("ZipCode", "11790");
doc.SetValue("Address", "Stonybrook");
doc.SetValue("City", "Stonybrook");
doc.SetValue("Type", "Goverment");
documents[2] = await this.container.CreateItemAsync<Document>(doc);

//Document Upsert Test
doc = new Document { Id = "document4" };
doc.SetValue("ZipCode", "97756");
doc.SetValue("Address", "Redmond");
doc.SetValue("City", "Redmond");
doc.SetValue("Type", "Residence");

pKey = new PartitionKeyBuilder()
.Add(doc.GetPropertyValue<string>("ZipCode"))
.Add(doc.GetPropertyValue<string>("Address"))
.Add(doc.GetPropertyValue<string>("City"))
.Build();

//insert check
Expand All @@ -302,26 +315,26 @@ public async Task MultiHashUpsertItemTest()
Document readCheck = (await this.container.ReadItemAsync<Document>(doc.Id, pKey)).Resource;

Assert.AreEqual(doc.GetPropertyValue<string>("ZipCode"), readCheck.GetPropertyValue<string>("ZipCode"));
Assert.AreEqual(doc.GetPropertyValue<string>("Address"), readCheck.GetPropertyValue<string>("Address"));
Assert.AreEqual(doc.GetPropertyValue<string>("City"), readCheck.GetPropertyValue<string>("City"));
Assert.AreEqual(doc.GetPropertyValue<string>("Type"), readCheck.GetPropertyValue<string>("Type"));

doc = new Document { Id = "document4" };
doc.SetValue("ZipCode", "97756");
doc.SetValue("Address", "Redmond");
doc.SetValue("City", "Redmond");
doc.SetValue("Type", "Business");

//update check
pKey = new PartitionKeyBuilder()
.Add(doc.GetPropertyValue<string>("ZipCode"))
.Add(doc.GetPropertyValue<string>("Address"))
.Add(doc.GetPropertyValue<string>("City"))
.Build();

documents.Append<ItemResponse<Document>>(await this.container.UpsertItemAsync<Document>(doc, pKey));

readCheck = (await this.container.ReadItemAsync<Document>(doc.Id, pKey)).Resource;

Assert.AreEqual(doc.GetPropertyValue<string>("ZipCode"), readCheck.GetPropertyValue<string>("ZipCode"));
Assert.AreEqual(doc.GetPropertyValue<string>("Address"), readCheck.GetPropertyValue<string>("Address"));
Assert.AreEqual(doc.GetPropertyValue<string>("City"), readCheck.GetPropertyValue<string>("City"));
Assert.AreEqual(doc.GetPropertyValue<string>("Type"), readCheck.GetPropertyValue<string>("Type"));

count = 0;
Expand All @@ -335,7 +348,7 @@ public async Task MultiHashUpsertItemTest()
//Negative test - using incomplete partition key
doc = new Document { Id = "document4" };
doc.SetValue("ZipCode", "97756");
doc.SetValue("Address", "Redmond");
doc.SetValue("City", "Redmond");
doc.SetValue("Type", "Residence");

badPKey = new PartitionKeyBuilder()
Expand All @@ -351,7 +364,7 @@ public async Task MultiHashUpsertItemTest()
readCheck = (await this.container.ReadItemAsync<Document>(doc.Id, pKey)).Resource;

Assert.AreEqual(doc.GetPropertyValue<string>("ZipCode"), readCheck.GetPropertyValue<string>("ZipCode"));
Assert.AreEqual(doc.GetPropertyValue<string>("Address"), readCheck.GetPropertyValue<string>("Address"));
Assert.AreEqual(doc.GetPropertyValue<string>("City"), readCheck.GetPropertyValue<string>("City"));
Assert.AreNotEqual(doc.GetPropertyValue<string>("Type"), readCheck.GetPropertyValue<string>("Type"));
}

Expand All @@ -365,19 +378,19 @@ public async Task MultiHashReplaceItemTest()
ItemResponse<Document>[] documents = new ItemResponse<Document>[3];
Document doc = new Document { Id = "document1" };
doc.SetValue("ZipCode", "500026");
doc.SetValue("Address", "Secunderabad");
doc.SetValue("City", "Secunderabad");
doc.SetValue("Type", "Residence");
documents[0] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document2" };
doc.SetValue("ZipCode", "15232");
doc.SetValue("Address", "Pittsburgh");
doc.SetValue("City", "Pittsburgh");
doc.SetValue("Type", "Business");
documents[1] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document3" };
doc.SetValue("ZipCode", "11790");
doc.SetValue("Address", "Stonybrook");
doc.SetValue("City", "Stonybrook");
doc.SetValue("Type", "Goverment");
documents[2] = await this.container.CreateItemAsync<Document>(doc);

Expand All @@ -386,7 +399,7 @@ public async Task MultiHashReplaceItemTest()
{
pKey = new PartitionKeyBuilder()
.Add(document.GetPropertyValue<string>("ZipCode"))
.Add(document.GetPropertyValue<string>("Address"))
.Add(document.GetPropertyValue<string>("City"))
.Build();


Expand Down Expand Up @@ -423,19 +436,19 @@ public async Task MultiHashQueryItemTest()
ItemResponse<Document>[] documents = new ItemResponse<Document>[3];
Document doc = new Document { Id = "document1" };
doc.SetValue("ZipCode", "500026");
doc.SetValue("Address", "Secunderabad");
doc.SetValue("City", "Secunderabad");
doc.SetValue("Type", "Residence");
documents[0] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document2" };
doc.SetValue("ZipCode", "15232");
doc.SetValue("Address", "Pittsburgh");
doc.SetValue("City", "Pittsburgh");
doc.SetValue("Type", "Business");
documents[1] = await this.container.CreateItemAsync<Document>(doc);

doc = new Document { Id = "document3" };
doc.SetValue("ZipCode", "11790");
doc.SetValue("Address", "Stonybrook");
doc.SetValue("City", "Stonybrook");
doc.SetValue("Type", "Goverment");
documents[2] = await this.container.CreateItemAsync<Document>(doc);

Expand All @@ -444,7 +457,7 @@ public async Task MultiHashQueryItemTest()
{
pKey = new PartitionKeyBuilder()
.Add(document.GetPropertyValue<string>("ZipCode"))
.Add(document.GetPropertyValue<string>("Address"))
.Add(document.GetPropertyValue<string>("City"))
.Build();

badPKey = new PartitionKeyBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Documents;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Database = Database;
using PartitionKey = PartitionKey;
using PartitionKeyDefinitionVersion = PartitionKeyDefinitionVersion;

/// <summary>
/// Testing Prefix and Full Partition for <see cref="ChangeFeed"/>, <see cref="Query"/> against a <see cref="Container"/> with Hierarchical Partition Keys.
Expand All @@ -20,10 +25,13 @@ public class FeedRangeCreateFromPartitionKeyAsyncEmulatorTests
private CosmosClient cosmosClient = null;
private Database cosmosDatabase = null;

private readonly string currentVersion = HttpConstants.Versions.CurrentVersion;

[TestInitialize]
public async Task TestInit()
{
this.cosmosClient = TestCommon.CreateCosmosClient();
HttpConstants.Versions.CurrentVersion = "2020-07-15";
this.cosmosClient = TestCommon.CreateCosmosClient(true);

string databaseName = Guid.NewGuid().ToString();
DatabaseResponse cosmosDatabaseResponse = await this.cosmosClient.CreateDatabaseIfNotExistsAsync(databaseName);
Expand All @@ -41,6 +49,7 @@ public async Task TestCleanup()
if (this.cosmosDatabase != null)
{
await this.cosmosDatabase.DeleteStreamAsync();
HttpConstants.Versions.CurrentVersion = this.currentVersion;
}
this.cosmosClient.Dispose();
}
Expand Down

0 comments on commit 9b531c6

Please sign in to comment.