<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using SFA.DAS.EAS.Account.Api.Types;
using SFA.DAS.EAS.Account.API.IntegrationTests.TestUtils.ApiTester;
using SFA.DAS.EAS.Account.API.IntegrationTests.TestUtils.DataAccess.DataHelpers;
using SFA.DAS.EAS.Domain.Models.Payments;
using SFA.DAS.EAS.Infrastructure.Data;
using SFA.DAS.Hashing;
using SFA.DAS.NLog.Logger;
using SFA.DAS.Provider.Events.Api.Types;

namespace SFA.DAS.EAS.Account.API.IntegrationTests.StatisticsControllerTests
{
    [TestFixture]
    public class WhenIGetTheStatistics
    {
        private ApiIntegrationTester _apiTester;
        private CallResponse<StatisticsViewModel> _actualResponse;
        private StatisticsViewModel _expectedStatisticsViewModel;

        [SetUp]
        public async Task Setup()
        {
            var call = new CallRequirements("api/statistics")
                .AllowStatusCodes(HttpStatusCode.OK);
            _apiTester = new ApiIntegrationTester();
            var accountStatisticsDataHelper = new AccountStatisticsDataHelper(_apiTester.EmployerApprenticeshipsServiceConfiguration.DatabaseConnectionString);
            var financeStatisticsDataHelper = new FinanceStatisticsDataHelper(_apiTester.LevyDeclarationProviderConfiguration.DatabaseConnectionString);
            
            _expectedStatisticsViewModel = await accountStatisticsDataHelper.GetStatistics();
            if (_expectedStatisticsViewModel.TotalAccounts == 0
                || _expectedStatisticsViewModel.TotalAgreements == 0
                || _expectedStatisticsViewModel.TotalLegalEntities == 0
                || _expectedStatisticsViewModel.TotalPayeSchemes == 0)
            {
                using (var connection = new SqlConnection(_apiTester.EmployerApprenticeshipsServiceConfiguration.DatabaseConnectionString))
                {
                    connection.Open();
                    var transaction = connection.BeginTransaction();
                    var accountDbContext = new EmployerAccountsDbContext(connection, transaction);
                    var lazyDb = new Lazy<EmployerAccountsDbContext>(() => accountDbContext);
                    var accountRepo = new AccountRepository(_apiTester.EmployerApprenticeshipsServiceConfiguration,
                        Mock.Of<ILog>(), lazyDb, Mock.Of<IAccountLegalEntityPublicHashingService>());
                    await accountRepo.CreateAccount(10003, "234", "integration-tests", "address", DateTime.Today, "emp-ref", 
                        "access-token", "refresh-token", "company-status", "emp ref name", 2, 1, "sector");
                    transaction.Commit();
                }
                
                _expectedStatisticsViewModel = await accountStatisticsDataHelper.GetStatistics();
            }

            var financialStatistics = await financeStatisticsDataHelper.GetStatistics();
            if (financialStatistics.TotalPayments == 0)
            {
                //todo: this does not need the using as it currently stands.
                using (var connection = new SqlConnection(_apiTester.LevyDeclarationProviderConfiguration.DatabaseConnectionString))
                {
                    connection.Open();

                    var financeDbContext = new EmployerFinanceDbContext(_apiTester.LevyDeclarationProviderConfiguration.DatabaseConnectionString);
                    var lazyDb = new Lazy<EmployerFinanceDbContext>(() => financeDbContext);
                    var levyRepository = new DasLevyRepository(_apiTester.LevyDeclarationProviderConfiguration,
                        Mock.Of<ILog>(), lazyDb);
                    await levyRepository.CreatePayments(new List<PaymentDetails>
                    {
                        new PaymentDetails
                        {
                            ProviderName = "prov name",
                            StandardCode = 31,
                            ProgrammeType = 3,
                            CourseName = "course name",
                            ApprenticeName = "apprentice name",
                            ApprenticeNINumber = "nin",
                            CourseLevel = 3,
                            CourseStartDate = DateTime.Today,

                            Ukprn = 134234,
                            Uln   = 323423,
                            EmployerAccountId = 342,
                            ApprenticeshipId = 1,
                            DeliveryPeriodMonth = 5,
                            DeliveryPeriodYear = 2018,
                            CollectionPeriodId = "R05",
                            CollectionPeriodMonth = 4,
                            CollectionPeriodYear = 2018,
                            EvidenceSubmittedOn = DateTime.Today,
                            EmployerAccountVersion = "emp acct ver",
                            ApprenticeshipVersion = "appr ver",
                            FundingSource = FundingSource.Levy,
                            TransactionType = TransactionType.Learning,
                            Amount = 234,
                            PeriodEnd = "R06"
                        }
                    });
                    // todo: get hold of the trans and commit it.
                }

                financialStatistics = await financeStatisticsDataHelper.GetStatistics();
            }

            _expectedStatisticsViewModel.TotalPayments = financialStatistics.TotalPayments;

            _actualResponse = await _apiTester.InvokeGetAsync<StatisticsViewModel>(call);
        }

