public void TestMineOrCalculateCarbonEmissionsRelativeMerit_CustomInternalCalculation()
        {
            // Arrange
            var regionWattTimeName  = "PJM";
            var smartGridRegionName = "US_PJM";

            var startDateTime = DateTime.UtcNow.AddDays(-10);
            var endDateTime   = DateTime.UtcNow.AddDays(-9);

            var    wattTimeApiUrl            = CloudConfigurationManager.GetSetting("WattTimeApiUrl");
            var    wattTimeApiKey            = CloudConfigurationManager.GetSetting("WattTimeApiKey");
            string wattTimeApiV2Url          = CloudConfigurationManager.GetSetting("WattTimeApiV2Url");
            string WattTimeUsername          = CloudConfigurationManager.GetSetting("WattTimeUsername");
            string WattTimePassword          = CloudConfigurationManager.GetSetting("WattTimePassword");
            string WattTimeEmail             = CloudConfigurationManager.GetSetting("WattTimeEmail");
            string WattTimeOrganization      = CloudConfigurationManager.GetSetting("WattTimeOrganization");
            var    selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var    maxNumberOfCallsPerMinute = 200;

            var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            CarbonEmissionsMiner carbonEmissionsMiner = new CarbonEmissionsMiner(
                wattTimeApiUrl,
                wattTimeApiKey,
                wattTimeApiV2Url,
                WattTimeUsername,
                WattTimePassword,
                WattTimeEmail,
                WattTimeOrganization,
                selfThrottlingMethod,
                databaseConnectionString,
                maxNumberOfCallsPerMinute,
                wattTimeInteraction,
                "CustomInternalCalculation");

            int regionId;

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                regionId = _objectModel.FindEmissionsRegion(smartGridRegionName).EmissionsRegionID;
            }
            Assert.IsNotNull(regionId, "Couldn't find specified emissions region in the database");

            // Act
            carbonEmissionsMiner.MineOrCalculateCarbonEmissionsRelativeMerit(
                regionWattTimeName,
                regionId);

            // Assert
            // Verify that each data point has been recorded in the database
            var results = carbonEmissionsMiner.CalculateHistoricRelativeMeritDataResults(
                regionId,
                startDateTime,
                endDateTime);

            foreach (var result in results)
            {
                using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                {
                    var dataPoint = _objectModel.FindCarbonEmissionsRelativeMeritDataPoints(regionId, result.Timestamp.AddMinutes(-5), result.Timestamp.AddMinutes(15));
                    Assert.IsNotNull(dataPoint);
                }
            }
        }
        public void TestCalculateHistoricRelativeMeritDataResults()
        {
            // Arrange
            var smartGridRegionName = "US_PJM";

            var startDateTime = DateTime.UtcNow.AddDays(-10);
            var endDateTime   = DateTime.UtcNow.AddDays(-9);

            var    wattTimeApiUrl            = CloudConfigurationManager.GetSetting("WattTimeApiUrl");
            var    wattTimeApiKey            = CloudConfigurationManager.GetSetting("WattTimeApiKey");
            string wattTimeApiV2Url          = CloudConfigurationManager.GetSetting("WattTimeApiV2Url");
            string WattTimeUsername          = CloudConfigurationManager.GetSetting("WattTimeUsername");
            string WattTimePassword          = CloudConfigurationManager.GetSetting("WattTimePassword");
            string WattTimeEmail             = CloudConfigurationManager.GetSetting("WattTimeEmail");
            string WattTimeOrganization      = CloudConfigurationManager.GetSetting("WattTimeOrganization");
            var    selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var    maxNumberOfCallsPerMinute = 200;

            var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            var carbonEmissionsMiner = new CarbonEmissionsMiner(
                wattTimeApiUrl,
                wattTimeApiKey,
                wattTimeApiV2Url,
                WattTimeUsername,
                WattTimePassword,
                WattTimeEmail,
                WattTimeOrganization,
                selfThrottlingMethod,
                databaseConnectionString,
                maxNumberOfCallsPerMinute,
                wattTimeInteraction);

            int regionId;

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                regionId = _objectModel.FindEmissionsRegion(smartGridRegionName).EmissionsRegionID;
            }
            Assert.IsNotNull(regionId, "Couldn't find specified emissions region in the database");

            // Act
            var results = carbonEmissionsMiner.CalculateHistoricRelativeMeritDataResults(
                regionId,
                startDateTime,
                endDateTime
                );

            // Assert
            foreach (var item in results)
            {
                if (item.EmissionsRelativeMerit != null)
                {
                    Assert.IsTrue(item.EmissionsRelativeMerit >= 0, "item.EmissionsRelativeMerit >= 0");
                    Assert.IsTrue(item.EmissionsRelativeMerit <= 1, "item.EmissionsRelativeMerit <= 1");
                }
                if (item.EmissionsRelativeMerit_Forcast != null)
                {
                    Assert.IsTrue(item.EmissionsRelativeMerit_Forcast >= 0, "item.EmissionsRelativeMerit_Forcast >= 0");
                    Assert.IsTrue(item.EmissionsRelativeMerit_Forcast <= 1, "item.EmissionsRelativeMerit_Forcast <= 1");
                }
            }
        }