<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Autofac;
using MarginTrading.AzureRepositories;
using MarginTrading.Backend.Core;
using MarginTrading.Backend.Core.DayOffSettings;
using MarginTrading.Backend.Core.MatchingEngines;
using MarginTrading.Backend.Core.Orderbooks;
using MarginTrading.Backend.Core.Settings;
using MarginTrading.Backend.Services;
using MarginTrading.Backend.Services.AssetPairs;
using MarginTrading.Backend.Services.Events;
using MarginTrading.Backend.Services.MatchingEngines;
using MarginTrading.Backend.Services.Modules;
using MarginTrading.Backend.Services.TradingConditions;
using MarginTrading.Common.Services;
using MarginTrading.Common.Settings;
using MarginTrading.Common.Settings.Models;
using MarginTradingTests.Helpers;
using MarginTradingTests.Modules;
using Moq;

namespace MarginTradingTests
{
    public class BaseTests
    {
        private const string ClientId1 = "1";
        private const string ClientId2 = "2";

        protected IContainer Container { get; set; }

        protected void RegisterDependencies(bool mockEvents = false)
        {
            var builder = new ContainerBuilder();

            builder.RegisterModule(new SettingsModule());
            builder.RegisterModule(new MockBaseServicesModule());
            builder.RegisterModule(new MockRepositoriesModule(Accounts));

            if (mockEvents)
            {
                builder.RegisterModule(new MockEventModule());
            }
            else
            {
                builder.RegisterModule(new EventModule());
            }

            builder.RegisterModule(new CacheModule());
            builder.RegisterModule(
                new ServicesModule(new StaticSettingsManager<RiskInformingSettings>(new RiskInformingSettings
                {
                    Data = new[]
                    {
                        new RiskInformingParams
                        {
                            EventTypeCode = "BE01",
                            Level = "None",
                            System = "QuotesMonitor",
                        },
                        new RiskInformingParams
                        {
                            EventTypeCode = "BE02",
                            Level = "None",
                            System = "QuotesMonitor",
                        }
                    }
                })));
            builder.RegisterModule(new ManagersModule());

            builder.RegisterType<EventChannel<AccountBalanceChangedEventArgs>>()
                .As<IEventChannel<AccountBalanceChangedEventArgs>>()
                .SingleInstance();

            var settingsServiceMock = new Mock<IMarginTradingSettingsService>();
            settingsServiceMock.Setup(s => s.IsMarginTradingEnabled(It.IsAny<string>()))
                .ReturnsAsync(new EnabledMarginTradingTypes {Live = true, Demo = true});
            settingsServiceMock.Setup(s => s.IsMarginTradingEnabled(It.IsAny<string>(), It.IsAny<bool>()))
                .ReturnsAsync(true);


            builder.RegisterInstance(settingsServiceMock.Object)
                .As<IMarginTradingSettingsService>()
                .SingleInstance();


            builder.RegisterInstance(new Mock<IMarginTradingOperationsLogService>().Object)
                .As<IMarginTradingOperationsLogService>()
                .SingleInstance();

            var settings = new ScheduleSettings(
                DayOfWeek.Sunday,
                new TimeSpan(21, 0, 0),
                DayOfWeek.Sunday,
                new TimeSpan(21, 0, 0),
                new[] {"BTCCHF"}.ToHashSet(),
                TimeSpan.Zero);
            var dayOffSettingsService = new Mock<IDayOffSettingsService>(MockBehavior.Strict);
            dayOffSettingsService.Setup(s => s.GetScheduleSettings()).Returns(settings);
            dayOffSettingsService.Setup(s => s.GetExclusions(It.IsNotNull<string>()))
                .Returns(ImmutableArray<DayOffExclusion>.Empty);
            builder.RegisterInstance(dayOffSettingsService.Object).SingleInstance();
            builder.Register<IDayOffSettingsRepository>(c =>
                new DayOffSettingsRepository(c.Resolve<IMarginTradingBlobRepository>())).SingleInstance();

            builder.RegisterBuildCallback(c => c.Resolve<AccountAssetsManager>());
            builder.RegisterBuildCallback(c => c.Resolve<OrderCacheManager>());

            Container = builder.Build();

            var meRepository = Container.Resolve<IMatchingEngineRepository>();
            meRepository.InitMatchingEngines(new List<IMatchingEngineBase>
            {
                Container.Resolve<IInternalMatchingEngine>(),
                new RejectMatchingEngine()
            });

            MtServiceLocator.FplService = Container.Resolve<IFplService>();
            MtServiceLocator.AccountUpdateService = Container.Resolve<IAccountUpdateService>();
            MtServiceLocator.AccountsCacheService = Container.Resolve<IAccountsCacheService>();
            MtServiceLocator.SwapCommissionService = Container.Resolve<ICommissionService>();

            Container.Resolve<OrderBookList>().Init(null);
        }

