Пример #1
0
        public void WhenSetValueIsCalledWith2000AsFormulaAndAPrecisionOf10000IsShouldWriteAHalfTest()
        {
            // arrange
            _dataStorageContext.SerialNumber = "Interval";
            _dataStorageContext.Precision    = Precision.mWh;

            var start = DateTime.Now.AddMinutes(-2);

            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                // 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);
            }
        }
Пример #2
0
        public void WhenValueIsWrittenOneMinuteBeforeStartShouldThrowException()
        {
            _dataStorageContext.SerialNumber = "00-00-000-003";
            DateTime startOfFile;
            DateTime endOfFile;

            Assert.Throws(typeof(InvalidOperationException), delegate
            {
                using (var storage = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
                {
                    // Write first value, this should set the start time of the storage.
                    storage.SetValue(new DateTime(2013, 5, 7, 22, 58, 0), 3000, 300.0m, 0.21m);
                    startOfFile = storage.StartOfFile;
                    endOfFile   = storage.EndOfFile;
                    // Try to write a value one minute before the start.
                    var timestamp = startOfFile.AddMinutes(-1);
                    storage.SetValue(timestamp, 3000, 300.0m, 0.21m);
                }

                // If it doesn't throw an exception, we should be able to open the file again and the header should be intact.
                using (var storage = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
                {
                    storage.GetValue(new DateTime(2013, 5, 7, 22, 58, 0));

                    Assert.AreEqual(startOfFile, storage.StartOfFile);
                    Assert.AreEqual(endOfFile, storage.EndOfFile);
                }
            });
        }
Пример #3
0
        public void SetValueShouldStoreTheRunningTotalsInTheFileTest()
        {
            // arrange
            _dataStorageContext.SerialNumber = "12-13-001-075";

            var time = new DateTime(2012, 8, 17, 13, 20, 0);

            using (var storageProvider = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                // act
                for (int i = 0; i < 10; i++)
                {
                    storageProvider.SetValue(time.AddMinutes(i), (ulong)(50 * i), 1000m, 21m);
                }
            }
            using (var storageProvider = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                // assert
                string fileName = GetTestPath($"Qbox_12-13-001-075/12-13-001-075_{_dataStorageContext.CounterId:00000000}.qbx");
                Assert.IsTrue(File.Exists(fileName));
                for (int i = 0; i < 10; i++)
                {
                    var actual = storageProvider.GetValue(time.AddMinutes(i));
                    Assert.AreEqual((ulong)(50 * i), actual.Raw);
                    Assert.AreEqual((ulong)(50 * i), actual.KiloWattHour * 1000m);
                }
            }
        }
Пример #4
0
        public void WhenTheLastFiveMinutesAreRequestedFromTheFileItShouldCaluclateTheWeightForTheActualTimeGapTest()
        {
            // arrange
            _dataStorageContext.SerialNumber = "Interval";
            _dataStorageContext.CounterId    = 2421;

            var start = DateTime.Now.AddMinutes(-20);

            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                // 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);
            }
        }
Пример #5
0
        public void WhenSumIsCalledItCalculatesTheSameAsTheIndividualResolutionTest()
        {
            // arrange
            _dataStorageContext.SerialNumber = "Interval";
            _dataStorageContext.CounterId    = 2421;

            var start = DateTime.Today.AddDays(-14);

            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                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 = Enumerable.Sum <SeriesValue>(thisWeekSeries, s => s.Value);

                var thisWeekSum = target.Sum(thisWeek.From, thisWeek.To, Unit.M3);
                Assert.AreEqual(thisWeekSum, thisWeekSeriesSum);
            }
        }
Пример #6
0
        public void GetValueShouldReturnTheAverageValueBetweenEmptySlotsTest()
        {
            var    measureTime = DateTime.Now.AddMinutes(-15);
            Record actual      = null;

            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                target.SetValue(measureTime, 50, 25, 15);
                Console.WriteLine(target.GetValue(measureTime).Raw);
                target.SetValue(measureTime.AddMinutes(10), 100, 25, 15);
            }

            // act
            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                for (var i = 0; i < 10; i++)
                {
                    actual = target.GetValue(measureTime.AddMinutes(i));
                    Console.WriteLine("Raw: {0} | Value: {1} | Quality: {2}", actual.Raw, actual.KiloWattHour, actual.Quality);
                }
            }
            // assert
            Assert.IsNotNull(actual, "actual != null");
            Assert.AreEqual(3.8m, actual.KiloWattHour);
            Assert.AreEqual((ushort)10000, actual.Quality);
        }
Пример #7
0
        public void SetValueShouldIncreaseTheFileSizeWithSevenDaysIfMeasureFallsBeyondTheEndOfTheFileTest()
        {
            var measureTime = DateTime.Now.AddDays(-10);

            var secondMeasureTime = DateTime.Now.AddDays(-1);

            // act
            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                target.SetValue(measureTime, 25, 50, 15);
                target.SetValue(secondMeasureTime, 25, 50, 15);
            }

            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                // assert
                Assert.That(target.StartOfFile.Equals(RoundDown(measureTime)));
                Assert.That(target.EndOfFile > secondMeasureTime);
                Assert.AreEqual((ulong)25, target.GetValue(secondMeasureTime).Raw);
                Assert.AreEqual(ulong.MaxValue, target.GetValue(secondMeasureTime.AddMinutes(1)).Raw);
            }
        }
