frequenz-resampling 0.3.0

A library for resampling a stream of samples to a given interval.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# License: MIT
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH

"""Tests to verify that the resampler can be used successfully from Python."""

import datetime as dt
from typing import Literal

import pandas as pd
import pytest
from frequenz.resampling import Closed, Label, Resampler, ResamplingFunction, resample


def test_resampler_resampling_function_average() -> None:
    """Test the resampler."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Right,
    )

    # Data starts at t=0 with values 1-10
    # Interval [0, 5): t=0,1,2,3,4 with values 1,2,3,4,5 → avg = 3.0
    # Interval [5, 10): t=5,6,7,8,9 with values 6,7,8,9,10 → avg = 8.0
    for i in range(10):
        resampler.push_sample(timestamp=start + i * step, value=i + 1)

    expected = [
        (start + 5 * step, 3.0),
        (start + 10 * step, 8.0),
    ]

    resampled = resampler.resample(start + 10 * step)

    assert resampled == expected


def test_resampler_resampling_function_sum() -> None:
    """Test the resampler."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.Sum,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Right,
    )

    # Data starts at t=0 with values 1-10
    # Interval [0, 5): t=0,1,2,3,4 with values 1,2,3,4,5 → sum = 15.0
    # Interval [5, 10): t=5,6,7,8,9 with values 6,7,8,9,10 → sum = 40.0
    for i in range(10):
        resampler.push_sample(timestamp=start + i * step, value=i + 1)

    expected = [
        (start + 5 * step, 15.0),
        (start + 10 * step, 40.0),
    ]

    resampled = resampler.resample(start + 10 * step)

    assert resampled == expected


def test_resampler_resampling_function_max() -> None:
    """Test the resampler."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.Max,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Right,
    )

    # Data starts at t=0 with values 1-10
    # Interval [0, 5): t=0,1,2,3,4 with values 1,2,3,4,5 → max = 5.0
    # Interval [5, 10): t=5,6,7,8,9 with values 6,7,8,9,10 → max = 10.0
    for i in range(10):
        resampler.push_sample(timestamp=start + i * step, value=i + 1)

    expected = [
        (start + 5 * step, 5.0),
        (start + 10 * step, 10.0),
    ]

    resampled = resampler.resample(start + 10 * step)

    assert resampled == expected


def test_resampler_resampling_function_min() -> None:
    """Test the resampler."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.Min,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Right,
    )

    # Data starts at t=0 with values 1-10
    # Interval [0, 5): t=0,1,2,3,4 with values 1,2,3,4,5 → min = 1.0
    # Interval [5, 10): t=5,6,7,8,9 with values 6,7,8,9,10 → min = 6.0
    for i in range(10):
        resampler.push_sample(timestamp=start + i * step, value=i + 1)

    expected = [
        (start + 5 * step, 1.0),
        (start + 10 * step, 6.0),
    ]

    resampled = resampler.resample(start + 10 * step)

    assert resampled == expected


def test_resampler_resampling_function_first() -> None:
    """Test the resampler."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.First,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Right,
    )

    # Data starts at t=0 with values 1-10
    # Interval [0, 5): t=0,1,2,3,4 with values 1,2,3,4,5 → first = 1.0
    # Interval [5, 10): t=5,6,7,8,9 with values 6,7,8,9,10 → first = 6.0
    for i in range(10):
        resampler.push_sample(timestamp=start + i * step, value=i + 1)

    expected = [
        (start + 5 * step, 1.0),
        (start + 10 * step, 6.0),
    ]

    resampled = resampler.resample(start + 10 * step)

    assert resampled == expected


def test_resampler_resampling_function_last() -> None:
    """Test the resampler."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.Last,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Right,
    )

    # Data starts at t=0 with values 1-10
    # Interval [0, 5): t=0,1,2,3,4 with values 1,2,3,4,5 → last = 5.0
    # Interval [5, 10): t=5,6,7,8,9 with values 6,7,8,9,10 → last = 10.0
    for i in range(10):
        resampler.push_sample(timestamp=start + i * step, value=i + 1)

    expected = [
        (start + 5 * step, 5.0),
        (start + 10 * step, 10.0),
    ]

    resampled = resampler.resample(start + 10 * step)

    assert resampled == expected


