public void WhenCanAddFillupWithInvalidVehicleId_ThenReturnsValidationResult()
        {
            var fillUp = new FillupEntryFormModel
                             {
                                 Date = DateTime.UtcNow,
                                 TotalUnits = 10,
                                 PricePerUnit = 1.0,
                             };

            var fillUps = new List<FillupEntry>();
            _vehicleRepo
                .Setup(r => r.GetVehicle(DefaultUserId, DefaultVehicleId))
                .Returns((Vehicle)null);

            _fillupRepo
                .Setup(x => x.GetFillups(DefaultVehicleId))
                .Returns(fillUps);

            var handler = new CanAddFillup(_vehicleRepo.Object, _fillupRepo.Object);
            var actual = handler.Execute(DefaultUserId, DefaultVehicleId, fillUp);

            var actualList = new List<ValidationResult>(actual);
            Assert.Equal(1, actualList.Count);
            Assert.Contains("not found", actualList[0].Message, StringComparison.CurrentCultureIgnoreCase);
        }
        public void WhenAddingFillup_ThenCalculatesDistance()
        {
            _vehicleRepo
                .Setup(r => r.GetVehicle(DefaultUserId, DefaultVehicleId))
                .Returns(_vehicle);

            var fillups = new[]
                {
                    new FillupEntry{FillupEntryId = 1, Date = new DateTime(2011, 3, 23), Odometer = 500},
                    new FillupEntry{FillupEntryId = 2, Date = new DateTime(2011, 3, 24), Odometer = 750}
                };

            var fillupForm = new FillupEntryFormModel
                                 {
                                     FillupEntryId = 3,
                                     Date = new DateTime(2011, 3, 26),
                                     Odometer = 1000
                                 };

            _fillupRepositoryMock
                .Setup(x => x.GetFillups(DefaultVehicleId))
                .Returns(fillups);

            var handler = new AddFillupToVehicle(_vehicleRepo.Object, _fillupRepositoryMock.Object);
            handler.Execute(DefaultUserId, DefaultVehicleId, fillupForm);

            Assert.Equal(250, fillupForm.Distance);
        }
        public void WhenCanAddFillupWithInvalidFillupOdometer_ThenReturnsValidationResult()
        {
            var fillUp = new FillupEntryFormModel
                             {
                                 Date = DateTime.UtcNow,
                                 TotalUnits = 10,
                                 PricePerUnit = 1.0,
                                 Odometer = 500 //less than prior fillup
                             };

            var fillUps = new List<FillupEntry>
                              {
                                  new FillupEntry
                                      {
                                          FillupEntryId = 1,
                                          Date = DateTime.UtcNow.AddDays(-1),
                                          Odometer = 1000
                                      }
                              };

            _vehicleRepo
                .Setup(vr => vr.GetVehicle(DefaultUserId, DefaultVehicleId))
                .Returns(new Vehicle { VehicleId = DefaultVehicleId, Name = "Vehicle" });

            _fillupRepo
                .Setup(x => x.GetFillups(DefaultVehicleId))
                .Returns(fillUps);

            var handler = new CanAddFillup(_vehicleRepo.Object, _fillupRepo.Object);
            var actual = handler.Execute(DefaultUserId, DefaultVehicleId, fillUp);

            var actualList = new List<ValidationResult>(actual);
            Assert.Equal(1, actualList.Count);
            Assert.Contains("odometer ", actualList[0].Message, StringComparison.CurrentCultureIgnoreCase);
        }
