Skip to content

Commit b30f95e

Browse files
committed
Add tests for window method in ring buffer
Test for expected copy behavior on missing values does not work as documented. Signed-off-by: cwasicki <[email protected]>
1 parent 8369b7b commit b30f95e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/timeseries/test_ringbuffer.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from frequenz.sdk.timeseries import Sample
1616
from frequenz.sdk.timeseries._quantities import Quantity
1717
from frequenz.sdk.timeseries._ringbuffer import Gap, OrderedRingBuffer
18+
from frequenz.sdk.timeseries._ringbuffer.buffer import FloatArray
1819

1920
FIVE_MINUTES = timedelta(minutes=5)
2021
ONE_MINUTE = timedelta(minutes=1)
@@ -494,3 +495,30 @@ def test_delete_oudated_gap() -> None:
494495
buffer.update(Sample(datetime.fromtimestamp(202, tz=timezone.utc), Quantity(2)))
495496

496497
assert len(buffer.gaps) == 0
498+
499+
500+
def get_orb(data: FloatArray) -> OrderedRingBuffer[FloatArray]:
501+
"""Get OrderedRingBuffer with data.
502+
503+
Args:
504+
data: Data to fill the buffer with.
505+
506+
Returns:
507+
OrderedRingBuffer with data.
508+
"""
509+
buffer = OrderedRingBuffer(data, ONE_SECOND)
510+
for i, d in enumerate(data): # pylint: disable=invalid-name
511+
buffer.update(Sample(dt(i), Quantity(d) if d is not None else None))
512+
return buffer
513+
514+
515+
def test_window() -> None:
516+
"""Test the window function."""
517+
buffer = get_orb(np.array([0, None, 2, 3, 4]))
518+
win = buffer.window(dt(0), dt(3), force_copy=False)
519+
assert [0, np.nan, 2] == list(win)
520+
buffer._buffer[1] = 1 # pylint: disable=protected-access
521+
# Test whether the window is a view or a copy
522+
assert [0, 1, 2] == list(win) # NB: second element should be NaN according to docs
523+
win = buffer.window(dt(0), dt(3), force_copy=False)
524+
assert [0, 1, 2] == list(win)

0 commit comments

Comments
 (0)