Skip to content

Commit

Permalink
[Internal] Subpartitioning: Adds updates to test coverage for subpart…
Browse files Browse the repository at this point in the history
…itioning (#3618)

* updates to test coverage for subpartitioning

* bug fixes

* now useses Assert.ThrowsException

* Seperated into multiple tests for clarity

* Put MultiHash test into seperate test file

* nit
  • Loading branch information
NaluTripician committed Jan 9, 2023
1 parent 77e3aa4 commit 3c875c7
Show file tree
Hide file tree
Showing 2 changed files with 445 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string> { "/ZipCode", "/Address" });
Container container = await database.CreateContainerAsync(containerProperties);

//Document create.
ItemResponse<Document>[] documents = new ItemResponse<Document>[3];
Document doc1 = new Document { Id = "document1" };
doc1.SetValue("ZipCode", "500026");
doc1.SetValue("Address", "Secunderabad");
documents[0] = await container.CreateItemAsync<Document>(doc1);

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

doc1 = new Document { Id = "document3" };
doc1.SetValue("ZipCode", "11790");
doc1.SetValue("Address", "Stonybrook");
documents[2] = await container.CreateItemAsync<Document>(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<long>("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<string>("ZipCode"))
.Add(document.GetPropertyValue<string>("Address"))
.Build();

Document readDocument = (await container.ReadItemAsync<Document>(document.Id, pKey)).Resource;
Assert.AreEqual(document.ToString(), readDocument.ToString());
}

//Document Update.
foreach (ItemResponse<Document> obj in documents)
{
Cosmos.PartitionKey pKey = new PartitionKeyBuilder()
.Add(obj.Resource.GetValue<string>("ZipCode"))
.Add(obj.Resource.GetPropertyValue<string>("Address"))
.Build();

Document document = (await container.ReadItemAsync<Document>(obj.Resource.Id, pKey)).Resource;
document.SetPropertyValue("Name", document.Id);

Document readDocument = (await container.ReplaceItemAsync<Document>(document, document.Id, pKey)).Resource;
Assert.AreEqual(readDocument.GetValue<string>("Name"), document.GetValue<string>("Name"));
}

//Document Delete.
foreach (Document document in documents)
{
Cosmos.PartitionKey pKey = new PartitionKeyBuilder()
.Add(document.GetPropertyValue<string>("ZipCode"))
.Add(document.GetPropertyValue<string>("Address"))
.Build();

Document readDocument = (await container.DeleteItemAsync<Document>(document.Id, pKey)).Resource;
try
{
readDocument = await container.ReadItemAsync<Document>(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<T> AutoGenerateIdPatternTest<T>(Cosmos.PartitionKey pk, T itemWithoutId)
{
string autoId = Guid.NewGuid().ToString();
Expand Down
Loading

0 comments on commit 3c875c7

Please sign in to comment.