public FillupViewModel(FillupEntry entry)
 {
     FillupEntryId = entry.FillupEntryId;
     Date = String.Format("{0:MMMM d, yyyy}", entry.Date);
     DateShort = String.Format("{0:MM/dd/yyyy}", entry.Date);
     TotalUnits = String.Format("{0:#00.000}", entry.TotalUnits);
     Odometer = String.Format("{0:N0}", entry.Odometer);
     TransactionFee = String.Format("{0:C}", entry.TransactionFee);
     PricePerUnit = String.Format("{0:C}", entry.PricePerUnit);
     Remarks = entry.Remarks;
     Vendor = entry.Vendor;
     TotalCost = String.Format("{0:C}", entry.TotalCost);
 }
        private void AdjustSurroundingFillupEntries(FillupEntry newFillup)
        {
            if (newFillup == null) throw new ArgumentNullException("newFillup");

            var fillups = _fillupRepository.GetFillups(newFillup.VehicleId);

            // Prior fillups are ordered descending so that FirstOrDefault() chooses the one closest to the new fillup.
            // Secondary ordering is by entry ID ensure a consistent ordering/
            var priorFillup = fillups
                .OrderByDescending(f => f.Date).ThenByDescending(f => f.FillupEntryId)
                .FirstOrDefault(f => (f.Date <= newFillup.Date) && (f.FillupEntryId != newFillup.FillupEntryId));

            // Prior fillups are ordered ascending that FirstOrDefault() chooses the one closest to the new fillup.
            // Secondary ordering is by entry ID ensure a consistent ordering.
            var nextFillup = fillups
                .OrderBy(f => f.Date).ThenBy(f => f.FillupEntryId)
                .FirstOrDefault(f => (f.Date >= newFillup.Date) && (f.FillupEntryId != newFillup.FillupEntryId));

            CalculateInterFillupStatistics(newFillup, priorFillup);
            CalculateInterFillupStatistics(nextFillup, newFillup);
        }
        public void WhenAddingMinimalFillupEntry_ThenPersistsToTheDatabase()
        {
            var repository = new FillupRepository();

            var fillupEntry = new FillupEntry
                                  {
                                      Date = DateTime.Now,
                                      Odometer = 3000,
                                      PricePerUnit = 3.24d,
                                      TotalUnits = 13.01d,
                                  };

            repository.Create(defaultTestUser.UserId, defaultVehicle.VehicleId, fillupEntry);

            // Verification
            var repositoryForVerification = new FillupRepository();
            var retrievedFillup = repositoryForVerification.GetFillups(defaultVehicle.VehicleId).First();

            Assert.NotNull(retrievedFillup);
            Assert.Equal(fillupEntry.Date.ToShortDateString(), retrievedFillup.Date.ToShortDateString());
            // We only care that the dates map.
            AssertExtensions.PropertiesAreEqual(fillupEntry, retrievedFillup, "Odometer", "PricePerUnit", "TotalUnits");
        }
        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);
        }
        private static FillupEntry ToEntry(ICreateFillupEntryCommand source)
        {
            if (source == null)
            {
                return null;
            }

            var fillup = new FillupEntry
                             {
                                 FillupEntryId = source.FillupEntryId,
                                 Date = source.Date.Value,
                                 Distance = source.Distance,
                                 Odometer = source.Odometer,
                                 PricePerUnit = source.PricePerUnit.Value,
                                 Remarks = source.Remarks,
                                 TotalUnits = source.TotalUnits.Value,
                                 TransactionFee = source.TransactionFee.Value,
                                 VehicleId = source.VehicleId,
                                 Vendor = source.Vendor ?? source.Location,
                                 UnitOfMeasure = source.UnitOfMeasure
                             };
            return fillup;
        }
 private static void CalculateInterFillupStatistics(FillupEntry fillup, FillupEntry priorFillup)
 {
     if (priorFillup != null && fillup != null)
     {
         fillup.Distance = Math.Abs(fillup.Odometer - priorFillup.Odometer);
     }
 }
        public void WhenAddingFillupEntry_ThenFillupAssignedNewId()
        {
            var repository = new FillupRepository();

            var fillupEntry = new FillupEntry
                                  {
                                      Date = DateTime.Now,
                                      Odometer = 3000,
                                      PricePerUnit = 3.24d,
                                      TotalUnits = 13.01d,
                                  };

            repository.Create(defaultTestUser.UserId, defaultVehicle.VehicleId, fillupEntry);

            Assert.NotEqual(0, fillupEntry.FillupEntryId);
        }
        public void WhenAddingFillupEntryWithAllData_ThenPersistsToTheDatabase()
        {
            var repository = new FillupRepository();

            var fillupEntry = new FillupEntry
                                  {
                                      Date = DateTime.Now,
                                      Odometer = 3000,
                                      PricePerUnit = 3.24d,
                                      TotalUnits = 13.01d,
                                      Remarks = "Remarkable",
                                      TransactionFee = 1.25d,
                                      Vendor = "Ideal Vendor"
                                  };

            repository.Create(defaultTestUser.UserId, defaultVehicle.VehicleId, fillupEntry);

            // Verification
            var repositoryForVerification = new FillupRepository();
            var enteredFillup = repositoryForVerification.GetFillups(defaultVehicle.VehicleId).First();
            Assert.NotNull(enteredFillup);
            Assert.Equal(fillupEntry.Date.ToShortDateString(), enteredFillup.Date.ToShortDateString());
            // We only care that the dates map.
            AssertExtensions.PropertiesAreEqual(fillupEntry, enteredFillup, "Odometer", "PricePerUnit", "TotalUnits",
                                                "Remarks", "TransactionFee", "Vendor");
        }
        public void WhenGettingAllFillups_ThenReturnsAllFillups()
        {
            var repository = new FillupRepository();

            var fillupEntry1 = new FillupEntry
                                   {
                                       Date = DateTime.Now,
                                       Odometer = 3000,
                                       PricePerUnit = 3.24d,
                                       TotalUnits = 13.01d,
                                   };
            repository.Create(defaultTestUser.UserId, defaultVehicle.VehicleId, fillupEntry1);

            var fillupEntry2 = new FillupEntry
                                   {
                                       Date = DateTime.Now,
                                       Odometer = 3001,
                                       PricePerUnit = 3.24d,
                                       TotalUnits = 13.01d,
                                   };
            repository.Create(defaultTestUser.UserId, defaultVehicle.VehicleId, fillupEntry2);


            var fillups = repository.GetFillups(defaultVehicle.VehicleId);

            Assert.NotNull(fillups);
            Assert.Equal(2, fillups.Count());
        }
예제 #10
0
        private void CreateFillups(Vehicle vehicle, FillupData data)
        {
            int odometer = data.Mileage;
            DateTime date = data.Date;
            Double unitsModifier = data.Units;
            Double distanceModifier = data.Distance;
            int[] randomArray = RandomizeArray(new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
            int currentIndex = 0;
            var random = new Random();
            bool isFirst = true;
            while (date < DateTime.Now)
            {
                int dataIndex = randomArray[currentIndex];
                var distance = (int) (_distance[dataIndex]*distanceModifier);
                var fillup = new FillupEntry
                                 {
                                     Date = date,
                                     Odometer = odometer,
                                     PricePerUnit = _price[dataIndex],
                                     TotalUnits = _units[dataIndex]*unitsModifier,
                                     TransactionFee = _fee[dataIndex],
                                     VehicleId = vehicle.VehicleId,
                                     Vendor = _vendor[dataIndex]
                                 };
                if (isFirst)
                {
                    isFirst = false;
                    fillup.Distance = null;
                }
                else
                {
                    fillup.Distance = distance;
                }
                odometer += distance;

                currentIndex += 1;
                if (currentIndex > 11)
                {
                    currentIndex = 0;
                }
                date = date.AddDays(random.Next(3, 14));
                _fillups.Create(vehicle.UserId, vehicle.VehicleId, fillup);
            }
        }