private static StoreALB StoreALBSetup()
        {
            var fundingOutput =
                new ALB_global
            {
                UKPRN       = _ukprn,
                ALB_Learner = new List <ALB_Learner>
                {
                    new ALB_Learner
                    {
                        LearnRefNumber     = "0ALB01",
                        UKPRN              = _ukprn,
                        ALB_Learner_Period = new List <ALB_Learner_Period>
                        {
                            new ALB_Learner_Period
                            {
                                LearnRefNumber = "0ALB01",
                                UKPRN          = _ukprn,
                                Period         = 1,
                                ALBSeqNum      = 1
                            }
                        },
                        ALB_Learner_PeriodisedValues = new List <ALB_Learner_PeriodisedValues>
                        {
                            new ALB_Learner_PeriodisedValues
                            {
                                LearnRefNumber = "0ALB01",
                                UKPRN          = _ukprn,
                                AttributeName  = "ALBSeqNum",
                                Period_1       = 1.0m,
                                Period_2       = 1.0m,
                                Period_3       = 1.0m,
                                Period_4       = 1.0m,
                                Period_5       = 1.0m,
                                Period_6       = 1.0m,
                                Period_7       = 1.0m,
                                Period_8       = 1.0m,
                                Period_9       = 1.0m,
                                Period_10      = 1.0m,
                                Period_11      = 1.0m,
                                Period_12      = 1.0m,
                            }
                        },
                        ALB_LearningDelivery = new List <ALB_LearningDelivery>
                        {
                            new ALB_LearningDelivery
                            {
                                UKPRN          = _ukprn,
                                LearnRefNumber = "0ALB01",
                                AimSeqNumber   = 1,
                                ALB_LearningDelivery_Period = new List <ALB_LearningDelivery_Period>
                                {
                                    new ALB_LearningDelivery_Period
                                    {
                                        UKPRN          = _ukprn,
                                        LearnRefNumber = "0ALB01",
                                        AimSeqNumber   = 1,
                                        Period         = 1
                                    }
                                },
                                ALB_LearningDelivery_PeriodisedValues = new List <ALB_LearningDelivery_PeriodisedValues>
                                {
                                    new ALB_LearningDelivery_PeriodisedValues
                                    {
                                        UKPRN          = _ukprn,
                                        LearnRefNumber = "0ALB01",
                                        AimSeqNumber   = 1,
                                        AttributeName  = "Attribute1"
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var global = new List <ALB_global>()
            {
                fundingOutput
            };
            var learners                         = fundingOutput.ALB_Learner;
            var learnerPeriods                   = fundingOutput.ALB_Learner.SelectMany(l => l.ALB_Learner_Period);
            var learnerPeriodisedValues          = fundingOutput.ALB_Learner.SelectMany(l => l.ALB_Learner_PeriodisedValues);
            var learningDeliveries               = fundingOutput.ALB_Learner.SelectMany(ld => ld.ALB_LearningDelivery);
            var learningDeliveryPeriod           = fundingOutput.ALB_Learner.SelectMany(ld => ld.ALB_LearningDelivery.SelectMany(p => p.ALB_LearningDelivery_Period));
            var learningDeliveryPeriodisedValues = fundingOutput.ALB_Learner.SelectMany(ld => ld.ALB_LearningDelivery.SelectMany(p => p.ALB_LearningDelivery_PeriodisedValues));

            var         albMapperMock = new Mock <IALBMapper>();
            IBulkInsert bulkInsert    = new BulkInsert();

            albMapperMock.Setup(fm => fm.MapGlobals(_fundingOutputs)).Returns(global);
            albMapperMock.Setup(fm => fm.MapLearners(_fundingOutputs)).Returns(learners);
            albMapperMock.Setup(fm => fm.MapLearnerPeriods(_fundingOutputs)).Returns(learnerPeriods);
            albMapperMock.Setup(fm => fm.MapLearnerPeriodisedValues(_fundingOutputs)).Returns(learnerPeriodisedValues);
            albMapperMock.Setup(fm => fm.MapLearningDeliveries(_fundingOutputs)).Returns(learningDeliveries);
            albMapperMock.Setup(fm => fm.MapLearningDeliveryPeriods(_fundingOutputs)).Returns(learningDeliveryPeriod);
            albMapperMock.Setup(fm => fm.MapLearningDeliveryPeriodisedValues(_fundingOutputs)).Returns(learningDeliveryPeriodisedValues);

            return(new StoreALB(bulkInsert));
        }
Exemplo n.º 2
0
        public async Task StoreIlr(string ilrFilename, string albDataFilename, string valErrorsFilename, int ukPrn, string[] validLearners)
        {
            CancellationToken cancellationToken = default(CancellationToken);
            var logger    = new Mock <ILogger>();
            var storage   = new Mock <IKeyValuePersistenceService>();
            var persist   = new Mock <IKeyValuePersistenceService>();
            var serialise = new Mock <ISerializationService>();
            var validationErrorsService = new Mock <IValidationErrorsService>();
            var dateTimeProviderMock    = new Mock <IDateTimeProvider>();

            dateTimeProviderMock.Setup(p => p.GetNowUtc()).Returns(new DateTime(2018, 1, 1));

            Stopwatch stopwatch = new Stopwatch();

            var validLearnersBuilder   = new ValidLearnerDataMapper();
            var invalidLearnersBuilder = new InvalidLearnerDataMapper();

            if (validLearners.Length == 1 && validLearners[0].EndsWith(".csv", StringComparison.OrdinalIgnoreCase))
            {
                validLearners = ReadValidLearners(stopwatch, ilrFilename, validLearners[0]);
            }

            Message message = null;
            Tuple <Message, ALBGlobal, ValidationErrorDto[], IDataStoreContext> readAndSerialise = await ReadAndDeserialiseAsync(ilrFilename, albDataFilename, valErrorsFilename, validLearners.ToList(), storage, persist, serialise, validationErrorsService);

            var dataStoreContext = readAndSerialise.Item4;

            message = readAndSerialise.Item1;

            try
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["IlrTestConnectionString"]))
                    {
                        await connection.OpenAsync();

                        output.WriteLine($"SQL Connect: {stopwatch.ElapsedMilliseconds}");
                        stopwatch.Restart();

                        StoreClear storeClear = new StoreClear();
                        await storeClear.ClearAsync(dataStoreContext, connection, cancellationToken);

                        output.WriteLine($"Clear: {stopwatch.ElapsedMilliseconds} {ukPrn} {ilrFilename}");
                        stopwatch.Restart();

                        ////StoreProcessingInformationData storeFileDetails = new StoreProcessingInformationData(dateTimeProviderMock.Object);
                        ////await storeFileDetails.StoreAsync(dataStoreContext, connection, cancellationToken);

                        output.WriteLine($"File details: {stopwatch.ElapsedMilliseconds}");
                        stopwatch.Restart();

                        ////StoreIlr storeIlr = new StoreIlr(logger.Object, validLearnersBuilder, invalidLearnersBuilder);
                        ////await storeIlr.StoreAsync(dataStoreContext, connection, message, validLearners.ToList(), cancellationToken);

                        output.WriteLine($"Store ILR: {stopwatch.ElapsedMilliseconds}");
                        stopwatch.Restart();

                        var fundingOutput =
                            new ALB_global
                        {
                            UKPRN       = _ukprn,
                            ALB_Learner = new List <ALB_Learner>
                            {
                                new ALB_Learner
                                {
                                    LearnRefNumber     = "0ALB01",
                                    UKPRN              = _ukprn,
                                    ALB_Learner_Period = new List <ALB_Learner_Period>
                                    {
                                        new ALB_Learner_Period
                                        {
                                            LearnRefNumber = "0ALB01",
                                            UKPRN          = _ukprn,
                                            Period         = 1,
                                            ALBSeqNum      = 1
                                        }
                                    },
                                    ALB_Learner_PeriodisedValues = new List <ALB_Learner_PeriodisedValues>
                                    {
                                        new ALB_Learner_PeriodisedValues
                                        {
                                            LearnRefNumber = "0ALB01",
                                            UKPRN          = _ukprn,
                                            AttributeName  = "ALBSeqNum",
                                            Period_1       = 1.0m,
                                            Period_2       = 1.0m,
                                            Period_3       = 1.0m,
                                            Period_4       = 1.0m,
                                            Period_5       = 1.0m,
                                            Period_6       = 1.0m,
                                            Period_7       = 1.0m,
                                            Period_8       = 1.0m,
                                            Period_9       = 1.0m,
                                            Period_10      = 1.0m,
                                            Period_11      = 1.0m,
                                            Period_12      = 1.0m,
                                        }
                                    },
                                    ALB_LearningDelivery = new List <ALB_LearningDelivery>
                                    {
                                        new ALB_LearningDelivery
                                        {
                                            UKPRN          = _ukprn,
                                            LearnRefNumber = "0ALB01",
                                            AimSeqNumber   = 1,
                                            ALB_LearningDelivery_Period = new List <ALB_LearningDelivery_Period>
                                            {
                                                new ALB_LearningDelivery_Period
                                                {
                                                    UKPRN          = _ukprn,
                                                    LearnRefNumber = "0ALB01",
                                                    AimSeqNumber   = 1,
                                                    Period         = 1
                                                }
                                            },
                                            ALB_LearningDelivery_PeriodisedValues = new List <ALB_LearningDelivery_PeriodisedValues>
                                            {
                                                new ALB_LearningDelivery_PeriodisedValues
                                                {
                                                    UKPRN          = _ukprn,
                                                    LearnRefNumber = "0ALB01",
                                                    AimSeqNumber   = 1,
                                                    AttributeName  = "Attribute1"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        };

                        var global                           = fundingOutput;
                        var learners                         = fundingOutput.ALB_Learner;
                        var learnerPeriods                   = fundingOutput.ALB_Learner.SelectMany(l => l.ALB_Learner_Period);
                        var learnerPeriodisedValues          = fundingOutput.ALB_Learner.SelectMany(l => l.ALB_Learner_PeriodisedValues);
                        var learningDeliveries               = fundingOutput.ALB_Learner.SelectMany(ld => ld.ALB_LearningDelivery);
                        var learningDeliveryPeriod           = fundingOutput.ALB_Learner.SelectMany(ld => ld.ALB_LearningDelivery.SelectMany(p => p.ALB_LearningDelivery_Period));
                        var learningDeliveryPeriodisedValues = fundingOutput.ALB_Learner.SelectMany(ld => ld.ALB_LearningDelivery.SelectMany(p => p.ALB_LearningDelivery_PeriodisedValues));

                        IALBMapper  albMapperMock = new ALBMapper();
                        IBulkInsert bulkInsert    = new BulkInsert();

                        ////StoreALB storeRuleAlb = new StoreALB(albMapperMock, bulkInsert);
                        ////await storeRuleAlb.StoreAsync(readAndSerialise.Item2, connection, cancellationToken);

                        output.WriteLine($"Store ALB: {stopwatch.ElapsedMilliseconds}");
                        stopwatch.Restart();

                        // StoreValidationOutput storeValidationOutput = new StoreValidationOutput(null, validationErrorsService.Object);
                        // await storeValidationOutput.StoreAsync(dataStoreContext, transaction, ukPrn, message, cancellationToken);
                        output.WriteLine($"Store Val: {stopwatch.ElapsedMilliseconds}");
                        stopwatch.Restart();

                        output.WriteLine($"Commit: {stopwatch.ElapsedMilliseconds}");
                        stopwatch.Restart();

                        using (SqlCommand sqlCommand =
                                   new SqlCommand($"SELECT Count(LearnRefNumber) FROM Valid.Learner Where UKPRN = {ukPrn}", connection))
                        {
                            Assert.Equal(validLearners.Length, sqlCommand.ExecuteScalar());
                        }

                        using (SqlCommand sqlCommand =
                                   new SqlCommand(
                                       $"SELECT Count(LearnRefNumber) FROM Invalid.Learner Where UKPRN = {ukPrn}",
                                       connection))
                        {
                            Assert.Equal(message?.Learner.Length - validLearners.Length, sqlCommand.ExecuteScalar());
                        }

                        using (SqlCommand sqlCommand =
                                   new SqlCommand($"SELECT Count(LearnRefNumber) FROM Rulebase.ALB_Learner Where UKPRN = {ukPrn}", connection))
                        {
                            Assert.Equal(4, sqlCommand.ExecuteScalar());
                        }

                        output.WriteLine($"Assert: {stopwatch.ElapsedMilliseconds}");
                        stopwatch.Restart();
                    }

                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }