Exemple #1
0
        public void WhenVehiclePopulated_ThenTotalUnitsCalculated()
        {
            var fillups = new[]
            {
                new FillupEntry()
                {
                    Date = DateTime.UtcNow.AddMonths(-5), TotalUnits = 20.5
                },
                new FillupEntry()
                {
                    Date = DateTime.UtcNow.AddMonths(-4), TotalUnits = 22
                },
                new FillupEntry()
                {
                    Date = DateTime.UtcNow.AddMonths(-3), TotalUnits = 36.253
                },
                new FillupEntry()
                {
                    Date = DateTime.UtcNow.AddMonths(-3), TotalUnits = 21.55
                }
            };

            var target = CalculateStatistics.Calculate(fillups, includeFirst: false);

            Assert.Equal(79.8, Math.Round(target.TotalUnits, 2));
        }
Exemple #2
0
        public void WhenVehiclePopulated_ThenTotalDistanceCalculated()
        {
            var fillups = new[]
            {
                new FillupEntry
                {
                    Date     = DateTime.UtcNow.AddMonths(-5),
                    Odometer = 10000,
                    Distance = null
                },
                new FillupEntry
                {
                    Date     = DateTime.UtcNow.AddMonths(-4),
                    Odometer = 12000,
                    Distance = 2000
                },
                new FillupEntry
                {
                    Date     = DateTime.UtcNow.AddMonths(-3),
                    Odometer = 14000,
                    Distance = 2000
                },
                new FillupEntry
                {
                    Date     = DateTime.UtcNow.AddMonths(-3),
                    Odometer = 16000,
                    Distance = 2000
                }
            };

            var target = CalculateStatistics.Calculate(fillups);

            Assert.Equal(16000 - 10000, target.TotalDistance);
        }
Exemple #3
0
        public void WhenVehiclePopulated_ThenAverageFillupPriceCalculated()
        {
            var fillups = new List <FillupEntry>();

            fillups.Add(new FillupEntry()
            {
                Date = DateTime.UtcNow.AddMonths(-5), PricePerUnit = 2.95, TotalUnits = 20.5,
            });
            fillups.Add(new FillupEntry()
            {
                Date           = DateTime.UtcNow.AddMonths(-4),
                PricePerUnit   = 3.16,
                TotalUnits     = 22,
                TransactionFee = 2.90
            });
            fillups.Add(new FillupEntry()
            {
                Date = DateTime.UtcNow.AddMonths(-3), PricePerUnit = 1.90, TotalUnits = 36.253
            });
            fillups.Add(new FillupEntry()
            {
                Date           = DateTime.UtcNow.AddMonths(-3),
                PricePerUnit   = 3.16,
                TotalUnits     = 21.55,
                TransactionFee = 1.13
            });

            var target = CalculateStatistics.Calculate(fillups, includeFirst: false);

            Assert.Equal(2.59, Math.Round(target.AverageFillupPrice, 2));
        }
Exemple #4
0
        public void WhenVehiclePopulatedWithIntraMonthData_ThenAverageCostPerMonthCalculatedAtOneMonth()
        {
            List <FillupEntry> fillups = new List <FillupEntry>();

            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddDays(-5),
                Odometer     = 10000,
                Distance     = null,
                PricePerUnit = 2.95,
                TotalUnits   = 20.5,
            });
            fillups.Add(new FillupEntry()
            {
                Date           = DateTime.UtcNow.AddDays(-4),
                Odometer       = 10220,
                Distance       = 220,
                PricePerUnit   = 3.16,
                TotalUnits     = 22,
                TransactionFee = 2.90
            });
            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddDays(-3),
                Odometer     = 11000,
                Distance     = 780,
                PricePerUnit = 1.90,
                TotalUnits   = 36.253
            });
            fillups.Add(new FillupEntry()
            {
                Date           = DateTime.UtcNow.AddDays(-3),
                Odometer       = 11132,
                Distance       = 132,
                PricePerUnit   = 3.16,
                TotalUnits     = 21.55,
                TransactionFee = 1.13
            });

            var target = CalculateStatistics.Calculate(fillups, includeFirst: false);
            // Ignore the cost of the first fillup, otherwise stats are off
            double totalGasCost = (22 * 3.16 + 36.253 * 1.90 + 21.55 * 3.16);
            double totalCost    = Math.Round(totalGasCost + +2.90 + 1.13, 2);

            Assert.Equal((totalCost) / 1,
                         Math.Round(target.AverageCostPerMonth, 2));
            Assert.Equal(Math.Round(target.TotalCost, 2), Math.Round(target.AverageCostPerMonth, 2));
        }
