Skip to content

Commit

Permalink
Add tests for sequence module
Browse files Browse the repository at this point in the history
  • Loading branch information
p-snft committed Aug 13, 2024
1 parent 519f95c commit 41ff608
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions tests/test_plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,60 @@
SPDX-License-Identifier: MIT
"""
import numpy as np
import pytest

from oemof.solph._plumbing import _FakeSequence
from oemof.solph._plumbing import sequence


def test_sequence():
seq0 = sequence(0)
assert seq0[0] == 0
def test_fake_sequence():
seq0 = _FakeSequence(42)
assert seq0[0] == 42
assert seq0.size is None

assert seq0[10] == 0
assert seq0[10] == 42
assert seq0.size is None

assert seq0.max() == 0
assert seq0.max() == 42
assert seq0.min() == 42
assert seq0.value == 42
assert seq0.sum() == np.inf

assert str(seq0) == "[42, 42, ..., 42]"

with pytest.raises(TypeError):
seq0.to_numpy()
assert (seq0.to_numpy(length=5) == np.array(5 * [42])).all()

with pytest.raises(TypeError):
len(seq0)

seq0.size = 2
assert seq0.size == 2
assert len(seq0) == 2

seq10 = sequence(10)
assert seq10.max() == 10
assert seq0.max() == 42
assert seq0.min() == 42
assert seq0.value == 42
assert seq0.sum() == 84

assert seq10[0] == 10
assert seq10.size is None
assert str(seq0) == "[42, 42]"

seq10.size = 10
assert seq10.size == 10
assert (seq0.to_numpy() == np.array(2 * [42])).all()
assert (seq0.to_numpy(length=5) == np.array(5 * [42])).all()


def test_sequence():
seq0 = sequence(0)
assert isinstance(seq0, _FakeSequence)
assert seq0.value == 0
assert seq0.size is None

seq12 = sequence([1, 3])
assert max(seq12) == 3
assert seq12[0] == 1
assert seq12[1] == 3
seq13 = sequence([1, 3])
assert isinstance(seq13, np.ndarray)
assert (seq13 == np.array([1, 3])).all()

with pytest.raises(IndexError):
_ = seq12[2]
seq_ab = sequence("ab")
assert isinstance(seq_ab, str)
assert seq_ab == "ab"

0 comments on commit 41ff608

Please sign in to comment.