Skip to content

Commit

Permalink
wip: still refactoring cosmos fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Confusingboat committed Oct 5, 2024
1 parent 9aa81c7 commit a5d4f5d
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 29 deletions.
10 changes: 0 additions & 10 deletions src/Ephemerally.Azure.Cosmos.Xunit/ConsumingSubjectFixture.cs

This file was deleted.

10 changes: 1 addition & 9 deletions src/Ephemerally.Azure.Cosmos.Xunit/CosmosClientFixture.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
using Microsoft.Azure.Cosmos;
using System.Diagnostics.CodeAnalysis;
using Ephemerally.Xunit;
using Xunit;

namespace Ephemerally.Azure.Cosmos.Xunit;

[SuppressMessage("ReSharper", "UseConfigureAwaitFalse")]
public class CosmosClientFixture : SubjectFixture<CosmosClient>
public abstract class CosmosClientFixture : CosmosSubjectFixture<CosmosClient>
{
public CosmosClient Client => GetOrCreateSubjectAsync().Result;

protected override Task<CosmosClient> CreateSubjectAsync() => Task.FromResult(CosmosEmulator.GetClient());

protected override Task DisposeSubjectAsync() => SafeCosmosDisposeAsync(GetOrCreateSubjectAsync);
}
17 changes: 17 additions & 0 deletions src/Ephemerally.Azure.Cosmos.Xunit/CosmosContainerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Ephemerally.Xunit;
using Microsoft.Azure.Cosmos;

namespace Ephemerally.Azure.Cosmos.Xunit;

public abstract class CosmosContainerFixture
: CosmosContainerFixture<Container>;

public abstract class CosmosContainerFixture<TContainer>
: CosmosSubjectFixture<TContainer>,
ISubjectFixture<Container> where TContainer : Container
{
Task<Container> ISubjectFixture<Container>.GetOrCreateSubjectAsync() =>
GetOrCreateSubjectAsync().ContinueWith(Container (x) => x.Result);

public TContainer Container => GetOrCreateSubjectAsync().Result;
}
17 changes: 17 additions & 0 deletions src/Ephemerally.Azure.Cosmos.Xunit/CosmosDatabaseFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Ephemerally.Xunit;
using Microsoft.Azure.Cosmos;

namespace Ephemerally.Azure.Cosmos.Xunit;

public abstract class CosmosDatabaseFixture
: CosmosDatabaseFixture<Database>;

public abstract class CosmosDatabaseFixture<TDatabase>
: CosmosSubjectFixture<TDatabase>,
ISubjectFixture<Database> where TDatabase : Database
{
Task<Database> ISubjectFixture<Database>.GetOrCreateSubjectAsync() =>
GetOrCreateSubjectAsync().ContinueWith(Database (x) => x.Result);

public TDatabase Database => GetOrCreateSubjectAsync().Result;
}
8 changes: 8 additions & 0 deletions src/Ephemerally.Azure.Cosmos.Xunit/CosmosSubjectFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Ephemerally.Xunit;

namespace Ephemerally.Azure.Cosmos.Xunit;

public abstract class CosmosSubjectFixture<TSubject> : SubjectFixture<TSubject> where TSubject : class
{
protected override Task DisposeSubjectAsync() => SafeCosmosDisposeAsync(GetOrCreateSubjectAsync);
}
14 changes: 14 additions & 0 deletions src/Ephemerally.Azure.Cosmos.Xunit/DefaultFixtures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Azure.Cosmos;

namespace Ephemerally.Azure.Cosmos.Xunit;

public class DefaultCosmosEmulatorClientFixture : CosmosClientFixture
{
protected override Task<CosmosClient> CreateSubjectAsync() => Task.FromResult(CosmosEmulator.GetClient());
}

public class DefaultEphemeralCosmosDatabaseFixture()
: EphemeralCosmosDatabaseFixture(new DefaultCosmosEmulatorClientFixture());

