public void WhenTheLastFiveMinutesAreRequestedFromTheFileItShouldCaluclateTheWeightForTheActualTimeGapTest() { // arrange const string serialNumber = "Interval"; const int counter = 2421; var start = DateTime.Now.AddMinutes(-20); using (var target = new kWhStorage(serialNumber, BaseDir, counter, Precision.mWh)) { // act var i = 0; while (start.AddMinutes(i) < DateTime.Now) { target.SetValue(start.AddMinutes(i), (ulong)(5 * i), 1000, 21m); i++; } target.GetValue(start); // assert var series = SeriesValueListBuilder.BuildSeries(DateTime.Today, DateTime.Now, SeriesResolution.FiveMinutes); target.GetRecords(DateTime.Today, DateTime.Now, Unit.kWh, series, false); Assert.AreEqual(300m, series[series.Count - 2].Value); } }
public void WhenSumIsCalledItCalculatesTheSameAsTheIndividualResolutionTest() { // arrange const string serialNumber = "Interval"; const int counter = 2421; var start = DateTime.Today.AddDays(-14); using (var target = new kWhStorage(serialNumber, BaseDir, counter, Precision.Wh)) { var totalMinutes = (ulong)(DateTime.Now - start).TotalMinutes; for (ulong i = 0; i < totalMinutes; i++) { target.SetValue(start.AddMinutes(i), (5 + (i * 2)), 1000, 10m); } // act target.GetValue(start); // assert var thisWeek = PeriodBuilder.LastWeek(); var thisWeekSeries = SeriesValueListBuilder.BuildSeries(thisWeek.From, thisWeek.To, SeriesResolution.Hour); target.GetRecords(thisWeek.From, thisWeek.To, Unit.M3, thisWeekSeries, false); var thisWeekSeriesSum = thisWeekSeries.Sum(s => s.Value); var thisWeekSum = target.Sum(thisWeek.From, thisWeek.To, Unit.M3); Assert.AreEqual(thisWeekSum, thisWeekSeriesSum); } }
public void WhenSetValueIsCalledWith2000AsFormulaAndAPrecisionOf10000IsShouldWriteAHalfTest() { // arrange const string serialNumber = "Interval"; const int counter = 1; var start = DateTime.Now.AddMinutes(-2); using (var target = new kWhStorage(serialNumber, BaseDir, counter, Precision.mWh)) { // act target.SetValue(start.AddMinutes(-5), 5, 2000, 1); target.SetValue(start.AddMinutes(-4), 10, 2000, 1); target.SetValue(start.AddMinutes(-3), 15, 2000, 1); target.SetValue(start.AddMinutes(-2), 25, 2000, 1); target.SetValue(start.AddMinutes(-1), 26, 2000, 1); target.SetValue(start, 31, 2000, 1); target.GetValue(start); // assert Assert.AreEqual(25, target.GetValue(start.AddMinutes(-2)).Raw); Assert.AreEqual(26, target.GetValue(start.AddMinutes(-1)).Raw); var series = SeriesValueListBuilder.BuildSeries(start.AddMinutes(-5), start, SeriesResolution.OneMinute); target.GetRecords(start.AddMinutes(-5), start, Unit.kWh, series, false); Assert.AreEqual(300, series[2].Value); Assert.AreEqual(30, series[3].Value); } }
public void GetRecordsShouldNotThrowWhenFileDoesNotExitTest() { // arrange const string serialNumber = "Serial"; const int counter = 1; // act using (var target = new kWhStorage(serialNumber, BaseDir, counter, Precision.Wh)) { try { var values = SeriesValueListBuilder.BuildSeries(DateTime.Today.AddDays(-7), DateTime.Today, SeriesResolution.Day); var result = target.GetRecords(DateTime.Today.AddDays(-7), DateTime.Today, Unit.kWh, values, false); // assert Assert.IsFalse(result); Assert.AreEqual(7, values.Count); Assert.IsTrue(values.TrueForAll(v => v.Value == null)); } catch { Assert.Fail("Should not throw"); } } }
public void WhenFirstSlotIsInvalidRestOfSlotsShouldBeFilledTest() { using (var storage = new kWhStorage("00-00-000-006", BaseDir, 1, Precision.Wh, "")) { var firstZeroTimestamp = new DateTime(2013, 12, 31, 0, 0, 0); storage.SetValue(firstZeroTimestamp, 0, 1000.0m, 0.21m); var lastZeroTimestamp = new DateTime(2013, 12, 31, 23, 59, 0); var record = storage.SetValue(lastZeroTimestamp, 0, 1000.0m, 0.21m); var firstActualTimestamp = new DateTime(2014, 1, 2, 12, 0, 0); // Fake that running total is computed, but not written to file. This should mess up the interpolation which leads to // empty slots between lastZeroTimestamp and firstActualTimestamp. record.Time = firstActualTimestamp.AddMinutes(-1); storage.SetValue(firstActualTimestamp, 90913, 1000.0m, 0.21m, record); var startPeriod = new DateTime(2014, 1, 1); var endPeriod = new DateTime(2014, 2, 1); // Make sure we have data to fill each day in a month. var timestamp = firstActualTimestamp.Date.AddDays(1); ulong value = 100000; while (timestamp <= endPeriod) { storage.SetValue(timestamp, value, 1000.0m, 0.21m); value += 10000; timestamp = timestamp.AddDays(1); } // Now check that we get a full month of data, even if some data is missing. var slots = SeriesValueListBuilder.BuildSeries(startPeriod, endPeriod, SeriesResolution.Day); Assert.IsTrue(storage.GetRecords(startPeriod, endPeriod, Unit.kWh, slots, false)); Assert.IsNull(slots[0].Value); foreach (var slot in slots.Skip(1)) { Assert.IsNotNull(slot.Value); } } }
public void WhenSlotEndsAfterEndOfFileButBeforeReferenceDateGetSeriesShouldReturnValue() { using (var storage = new kWhStorage("00-00-000-005", BaseDir, 1, Precision.Wh, "", false, 7)) { var baseTimestamp = new DateTime(2013, 1, 1, 0, 0, 0); // Create a file that start and ends before now and that contains valid usage. storage.SetValue(baseTimestamp, 3000, 300.0m, 0.21m); storage.SetValue(baseTimestamp.AddHours(1), 6000, 300.0m, 0.21m); Assert.IsTrue(storage.EndOfFile < DateTime.Now); // If we now ask for a month of data, the slot will fall inside the file, but the end will not. // This should still result in a bit of consumption. var slots = SeriesValueListBuilder.BuildSeries(baseTimestamp, baseTimestamp.AddMonths(1), SeriesResolution.Month); Assert.AreEqual(1, slots.Count); Assert.IsNull(slots[0].Value); storage.GetRecords(baseTimestamp, baseTimestamp.AddMonths(1), Unit.kWh, slots, false); Assert.IsNotNull(slots[0].Value); } }
public void GetRecordsShouldReturnWithinReasonableTimeWhenMeasurementsAreMissingfromTheFileTest() { // arrange const string serialNumber = "Serial"; const int counter = 1; // Do this twice to miss the overhead of just in time compilation. for (int i = 0; i < 2; i++) { using (var target = new kWhStorage(serialNumber, BaseDir, counter, Precision.Wh)) { target.SetValue(DateTime.Now.AddDays(-8), 50, 1, 1); target.SetValue(DateTime.Now.AddDays(-7), 100, 1, 1); // act var watch = new Stopwatch(); watch.Start(); var values = SeriesValueListBuilder.BuildSeries(DateTime.Today.AddDays(-30), DateTime.Today, SeriesResolution.Day); var result = target.GetRecords(DateTime.Today.AddDays(-30), DateTime.Today, Unit.kWh, values, false); watch.Stop(); var time = watch.ElapsedMilliseconds; // assert Assert.IsNotNull(result); const int maxTime = 250; if (time < maxTime) { break; } if (i > 0) { Assert.Less(time, maxTime); //Refactor the find algo because the file is too slow } } } }
public void WhenReferenceDateIsBeforeEndOfFileGetSeriesShouldReturnValue() { // Test a new Qbox that has just started. The storage file starts at the first measurement, and ends after the ReferenceDate (now). using (var storage = new kWhStorage("00-00-000-005", BaseDir, 1, Precision.Wh, "")) { var baseTimestamp = DateTime.Now.Date; // Create a file that start and ends before now and that contains valid usage on the first day. storage.SetValue(baseTimestamp.AddHours(1), 3000, 300.0m, 0.21m); storage.SetValue(baseTimestamp.AddHours(2), 6000, 300.0m, 0.21m); // If we now ask for a week of day-data, the start of the first slot will be before the actual data, // and the end of the first slot will be after the actual data. // This should still result in a bit of consumption. var slots = SeriesValueListBuilder.BuildSeries(baseTimestamp, baseTimestamp.AddDays(7), SeriesResolution.Day); Assert.AreEqual(7, slots.Count); Assert.IsNull(slots[0].Value); storage.GetRecords(baseTimestamp, baseTimestamp.AddDays(7), Unit.kWh, slots, false); Assert.IsNotNull(slots[0].Value); } }