Exemple #5
0
        public void WhenVehiclePopulated_ThenAverageFuelEfficiencyCalculated()
        {
            List <FillupEntry> fillups = new List <FillupEntry>();

            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-5),
                Odometer     = 10000,
                Distance     = null,
                PricePerUnit = 2.95,
                TotalUnits   = 20.5,
            });
            fillups.Add(new FillupEntry()
            {
                Date           = DateTime.UtcNow.AddMonths(-4),
                Odometer       = 10220,
                Distance       = 220,
                PricePerUnit   = 3.16,
                TotalUnits     = 22,
                TransactionFee = 2.90
            });
            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-3),
                Odometer     = 11000,
                Distance     = 780,
                PricePerUnit = 1.90,
                TotalUnits   = 36.253
            });
            fillups.Add(new FillupEntry()
            {
                Date           = DateTime.UtcNow.AddMonths(-3),
                Odometer       = 11131,
                Distance       = 131,
                PricePerUnit   = 3.16,
                TotalUnits     = 21.55,
                TransactionFee = 1.13
            });

            var    target        = CalculateStatistics.Calculate(fillups, includeFirst: false);
            double totalDistance = 11131 - 10000;
            // Ignore the cost of the first fillup, otherwise stats are off
            double totalFuel = 22 + 36.253 + 21.55;

            double efficiency = Math.Round(totalDistance / totalFuel, 2);

            Assert.Equal(efficiency, Math.Round(target.AverageFuelEfficiency, 2));
        }
Exemple #6
0
        public void WhenConstructed_ThenIntialized()
        {
            var target = CalculateStatistics.Calculate(new FillupEntry[] {});

            Assert.NotNull(target);
            Assert.Equal(0.0, target.AverageCostPerMonth);
            Assert.Equal(0.0, target.AverageCostToDrive);
            Assert.Equal(0.0, target.AverageFillupPrice);
            Assert.Equal(0.0, target.AverageFuelEfficiency);
            Assert.Null(target.Name);
            Assert.Null(target.Odometer);
            Assert.Equal(0.0, target.TotalCost);
            Assert.Equal(0.0, target.TotalDistance);
            Assert.Equal(0.0, target.TotalFuelCost);
            Assert.Equal(0.0, target.TotalUnits);
        }