示例#4
0
        //
        // GET: /Fillup/Add/1
        public ActionResult Add(int vehicleId)
        {
            var vehicles = Using<GetVehicleListForUser>()
                .Execute(CurrentUserId);

            var vehicle = vehicles.First(v => v.VehicleId == vehicleId);

            var newFillupEntry = new FillupEntryFormModel
                                    {
                                        Odometer = vehicle.Odometer.HasValue ? vehicle.Odometer.Value : 0
                                    };

            var fillups = Using<GetFillupsForVehicle>()
                .Execute(vehicleId)
                .OrderByDescending(f => f.Date);

            var viewModel = new FillupAddViewModel
                                {
                                    VehicleList = new VehicleListViewModel(vehicles, vehicleId) {IsCollapsed = true},
                                    FillupEntry = newFillupEntry,
                                    Fillups = new SelectedItemList<Model.FillupEntry>(fillups),
                                };

            ViewBag.IsFirstFillup = (!fillups.Any());

            return View(viewModel);
        }
        public void WhenAddingFillup_ThenDelegatesToFillupRepository()
        {
            _vehicleRepo
                .Setup(r => r.GetVehicle(DefaultUserId, DefaultVehicleId))
                .Returns(_vehicle);

            var fillupForm = new FillupEntryFormModel();

            var handler = new AddFillupToVehicle(_vehicleRepo.Object, _fillupRepositoryMock.Object);
            handler.Execute(DefaultUserId, DefaultVehicleId, fillupForm);

            _fillupRepositoryMock
                .Verify(r => r.Create(DefaultUserId, DefaultVehicleId, It.IsAny<FillupEntry>()),
                Times.Once());
        }
        public void WhenCanAddFillup_ThenReturnsEmptyCollection()
        {
            var fillUp = new FillupEntryFormModel
                             {
                                 Date = DateTime.UtcNow,
                                 TotalUnits = 10,
                                 PricePerUnit = 1.0,
                             };

            _vehicleRepo
                .Setup(vr => vr.GetVehicle(DefaultUserId, DefaultVehicleId))
                .Returns(new Vehicle { VehicleId = DefaultVehicleId, Name = "Vehicle" });

            _fillupRepo
                .Setup(x => x.GetFillups(It.IsAny<int>()))
                .Returns(new List<FillupEntry>());

            var handler = new CanAddFillup(_vehicleRepo.Object, _fillupRepo.Object);
            var actual = handler.Execute(DefaultUserId, DefaultVehicleId, fillUp);

            Assert.Empty(actual);
        }
        public ActionResult Add(int vehicleId)
        {
            var vehicle = Using<GetVehicleById>()
                .Execute(CurrentUserId, vehicleId);

            if (vehicle == null)
                throw new HttpException((int)HttpStatusCode.NotFound,
                    Messages.FillupController_VehicleNotFound);

            var fillups = Using<GetFillupsForVehicle>()
                .Execute(vehicleId)
                .OrderByDescending(f => f.Date);

            var newFillupEntry = new FillupEntryFormModel
                                        {
                                            Odometer = vehicle.Odometer.HasValue ? vehicle.Odometer.Value : 0,
                                            VehicleId = vehicleId
                                        };

            ViewBag.IsFirstFillup = (!fillups.Any());

            return new ContentTypeAwareResult(newFillupEntry); 
        }
        public ActionResult Add(int vehicleId, FillupEntryFormModel model)
        {
            // Vehicle does not need to validate here as we are using a forgery token.

            if (ModelState.IsValid)
            {
                var errors = Using<CanAddFillup>()
                    .Execute(CurrentUserId, vehicleId, model);

                if (errors.Any())
                {
                    ModelState.AddModelErrors(errors, "AddFillup");
                }
                else
                {
                    Using<AddFillupToVehicle>().Execute(CurrentUserId, vehicleId, model);

                    this.SetConfirmationMessage(Messages.FillupController_FillupAddedMessage);

                    return new ContentTypeAwareResult
                               {
                                   WhenHtml = (m,v,t) => RedirectToAction("List", "Fillup", new { vehicleId })
                               };
                }
            }

            this.SetAlertMessage(Messages.PleaseFixInvalidData);

            var fillups = Using<GetFillupsForVehicle>()
                .Execute(vehicleId)
                .OrderByDescending(f => f.Date);

            ViewBag.IsFirstFillup = (!fillups.Any());

            return new ContentTypeAwareResult(model);
        }
        public void WhenAddingFirstFillup_ThenDoesNotCalculatesDistance()
        {
            _vehicleRepo
                .Setup(r => r.GetVehicle(DefaultUserId, DefaultVehicleId))
                .Returns(_vehicle);

            var fillups = new FillupEntry[] {};

            var fillupForm = new FillupEntryFormModel { FillupEntryId = 3, Date = new DateTime(2011, 3, 25), Odometer = 1000 };

            _fillupRepositoryMock
                 .Setup(x => x.GetFillups(DefaultVehicleId))
                 .Returns(fillups);

            var handler = new AddFillupToVehicle(_vehicleRepo.Object, _fillupRepositoryMock.Object);
            handler.Execute(DefaultUserId, DefaultVehicleId, fillupForm);

            Assert.Null(fillupForm.Distance);
        }
        public ActionResult Add(int vehicleId, FillupEntryFormModel model)
        {
            var vehicles = Using<GetVehicleListForUser>()
                .Execute(CurrentUserId );

            if (ModelState.IsValid)
            {
                var errors = Using<CanAddFillup>()
                    .Execute(CurrentUserId, vehicleId, model);

                ModelState.AddModelErrors(errors, "AddFillup");

                if (ModelState.IsValid)
                {
                    Using<AddFillupToVehicle>().Execute(CurrentUserId, vehicleId, model);

                    TempData["LastActionMessage"] = Resources.VehicleController_AddFillupSuccessMessage;
                    return RedirectToAction("List", "Fillup", new { vehicleId = vehicleId });
                }
            }

            var fillups = Using<GetFillupsForVehicle>()
                .Execute(vehicleId)
                .OrderByDescending(f => f.Date);

            var viewModel = new FillupAddViewModel
            {
                VehicleList = new VehicleListViewModel(vehicles, vehicleId) { IsCollapsed = true },
                FillupEntry = model,
                Fillups = new SelectedItemList<Model.FillupEntry>(fillups),
            };

            ViewBag.IsFirstFillup = (!fillups.Any());

            return View(viewModel);
        }
        public void WhenAddingFillup_UsesLocationIfVendorNull()
        {
            _vehicleRepo
                .Setup(r => r.GetVehicle(DefaultUserId, DefaultVehicleId))
                .Returns(_vehicle);

            var fillupForm = new FillupEntryFormModel
                                 {
                                     Vendor = null,
                                     Location = "testlocation",
                                     PricePerUnit = 0,
                                     TotalUnits = 0,
                                     TransactionFee = 0
                                 };

            var handler = new AddFillupToVehicle(_vehicleRepo.Object, _fillupRepositoryMock.Object);
            handler.Execute(DefaultUserId, DefaultVehicleId, fillupForm);

            _fillupRepositoryMock
                .Verify(r => r.Create(DefaultUserId, DefaultVehicleId, It.Is<FillupEntry>(f=>f.Vendor=="testlocation")));
        }