Skip to content

Commit

Permalink
Don't require setting id on supplied cosmos container properties (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Confusingboat authored Nov 17, 2023
1 parent ffd56d7 commit 14a5950
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Ephemerally.Azure.Cosmos/PublicExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -28,7 +30,9 @@ public static async Task<EphemeralCosmosContainer> 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);
}
Expand Down
55 changes: 53 additions & 2 deletions tests/Ephemerally.Azure.Cosmos.Tests/EphemeralContainerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Ephemerally.Azure.Cosmos.Tests;
using Microsoft.Azure.Cosmos;

namespace Ephemerally.Azure.Cosmos.Tests;

public class EphemeralContainerTests
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 14a5950

Please sign in to comment.