Exemple #7
0
        public void WhenVehiclePopulated_ThenAverageCostToDriveCalculated()
        {
            List <FillupEntry> fillups = new List <FillupEntry>();

            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-5),
                Odometer     = 10000,
                Distance     = null,
                PricePerUnit = 2.95,
                TotalUnits   = 20.5,
            });
            fillups.Add(new FillupEntry()
            {
                Date           = DateTime.UtcNow.AddMonths(-4),
                Odometer       = 10220,
                Distance       = 220,
                PricePerUnit   = 3.16,
                TotalUnits     = 22,
                TransactionFee = 2.90
            });
            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-3),
                Odometer     = 11000,
                Distance     = 780,
                PricePerUnit = 1.90,
                TotalUnits   = 36.253
            });
            fillups.Add(new FillupEntry()
            {
                Date           = DateTime.UtcNow.AddMonths(-3),
                Odometer       = 11132,
                Distance       = 132,
                PricePerUnit   = 3.16,
                TotalUnits     = 21.55,
                TransactionFee = 1.13
            });

            var    target        = CalculateStatistics.Calculate(fillups, includeFirst: false);
            double totalDistance = 11132 - 10000;
            // Ignore the cost of the first fillup, otherwise stats are off
            double totalCost       = 22 * 3.16 + 36.253 * 1.90 + 21.55 * 3.16 + 2.9 + 1.13;
            double costPerDistance = Math.Round(totalCost / totalDistance, 2);

            Assert.Equal(costPerDistance, Math.Round(target.AverageCostToDrive, 2));
        }
        public void WhenAddingFillupGet_ProvidesPrePopulatedModel()
        {
            // this test has some unnessary setup, just to reflect what is happening with data access
            // ultimately, the data access should be improved to remove the extra calls to the db

            var fillups = new List <FillupEntry>
            {
                new FillupEntry
                {
                    VehicleId     = defaultVehicleId,
                    FillupEntryId = defaultFillupId,
                    Odometer      = 500
                },
            };

            // this is where the actual odometer reading originates
            var statistics = CalculateStatistics.Calculate(fillups);

            // fillups is not required on the vehicle for this test to pass
            var vehicles = new List <VehicleModel>
            {
                new VehicleModel(new Vehicle {
                    VehicleId = defaultVehicleId, Fillups = fillups
                }, statistics)
            };

            MockHandlerFor(
                () => new Mock <GetVehicleListForUser>(null),
                x => x
                .Setup(h => h.Execute(defaultUserInfo.UserId))
                .Returns(vehicles));

            // this test will pass even if this handler returns the wrong set of fillups
            MockHandlerFor(
                () => new Mock <GetFillupsForVehicle>(null),
                x => x
                .Setup(h => h.Execute(defaultVehicleId))
                .Returns(fillups));

            var controller = GetTestableFillupController();

            var result = controller.Add(defaultVehicleId);
            var model  = result.Extract <FillupAddViewModel>();

            Assert.NotNull(model);
            Assert.Equal(500, model.FillupEntry.Odometer);
        }
Exemple #9
0
        public void WhenEfficiencyGreaterThan99Thousand_ThenNumberFormatsCorrectly()
        {
            var fillups = new[]
            {
                new FillupEntry {
                    Distance = 0, TotalUnits = 0
                },
                new FillupEntry {
                    Distance = 150000, TotalUnits = 10
                },
            };
            var vehicle = new VehicleModel(new Vehicle(), CalculateStatistics.Calculate(fillups));

            var actual   = _helper.AverageFuelEfficiencyText(vehicle);
            var expected = MvcHtmlString.Create("15.0k");

            Assert.Equal(expected.ToHtmlString(), actual.ToHtmlString());
        }
