public void AddCarbonEmissionEntryMustCallDbSetAdd()
        {
            //Arrange
            var mockContext = new Mock<DataContext>();
            var mockSet = new Mock<FakeDbSet<CarbonEmissionEntry>>();
            mockContext
                .Setup(context => context.CarbonEmissionEntries)
                .Returns(mockSet.Object);
            var entry = new CarbonEmissionEntry {Id = 3};
            var sut = new DataEntriesUnitOfWork(() => mockContext.Object);

            //Act
            sut.AddCarbonEmissionEntry(entry);

            //Assert
            mockSet.Verify(set => set.Add(It.Is<CarbonEmissionEntry>(e => e.Id == 3)));
        }
예제 #2
0
        public virtual SourceResultDataContract CalculateEmissions(Guid sourceId)
        {
            var source = Context.GetDataSource<DataSource>(sourceId);
            if ((source == null) || (source.InputStatus != SourceStatus.PendingCalculation))
            {
                return DataContractError(sourceId, DataSourceServiceResources.FileSourceIsNotPendingCalculation);
            }
            if (Context.SourceContainsErrors(sourceId))
            {
                return DataContractError(sourceId, DataSourceServiceResources.SourceContainsError);
            }
            if (Context.SourceContainsDataEntriesInError(sourceId))
            {
                return DataContractError(sourceId, DataSourceServiceResources.SourceContainsDataEntriesInError);
            }
            var returnResult = DataContractSuccess(sourceId);
            try
            {
                Context.RemoveSourceCalculations(sourceId);
                source.InputStatus = SourceStatus.Calculating;
                Context.UpdateDataSource(source);
                using (var unitOfWork = Context.CreateUnitOfWork())
                {
                    var dataEntries = unitOfWork.GetDataEntriesForSource(sourceId);

                    foreach (var entry in dataEntries)
                    {
                        try
                        {
                            var calculation = Container.Resolve<ICalculation>(entry.CalculationId.ToString());
                            if (calculation == null)
                            {
                                var errorMessage = DataSourceServiceResources.CalculationNotFound;
                                errorMessage = string.Format(errorMessage, entry.CalculationId);
                                throw new NullReferenceException(errorMessage);
                            }
                            var totalDays = calculation.GetDayDifference(entry);
                            if ((entry.StartDate == null) || (entry.EndDate == null))
                            {
                                var errorMessage = DataSourceServiceResources.StartDateEndDateNull;
                                errorMessage = string.Format(errorMessage, entry.Id);
                                throw new NullReferenceException(errorMessage);
                            }

                            var startDate = entry.StartDate.Value;
                            var dailyValue = calculation.CalculateDailyData(entry);
                            for (var day = 0; day <= totalDays; day++)
                            {
                                var effectiveDate = startDate.AddDays(day);
                                var calculatedValue = calculation.CalculateEmission(effectiveDate, dailyValue, entry);
                                
                                var emissionEntry = new CarbonEmissionEntry
                                    {
                                        ActivityGroupId = calculatedValue.ActivityGroupId,
                                        CostCentreCostCode = entry.CostCode,
                                        SourceEntry = entry,
                                        EntryDate = effectiveDate,
                                        CarbonEmissions = calculatedValue.Emissions ?? 0,
                                        Money = dailyValue.MoneyPerDay ?? 0,
                                        Units = dailyValue.UnitsPerDay ?? 0,
                                        CalculationDate = calculatedValue.CalculationDate ?? DateTime.Today
                                    };
                                unitOfWork.AddCarbonEmissionEntry(emissionEntry);
                            }
                        }
                        catch (Exception ex)
                        {
                            try
                            {
                                var correlationId = Guid.NewGuid();
                                ex.Data["CorrelationId"] = correlationId;
                                var entryError = new DataError
                                    {
                                        Column = string.Empty,
                                        DataEntryId = entry.Id,
                                        ErrorType = DataErrorType.CalculationError,
                                        Message =
                                            string.Format(Resources.CalculationError,
                                                          correlationId)
                                    };
                                Context.AddDataError(entryError);
                            }
                            catch (Exception dbEx)
                            {
                                HandleException(dbEx);
                            }
                            ExceptionDispatchInfo.Capture(ex).Throw();
                        }
                    }

                    unitOfWork.CommitWork();
                }
                source.InputStatus = SourceStatus.Calculated;
                Context.UpdateDataSource(source);
            }
            catch (Exception ex)
            {
                HandleException(ex);
                returnResult.Succeeded = false;
                returnResult.ErrorMessages.Add(DataSourceServiceResources.ExceptionOccuredDuringCalculation);
                try
                {
                    source.InputStatus = SourceStatus.PendingCalculation;
                    Context.UpdateDataSource(source);
                }
                catch (Exception dbEx)
                {
                    HandleException(dbEx);
                }
            }
            var profile = Context.GetUserProfile(source.UserName);
            if (profile == null)
            {
                returnResult.ErrorMessages.Add(
                    string.Format(DataSourceServiceResources.UserNameNotFound, source.UserName));
            }
            else
            {
                emailManager.SendMail(source, EmailTemplate.CalculationComplete, profile.Email);
            }
            return returnResult;
        }