Пример #8
0
 public void GetValueShouldNotThrowExceptionWhenFileDoesNotExistTest()
 {
     // act
     using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
     {
         Record result = null;
         try
         {
             result = target.GetValue(DateTime.Now);
             // assert
         }
         catch
         {
             Assert.Fail("Should not throw");
         }
         Assert.IsNull(result);
     }
 }
Пример #9
0
        public void WhenSetValueIsCalledWithAnIntervalItShouldLevelTheValuesWrittenTest()
        {
            // arrange
            _dataStorageContext.SerialNumber = "Interval";
            _dataStorageContext.Precision    = Precision.mWh;
            var start = DateTime.Now.AddMinutes(-2);

            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                // act
                target.SetValue(start.AddMinutes(-10), 50, 300, 1);
                target.SetValue(start, 99, 300, 1);
                target.GetValue(start);
                // assert
                Assert.AreEqual(216.667m, target.GetValue(start.AddMinutes(-7)).KiloWattHour * 1000m);
                Assert.AreEqual(200.0m, target.GetValue(start.AddMinutes(-8)).KiloWattHour * 1000m);
                Assert.AreEqual(183.334m, target.GetValue(start.AddMinutes(-9)).KiloWattHour * 1000m);
                Assert.AreEqual(UInt64.MaxValue, target.GetValue(start.AddMinutes(-8)).Raw);
                Assert.AreEqual(UInt64.MaxValue, target.GetValue(start.AddMinutes(-1)).Raw);
                Assert.AreEqual(UInt64.MaxValue, target.GetValue(start.AddMinutes(-9)).Raw);
                Assert.AreEqual(99, target.GetValue(start).Raw);
                Assert.AreEqual(50, target.GetValue(start.AddMinutes(-10)).Raw);
            }
        }
Пример #10
0
        public void GetValueShouldReturnNullIfTheTimeFallsBeyondTheCurrentFileRangeTest()
        {
            var    measureTime = DateTime.Now;
            Record actual;

            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                target.SetValue(measureTime, 50, 25, 15);
            }

            // act
            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                actual = target.GetValue(measureTime.AddYears(1));
            }

            // assert
            Assert.IsNull(actual);
        }
Пример #11
0
        public void GetValueShouldReturnNullIfTheTimeFallsOutsideTheCurrentFileRangeTest()
        {
            DateTime measureTime = DateTime.Now;
            Record   actual;

            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                target.SetValue(measureTime, 50, 5, 21);
            }

            // act
            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                actual = target.GetValue(measureTime.AddMinutes(-1));
            }

            // assert
            Assert.IsNull(actual);
        }
Пример #12
0
        public void SetValueShouldIncreaseTheFileSizeWithOneYearIfMeasureIsOneMinuteBeyondTheEndOfTheFileTest()
        {
            var measureTime       = new DateTime(DateTime.Today.Year - 1, 1, 1);
            var secondMeasureTime = new DateTime(DateTime.Today.Year, 1, 1);

            // act
            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                target.SetValue(measureTime, 25, 50, 15);
                target.SetValue(secondMeasureTime, 25, 50, 15);
            }

            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                // assert
                Assert.That(target.StartOfFile.Equals(RoundDown(measureTime)));
                Assert.AreEqual((ulong)25, target.GetValue(secondMeasureTime).Raw);
            }
        }
Пример #13
0
        public void GetValueTest()
        {
            var    measureTime = DateTime.Now;
            Record actual;

            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                target.SetValue(measureTime, 50, 2, 3);
            }

            // act
            using (var target = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                actual = target.GetValue(measureTime);
            }

            // assert
            Assert.AreEqual(50, actual.Raw);
            Assert.AreEqual((ulong)(25), actual.KiloWattHour); // 50 / 2
            Assert.AreEqual((ulong)(75), actual.Money);        // 50 / 2 * 3
        }
Пример #14
0
        public void WhenValuesAreWrittenWithRunningTotalGetValueShouldReturnSameValues()
        {
            _dataStorageContext.SerialNumber = "00-00-000-005";

            using (var storage = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                Record runningTotal = null;
                for (int i = 0; i < 10; ++i)
                {
                    runningTotal = storage.SetValue(new DateTime(2013, 5, 7, 0, i, 0), (ulong)(3000 + i), 300.0m, 0.21m, runningTotal);
                }
            }

            using (var storage = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                for (int i = 0; i < 10; ++i)
                {
                    var record = storage.GetValue(new DateTime(2013, 5, 7, 0, i, 0));
                    Assert.IsTrue(record.IsValidMeasurement);
                    Assert.AreEqual((ulong)(3000 + i), record.Raw);
                }
            }
        }
Пример #15
0
        public void WhenValueIsWrittenInLastSlotShouldStoreValueAndNotGrowFile()
        {
            _dataStorageContext.SerialNumber = "00-00-000-003";
            DateTime endOfFile;
            DateTime timestampAtEnd;

            using (var storage = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                // Write first value, this should set the start time of the storage.
                storage.SetValue(new DateTime(2013, 5, 7, 22, 58, 0), 3000, 300.0m, 0.21m);
                endOfFile = storage.EndOfFile;

                // Write a value in the last slot. Since the end of file is the end of the slot, we have to subtract one minute.
                timestampAtEnd = endOfFile.AddMinutes(-1);
                storage.SetValue(timestampAtEnd, 3000, 300.0m, 0.21m);
            }

            using (var storage = new kWhStorage(new LoggerFactory(), _options, _dataStorageContext))
            {
                var record = storage.GetValue(timestampAtEnd);
                Assert.AreEqual(3000, record.Raw);
                Assert.AreEqual(endOfFile, storage.EndOfFile);
            }
        }