generated from SwissLife-OSS/template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MongoDbContext auto initialization configurable. (#21)
Co-authored-by: Gabriel Lucaci <[email protected]>
- Loading branch information
Showing
2 changed files
with
113 additions
and
10 deletions.
There are no files selected for viewing
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,85 @@ | ||
using MongoDB.Driver; | ||
using Squadron; | ||
using Xunit; | ||
|
||
namespace MongoDB.Extensions.Context.Tests | ||
{ | ||
public class MongoDbContextTests : IClassFixture<MongoResource> | ||
{ | ||
private readonly MongoOptions _mongoOptions; | ||
private readonly IMongoDatabase _mongoDatabase; | ||
|
||
public MongoDbContextTests(MongoResource mongoResource) | ||
{ | ||
_mongoDatabase = mongoResource.CreateDatabase(); | ||
_mongoOptions = new MongoOptions | ||
{ | ||
ConnectionString = mongoResource.ConnectionString, | ||
DatabaseName = _mongoDatabase.DatabaseNamespace.DatabaseName | ||
}; | ||
} | ||
|
||
#region Constructor Tests | ||
|
||
[Fact] | ||
public void Constructor_AutoInitializeDefault_InitializationExecuted() | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
var testMongoDbContext = new TestMongoDbContext(_mongoOptions); | ||
|
||
// Assert | ||
Assert.True(testMongoDbContext.IsInitialized); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_AutoInitializeManual_InitializationExecuted() | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
var testMongoDbContext = new TestMongoDbContext(_mongoOptions, true); | ||
|
||
// Assert | ||
Assert.True(testMongoDbContext.IsInitialized); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_NoInitializeManual_InitializationExecuted() | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
var testMongoDbContext = new TestMongoDbContext(_mongoOptions, false); | ||
|
||
// Assert | ||
Assert.False(testMongoDbContext.IsInitialized); | ||
} | ||
|
||
#endregion | ||
|
||
#region Private Helpers | ||
|
||
private class TestMongoDbContext : MongoDbContext | ||
{ | ||
public TestMongoDbContext(MongoOptions mongoOptions) : base(mongoOptions) | ||
{ | ||
} | ||
|
||
public TestMongoDbContext(MongoOptions mongoOptions, bool enableAutoInit) | ||
: base(mongoOptions, enableAutoInit) | ||
{ | ||
} | ||
|
||
protected override void OnConfiguring(IMongoDatabaseBuilder mongoDatabaseBuilder) | ||
{ | ||
IsInitialized = true; | ||
} | ||
|
||
public bool IsInitialized { get; private set; } | ||
} | ||
|
||
#endregion | ||
} | ||
} |
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