Skip to content

Commit

Permalink
Properly test create_time_index()
Browse files Browse the repository at this point in the history
  • Loading branch information
p-snft committed Jul 2, 2024
1 parent 0869463 commit 12f5bcf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/oemof/solph/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ def create_time_index(
else:
hoy = 8760
number = round(hoy / interval)
if start is None:
if start is not None:
if year is not None:
raise ValueError(
"Arguments 'start' and 'year' are mutually exclusive."
)
else:
start = f"1/1/{year}"
try:
time_index = pd.date_range(
Expand Down
24 changes: 24 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,36 @@
"""

import os
import pytest

from oemof.solph import helpers
from oemof.solph import create_time_index


def test_creation_of_extended_path():
"""Creation of a sub-folder based on the base path failed."""
p = helpers.extend_basic_path("test_subfolder_X345qw34_tmp")
assert os.path.isdir(p)
os.rmdir(p)


def test_create_time_index():
assert len(create_time_index(2014)) == 8761
assert len(create_time_index(2012)) == 8785 # leap year
assert len(create_time_index(2014, interval=0.5)) == 17521
assert len(create_time_index(2014, interval=0.5, number=10)) == 11
assert len(create_time_index(2014, number=10)) == 11
assert (
str(create_time_index(2014, interval=0.5, number=10)[-1])
== "2014-01-01 05:00:00"
)
assert (
str(create_time_index(2014, interval=2, number=10)[-1])
== "2014-01-01 20:00:00"
)
assert (
str(create_time_index(interval=0.5, number=10, start="2025-01-02")[-1])
== "2025-01-02 05:00:00"
)
with pytest.raises(ValueError, match="mutually exclusive"):
create_time_index(year=2015, start="2025-01-02")

0 comments on commit 12f5bcf

Please sign in to comment.