From 14a59507910d2d0a51d92a73cfef8fa65ce0183f Mon Sep 17 00:00:00 2001 From: Colton Fussy <519596+Confusingboat@users.noreply.github.com> Date: Fri, 17 Nov 2023 09:01:49 -0600 Subject: [PATCH] Don't require setting id on supplied cosmos container properties (#8) --- .../PublicExtensions.cs | 6 +- .../EphemeralContainerTests.cs | 55 ++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/Ephemerally.Azure.Cosmos/PublicExtensions.cs b/src/Ephemerally.Azure.Cosmos/PublicExtensions.cs index b1ce80b..c202557 100644 --- a/src/Ephemerally.Azure.Cosmos/PublicExtensions.cs +++ b/src/Ephemerally.Azure.Cosmos/PublicExtensions.cs @@ -6,6 +6,8 @@ namespace Ephemerally; public static class PublicExtensions { + private const string DefaultPartitionKeyPath = "/id"; + public static EphemeralCosmosDatabase ToEphemeral(this Database database, EphemeralOptions options = default) => new(new CosmosDatabaseEphemeral(database, options)); @@ -28,7 +30,9 @@ public static async Task CreateEphemeralContainerAsync ThroughputProperties throughputProperties = default) { var metadata = options.OrDefault().GetNewMetadata(); - containerProperties ??= new ContainerProperties(metadata.FullName, "/id"); + containerProperties ??= new(); + containerProperties.Id ??= metadata.FullName; + containerProperties.PartitionKeyPath ??= DefaultPartitionKeyPath; var response = await database.CreateContainerIfNotExistsAsync(containerProperties, throughputProperties).ConfigureAwait(false); return database.GetContainer(response.Resource.Id).ToEphemeral(options); } diff --git a/tests/Ephemerally.Azure.Cosmos.Tests/EphemeralContainerTests.cs b/tests/Ephemerally.Azure.Cosmos.Tests/EphemeralContainerTests.cs index 2f9d0c4..6dcc836 100644 --- a/tests/Ephemerally.Azure.Cosmos.Tests/EphemeralContainerTests.cs +++ b/tests/Ephemerally.Azure.Cosmos.Tests/EphemeralContainerTests.cs @@ -1,4 +1,6 @@ -namespace Ephemerally.Azure.Cosmos.Tests; +using Microsoft.Azure.Cosmos; + +namespace Ephemerally.Azure.Cosmos.Tests; public class EphemeralContainerTests { @@ -38,7 +40,7 @@ public async Task CleanupBehavior_SelfAndExpired_should_remove_self_and_expired_ { var client = CosmosEmulator.Client; await using var db = await client.CreateEphemeralDatabaseAsync(); - + var orphanContainer = await db.CreateEphemeralContainerAsync(new EphemeralCreationOptions(DateTimeOffset.MinValue)); Assert.That(await orphanContainer.ExistsAsync(), Is.True); @@ -109,4 +111,53 @@ public async Task CleanupBehavior_NoCleanup_should_not_remove_anything() Assert.That(await sut.ExistsAsync(), Is.True); Assert.That(await orphanContainer.ExistsAsync(), Is.True); } + + [Test] + public async Task Should_create_container_when_container_properties_Id_is_provided() + { + const string userSuppliedId = "user-supplied-id"; + + var client = CosmosEmulator.Client; + await using var db = await client.CreateEphemeralDatabaseAsync(); + + await using var sut = await db.CreateEphemeralContainerAsync(containerProperties: new ContainerProperties { Id = userSuppliedId }); + + Assert.That(await sut.ExistsAsync(), Is.True); + Assert.That(sut.Id, Is.EqualTo(userSuppliedId)); + } + + [Test] + public async Task Should_create_container_when_container_properties_Id_is_not_provided() + { + var client = CosmosEmulator.Client; + await using var db = await client.CreateEphemeralDatabaseAsync(); + + await using var sut = await db.CreateEphemeralContainerAsync(containerProperties: new()); + + Assert.That(await sut.ExistsAsync(), Is.True); + } + + [Test] + public async Task Should_create_container_when_container_properties_PartitionKeyPath_is_provided() + { + const string userSuppliedKey = "/userSuppliedKey"; + + var client = CosmosEmulator.Client; + await using var db = await client.CreateEphemeralDatabaseAsync(); + + await using var sut = await db.CreateEphemeralContainerAsync(containerProperties: new ContainerProperties { PartitionKeyPath = userSuppliedKey }); + + Assert.That(await sut.ExistsAsync(), Is.True); + } + + [Test] + public async Task Should_create_container_when_container_properties_PartitionKeyPath_is_not_provided() + { + var client = CosmosEmulator.Client; + await using var db = await client.CreateEphemeralDatabaseAsync(); + + await using var sut = await db.CreateEphemeralContainerAsync(containerProperties: new()); + + Assert.That(await sut.ExistsAsync(), Is.True); + } } \ No newline at end of file