Exemple #10
0
        public void WhenEfficiencyGreaterThanHundred_ThenMagnitudeFormatsCorrectly()
        {
            var fillups = new[]
            {
                new FillupEntry {
                    Distance = 0, TotalUnits = 0
                },
                new FillupEntry {
                    Distance = 8888, TotalUnits = 10
                },
            };
            var vehicle = new VehicleModel(new Vehicle(), CalculateStatistics.Calculate(fillups));

            var actual   = _helper.AverageFuelEfficiencyMagnitude(vehicle);
            var expected = MvcHtmlString.Create("hundreds");

            Assert.Equal(expected.ToHtmlString(), actual.ToHtmlString());
        }
        private void CalculateSeriesForVehicle(Vehicle vehicle, StatisticSeries series, DateTime?startDate, DateTime?endDate)
        {
            Debug.Assert(series != null);

            DateTime startFilterDate = startDate ?? DateTime.MinValue;
            DateTime endFilterDate   = endDate ?? DateTime.UtcNow;

            var fillUps = _fillupRepository.GetFillups(vehicle.VehicleId);

            var fillupGroups = from fillUp in fillUps
                               where ((fillUp.Date >= startFilterDate) && (fillUp.Date <= endFilterDate))
                               group fillUp by new { Year = fillUp.Date.Year, Month = fillUp.Date.Month }
            into g
            orderby g.Key.Year, g.Key.Month
            select g;

            var firstFillUp = fillUps.OrderBy(x => x.Date).FirstOrDefault();

            VehicleStatisticsModel statistics;

            foreach (var fillupGroup in fillupGroups)
            {
                var includeFirstFillup = (fillupGroup.Key.Year != firstFillUp.Date.Year) ||
                                         (fillupGroup.Key.Month != firstFillUp.Date.Month);

                statistics = CalculateStatistics.Calculate(fillupGroup, includeFirstFillup);

                Debug.Assert(firstFillUp != null);


                var seriesEntry = new StatisticSeriesEntry
                {
                    Id    = vehicle.VehicleId,
                    Name  = vehicle.Name,
                    Year  = fillupGroup.Key.Year,
                    Month = fillupGroup.Key.Month,
                    AverageFuelEfficiency = Math.Round(statistics.AverageFuelEfficiency, 2),
                    TotalCost             = Math.Round(statistics.TotalCost, 2),
                    TotalDistance         = statistics.TotalDistance,
                };
                series.Entries.Add(seriesEntry);
            }
        }
Exemple #12
0
        public void WhenVehiclePopulated_ThenTotalCostCalculated()
        {
            List <FillupEntry> fillups = new List <FillupEntry>();

            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-5),
                PricePerUnit = 2.95,
                TotalUnits   = 20.5,
            });
            fillups.Add(new FillupEntry()
            {
                Date           = DateTime.UtcNow.AddMonths(-4),
                PricePerUnit   = 3.16,
                TotalUnits     = 22,
                TransactionFee = 2.90
            });
            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-3),
                PricePerUnit = 1.90,
                TotalUnits   = 36.253
            });
            fillups.Add(new FillupEntry()
            {
                Date           = DateTime.UtcNow.AddMonths(-3),
                PricePerUnit   = 3.16,
                TotalUnits     = 21.55,
                TransactionFee = 1.13
            });

            var target = CalculateStatistics.Calculate(fillups, includeFirst: false);
            // Ignore the cost of the first fillup, otherwise stats are off
            double totalCost = (22 * 3.16 + 36.253 * 1.90 + 21.55 * 3.16 + 2.9 + 1.13);

            Assert.Equal(Math.Round(totalCost, 2), Math.Round(target.TotalCost, 2));
        }
