Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subpartitioning: Add tests for Transactional Batch #3930

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions Microsoft.Azure.Cosmos/src/Batch/BatchExecUtils.cs
Expand Up @@ -73,19 +73,19 @@ internal static class BatchExecUtils
}
}

public static void EnsureValid(
public static async Task EnsureValidAsync(
IReadOnlyList<ItemBatchOperation> operations,
RequestOptions batchOptions)
{
string errorMessage = BatchExecUtils.IsValid(operations, batchOptions);
string errorMessage = await BatchExecUtils.IsValidAsync(operations, batchOptions);

if (errorMessage != null)
{
throw new ArgumentException(errorMessage);
}
}

internal static string IsValid(
internal static async Task<string> IsValidAsync(
IReadOnlyList<ItemBatchOperation> operations,
RequestOptions batchOptions)
{
Expand Down Expand Up @@ -128,6 +128,18 @@ internal static class BatchExecUtils
{
errorMessage = ClientResources.PKAndEpkSetTogether;
}

if (operation.PartitionKey != null)
{
int specifiedPartitionKeyComponentCount = operation.PartitionKey.Value.InternalKey.Components.Count;
PartitionKeyDefinition partitionKeyDefinition = await operation.ContainerInternal.GetPartitionKeyDefinitionAsync(CancellationToken.None);
if (partitionKeyDefinition.Paths.Count != specifiedPartitionKeyComponentCount)
{
errorMessage = specifiedPartitionKeyComponentCount < partitionKeyDefinition.Paths.Count ?
ClientResources.TooFewPartitionKeyComponents :
ClientResources.TooManyPartitionKeyComponents;
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Batch/BatchExecutor.cs
Expand Up @@ -43,7 +43,7 @@ public async Task<TransactionalBatchResponse> ExecuteAsync(ITrace trace, Cancell
{
using (ITrace executeNextBatchTrace = trace.StartChild("Execute Next Batch", TraceComponent.Batch, Tracing.TraceLevel.Info))
{
BatchExecUtils.EnsureValid(this.inputOperations, this.batchOptions);
await BatchExecUtils.EnsureValidAsync(this.inputOperations, this.batchOptions);

PartitionKey? serverRequestPartitionKey = this.partitionKey;
if (this.batchOptions != null && this.batchOptions.IsEffectivePartitionKeyRouting)
Expand Down
20 changes: 19 additions & 1 deletion Microsoft.Azure.Cosmos/src/ClientResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Microsoft.Azure.Cosmos/src/ClientResources.resx
Expand Up @@ -318,4 +318,10 @@
<data name="FeedToken_InvalidFeedTokenForContainer" xml:space="preserve">
<value>The continuation was generated for container {0} but current container is {1}.</value>
</data>
<data name="TooFewPartitionKeyComponents" xml:space="preserve">
<value>PartitionKey has fewer components than defined the collection resource.</value>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"than defined for the container." instead of "than defined the collection resource."

</data>
<data name="TooManyPartitionKeyComponents" xml:space="preserve">
<value>PartitionKey has more components than defined the collection resource.</value>
</data>
</root>
Expand Up @@ -33,6 +33,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.Azure.Cosmos.Diagnostics;
using global::Azure;

[TestClass]
public class CosmosItemTests : BaseCosmosClientHelper
Expand Down Expand Up @@ -841,6 +842,73 @@ public async Task PartitionKeyDeleteTestForSubpartitionedContainer()
}
}

[TestMethod]
public async Task TransactionalBatchTestForSubpartitionedContainer()
{
string currentVersion = HttpConstants.Versions.CurrentVersion;
HttpConstants.Versions.CurrentVersion = "2020-07-15";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this?

using CosmosClient client = TestCommon.CreateCosmosClient(true);
Cosmos.Database database = null;
try
{
database = await client.CreateDatabaseIfNotExistsAsync("mydb");

ContainerProperties containerProperties = new ContainerProperties("subpartitionedcontainer", new List<string> { "/Country", "/City" });
Container container = await database.CreateContainerAsync(containerProperties);
ContainerInternal containerInternal = (ContainerInternal)container;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these casts needed? Aren't all the APIs being accessed in the non-internal interface?


TransactionalBatchInternal transactionalBatchInternal = (TransactionalBatchInternal)containerInternal.CreateTransactionalBatch(
new PartitionKeyBuilder().Add("USA").Add("Redmond").Build()) ;

//Document create.
ItemResponse<Document>[] documents = new ItemResponse<Document>[5];
Document doc1 = new Document { Id = "document1" };
doc1.SetValue("Country", "USA");
doc1.SetValue("City", "Redmond");

Document doc2 = new Document { Id = "document2" };
doc2.SetValue("Country", "USA");
doc2.SetValue("City", "Redmond");
await transactionalBatchInternal
.CreateItem<Document>(doc1)
.CreateItem<Document>(doc2)
.ExecuteAsync();

//Specifying a partial partition key should fail
Cosmos.PartitionKey partialPartitionKey = new PartitionKeyBuilder().Add("USA").Build();
TransactionalBatch transactionalBatchInternalPartialKey = containerInternal.CreateTransactionalBatch(partialPartitionKey);
try
{
TransactionalBatchResponse response = await transactionalBatchInternalPartialKey.CreateItem<Document>(doc1).ExecuteAsync();
Assert.Fail("Should have thrown");
}
catch (Exception ex)
{
Assert.AreEqual(typeof(ArgumentException), ex.GetType());
Assert.IsTrue(ex.Message.Contains(ClientResources.TooFewPartitionKeyComponents));
}

//OverSpecifying a partition key should fail
Cosmos.PartitionKey overSpecifiedPartitionKey = new PartitionKeyBuilder().Add("USA").Add("Redmond").Add("someValue").Build();
TransactionalBatch transactionalBatchOverSpecifiedPartitionKey = containerInternal.CreateTransactionalBatch(overSpecifiedPartitionKey);
try
{
TransactionalBatchResponse response = await transactionalBatchOverSpecifiedPartitionKey.CreateItem<Document>(doc1).ExecuteAsync();
Assert.Fail("Should have thrown");
}
catch (Exception ex)
{
Assert.AreEqual(typeof(ArgumentException), ex.GetType());
Assert.IsTrue(ex.Message.Contains(ClientResources.TooManyPartitionKeyComponents));
}
}
finally
{
HttpConstants.Versions.CurrentVersion = currentVersion;
if (database != null) await database.DeleteAsync();
}
}

[TestMethod]
public async Task ItemCustomSerialzierTest()
{
Expand Down