def test_resampler_resampling_function_coalesce() -> None:
    """Test the resampler."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.Coalesce,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Right,
    )

    # Data starts at t=0 with values 1-10, but t=5 is None
    # Interval [0, 5): t=0,1,2,3,4 with values 1,2,3,4,5 → coalesce = 1.0
    # Interval [5, 10): t=5,6,7,8,9 with values None,7,8,9,10 → coalesce = 7.0
    for i in range(10):
        if i == 5:
            resampler.push_sample(timestamp=start + i * step, value=None)
        else:
            resampler.push_sample(timestamp=start + i * step, value=i + 1)

    expected = [
        (start + 5 * step, 1.0),
        (start + 10 * step, 7.0),
    ]

    resampled = resampler.resample(start + 10 * step)

    assert resampled == expected


def test_resampler_resampling_function_count() -> None:
    """Test the resampler."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.Count,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Right,
    )

    # Data starts at t=0 with values 1-10
    # Interval [0, 5): t=0,1,2,3,4 → count = 5.0
    # Interval [5, 10): t=5,6,7,8,9 → count = 5.0
    for i in range(10):
        resampler.push_sample(timestamp=start + i * step, value=i + 1)

    expected = [
        (start + 5 * step, 5.0),
        (start + 10 * step, 5.0),
    ]

    resampled = resampler.resample(start + 10 * step)

    assert resampled == expected


def test_resampling_none() -> None:
    """Test resampling with None values."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Right,
    )

    # All values are None
    for i in range(10):
        resampler.push_sample(timestamp=start + i * step, value=None)

    expected = [
        (start + 5 * step, None),
        (start + 10 * step, None),
    ]

    resampled = resampler.resample(start + 10 * step)

    assert resampled == expected


def test_enum_values() -> None:
    """Test the ResamplingFunction enum."""
    assert ResamplingFunction.values() == [0, 1, 2, 3, 4, 5, 6, 7]


def test_enum_members() -> None:
    """Test the ResamplingFunction enum."""
    assert ResamplingFunction.members() == [
        ("Average", 0),
        ("Sum", 1),
        ("Max", 2),
        ("Min", 3),
        ("Last", 4),
        ("Count", 5),
        ("First", 6),
        ("Coalesce", 7),
    ]


def test_enum_str_repr() -> None:
    """Test the ResamplingFunction enum."""
    assert str(ResamplingFunction.Average) == "ResamplingFunction.Average"
    assert repr(ResamplingFunction.Average) == "<ResamplingFunction.Average: 0>"
    assert str(ResamplingFunction.Sum) == "ResamplingFunction.Sum"
    assert repr(ResamplingFunction.Sum) == "<ResamplingFunction.Sum: 1>"
    assert str(ResamplingFunction.Max) == "ResamplingFunction.Max"
    assert repr(ResamplingFunction.Max) == "<ResamplingFunction.Max: 2>"
    assert str(ResamplingFunction.Min) == "ResamplingFunction.Min"
    assert repr(ResamplingFunction.Min) == "<ResamplingFunction.Min: 3>"
    assert str(ResamplingFunction.Last) == "ResamplingFunction.Last"
    assert repr(ResamplingFunction.Last) == "<ResamplingFunction.Last: 4>"
    assert str(ResamplingFunction.Count) == "ResamplingFunction.Count"
    assert repr(ResamplingFunction.Count) == "<ResamplingFunction.Count: 5>"
    assert str(ResamplingFunction.First) == "ResamplingFunction.First"
    assert repr(ResamplingFunction.First) == "<ResamplingFunction.First: 6>"
    assert str(ResamplingFunction.Coalesce) == "ResamplingFunction.Coalesce"
    assert repr(ResamplingFunction.Coalesce) == "<ResamplingFunction.Coalesce: 7>"


def test_resampling_function_name_value() -> None:
    """Test the ResamplingFunction name and value interface."""
    assert ResamplingFunction.Average.name == "Average"
    assert ResamplingFunction.Average.value == 0
    assert ResamplingFunction.Sum.name == "Sum"
    assert ResamplingFunction.Sum.value == 1
    assert ResamplingFunction.Max.name == "Max"
    assert ResamplingFunction.Max.value == 2
    assert ResamplingFunction.Min.name == "Min"
    assert ResamplingFunction.Min.value == 3
    assert ResamplingFunction.Last.name == "Last"
    assert ResamplingFunction.Last.value == 4
    assert ResamplingFunction.Count.name == "Count"
    assert ResamplingFunction.Count.value == 5
    assert ResamplingFunction.First.name == "First"
    assert ResamplingFunction.First.value == 6
    assert ResamplingFunction.Coalesce.name == "Coalesce"
    assert ResamplingFunction.Coalesce.value == 7


def test_resampling_function_init() -> None:
    """Test the ResamplingFunction init."""
    assert ResamplingFunction(0) == ResamplingFunction.Average
    assert ResamplingFunction(1) == ResamplingFunction.Sum
    assert ResamplingFunction(2) == ResamplingFunction.Max
    assert ResamplingFunction(3) == ResamplingFunction.Min
    assert ResamplingFunction(4) == ResamplingFunction.Last
    assert ResamplingFunction(5) == ResamplingFunction.Count
    assert ResamplingFunction(6) == ResamplingFunction.First
    assert ResamplingFunction(7) == ResamplingFunction.Coalesce


def test_resampler_label_left() -> None:
    """Test the resampler with the left interval label."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=0.5)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Left,
    )

    for i in range(0, 20):
        resampler.push_sample(timestamp=start + i * step, value=i + 1)

    expected = [
        (start + 0 * step, 5.5),
        (start + 10 * step, 15.5),
    ]

    resampled = resampler.resample(start + 20 * step)

    assert resampled == expected