Exemple #13
0
        public void WhenIncludeFirstFillupTrue_ThenFirstFillupIncluded()
        {
            List <FillupEntry> fillups = new List <FillupEntry>();

            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-5),
                Odometer     = 10000,
                Distance     = null,
                PricePerUnit = 1,
                TotalUnits   = 10,
            });
            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-4),
                Odometer     = 11000,
                Distance     = 1000,
                PricePerUnit = 1,
                TotalUnits   = 10,
            });
            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-3),
                Odometer     = 12000,
                Distance     = 1000,
                PricePerUnit = 1,
                TotalUnits   = 10,
            });
            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-2),
                Odometer     = 13000,
                Distance     = 1000,
                PricePerUnit = 1,
                TotalUnits   = 10,
            });
            fillups.Add(new FillupEntry()
            {
                Date         = DateTime.UtcNow.AddMonths(-1),
                Odometer     = 14000,
                Distance     = 1000,
                PricePerUnit = 1,
                TotalUnits   = 10,
            });

            var target = CalculateStatistics.Calculate(fillups, includeFirst: true);

            double totalDistance = 14000 - 10000;
            // Ignore the cost of the first fillup, otherwise stats are off
            double totalCost       = 10 * 5;
            double fuelCost        = 10 * 5;
            double totalFuel       = 10 * 5;
            double avgCostPerMonth = Math.Round(totalCost / 5, 2);
            double avgCostToDrive  = Math.Round(totalCost / totalDistance, 2);
            double avgEfficiency   = Math.Round(totalDistance / totalFuel, 2);

            Assert.Equal(avgCostPerMonth, Math.Round(target.AverageCostPerMonth, 2));
            Assert.Equal(avgCostToDrive, Math.Round(target.AverageCostToDrive, 2));
            Assert.Equal(1, Math.Round(target.AverageFillupPrice, 2));
            Assert.Equal(avgEfficiency, Math.Round(target.AverageFuelEfficiency, 2));
            Assert.Equal(14000, target.Odometer);
            Assert.Equal(totalCost, Math.Round(target.TotalCost, 2));
            Assert.Equal(totalDistance, target.TotalDistance);
            Assert.Equal(fuelCost, Math.Round(target.TotalFuelCost, 2));
            Assert.Equal(totalFuel, Math.Round(target.TotalUnits, 2));
        }
 private void InitializeFixture()
 {
     this.target = new FleetStatistics(new[]
     {
         CalculateStatistics.Calculate(new[]
         {
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-5),
                 Odometer       = 1000,
                 Distance       = null,
                 PricePerUnit   = 1,
                 TotalUnits     = 10,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-4),
                 Odometer       = 2000,
                 Distance       = 1000,
                 PricePerUnit   = 1,
                 TotalUnits     = 10,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-3),
                 Odometer       = 3000,
                 Distance       = 1000,
                 PricePerUnit   = 1,
                 TotalUnits     = 10,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-2),
                 Odometer       = 4000,
                 Distance       = 1000,
                 PricePerUnit   = 1,
                 TotalUnits     = 10,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-1),
                 Odometer       = 5000,
                 Distance       = 1000,
                 PricePerUnit   = 1,
                 TotalUnits     = 10,
                 TransactionFee = 1.5
             },
         }, includeFirst: false),
         CalculateStatistics.Calculate(new[]
         {
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-6),
                 Odometer       = 1000,
                 Distance       = null,
                 PricePerUnit   = 3,
                 TotalUnits     = 20,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-5),
                 Odometer       = 2000,
                 Distance       = 1000,
                 PricePerUnit   = 3,
                 TotalUnits     = 20,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-3),
                 Odometer       = 3000,
                 Distance       = 1000,
                 PricePerUnit   = 3,
                 TotalUnits     = 20,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-3),
                 Odometer       = 4000,
                 Distance       = 1000,
                 PricePerUnit   = 3,
                 TotalUnits     = 20,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow,
                 Odometer       = 5000,
                 Distance       = 1000,
                 PricePerUnit   = 3,
                 TotalUnits     = 20,
                 TransactionFee = 1.5
             },
         }, includeFirst: false),
         CalculateStatistics.Calculate(new[]
         {
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-12),
                 Odometer       = 3000,
                 Distance       = null,
                 PricePerUnit   = 2,
                 TotalUnits     = 30,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-10),
                 Odometer       = 4000,
                 Distance       = 1000,
                 PricePerUnit   = 2,
                 TotalUnits     = 30,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-8),
                 Odometer       = 5000,
                 Distance       = 1000,
                 PricePerUnit   = 2,
                 TotalUnits     = 30,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-6),
                 Odometer       = 6000,
                 Distance       = 1000,
                 PricePerUnit   = 2,
                 TotalUnits     = 30,
                 TransactionFee = 1.5
             },
             new FillupEntry
             {
                 Date =
                     DateTime.UtcNow.
                     AddMonths(-4),
                 Odometer       = 7000,
                 Distance       = 1000,
                 PricePerUnit   = 2,
                 TotalUnits     = 30,
                 TransactionFee = 1.5
             },
         }, includeFirst: false)
     });
 }