<commit_before>#region License
// Copyright (c) 2019, FluentMigrator Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using FluentMigrator.Expressions;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Conventions;

using Microsoft.Extensions.DependencyInjection;

using NUnit.Framework;

namespace FluentMigrator.Tests.Unit.Initialization
{
    public class ConventionSetTests
    {
        [Test]
        public void CanLoadConventionSetFromAssembly()
        {
            var services = new ServiceCollection()
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb.AddSQLite().ScanIn(this.GetType().Assembly).For.ConventionSet())
                .BuildServiceProvider(false);
            var conventionSet = services.GetRequiredService<IConventionSet>();
            ISchemaExpression expr = new CreateTableExpression();
            conventionSet.SchemaConvention.Apply(expr);
            Assert.AreEqual("custom-schema", expr.SchemaName);
        }

        [Test]
        public void CanLoadConventionSetFromAssemblyWithScanForAll()
        {
            var services = new ServiceCollection()
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb.AddSQLite().ScanIn(this.GetType().Assembly).For.All())
                .BuildServiceProvider(false);
            var conventionSet = services.GetRequiredService<IConventionSet>();
            ISchemaExpression expr = new CreateTableExpression();
            conventionSet.SchemaConvention.Apply(expr);
            Assert.AreEqual("custom-schema", expr.SchemaName);
        }

        [Test]
        public void NotLoadingConventionSetFromAssemblyWithScanForMigrations()
        {
            var services = new ServiceCollection()
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb.AddSQLite().ScanIn(this.GetType().Assembly).For.Migrations())
                .BuildServiceProvider(false);
            var conventionSet = services.GetRequiredService<IConventionSet>();
            ISchemaExpression expr = new CreateTableExpression();
            conventionSet.SchemaConvention.Apply(expr);
            Assert.AreEqual(null, expr.SchemaName);
        }

        // ReSharper disable once UnusedMember.Global
        public class CustomConventionSet : ConventionSet
        {
            public CustomConventionSet()
            {
                base.SchemaConvention = new DefaultSchemaConvention("custom-schema");
            }
        }
    }
}<commit_msg>Create demonstration test for IConventionSet<commit_after>#region License
// Copyright (c) 2019, FluentMigrator Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System.Linq;

using FluentMigrator.Expressions;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Conventions;

using Microsoft.Extensions.DependencyInjection;

using NUnit.Framework;

namespace FluentMigrator.Tests.Unit.Initialization
{
    public class ConventionSetTests
    {
        [Test]
        public void CanLoadConventionSetFromAssembly()
        {
            var services = new ServiceCollection()
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb.AddSQLite().ScanIn(this.GetType().Assembly).For.ConventionSet())
                .BuildServiceProvider(false);
            var conventionSet = services.GetRequiredService<IConventionSet>();
            ISchemaExpression expr = new CreateTableExpression();
            conventionSet.SchemaConvention.Apply(expr);
            Assert.AreEqual("custom-schema", expr.SchemaName);
        }

        [Test]
        public void CanLoadConventionSetFromAssemblyWithScanForAll()
        {
            var services = new ServiceCollection()
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb.AddSQLite().ScanIn(this.GetType().Assembly).For.All())
                .BuildServiceProvider(false);
            var conventionSet = services.GetRequiredService<IConventionSet>();
            ISchemaExpression expr = new CreateTableExpression();
            conventionSet.SchemaConvention.Apply(expr);
            Assert.AreEqual("custom-schema", expr.SchemaName);
        }

        [Test]
        public void NotLoadingConventionSetFromAssemblyWithScanForMigrations()
        {
            var services = new ServiceCollection()
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb.AddSQLite().ScanIn(this.GetType().Assembly).For.Migrations())
                .BuildServiceProvider(false);
            var conventionSet = services.GetRequiredService<IConventionSet>();
            ISchemaExpression expr = new CreateTableExpression();
            conventionSet.SchemaConvention.Apply(expr);
            Assert.AreEqual(null, expr.SchemaName);
        }

        /// <summary>Demonstrate that AddFluentMigratorCore() always registers a class to handle
        /// IConventionSet; even when one has already been registered in the services collection.
        /// Because the ServiceCollection is LIFO, the last one registered is the one that gets
        /// used.</summary>
        [Test]
        public void RegisterOnlyOneConventionSet()
        {
            const string schemaName = "RegTestSchemaA";

            var services = new ServiceCollection();

            services.AddScoped<IConventionSet>(provider =>
                new DefaultConventionSet(
                    defaultSchemaName: schemaName,
                    workingDirectory: null
                ));

            services
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb
                    .AddSQLite()
                    .ScanIn(this.GetType().Assembly).For.Migrations()
                );

            var serviceProvider = services.BuildServiceProvider(false);

            var conventionSets = serviceProvider.GetServices<IConventionSet>();

            Assert.AreEqual(1, conventionSets.Count());
        }

        [Test]
        public void GetCorrectSchemaFromCustomConventionSetIfRegisteredBeforeAddCall()
        {
            const string schemaName = "RegTestSchemaA";

            var services = new ServiceCollection();

            // register our IConventionSet before the call to AddFluentMigratorCore()
            services.AddScoped<IConventionSet>(provider =>
                new DefaultConventionSet(
                    defaultSchemaName: schemaName,
                    workingDirectory: null
                ));

            services
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb
                    .AddSQLite()
                    .ScanIn(this.GetType().Assembly).For.Migrations()
                );

            var serviceProvider = services.BuildServiceProvider(false);

            var conventionSet = serviceProvider.GetRequiredService<IConventionSet>();
            ISchemaExpression expr = new CreateTableExpression();
            conventionSet.SchemaConvention.Apply(expr);

            Assert.AreEqual(schemaName, expr.SchemaName);
        }

        [Test]
        public void GetCorrectSchemaFromCustomConventionSetIfRegisteredAfterAddCall()
        {
            const string schemaName = "RegTestSchemaA";

            var services = new ServiceCollection();

            services
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb
                    .AddSQLite()
                    .ScanIn(this.GetType().Assembly).For.Migrations()
                );

            // register our IConventionSet after the call to AddFluentMigratorCore()
            services.AddScoped<IConventionSet>(provider =>
                new DefaultConventionSet(
                    defaultSchemaName: schemaName,
                    workingDirectory: null
                ));

            var serviceProvider = services.BuildServiceProvider(false);

            var conventionSet = serviceProvider.GetRequiredService<IConventionSet>();
            ISchemaExpression expr = new CreateTableExpression();
            conventionSet.SchemaConvention.Apply(expr);

            Assert.AreEqual(schemaName, expr.SchemaName);
        }

        // ReSharper disable once UnusedMember.Global
        public class CustomConventionSet : ConventionSet
        {
            public CustomConventionSet()
            {
                base.SchemaConvention = new DefaultSchemaConvention("custom-schema");
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08438685208596713, 'fraction_numerical': 0.0017909818794774547, 'mean_word_length': 2.2678141135972463, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2561705', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2237, 'n_words': 527}