def test_resampler_last_timestamp() -> None:
    """Test the resampler with the right interval label."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=0.5)
    resampler = Resampler(
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        max_age_in_intervals=1,
        start=start,
        closed=Closed.Left,
        label=Label.Right,
    )

    # Data starts at t=0, step=0.5s, 20 samples
    # Interval [0, 5): t=0,0.5,1,1.5,2,2.5,3,3.5,4,4.5 → values 1-10 → avg = 5.5
    # Interval [5, 10): t=5,5.5,6,6.5,7,7.5,8,8.5,9,9.5 → values 11-20 → avg = 15.5
    for i in range(20):
        resampler.push_sample(timestamp=start + i * step, value=i + 1)

    expected = [
        (start + 10 * step, 5.5),
        (start + 20 * step, 15.5),
    ]

    resampled = resampler.resample(start + 20 * step)

    assert resampled == expected


# Tests for the one-shot resample function


def test_resample_function_basic() -> None:
    """Test the resample function with basic usage."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)

    # Data: t=0,1,2,3,4,5,6,7,8,9 with values 1-10
    # Interval [0, 5): t=0,1,2,3,4 with values 1,2,3,4,5 → avg = 3.0
    # Interval [5, 10): t=5,6,7,8,9 with values 6,7,8,9,10 → avg = 8.0
    data = [(start + i * step, float(i + 1)) for i in range(10)]

    result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        closed=Closed.Left,
        label=Label.Left,
    )

    assert len(result) == 2
    assert result[0] == (start, 3.0)
    assert result[1] == (start + 5 * step, 8.0)


def test_resample_function_label_right() -> None:
    """Test the resample function with label='right'."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)

    data = [(start + i * step, float(i + 1)) for i in range(10)]

    result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        closed=Closed.Left,
        label=Label.Right,
    )

    assert len(result) == 2
    # With label='right', timestamps are at end of interval
    assert result[0] == (start + 5 * step, 3.0)
    assert result[1] == (start + 10 * step, 8.0)


def test_resample_function_closed_right() -> None:
    """Test the resample function with right-closed intervals."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    data = [
        (start, 10.0),
        (start + 5 * step, 20.0),
    ]

    result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Sum,
        closed=Closed.Right,
        label=Label.Right,
    )

    assert result == [
        (start, 10.0),
        (start + 5 * step, 20.0),
    ]


