public void CalculatePriceTest_ExpectedlValue(double actual, double gallonsRequested, string deliveryDate, bool hasQuoteBefore, string state) { // Arrange: Create a new client Profile var listQuote = new List <Quote>(); var user = new User() { UserId = 1, Username = "******", ClientProfile = new ClientProfile() { State = state } }; var quote = new Quote() { Id = 1 }; if (hasQuoteBefore) { user.Quote = ProfileControllerTest.GetQuote(); } ; IUserRepository _repo = new UserRepository(); var quoteForGenerationDto = new QuoteForDetailedDto(); quoteForGenerationDto.GallonsRequested = gallonsRequested; quoteForGenerationDto.DeliveryDate = Convert.ToDateTime(deliveryDate); // Action : calculate using the method var expected = _repo.CalculatePrice(user, quoteForGenerationDto); //Assert : compares the calculated suggested prices with the actual value. Assert.AreEqual(actual, expected); }
// This function test if quote is generated succesfully // Expected Ok(profile) Ok object result public async Task GenerateNewQuote_Success_ResponseOkWithObject(string username) { // Arrage: the form quote is completed Mock <IUserRepository> mockIUserRepository = new Mock <IUserRepository>(); Mock <IMapper> mockIMapper = new Mock <IMapper>(); Mock <IPhotoUploadService> mockICloudinary = new Mock <IPhotoUploadService>(); mockIUserRepository.Setup(user_repo => user_repo.GetUser(It.IsAny <string>())) .Returns(Task.FromResult(new User() { Username = username, ClientProfile = GetClientProfile(), Quote = GetQuote() })); var quoteForGeneration = new QuoteForDetailedDto() { SuggestedPrice = 12, AmountDue = 12 }; mockIUserRepository.Setup(user_repo => user_repo.CalculatePrice(It.IsAny <User>(), It.IsAny <QuoteForDetailedDto>())).Returns(12.00); mockIMapper.Setup(_mapper => _mapper.Map <Quote>(It.IsAny <QuoteForDetailedDto>())). Returns(new Quote() { Id = 1 }); mockIUserRepository.Setup(user_repo => user_repo.SaveAll()).Returns(Task.FromResult(true)); //Action: Call Generate Quote function var profileController = new ProfileController(mockIUserRepository.Object, mockIMapper.Object, mockICloudinary.Object); var response = await profileController.GenerateNewQuote(It.IsAny <string>(), quoteForGeneration); //Assert: Check if it's OkObject Result Assert.IsInstanceOf <Microsoft.AspNetCore.Mvc.OkObjectResult>(response); }
public async Task <IActionResult> GetPrice(string username, [FromForm] QuoteForDetailedDto quoteForGenerationDto) { // Fetch user from user table with username var userFromRepo = await _repo.GetUser(username); // Call business login function from user repository to calculate the suggested price var suggestedPrice = _repo.CalculatePrice(userFromRepo, quoteForGenerationDto); // Assign & Map all values back to database quoteForGenerationDto.SuggestedPrice = suggestedPrice; quoteForGenerationDto.AmountDue = suggestedPrice * quoteForGenerationDto.GallonsRequested; var newQuote = _mapper.Map <Quote>(quoteForGenerationDto); // Save and return ok status & the new quote if success return(Ok(new { newQuote.SuggestedPrice, newQuote.AmountDue })); }
/* Suggested Price = Current Price + Margin * Where, * * Current price per gallon = $1.50 (this is the price what distributor gets from refinery and it varies based upon crude price. But we are keeping it constant for simplicity) * Margin = Current Price * (Location Factor - Rate History Factor + Gallons Requested Factor + Company Profit Factor + Rate Fluctuation) * * Consider these factors: * * Location Factor = 2% for Texas, 4% for out of state. * Rate History Factor = 1% if client requested fuel before, 0% if no history (you can query fuel quote table to check if there are any rows for the client) * Gallons Requested Factor = 2% if more than 1000 Gallons, 3% if less * Company Profit Factor = 10% always * Rate Fluctuation = 4% for summer, 3% otherwise */ public double CalculatePrice(User userFromRepo, QuoteForDetailedDto quoteForGenerationDto) { const double currentPricePerGallon = 1.5; double locationFactor = 0.04; double rateHistory = 0; double gallonsRequestedFactor = 0.03; double fluctuationRate = 0.03; double companyProfit = 0.1; // Check Location Factor. If Texas = 0.02, out of state = 0.04 if (userFromRepo.ClientProfile.State == "TX" || userFromRepo.ClientProfile.State == "Texas") { locationFactor = 0.02; } // if user has quote before. // Check if user has any quote if (userFromRepo.Quote.Any(q => q.User.UserId == userFromRepo.UserId)) // if(userFromRepo.Quote != null) { rateHistory = 0.01; } // Check gallons requested factor if (quoteForGenerationDto.GallonsRequested > 1000) { gallonsRequestedFactor = 0.02; } // Check if summer time quoteForGenerationDto.DeliveryDate = Convert.ToDateTime(quoteForGenerationDto.DeliveryDate); if (IsSummer(quoteForGenerationDto.DeliveryDate)) { fluctuationRate = 0.04; } //Calulate based on provided formula var margin = currentPricePerGallon * (locationFactor - rateHistory + gallonsRequestedFactor + companyProfit + fluctuationRate); var suggestedPrice = currentPricePerGallon + margin; // Round the prices 7 decimals place suggestedPrice = Math.Round(suggestedPrice, 7, MidpointRounding.AwayFromZero); return(suggestedPrice); }