Ejemplo n.º 1
0
        public async Task <bool> CreateAsync(ReportServiceModel model)
        {
            var adToReport = await this.db.Ads
                             .FindAsync(model.Id);

            if (adToReport == null)
            {
                return(false);
            }

            adToReport.IsReported = true;

            var report = new Report
            {
                Description = model.Description,
                Ad          = adToReport,
            };

            try
            {
                this.db.Update(adToReport);
                await this.db.Reports.AddAsync(report);

                await this.db.SaveChangesAsync();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public async Task GetReportDetails_WithCorrectData_ShouldSuccessfullyGetReportDetails()
        {
            string errorMessagePrefix = "ReportService GetReportDetails() method does not work properly.";

            var context = MdmsDbContextInMemoryFactory.InitializeContext();

            await SeedReportData(context);

            _reportService = new ReportService(context);

            ReportServiceModel actualReportData = await _reportService.GetReportDetails("Custom_2019_1_2019_12");

            ReportServiceModel expectedReportData = context.Reports
                                                    .SingleOrDefaultAsync(x => x.Name == "Custom_2019_1_2019_12")
                                                    .Result.To <ReportServiceModel>();

            var actualVehicles   = actualReportData.VehiclesInReport.ToList();
            var expectedVehicles = expectedReportData.VehiclesInReport.ToList();

            Assert.True(actualReportData.Id == expectedReportData.Id, errorMessagePrefix + " " + "Id is not returned properly.");
            Assert.True(actualReportData.Name == expectedReportData.Name, errorMessagePrefix + " " + "Id is not returned properly.");
            Assert.True(actualReportData.StartYear == expectedReportData.StartYear, errorMessagePrefix + " " + "Id is not returned properly.");
            Assert.True(actualReportData.EndYear == expectedReportData.EndYear, errorMessagePrefix + " " + "Id is not returned properly.");
            Assert.True(actualReportData.StartMonth == expectedReportData.StartMonth, errorMessagePrefix + " " + "Id is not returned properly.");
            Assert.True(actualReportData.EndMonth == expectedReportData.EndMonth, errorMessagePrefix + " " + "Id is not returned properly.");


            for (int i = 0; i < actualReportData.VehiclesInReport.Count; i++)
            {
                var actualData   = actualVehicles[i];
                var expectedData = expectedVehicles[i];

                Assert.True(expectedData.Id == actualData.Id, errorMessagePrefix + " " + "Id is not returned properly.");
                Assert.True(expectedData.Name == actualData.Name, errorMessagePrefix + " " + "Name is not returned properly.");
                Assert.True(expectedData.Price == actualData.Price, errorMessagePrefix + " " + "Price is not returned properly.");
                Assert.True(expectedData.Picture == actualData.Picture, errorMessagePrefix + " " + "Picture is not returned properly.");
                Assert.True(expectedData.Depreciation == actualData.Depreciation, errorMessagePrefix + "Depreciation" + "Depreciation is not returned properly.");
                Assert.True(expectedData.Model == actualData.Model, errorMessagePrefix + " " + "Model is not returned properly.");
                Assert.True(expectedData.Make == actualData.Make, errorMessagePrefix + " " + "Make is not returned properly.");
                Assert.True(expectedData.VSN == actualData.VSN, errorMessagePrefix + " " + "VSN is not returned properly.");
                Assert.True(expectedData.Mileage == actualData.Mileage, errorMessagePrefix + " " + "Mileage is not returned properly.");
                Assert.True(expectedData.RegistrationNumber == actualData.RegistrationNumber, errorMessagePrefix + " " + "RegistrationNumber is not returned properly.");
                Assert.True(expectedData.IsInRepair == actualData.IsInRepair, errorMessagePrefix + " " + "IsInRepair is not returned properly.");
                Assert.True(expectedData.AcquiredOn == actualData.AcquiredOn, errorMessagePrefix + " " + "AcquiredOn is not returned properly.");
                Assert.True(expectedData.ManufacturedOn == actualData.ManufacturedOn, errorMessagePrefix + " " + "ManufacturedOn is not returned properly.");
                Assert.True(expectedData.IsActive == actualData.IsActive, errorMessagePrefix + " " + "IsActive is not returned properly.");
                Assert.True(expectedData.IsDeleted == actualData.IsDeleted, errorMessagePrefix + " " + "IsDeleted is not returned properly.");
                Assert.True(expectedData.VehicleProvider.Name == actualData.VehicleProvider.Name, errorMessagePrefix + " " + "VehicleProviderName is not returned properly.");
                Assert.True(expectedData.VehicleType.Name == actualData.VehicleType.Name, errorMessagePrefix + " " + "VehicleTypeName is not returned properly.");
            }
        }
        public async Task CreateAsync_WithInvalidModel_ShouldReturnFalse()
        {
            // Arrange
            this.dbContext.Ads.Add(new Ad {
                Id = 1
            });
            this.dbContext.SaveChanges();
            var model = new ReportServiceModel();
            // Act
            var result = await this.reportService.CreateAsync(model);

            // Assert
            result
            .Should()
            .BeFalse();
        }
Ejemplo n.º 4
0
        public async Task <bool> CreateCustomReport(ReportServiceModel reportServiceModel)
        {
            ReportType reportType = await _context.ReportTypes.SingleOrDefaultAsync(x => x.Name == "Custom");

            reportServiceModel.Name =
                $"Custom_{reportServiceModel.StartYear}_{reportServiceModel.StartMonth}_{reportServiceModel.EndYear}_{reportServiceModel.EndMonth}";

            if (await _context.Reports.AnyAsync(x => x.Name == reportServiceModel.Name && x.IsDeleted == false))
            {
                return(false);
            }

            var report = reportServiceModel.To <Report>();

            if (report.StartYear > report.EndYear)
            {
                return(false);
            }
            else if (report.StartMonth > report.EndMonth)
            {
                return(false);
            }

            report.ReportType = reportType;

            report.VehiclesInReport = GetVehiclesInReport(reportServiceModel.StartMonth, reportServiceModel.StartYear,
                                                          reportServiceModel.EndMonth, reportServiceModel.EndYear);

            report.MonthlySalariesInReport = GetSalariesInReport(reportServiceModel.StartMonth,
                                                                 reportServiceModel.StartYear,
                                                                 reportServiceModel.EndMonth, reportServiceModel.EndYear);

            report.MechanicsBaseCosts  = report.MonthlySalariesInReport.Sum(x => x.BaseSalary);
            report.VehicleBaseCost     = report.VehiclesInReport.Sum(x => x.Depreciation);
            report.ExternalRepairCosts = report.VehiclesInReport.Sum(x => x.ExternalRepairs.Sum(y => y.LaborCost + y.PartsCost));
            report.InternalRepairCosts = report.VehiclesInReport.Sum(x => x.InternalRepairs.Sum(y => (decimal)y.HoursWorked * y.MdmsUser.AdditionalOnHourPayment) +
                                                                     x.InternalRepairs.Sum(y => y.InternalRepairParts.Sum(z => z.Quantity * z.Part.Price)));

            await _context.AddAsync(report);

            var result = await _context.SaveChangesAsync();

            return(result > 0);
        }
        public async Task CreateAsync_WithValidId_ShouldReturnTrue()
        {
            // Arrange
            this.dbContext.Ads.Add(new Ad {
                Id = 1
            });
            this.dbContext.SaveChanges();
            var model = new ReportServiceModel
            {
                Id          = 1,
                Description = "SampleDescription"
            };
            // Act
            var result = await this.reportService.CreateAsync(model);

            // Assert
            result
            .Should()
            .BeTrue();
        }
        public async Task CreateCustomReport_NotExistentPartProvider_ShouldNotCreatePart()
        {
            string errorMessagePrefix = "ReportService CreateCustomReport() method does not work properly.";

            var context = MdmsDbContextInMemoryFactory.InitializeContext();

            await SeedVehicleAndMonthlySalarytData(context);

            _reportService = new ReportService(context);

            ReportServiceModel report = new ReportServiceModel()
            {
                StartYear  = 0,
                StartMonth = 12,
                EndYear    = 2019,
                EndMonth   = 1
            };

            bool actualResult = await _reportService.CreateCustomReport(report);

            Assert.False(actualResult, errorMessagePrefix);
        }
        public async Task CreateCustomReport_WithCorrectData_ShouldSuccessfullyCreateCustomReport()
        {
            string errorMessagePrefix = "ReportService CreateCustomReport() method does not work properly.";

            var context = MdmsDbContextInMemoryFactory.InitializeContext();

            await SeedVehicleAndMonthlySalarytData(context);

            _reportService = new ReportService(context);

            ReportServiceModel report = new ReportServiceModel()
            {
                StartYear  = 2019,
                StartMonth = 1,
                EndYear    = 2019,
                EndMonth   = 12
            };

            bool actualResult = await _reportService.CreateCustomReport(report);

            Assert.True(actualResult, errorMessagePrefix);
        }