        [Test]
        public void ThenTheStatusShouldBeOk()
        {
            _actualResponse.Response.StatusCode
                .Should().Be(HttpStatusCode.OK);
        }

        [Test]
        public void ThenTotalAccountsIsCorrect()
        {
            _actualResponse.Data.TotalAccounts
                .Should().Be(_expectedStatisticsViewModel.TotalAccounts);
        }

        [Test]
        public void ThenTotalAgreementsIsCorrect()
        {
            _actualResponse.Data.TotalAgreements
                .Should().Be(_expectedStatisticsViewModel.TotalAgreements);
        }

        [Test]
        public void ThenTotalLegalEntitiesIsCorrect()
        {
            _actualResponse.Data.TotalLegalEntities
                .Should().Be(_expectedStatisticsViewModel.TotalLegalEntities);
        }

        [Test]
        public void ThenTotalPayeSchemesIsCorrect()
        {
            _actualResponse.Data.TotalPayeSchemes
                .Should().Be(_expectedStatisticsViewModel.TotalPayeSchemes);
        }

        [Test]
        public void ThenTotalPaymentsIsCorrect()
        {
            _actualResponse.Data.TotalPayments
                .Should().Be(_expectedStatisticsViewModel.TotalPayments);
        }
    }
}
<commit_msg>setting up finance data correctly now.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using SFA.DAS.EAS.Account.Api.Types;
using SFA.DAS.EAS.Account.API.IntegrationTests.TestUtils.ApiTester;
using SFA.DAS.EAS.Account.API.IntegrationTests.TestUtils.DataAccess.DataHelpers;
using SFA.DAS.EAS.Domain.Models.Payments;
using SFA.DAS.EAS.Infrastructure.Data;
using SFA.DAS.Hashing;
using SFA.DAS.NLog.Logger;
using SFA.DAS.Provider.Events.Api.Types;

namespace SFA.DAS.EAS.Account.API.IntegrationTests.StatisticsControllerTests
{
    [TestFixture]
    public class WhenIGetTheStatistics
    {
        private ApiIntegrationTester _apiTester;
        private CallResponse<StatisticsViewModel> _actualResponse;
        private StatisticsViewModel _expectedStatisticsViewModel;

