public async Task WhenCallingGetYearToDatePayments_WithMultipleProviders_TheYtdPaymentsAreCorrect()
        {
            var mocker = AutoMock.GetLoose();

            var payments = new List <PaymentModel>
            {
                new PaymentModel
                {
                    Ukprn            = 1,
                    CollectionPeriod = CollectionPeriodFactory.CreateFromAcademicYearAndPeriod(2021, 1),
                    ContractType     = ContractType.Act1,
                    Amount           = 1000,
                },
                new PaymentModel
                {
                    Ukprn            = 1,
                    CollectionPeriod = CollectionPeriodFactory.CreateFromAcademicYearAndPeriod(2021, 1),
                    ContractType     = ContractType.Act2,
                    Amount           = 2000,
                },

                new PaymentModel
                {
                    Ukprn            = 2,
                    CollectionPeriod = CollectionPeriodFactory.CreateFromAcademicYearAndPeriod(2021, 1),
                    ContractType     = ContractType.Act1,
                    Amount           = 10000,
                },
                new PaymentModel
                {
                    Ukprn            = 2,
                    CollectionPeriod = CollectionPeriodFactory.CreateFromAcademicYearAndPeriod(2021, 1),
                    ContractType     = ContractType.Act2,
                    Amount           = 20000,
                },
            };



            using (var context = new InMemoryMetricsQueryDataContext())
            {
                var factory = mocker.Mock <IMetricsQueryDataContextFactory>();
                factory.Setup(x => x.Create())
                .Returns(context);
                var sut = mocker.Create <PeriodEndMetricsRepository>();

                foreach (var payment in payments)
                {
                    await context.AddAsync(payment);
                }

                await context.SaveChangesAsync();

                var actual = await sut.GetYearToDatePayments(2021, 2, CancellationToken.None);

                actual.Where(x => x.Ukprn == 1).Should().HaveCount(1);
                actual.Where(x => x.Ukprn == 2).Should().HaveCount(1);
                actual.Where(x => x.Ukprn == 3).Should().BeEmpty();

                actual.Single(x => x.Ukprn == 1).Total.Should().Be(3000);
                actual.Single(x => x.Ukprn == 2).Total.Should().Be(30000);
            }
        }
        public async Task Includes_ClawbackPayments_In_Required_Payments()
        {
            await inMemoryMetricsQueryDataContext.RequiredPaymentEvents.AddAsync(new RequiredPaymentEventModel
            {
                Id               = 1,
                EventId          = Guid.NewGuid(),
                Ukprn            = 1234,
                JobId            = 123,
                CollectionPeriod = new CollectionPeriod {
                    AcademicYear = 1920, Period = 1
                },
                Amount          = 11000,
                TransactionType = TransactionType.Learning,
                ContractType    = ContractType.Act1,
            });

            await inMemoryMetricsQueryDataContext.RequiredPaymentEvents.AddAsync(new RequiredPaymentEventModel
            {
                Id               = 2,
                EventId          = Guid.NewGuid(),
                Ukprn            = 1234,
                JobId            = 123,
                CollectionPeriod = new CollectionPeriod {
                    AcademicYear = 1920, Period = 1
                },
                Amount          = 15000,
                TransactionType = TransactionType.Learning,
                ContractType    = ContractType.Act2
            });

            await inMemoryMetricsQueryDataContext.Payments.AddAsync(new PaymentModel
            {
                Id               = 3,
                EventId          = Guid.NewGuid(),
                Ukprn            = 1234,
                JobId            = 123,
                CollectionPeriod = new CollectionPeriod {
                    AcademicYear = 1920, Period = 1
                },
                Amount          = 300,
                FundingSource   = FundingSourceType.CoInvestedSfa,
                TransactionType = TransactionType.Learning,
                ContractType    = ContractType.Act1,
                ClawbackSourcePaymentEventId = Guid.NewGuid(),
            });

            await inMemoryMetricsQueryDataContext.Payments.AddAsync(new PaymentModel
            {
                Id               = 4,
                EventId          = Guid.NewGuid(),
                Ukprn            = 1234,
                JobId            = 123,
                CollectionPeriod = new CollectionPeriod {
                    AcademicYear = 1920, Period = 1
                },
                Amount          = 300,
                FundingSource   = FundingSourceType.FullyFundedSfa,
                TransactionType = TransactionType.Learning,
                ContractType    = ContractType.Act2,
                ClawbackSourcePaymentEventId = Guid.NewGuid(),
            });

            await inMemoryMetricsQueryDataContext.Payments.AddAsync(new PaymentModel
            {
                Id               = 5,
                EventId          = Guid.NewGuid(),
                Ukprn            = 1234,
                JobId            = 123,
                CollectionPeriod = new CollectionPeriod {
                    AcademicYear = 1920, Period = 1
                },
                Amount          = 300,
                FundingSource   = FundingSourceType.Levy,
                TransactionType = TransactionType.Learning,
                ContractType    = ContractType.Act1,
                ClawbackSourcePaymentEventId = Guid.NewGuid(),
            });

            await inMemoryMetricsQueryDataContext.SaveChangesAsync();

            var service = moqer.Create <SubmissionMetricsService>();
            await service.BuildMetrics(1234, 123, 1920, 1, CancellationToken.None).ConfigureAwait(false);

            submissionMetricsRepository
            .Verify(x => x.SaveSubmissionMetrics(It.Is <SubmissionSummaryModel>(s => s.RequiredPayments.ContractType1 == 11300 && s.RequiredPayments.ContractType2 == 15300), It.IsAny <CancellationToken>()), Times.Once);
        }