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

[Internal] Binary Encoding: Adds emulator tests with binary encoding. #4921

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,74 @@ public async Task ClientEncryptionPolicyFailureTest()
{
Assert.IsTrue(ex.Message.Contains("Path: /users which is part of the partition key has to be encrypted with Deterministic type Encryption."), ex.Message);
}
}

[TestMethod]
[DataRow(true, DisplayName = "Container creation and deletion with binary encoding enabled.")]
[DataRow(false, DisplayName = "Container creation and deletion with binary encoding disabled.")]
public async Task ContainerCreationDeletionWithBinaryEncodingTest(bool binaryEncodingEnabled)
{
string containerId = Guid.NewGuid().ToString();
string originalBinaryEncodingValue = Environment.GetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled);

try
{
if (binaryEncodingEnabled)
{
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, "True");
}
else
{
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, null);
}

string partitionKeyPath = "/pk";
ContainerProperties containerProperties = new ContainerProperties(containerId, partitionKeyPath);
ContainerResponse createResponse = await this.cosmosDatabase.CreateContainerAsync(containerProperties);
Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode);
Assert.IsNotNull(createResponse.Container);

// Validate the container was created
Container readContainer = this.cosmosDatabase.GetContainer(containerId);
ContainerResponse readResponse = await readContainer.ReadContainerAsync();
Assert.AreEqual(HttpStatusCode.OK, readResponse.StatusCode);
Assert.IsNotNull(readResponse.Resource);
Assert.AreEqual(containerId, readResponse.Resource.Id);

ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();
ItemResponse<ToDoActivity> itemResponse = await readContainer.CreateItemAsync(testItem, new PartitionKey(testItem.pk));
Assert.AreEqual(HttpStatusCode.Created, itemResponse.StatusCode);

// Delete the container
ContainerResponse deleteResponse = await readContainer.DeleteContainerAsync();
Assert.AreEqual(HttpStatusCode.NoContent, deleteResponse.StatusCode);

// Attempt to read the container again to ensure it was deleted
try
{
await readContainer.ReadContainerAsync();
Assert.Fail("Expected a NotFound exception after deleting the container.");
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
// exception
}
}
finally
{
// Restore the original binary encoding environment variable
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, originalBinaryEncodingValue);

try
{
Container container = this.cosmosDatabase.GetContainer(containerId);
await container.DeleteContainerAsync();
}
catch
{
// Ignore exceptions during cleanup
}
}
}

private void ValidateCreateContainerResponseContract(ContainerResponse containerResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,84 @@ public async Task DatabaseQueryIterator()
await database.DeleteAsync(cancellationToken: this.cancellationToken);
}
}
}
}

[TestMethod]
[DataRow(true, DisplayName = "Database creation and deletion with binary encoding enabled.")]
[DataRow(false, DisplayName = "Database creation and deletion with binary encoding disabled.")]
public async Task DatabaseCreationDeletionWithBinaryEncodingTest(bool binaryEncodingEnabled)
{
string databaseId = Guid.NewGuid().ToString();
string originalBinaryEncodingValue = Environment.GetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled);

try
{
// Enable or disable binary encoding as requested
if (binaryEncodingEnabled)
{
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, "True");
}
else
{
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, null);
}

// Create the database using the existing client and pattern
DatabaseResponse createResponse = await this.GetClient().CreateDatabaseAsync(databaseId);
Assert.AreEqual(HttpStatusCode.Created, createResponse.StatusCode);
Assert.IsNotNull(createResponse.Database);
Assert.IsNotNull(createResponse.Resource);
Assert.AreEqual(databaseId, createResponse.Resource.Id);

// Validate the database was created
Database createdDatabase = this.GetClient().GetDatabase(databaseId);
DatabaseResponse readResponse = await createdDatabase.ReadAsync();
Assert.AreEqual(HttpStatusCode.OK, readResponse.StatusCode);
Assert.IsNotNull(readResponse.Resource);
Assert.AreEqual(databaseId, readResponse.Resource.Id);

// Create a container to verify item operations under binary encoding
string containerId = Guid.NewGuid().ToString();
ContainerResponse containerResponse = await createdDatabase.CreateContainerAsync(new ContainerProperties(containerId, "/pk"));
Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode);

Container container = createdDatabase.GetContainer(containerId);
ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();
ItemResponse<ToDoActivity> itemResponse = await container.CreateItemAsync(testItem, new PartitionKey(testItem.pk));
Assert.AreEqual(HttpStatusCode.Created, itemResponse.StatusCode);

// Delete the database
DatabaseResponse deleteDbResponse = await createdDatabase.DeleteAsync();
Assert.AreEqual(HttpStatusCode.NoContent, deleteDbResponse.StatusCode);

// Validate that the database no longer exists
try
{
await createdDatabase.ReadAsync();
Assert.Fail("Expected a NotFound exception after deleting the database.");
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
// Expected exception
}
}
finally
{
// Restore the original binary encoding environment variable value
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, originalBinaryEncodingValue);

// Clean up if something fails before deletion
try
{
Database databaseToCleanup = this.GetClient().GetDatabase(databaseId);
await databaseToCleanup.DeleteAsync();
}
catch
{
// Ignore exceptions in cleanup
}
}
}

private Task<DatabaseResponse> CreateDatabaseHelper()
{
Expand Down