public void AddBsonClassMap_BsonClassMapNotAdded_BsonClassMapIsNotRegistered()
        {
            // Arrange
            var mongoCollectionBuilder = new MongoCollectionBuilder <Order>(_mongoDatabase);

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

            // Assert
            Assert.False(BsonClassMap.IsClassMapRegistered(typeof(ItemClassMapNotRegistered)));
        }
        public void WithCollectionName_CollectionNameNotSet_MongoCollectionNameIsDefault()
        {
            // Arrange
            var mongoCollectionBuilder = new MongoCollectionBuilder <Order>(_mongoDatabase);

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

            // Assert
            Assert.Equal("Order", result.CollectionNamespace.CollectionName);
        }
        public void AddBsonClassMap_AddNewBsonClassMapOnce_BsonClassMapIsRegistered()
        {
            // Arrange
            var mongoCollectionBuilder = new MongoCollectionBuilder <Order>(_mongoDatabase);

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

            // Assert
            Assert.True(BsonClassMap.IsClassMapRegistered(typeof(Order)));
        }
        public void WithCollectionName_CollectionNameSet_MongoCollectionNameIsSet()
        {
            // Arrange
            var mongoCollectionBuilder = new MongoCollectionBuilder <Order>(_mongoDatabase);

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

            // Assert
            Assert.Equal("nameOnlyForTest", result.CollectionNamespace.CollectionName);
        }
        public void WithMongoCollectionSettings_NoCollectionSettingsConfigured_DefaultMongoCollectionSettingsSet()
        {
            // Arrange
            var mongoCollectionBuilder = new MongoCollectionBuilder <Order>(_mongoDatabase);

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

            // Assert
            Assert.Equal(WriteConcern.Acknowledged, result.Settings.WriteConcern);
            Assert.Equal(ReadConcern.Majority, result.Settings.ReadConcern);
            Assert.Equal(ReadPreference.Primary, result.Settings.ReadPreference);
            Assert.True(result.Settings.AssignIdOnInsert);
        }
        public void AddBsonClassMap_AddNewBsonClassMapWithoutParameter_BsonClassMapIsRegistered()
        {
            // Arrange
            var mongoCollectionBuilder =
                new MongoCollectionBuilder <ItemWithoutSpecificClassMap>(_mongoDatabase);

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

            // Assert
            Assert.NotNull(result);
            Assert.True(BsonClassMap.IsClassMapRegistered(typeof(ItemWithoutSpecificClassMap)));
        }
        public void WithMongoCollectionSettings_ChangeCollectionSettings_MongoCollectionSettingsChangedSuccessfully()
        {
            // Arrange
            var mongoCollectionBuilder = new MongoCollectionBuilder <Order>(_mongoDatabase);

            // Act
            mongoCollectionBuilder.WithCollectionSettings(mongoCollectionSettings =>
            {
                mongoCollectionSettings.WriteConcern     = WriteConcern.Unacknowledged;
                mongoCollectionSettings.ReadConcern      = ReadConcern.Linearizable;
                mongoCollectionSettings.ReadPreference   = ReadPreference.Nearest;
                mongoCollectionSettings.AssignIdOnInsert = false;
            });
            IMongoCollection <Order> result = mongoCollectionBuilder.Build();

            // Assert
            Assert.Equal(WriteConcern.Unacknowledged, result.Settings.WriteConcern);
            Assert.Equal(ReadConcern.Linearizable, result.Settings.ReadConcern);
            Assert.Equal(ReadPreference.Nearest, result.Settings.ReadPreference);
            Assert.False(result.Settings.AssignIdOnInsert);
        }
        public void WithMongoCollectionConfiguration_ChangeCollectionConfiguration_MongoCollectionConfigurationChangedSuccessfully()
        {
            // Arrange
            var mongoCollectionBuilder = new MongoCollectionBuilder <Order>(_mongoDatabase);

            // Act
            mongoCollectionBuilder.WithCollectionConfiguration(mongoCollection =>
            {
                mongoCollection.Indexes.CreateOne(new CreateIndexModel <Order>(
                                                      Builders <Order> .IndexKeys.Ascending(order => order.Name),
                                                      new CreateIndexOptions {
                    Unique = true
                }));
            });

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

            //
            List <BsonDocument> indexes = result.Indexes.List().ToList();

            Assert.Equal("_id_", indexes.First().GetElement("name").Value.ToString());
            Assert.Equal("Name_1", indexes.Last().GetElement("name").Value.ToString(), ignoreCase: true);
        }