        protected List<MarginTradingAccount> Accounts = new List<MarginTradingAccount>
        {
            new MarginTradingAccount
            {
                Id = Guid.NewGuid().ToString("N"),<commit_msg>LWDEV-4991 tests fix<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Autofac;
using MarginTrading.AzureRepositories;
using MarginTrading.Backend.Core;
using MarginTrading.Backend.Core.DayOffSettings;
using MarginTrading.Backend.Core.MatchingEngines;
using MarginTrading.Backend.Core.Orderbooks;
using MarginTrading.Backend.Core.Settings;
using MarginTrading.Backend.Services;
using MarginTrading.Backend.Services.AssetPairs;
using MarginTrading.Backend.Services.Events;
using MarginTrading.Backend.Services.MatchingEngines;
using MarginTrading.Backend.Services.Modules;
using MarginTrading.Backend.Services.TradingConditions;
using MarginTrading.Common.Services;
using MarginTrading.Common.Settings;
using MarginTrading.Common.Settings.Models;
using MarginTradingTests.Helpers;
using MarginTradingTests.Modules;
using Moq;

namespace MarginTradingTests
{
    public class BaseTests
    {
        private const string ClientId1 = "1";
        private const string ClientId2 = "2";

        protected IContainer Container { get; set; }

        protected void RegisterDependencies(bool mockEvents = false)
        {
            var builder = new ContainerBuilder();

            builder.RegisterModule(new SettingsModule());
            builder.RegisterModule(new MockBaseServicesModule());
            builder.RegisterModule(new MockRepositoriesModule(Accounts));

            if (mockEvents)
            {
                builder.RegisterModule(new MockEventModule());
            }
            else
            {
                builder.RegisterModule(new EventModule());
            }

            builder.RegisterModule(new CacheModule());
            builder.RegisterModule(
                new ServicesModule(new StaticSettingsManager<RiskInformingSettings>(new RiskInformingSettings
                {
                    Data = new[]
                    {
                        new RiskInformingParams
                        {
                            EventTypeCode = "BE01",
                            Level = "None",
                            System = "QuotesMonitor",
                        },
                        new RiskInformingParams
                        {
                            EventTypeCode = "BE02",
                            Level = "None",
                            System = "QuotesMonitor",
                        }
                    }
                })));
            builder.RegisterModule(new ManagersModule());

            builder.RegisterType<EventChannel<AccountBalanceChangedEventArgs>>()
                .As<IEventChannel<AccountBalanceChangedEventArgs>>()
                .SingleInstance();

            var settingsServiceMock = new Mock<IMarginTradingSettingsService>();
            settingsServiceMock.Setup(s => s.IsMarginTradingEnabled(It.IsAny<string>()))
                .ReturnsAsync(new EnabledMarginTradingTypes {Live = true, Demo = true});
            settingsServiceMock.Setup(s => s.IsMarginTradingEnabled(It.IsAny<string>(), It.IsAny<bool>()))
                .ReturnsAsync(true);


            builder.RegisterInstance(settingsServiceMock.Object)
                .As<IMarginTradingSettingsService>()
                .SingleInstance();


            builder.RegisterInstance(new Mock<IMarginTradingOperationsLogService>().Object)
                .As<IMarginTradingOperationsLogService>()
                .SingleInstance();

            var settings = new ScheduleSettings(
                DayOfWeek.Sunday,
                new TimeSpan(21, 0, 0),
                DayOfWeek.Sunday,
                new TimeSpan(21, 0, 0),
                new[] {"BTCCHF"}.ToHashSet(),
                TimeSpan.Zero);
            var dayOffSettingsService = new Mock<IDayOffSettingsService>(MockBehavior.Strict);
            dayOffSettingsService.Setup(s => s.GetScheduleSettings()).Returns(settings);
            dayOffSettingsService.Setup(s => s.GetExclusions(It.IsNotNull<string>()))
                .Returns(ImmutableArray<DayOffExclusion>.Empty);
            builder.RegisterInstance(dayOffSettingsService.Object).SingleInstance();
            builder.Register<IDayOffSettingsRepository>(c =>
                new DayOffSettingsRepository(c.Resolve<IMarginTradingBlobRepository>())).SingleInstance();

            builder.RegisterBuildCallback(c => c.Resolve<AccountAssetsManager>());
            builder.RegisterBuildCallback(c => c.Resolve<OrderCacheManager>());
            builder.RegisterInstance(new Mock<IMtSlackNotificationsSender>(MockBehavior.Loose).Object).SingleInstance();

            Container = builder.Build();

            var meRepository = Container.Resolve<IMatchingEngineRepository>();
            meRepository.InitMatchingEngines(new List<IMatchingEngineBase>
            {
                Container.Resolve<IInternalMatchingEngine>(),
                new RejectMatchingEngine()
            });

            MtServiceLocator.FplService = Container.Resolve<IFplService>();
            MtServiceLocator.AccountUpdateService = Container.Resolve<IAccountUpdateService>();
            MtServiceLocator.AccountsCacheService = Container.Resolve<IAccountsCacheService>();
            MtServiceLocator.SwapCommissionService = Container.Resolve<ICommissionService>();

            Container.Resolve<OrderBookList>().Init(null);
        }

        protected List<MarginTradingAccount> Accounts = new List<MarginTradingAccount>
        {
            new MarginTradingAccount
            {
                Id = Guid.NewGuid().ToString("N"),
{'fraction_non_alphanumeric': 0.0902010505343235, 'fraction_numerical': 0.003260278934975548, 'mean_word_length': 2.259445100354191, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1672982', 'n_tokens_mistral': 2735, 'n_tokens_neox': 2649, 'n_words': 409}