public class DefaultEphemeralCosmosContainerFixture()
: EphemeralCosmosContainerFixture(new DefaultEphemeralCosmosDatabaseFixture());
53 changes: 53 additions & 0 deletions src/Ephemerally.Azure.Cosmos.Xunit/DependentSubjectFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Diagnostics.CodeAnalysis;
using Ephemerally.Xunit;

namespace Ephemerally.Azure.Cosmos.Xunit;

[SuppressMessage("ReSharper", "UseConfigureAwaitFalse")]
public abstract class DependentSubjectFixture<TSubject> : ISubjectFixture<TSubject>
{
private readonly ISubjectFixture<TSubject> _implementation;
private readonly ISubjectFixture[] _managedFixtures;

protected DependentSubjectFixture(
ISubjectFixture<TSubject> implementation,
params ISubjectFixture[] managedFixtures)
{
_implementation = implementation;
_managedFixtures = managedFixtures;
}

public Task<TSubject> GetOrCreateSubjectAsync() => _implementation.GetOrCreateSubjectAsync();

public Task InitializeAsync() => _implementation.InitializeAsync();

public async Task DisposeAsync()
{
try
{
await _implementation.DisposeAsync();
}
finally
{
var exceptions = new List<Exception>();
await Task.WhenAll(_managedFixtures.Select(async x =>
{
try
{
await x.DisposeAsync();
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
));
if (exceptions.Count > 0)
{
#pragma warning disable CA2219
throw new AggregateException(exceptions);
#pragma warning restore CA2219
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ namespace Ephemerally.Azure.Cosmos.Xunit;

[SuppressMessage("ReSharper", "UseConfigureAwaitFalse")]
public class EphemeralCosmosContainerFixture(ISubjectFixture<Database> cosmosDatabaseFixture)
: ConsumingSubjectFixture<EphemeralCosmosContainer>
: CosmosContainerFixture<EphemeralCosmosContainer>
{
public EphemeralCosmosContainer Container => GetOrCreateSubjectAsync().Result;

protected override async Task<EphemeralCosmosContainer> CreateSubjectAsync()
{
var database = await cosmosDatabaseFixture.GetOrCreateSubjectAsync();
return await database.CreateEphemeralContainerAsync();
}

protected override Task DisposeSubjectAsync() => SafeCosmosDisposeAsync(GetOrCreateSubjectAsync);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ namespace Ephemerally.Azure.Cosmos.Xunit;

[SuppressMessage("ReSharper", "UseConfigureAwaitFalse")]
public class EphemeralCosmosDatabaseFixture(ISubjectFixture<CosmosClient> cosmosClientFixture)
: ConsumingSubjectFixture<EphemeralCosmosDatabase>
: CosmosDatabaseFixture<EphemeralCosmosDatabase>
{
public EphemeralCosmosDatabase Database => GetOrCreateSubjectAsync().Result;

protected override async Task<EphemeralCosmosDatabase> CreateSubjectAsync()
{
var client = await cosmosClientFixture.GetOrCreateSubjectAsync();
return await client.CreateEphemeralDatabaseAsync();
}

protected override Task DisposeSubjectAsync() => SafeCosmosDisposeAsync(GetOrCreateSubjectAsync);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Ephemerally.Azure.Cosmos.Xunit;
using Shouldly;

namespace Ephemerally.Azure.Cosmos.Tests.Fixtures;

public class DefaultCosmosEmulatorClientFixtureTests : CosmosClientFixtureTests<DefaultCosmosEmulatorClientFixture>;

public abstract class CosmosClientFixtureTests<TFixture> where TFixture : CosmosClientFixture, new()
{
[Test]
public async Task Fixture_provides_usable_cosmos_client()
{
// Arrange
await using var fixture = new TFixture();
await fixture.InitializeAsync();

// Act
var canConnect = await fixture.Client.CanConnectAsync();

// Assert
canConnect.ShouldBeTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Ephemerally.Azure.Cosmos.Xunit;

namespace Ephemerally.Azure.Cosmos.Tests.Fixtures;

public abstract class CosmosDatabaseFixtureTests<TFixture> where TFixture : EphemeralCosmosDatabaseFixture
{

}

0 comments on commit a5d4f5d

Please sign in to comment.