def test_add_list_of_interval(self): interval = Interval('test', ('PT0H', 'PT1H')) interval.interval = [('PT1H', 'PT2H')] assert interval.name == 'test' assert interval.start == ['PT0H', 'PT1H'] assert interval.end == ['PT1H', 'PT2H']
def test_to_hours_list(self): interval = Interval('test', [('PT0H', 'PT1H'), ('PT2H', 'PT3H'), ('PT5H', 'PT7H')]) actual = interval.to_hours() assert actual == [(0, 1), (2, 3), (5, 7)]
def test_convert_month_to_hours(self): interval = Interval('test', ('P1M', 'P2M')) actual = interval._convert_to_hours(interval.start) expected = 744 assert actual == expected actual = interval._convert_to_hours(interval.end) expected = 1416 assert actual == expected
def test_convert_hour_to_hours(self): interval = Interval('test', ('PT0H', 'PT1H')) actual = interval._convert_to_hours(interval.start) expected = 0 assert actual == expected actual = interval._convert_to_hours(interval.end) expected = 1 assert actual == expected
def test_convert_week_to_hours(self): interval = Interval('test', ('P2D', 'P3D')) actual = interval._convert_to_hours(interval.start) expected = 48 assert actual == expected actual = interval._convert_to_hours(interval.end) expected = 72 assert actual == expected
def test_time_interval_start_before_end(get_time_intervals): with raises(ValueError) as excinfo: Interval('backwards', ('P1Y', 'P3M')) assert "A time interval must not end before it starts" in str(excinfo) interval = Interval('starts_ok', ('P0Y', 'P1M')) with raises(ValueError) as excinfo: interval.interval = ('P2M', 'P1M') assert "A time interval must not end before it starts" in str(excinfo)
def test_equality(self): a = Interval('test', ('PT0H', 'PT1H')) b = Interval('test', ('PT0H', 'PT1H')) c = Interval('another_test', ('PT0H', 'PT1H')) d = Interval('another_test', ('PT1H', 'PT2H')) assert a == b assert a != c assert b != c assert c != d
def test_load_multiple_interval(self): interval = Interval('test', [('PT0H', 'PT1H'), ('PT1H', 'PT2H')]) assert interval.name == 'test' assert interval.start == ['PT0H', 'PT1H'] assert interval.end == ['PT1H', 'PT2H']
def test_load_interval(self): interval = Interval('test', ('PT0H', 'PT1H')) assert interval.name == 'test' assert interval.start == 'PT0H' assert interval.end == 'PT1H'
def test_str_multiple_intervals(self): interval = Interval('test', [('PT0H', 'PT1H'), ('PT2H', 'PT3H'), ('PT5H', 'PT7H')]) actual = str(interval) assert actual == \ "Interval 'test' maps to:\n hour 0 to hour 1\n hour 2 to hour 3\n "\ "hour 5 to hour 7\n"
def test_interval_loads(self): """Pass a time-interval definition into the register """ data = [{'id': '1_1', 'start': 'PT0H', 'end': 'PT1H'}] register = TimeIntervalRegister() register.register(IntervalSet('energy_supply_hourly', data)) assert register.names == ['energy_supply_hourly'] actual = register.get_entry('energy_supply_hourly') element = Interval('1_1', ('PT0H', 'PT1H'), base_year=2010) expected = OrderedDict() expected['1_1'] = element assert actual.data == expected
def test_interval_loads(self): """Pass a time-interval definition into the register """ data = [{'name': '1_1', 'interval': [('PT0H', 'PT1H')]}] register = NDimensionalRegister() register.register(IntervalSet('energy_supply_hourly', data)) assert register.names == ['energy_supply_hourly'] actual = register.get_entry('energy_supply_hourly') element = Interval('1_1', ('PT0H', 'PT1H'), base_year=2010) expected = [] expected.append(element) assert actual.data == expected
def test_empty_interval_list_tuple(self): with raises(ValueError): Interval('test', [()])
def test_months_load(self, months): """Pass a monthly time-interval definition into the register """ register = TimeIntervalRegister() register.register(IntervalSet('months', months)) actual = register.get_entry('months') expected_names = \ ['1_0', '1_1', '1_2', '1_3', '1_4', '1_5', '1_6', '1_7', '1_8', '1_9', '1_10', '1_11'] expected = [Interval('1_0', ('P0M', 'P1M')), Interval('1_1', ('P1M', 'P2M')), Interval('1_2', ('P2M', 'P3M')), Interval('1_3', ('P3M', 'P4M')), Interval('1_4', ('P4M', 'P5M')), Interval('1_5', ('P5M', 'P6M')), Interval('1_6', ('P6M', 'P7M')), Interval('1_7', ('P7M', 'P8M')), Interval('1_8', ('P8M', 'P9M')), Interval('1_9', ('P9M', 'P10M')), Interval('1_10', ('P10M', 'P11M')), Interval('1_11', ('P11M', 'P12M'))] for name, interval in zip(expected_names, expected): assert actual.data[name] == interval
def test_load_remap_timeslices(self): interval = Interval('1', [('P2M', 'P3M'), ('P3M', 'P4M'), ('P4M', 'P5M')]) actual = interval.to_hours() assert actual == [(1416, 2160), (2160, 2880), (2880, 3624)]
def test_repr(self): interval = Interval('test', ('P2M', 'P3M'), 2011) actual = repr(interval) assert actual == "Interval('test', [('P2M', 'P3M')], base_year=2011)"
def test_str_one_interval(self): interval = Interval('test', ('P2M', 'P3M')) actual = str(interval) assert actual == \ "Interval 'test' maps to:\n hour 1416 to hour 2160\n"
def test_to_hours_month(self): interval = Interval('test', ('P2M', 'P3M')) actual = interval.to_hours() assert actual == [(1416, 2160)]
def test_to_hours_zero(self): interval = Interval('test', ('PT0H', 'PT1H')) actual = interval.to_hours() assert actual == [(0, 1)]
def test_raise_error_load_illegal(self): with raises(ValueError): Interval('test', 'start', 'end')
def test_invalid_set_interval(self): interval = Interval('test', ('PT0H', 'PT1H')) with raises(ValueError) as excinfo: interval.interval = None msg = "A time interval must add either a single tuple or a list of tuples" assert msg in str(excinfo)
def test_months_load(self, months): """Pass a monthly time-interval definition into the register """ register = NDimensionalRegister() register.register(IntervalSet('months', months)) actual = register.get_entry('months') expected = [ Interval('jan', ('P0M', 'P1M')), Interval('feb', ('P1M', 'P2M')), Interval('mar', ('P2M', 'P3M')), Interval('apr', ('P3M', 'P4M')), Interval('may', ('P4M', 'P5M')), Interval('jun', ('P5M', 'P6M')), Interval('jul', ('P6M', 'P7M')), Interval('aug', ('P7M', 'P8M')), Interval('sep', ('P8M', 'P9M')), Interval('oct', ('P9M', 'P10M')), Interval('nov', ('P10M', 'P11M')), Interval('dec', ('P11M', 'P12M')) ] for idx, interval in enumerate(expected): assert actual.data[idx] == interval