        [SetUp]
        public async Task Setup()
        {
            var call = new CallRequirements("api/statistics")
                .AllowStatusCodes(HttpStatusCode.OK);
            _apiTester = new ApiIntegrationTester();
            var accountStatisticsDataHelper = new AccountStatisticsDataHelper(_apiTester.EmployerApprenticeshipsServiceConfiguration.DatabaseConnectionString);
            var financeStatisticsDataHelper = new FinanceStatisticsDataHelper(_apiTester.LevyDeclarationProviderConfiguration.DatabaseConnectionString);
            
            _expectedStatisticsViewModel = await accountStatisticsDataHelper.GetStatistics();
            if (_expectedStatisticsViewModel.TotalAccounts == 0
                || _expectedStatisticsViewModel.TotalAgreements == 0
                || _expectedStatisticsViewModel.TotalLegalEntities == 0
                || _expectedStatisticsViewModel.TotalPayeSchemes == 0)
            {
                using (var connection = new SqlConnection(_apiTester.EmployerApprenticeshipsServiceConfiguration.DatabaseConnectionString))
                {
                    connection.Open();
                    var transaction = connection.BeginTransaction();
                    var accountDbContext = new EmployerAccountsDbContext(connection, transaction);
                    var lazyDb = new Lazy<EmployerAccountsDbContext>(() => accountDbContext);
                    var accountRepo = new AccountRepository(_apiTester.EmployerApprenticeshipsServiceConfiguration,
                        Mock.Of<ILog>(), lazyDb, Mock.Of<IAccountLegalEntityPublicHashingService>());
                    await accountRepo.CreateAccount(10003/**/, "234", "integration-tests", "address", DateTime.Today, "emp-ref", 
                        "access-token", "refresh-token", "company-status", "emp ref name", 2, 1, "sector");
                    transaction.Commit();
                }
                
                _expectedStatisticsViewModel = await accountStatisticsDataHelper.GetStatistics();
            }

            var financialStatistics = await financeStatisticsDataHelper.GetStatistics();
            if (financialStatistics.TotalPayments == 0)
            {
                var financeDbContext = new EmployerFinanceDbContext(_apiTester.LevyDeclarationProviderConfiguration.DatabaseConnectionString);
                var lazyDb = new Lazy<EmployerFinanceDbContext>(() => financeDbContext);
                var levyRepository = new DasLevyRepository(_apiTester.LevyDeclarationProviderConfiguration,
                    Mock.Of<ILog>(), lazyDb);
                await levyRepository.CreatePayments(new List<PaymentDetails>
                {
                    new PaymentDetails
                    {
                        ProviderName = "<NAME>",
                        StandardCode = 31,
                        ProgrammeType = 3,
                        CourseName = "course name",
                        ApprenticeName = "apprentice name",
                        ApprenticeNINumber = "nin",
                        CourseLevel = 3,
                        CourseStartDate = DateTime.Today,

                        Ukprn = 134234,
                        Uln   = 323423,
                        EmployerAccountId = 342,
                        ApprenticeshipId = 1,
                        DeliveryPeriodMonth = 5,
                        DeliveryPeriodYear = 2018,
                        CollectionPeriodId = "R05",
                        CollectionPeriodMonth = 4,
                        CollectionPeriodYear = 2018,
                        EvidenceSubmittedOn = DateTime.Today,
                        EmployerAccountVersion = "emp acct ver",
                        ApprenticeshipVersion = "appr ver",
                        FundingSource = FundingSource.Levy,
                        TransactionType = TransactionType.Learning,
                        Amount = 234,
                        PeriodEnd = "R06"
                    }
                });
                financeDbContext.Database.CurrentTransaction.Commit();

                financialStatistics = await financeStatisticsDataHelper.GetStatistics();
            }

            _expectedStatisticsViewModel.TotalPayments = financialStatistics.TotalPayments;

            _actualResponse = await _apiTester.InvokeGetAsync<StatisticsViewModel>(call);
        }

        [Test]
        public void ThenTheStatusShouldBeOk()
        {
            _actualResponse.Response.StatusCode
                .Should().Be(HttpStatusCode.OK);
        }

        [Test]
        public void ThenTotalAccountsIsCorrect()
        {
            _actualResponse.Data.TotalAccounts
                .Should().Be(_expectedStatisticsViewModel.TotalAccounts);
        }

        [Test]
        public void ThenTotalAgreementsIsCorrect()
        {
            _actualResponse.Data.TotalAgreements
                .Should().Be(_expectedStatisticsViewModel.TotalAgreements);
        }

        [Test]
        public void ThenTotalLegalEntitiesIsCorrect()
        {
            _actualResponse.Data.TotalLegalEntities
                .Should().Be(_expectedStatisticsViewModel.TotalLegalEntities);
        }

        [Test]
        public void ThenTotalPayeSchemesIsCorrect()
        {
            _actualResponse.Data.TotalPayeSchemes
                .Should().Be(_expectedStatisticsViewModel.TotalPayeSchemes);
        }

        [Test]
        public void ThenTotalPaymentsIsCorrect()
        {
            _actualResponse.Data.TotalPayments
                .Should().Be(_expectedStatisticsViewModel.TotalPayments);
        }
    }
}

{'fraction_non_alphanumeric': 0.07266256395047083, 'fraction_numerical': 0.007711129235560169, 'mean_word_length': 1.8808201623237932, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4559751', 'n_tokens_mistral': 3342, 'n_tokens_neox': 3044, 'n_words': 533}