@pytest.mark.parametrize(
    ("closed", "label", "pandas_closed", "pandas_label"),
    [
        (Closed.Left, Label.Left, "left", "left"),
        (Closed.Left, Label.Right, "left", "right"),
        (Closed.Right, Label.Left, "right", "left"),
        (Closed.Right, Label.Right, "right", "right"),
    ],
)
def test_resample_function_matches_pandas(
    closed: Closed,
    label: Label,
    pandas_closed: Literal["left", "right"],
    pandas_label: Literal["left", "right"],
) -> None:
    """Test the one-shot API against pandas with matching closed/label settings."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)
    data = [(start + i * step, float(i + 1)) for i in range(10)]

    result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        closed=closed,
        label=label,
    )

    pandas_series = pd.Series(
        [value for _, value in data],
        index=pd.DatetimeIndex([timestamp for timestamp, _ in data]),
        dtype="float64",
    )
    pandas_result = pandas_series.resample(
        "5s", closed=pandas_closed, label=pandas_label
    ).mean()
    expected = [
        (timestamp.to_pydatetime(), float(value))
        for timestamp, value in zip(
            pandas_result.index, pandas_result.to_list(), strict=True
        )
    ]

    assert result == expected


def test_label_init_invalid_value() -> None:
    """Test the Label enum rejects invalid values."""
    with pytest.raises(ValueError, match="Invalid label"):
        Label(99)


def test_closed_init_invalid_value() -> None:
    """Test the Closed enum rejects invalid values."""
    with pytest.raises(ValueError, match="Invalid closed"):
        Closed(99)


def test_label_values_members() -> None:
    """Test the Label enum helpers."""
    assert Label.values() == [0, 1]
    assert Label.members() == [("Left", 0), ("Right", 1)]


def test_closed_values_members() -> None:
    """Test the Closed enum helpers."""
    assert Closed.values() == [0, 1]
    assert Closed.members() == [("Left", 0), ("Right", 1)]


def test_resample_function_empty_data() -> None:
    """Test the resample function with empty data."""
    data: list[tuple[dt.datetime, float | None]] = []

    result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        closed=Closed.Left,
        label=Label.Left,
    )

    assert result == []


def test_resample_function_with_none_values() -> None:
    """Test the resample function with None values."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)

    # First value in each interval is None
    data: list[tuple[dt.datetime, float | None]] = [
        (start + i * step, None if i in (0, 5) else float(i + 1)) for i in range(10)
    ]

    result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        closed=Closed.Left,
        label=Label.Left,
    )

    assert len(result) == 2
    # Interval [0, 5): values 2,3,4,5 → avg = 3.5
    # Interval [5, 10): values 7,8,9,10 → avg = 8.5
    assert result[0] == (start, 3.5)
    assert result[1] == (start + 5 * step, 8.5)


def test_resample_function_interval_with_only_none() -> None:
    """Test a bucket that contains only None values."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)

    data: list[tuple[dt.datetime, float | None]] = [
        (start + i * step, None if i < 5 else float(i + 1)) for i in range(10)
    ]

    result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        closed=Closed.Left,
        label=Label.Left,
    )

    assert len(result) == 2
    assert result[0] == (start, None)
    assert result[1] == (start + 5 * step, 8.0)


def test_resample_function_sum() -> None:
    """Test the resample function with Sum method."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)

    data = [(start + i * step, float(i + 1)) for i in range(10)]

    result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Sum,
        closed=Closed.Left,
        label=Label.Left,
    )

    assert len(result) == 2
    # Interval [0, 5): sum(1,2,3,4,5) = 15.0
    # Interval [5, 10): sum(6,7,8,9,10) = 40.0
    assert result[0] == (start, 15.0)
    assert result[1] == (start + 5 * step, 40.0)


def test_resample_function_min_max() -> None:
    """Test the resample function with Min and Max methods."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)

    data = [(start + i * step, float(i + 1)) for i in range(10)]

    min_result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Min,
        closed=Closed.Left,
        label=Label.Left,
    )
    max_result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Max,
        closed=Closed.Left,
        label=Label.Left,
    )

    assert min_result[0] == (start, 1.0)
    assert min_result[1] == (start + 5 * step, 6.0)
    assert max_result[0] == (start, 5.0)
    assert max_result[1] == (start + 5 * step, 10.0)


def test_resample_function_single_sample() -> None:
    """Test the resample function with a single sample."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)

    data = [(start, 42.0)]

    result = resample(
        data,
        dt.timedelta(seconds=5),
        ResamplingFunction.Average,
        closed=Closed.Left,
        label=Label.Left,
    )

    assert len(result) == 1
    assert result[0] == (start, 42.0)


def test_resample_function_all_methods() -> None:
    """Test the resample function with all resampling methods."""
    start = dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
    step = dt.timedelta(seconds=1)

    data = [(start + i * step, float(i + 1)) for i in range(5)]

    # Test all methods work without errors
    for method in [
        ResamplingFunction.Average,
        ResamplingFunction.Sum,
        ResamplingFunction.Min,
        ResamplingFunction.Max,
        ResamplingFunction.First,
        ResamplingFunction.Last,
        ResamplingFunction.Count,
        ResamplingFunction.Coalesce,
    ]:
        result = resample(
            data,
            dt.timedelta(seconds=5),
            method,
            closed=Closed.Left,
            label=Label.Left,
        )
        assert len(result) == 1
        assert result[0][0] == start