using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Modularity;
using Volo.Abp.TestBase;
using Xunit;

namespace Volo.Abp.Data
{
    public class ConnectionStringResolver_Tests : AbpIntegratedTest<ConnectionStringResolver_Tests.TestModule>
    {
        private const string DefaultConnString = "default-value";
        private const string Database1Name = "Database1";
        private const string Database1ConnString = "database-1-value";
        private const string Database2Name = "Database2";

        private readonly IConnectionStringResolver _connectionStringResolver;

        public ConnectionStringResolver_Tests()
        {
            _connectionStringResolver = ServiceProvider.GetRequiredService<IConnectionStringResolver>();
        }

        [Fact]
        public void Should_Get_Default_ConnString_By_Default()
        {
            _connectionStringResolver.Resolve().ShouldBe(DefaultConnString);
        }

        [Fact]
        public void Should_Get_Specific_ConnString_IfDefined()
        {
            _connectionStringResolver.Resolve(Database1Name).ShouldBe(Database1ConnString);
        }

        [Fact]
        public void Should_Get_Default_ConnString_If_Not_Specified()
        {
            _connectionStringResolver.Resolve(Database2Name).ShouldBe(DefaultConnString);
        }

        public class TestModule : AbpModule
        {
            public override void ConfigureServices(IServiceCollection services)
            {
                services.Configure<DbConnectionOptions>(options =>
                {
                    options.ConnectionStrings.Default = DefaultConnString;
                    options.ConnectionStrings[Database1Name] = Database1ConnString;
                });
            }
        }
    }
}
