-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: still refactoring cosmos fixtures
- Loading branch information
1 parent
9aa81c7
commit a5d4f5d
Showing
11 changed files
with
143 additions
and
29 deletions.
There are no files selected for viewing
10 changes: 0 additions & 10 deletions
10
src/Ephemerally.Azure.Cosmos.Xunit/ConsumingSubjectFixture.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/Ephemerally.Azure.Cosmos.Xunit/CosmosContainerFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/Ephemerally.Azure.Cosmos.Xunit/CosmosDatabaseFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
53
src/Ephemerally.Azure.Cosmos.Xunit/DependentSubjectFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/Ephemerally.Azure.Cosmos.Tests/Fixtures/CosmosClientFixtureTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tests/Ephemerally.Azure.Cosmos.Tests/Fixtures/CosmosDatabaseFixtureTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |