Skip to content

Commit

Permalink
Removed create collection options for bootstrapper context. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
nscheibe authored Dec 18, 2019
1 parent 412d7b2 commit 0ab6af6
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 76 deletions.
49 changes: 0 additions & 49 deletions src/Context.Tests/MongoCollectionBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
Expand Down Expand Up @@ -190,54 +189,6 @@ public void WithMongoCollectionSettings_DefaultCollectionSettingsConfigured_Defa
Assert.True(result.Settings.AssignIdOnInsert);
}

#endregion

#region WithMongoCollectionSettings Tests

[Fact]
public async Task WithCreateCollectionOptions_SetCappedCollectionOptions_MongoCollectionOptionsChangedToCappedSuccessfully()
{
// Arrange
var mongoCollectionBuilder = new MongoCollectionBuilder<Order>(_mongoDatabase);

// Act
mongoCollectionBuilder.WithCreateCollectionOptions(createCollectionOptions =>
{
createCollectionOptions.Capped = true;
createCollectionOptions.MaxSize = 1;
createCollectionOptions.MaxDocuments = 1;
});
IMongoCollection<Order> result = mongoCollectionBuilder.Build();

// Assert
var command = new BsonDocumentCommand<BsonDocument>(new BsonDocument
{
{"collstats", nameof(Order)}
});

BsonDocument stats = await _mongoDatabase.RunCommandAsync(command);
Assert.True(stats["capped"].AsBoolean);
}

[Fact]
public async Task WithCreateCollectionOptions_NoCappedCollectionOptions_DefaultMongoCollectionSettingsSet()
{
// Arrange
var mongoCollectionBuilder = new MongoCollectionBuilder<Order>(_mongoDatabase);

// Act
IMongoCollection<Order> result = mongoCollectionBuilder.Build();

// Assert
var command = new BsonDocumentCommand<BsonDocument>(new BsonDocument
{
{"collstats", nameof(Order)}
});

BsonDocument stats = await _mongoDatabase.RunCommandAsync(command);
Assert.False(stats["capped"].AsBoolean);
}

#endregion

#region WithMongoCollectionConfiguration Tests
Expand Down
5 changes: 1 addition & 4 deletions src/Context/IMongoCollectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ public interface IMongoCollectionBuilder<TDocument>

IMongoCollectionBuilder<TDocument> AddBsonClassMap<TMapDocument>(
Action<BsonClassMap<TMapDocument>> bsonClassMapAction) where TMapDocument : class;

IMongoCollectionBuilder<TDocument> WithCreateCollectionOptions(
Action<CreateCollectionOptions> createCollectionOptions);


IMongoCollectionBuilder<TDocument> WithCollectionSettings(
Action<MongoCollectionSettings> collectionSettings);

Expand Down
24 changes: 1 addition & 23 deletions src/Context/Internal/MongoCollectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ internal class MongoCollectionBuilder<TDocument> : IMongoCollectionBuilder<TDocu
private string _collectionName;
private readonly IMongoDatabase _mongoDatabase;
private readonly List<Action> _classMapActions;
private readonly List<Action<CreateCollectionOptions>> _createCollectionOptionsActions;
private readonly List<Action<MongoCollectionSettings>> _collectionSettingsActions;
private readonly List<Action<IMongoCollection<TDocument>>> _collectionConfigurations;

Expand All @@ -26,7 +25,6 @@ public MongoCollectionBuilder(IMongoDatabase mongoDatabase)
_classMapActions = new List<Action>();
_collectionConfigurations = new List<Action<IMongoCollection<TDocument>>>();
_collectionSettingsActions = new List<Action<MongoCollectionSettings>>();
_createCollectionOptionsActions = new List<Action<CreateCollectionOptions>>();
}

public IMongoCollectionBuilder<TDocument> WithCollectionName(string collectionName)
Expand All @@ -46,14 +44,6 @@ public IMongoCollectionBuilder<TDocument> AddBsonClassMap<TMapDocument>(

return this;
}

public IMongoCollectionBuilder<TDocument> WithCreateCollectionOptions(
Action<CreateCollectionOptions> createCollectionOptions)
{
_createCollectionOptionsActions.Add(createCollectionOptions);

return this;
}

public IMongoCollectionBuilder<TDocument> WithCollectionSettings(
Action<MongoCollectionSettings> collectionSettings)
Expand All @@ -74,9 +64,7 @@ public IMongoCollectionBuilder<TDocument> WithCollectionConfiguration(
internal IMongoCollection<TDocument> Build()
{
_classMapActions.ForEach(action => action());

CreateMongoCollection();


IMongoCollection<TDocument> mongoCollection = GetMongoCollection();

_collectionConfigurations.ForEach(configuration => configuration(mongoCollection));
Expand All @@ -95,16 +83,6 @@ private IMongoCollection<TDocument> GetMongoCollection()
.GetCollection<TDocument>(_collectionName, mongoCollectionSettings);
}

private void CreateMongoCollection()
{
var createCollectionOptions = new CreateCollectionOptions();

_createCollectionOptionsActions
.ForEach(configure => configure(createCollectionOptions));

_mongoDatabase.CreateCollection(_collectionName, createCollectionOptions);
}

private void RegisterClassMapSync<TMapDocument>(
Action<BsonClassMap<TMapDocument>> bsonClassMapAction) where TMapDocument : class
{
Expand Down

0 comments on commit 0ab6af6

Please sign in to comment.