Exemple #1
0
        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);
            }
        }
Exemple #2
0
        public void SetValueShouldIncreaseTheFileSizeWithSevenDaysIfMeasureFallsBeyondTheEndOfTheFileTest()
        {
            // arrange
            const int counter = 1;

            var measureTime = DateTime.Now.AddDays(-10);

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

            // act
            using (var target = new kWhStorage("Serial", BaseDir, counter, Precision.Wh))
            {
                target.SetValue(measureTime, 25, 50, 15);
                target.SetValue(secondMeasureTime, 25, 50, 15);
            }

            using (var target = new kWhStorage("Serial", BaseDir, counter, Precision.Wh))
            {
                // 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);
            }
        }
Exemple #3
0
        public void WhenValueIsWrittenOneMinuteBeforeStartShouldThrowException()
        {
            DateTime startOfFile;
            DateTime endOfFile;

            Assert.Throws(typeof(InvalidOperationException), delegate
            {
                using (var storage = new kWhStorage("00-00-000-003", BaseDir, 1, Precision.Wh))
                {
                    // 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("00-00-000-003", BaseDir, 1, Precision.Wh))
                {
                    storage.GetValue(new DateTime(2013, 5, 7, 22, 58, 0));

                    Assert.AreEqual(startOfFile, storage.StartOfFile);
                    Assert.AreEqual(endOfFile, storage.EndOfFile);
                }
            });
        }
Exemple #4
0
        public void SetValueShouldStoreTheRunningTotalsInTheFileTest()
        {
            // arrange
            var time = new DateTime(2012, 8, 17, 13, 20, 0);

            using (var storageProvider = new kWhStorage("12-13-001-075", BaseDir, 1, Precision.Wh))
            {
                // act
                for (int i = 0; i < 10; i++)
                {
                    storageProvider.SetValue(time.AddMinutes(i), (ulong)(50 * i), 1000m, 21m);
                }
            }
            using (var storageProvider = new kWhStorage("12-13-001-075", BaseDir, 1, Precision.Wh))
            {
                // assert
                string fileName = GetTestPath("Qbox_12-13-001-075/12-13-001-075_00000001.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);
                }
            }
        }
Exemple #5
0
        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);
            }
        }
Exemple #6
0
        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);
            }
        }
Exemple #7
0
        public void GetValueShouldReturnTheAverageValueBetweenEmptySlotsTest()
        {
            // arrange
            const int counter = 1;

            var    measureTime = DateTime.Now.AddMinutes(-15);
            Record actual      = null;

            using (var target = new kWhStorage("Serial", BaseDir, counter, Precision.Wh))
            {
                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("Serial", BaseDir, counter, Precision.Wh))
            {
                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);
        }
Exemple #8
0
        public void WhenSetValueIsCalledWithAnIntervalItShouldLevelTheValuesWrittenTest()
        {
            // 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(-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);
            }
        }
Exemple #9
0
        public void WhenValuesAreWrittenWithRunningTotalGetValueShouldReturnSameValues()
        {
            using (var storage = new kWhStorage("00-00-000-005", BaseDir, 1, Precision.Wh))
            {
                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("00-00-000-005", BaseDir, 1, Precision.Wh))
            {
                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);
                }
            }
        }
Exemple #10
0
        public void GetValueShouldNotThrowExceptionWhenFileDoesNotExistTest()
        {
            // arrange
            const string serialNumber = "Serial";
            const int    counter      = 1;

            // act
            using (var target = new kWhStorage(serialNumber, BaseDir, counter, Precision.Wh))
            {
                Record result = null;
                try
                {
                    result = target.GetValue(DateTime.Now);
                    // assert
                }
                catch
                {
                    Assert.Fail("Should not throw");
                }
                Assert.IsNull(result);
            }
        }
Exemple #11
0
        public void GetValueShouldReturnNullIfTheTimeFallsBeyondTheCurrentFileRangeTest()
        {
            // arrange
            const int counter = 1;

            var    measureTime = DateTime.Now;
            Record actual;

            using (var target = new kWhStorage("Serial", BaseDir, counter, Precision.Wh))
            {
                target.SetValue(measureTime, 50, 25, 15);
            }

            // act
            using (var target = new kWhStorage("Serial", BaseDir, counter, Precision.Wh))
            {
                actual = target.GetValue(measureTime.AddYears(1));
            }

            // assert
            Assert.IsNull(actual);
        }
Exemple #12
0
        public void SetValueShouldIncreaseTheFileSizeWithOneYearIfMeasureIsOneMinuteBeyondTheEndOfTheFileTest()
        {
            // arrange
            const int counter = 1;

            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("Serial", BaseDir, counter, Precision.Wh))
            {
                target.SetValue(measureTime, 25, 50, 15);
                target.SetValue(secondMeasureTime, 25, 50, 15);
            }

            using (var target = new kWhStorage("Serial", BaseDir, counter, Precision.Wh))
            {
                // assert
                Assert.That(target.StartOfFile.Equals(RoundDown(measureTime)));
                Assert.AreEqual((ulong)25, target.GetValue(secondMeasureTime).Raw);
            }
        }
Exemple #13
0
        public void WhenValueIsWrittenInLastSlotShouldStoreValueAndNotGrowFile()
        {
            DateTime endOfFile;
            DateTime timestampAtEnd;

            using (var storage = new kWhStorage("00-00-000-003", BaseDir, 1, Precision.Wh))
            {
                // 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("00-00-000-003", BaseDir, 1, Precision.Wh))
            {
                var record = storage.GetValue(timestampAtEnd);
                Assert.AreEqual(3000, record.Raw);
                Assert.AreEqual(endOfFile, storage.EndOfFile);
            }
        }
Exemple #14
0
        public void GetValueTest()
        {
            // arrange
            const int counter = 1;

            var    measureTime = DateTime.Now;
            Record actual;

            using (var target = new kWhStorage("Serial", BaseDir, counter, Precision.Wh))
            {
                target.SetValue(measureTime, 50, 2, 3);
            }

            // act
            using (var target = new kWhStorage("Serial", BaseDir, counter, Precision.Wh))
            {
                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
        }