Skip to content

Hotfix/test audiodataset#275

Merged
Gautzilla merged 14 commits into
Project-OSmOSE:mainfrom
mathieudpnt:hotfix/test-audiodataset
Sep 15, 2025
Merged

Hotfix/test audiodataset#275
Gautzilla merged 14 commits into
Project-OSmOSE:mainfrom
mathieudpnt:hotfix/test-audiodataset

Conversation

@mathieudpnt
Copy link
Copy Markdown
Contributor

added a condition in BaseDataset class and a test to raise an error if begin > end

Comment thread src/osekit/core_api/base_dataset.py Outdated
Comment on lines +302 to +304
if begin > end:
msg = (f"`begin` ({begin}) must be smaller than `end`({end})")
raise ValueError(msg)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be if begin >= end, as the error message specifies must be smaller than? (And that a 0s-long dataset wouldn't make much sense)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ron-swanson-done

@Gautzilla Gautzilla self-requested a review September 11, 2025 14:54
Copy link
Copy Markdown
Contributor

@Gautzilla Gautzilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'm wondering if this logic shouldn't be put in the Event class:

At least for the begin > end part (I guess an Event could be instantaneous)?

@Gautzilla Gautzilla requested review from Gautzilla and removed request for Gautzilla September 11, 2025 14:55
Comment thread src/osekit/core_api/event.py Outdated
Comment on lines +33 to +34
self._begin = begin
self._end = end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we directly use the setters to avoid testing the values twice?

Comment thread src/osekit/core_api/event.py Outdated

@begin.setter
def begin(self, value: Timestamp) -> None:
if hasattr(self, "_end") and value > self._end:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not >=?

Comment thread src/osekit/core_api/event.py Outdated

@end.setter
def end(self, value: Timestamp) -> None:
if hasattr(self, "_begin") and value < self._begin:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not <=?

Comment thread tests/test_event.py Outdated
Comment on lines +413 to +418
for i, (attr, value) in enumerate(updates):
if error_at is not None and i == error_at:
with pytest.raises(ValueError, match="`end`.*must be greater than `begin`.*"):
setattr(cool_event, attr, value)
else:
setattr(cool_event, attr, value)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mathieudpnt
Copy link
Copy Markdown
Contributor Author

atg-studiocapa

@Gautzilla
Copy link
Copy Markdown
Contributor

YesGIF

Comment thread tests/test_event.py
Comment on lines +333 to +402
@pytest.mark.parametrize(
("initial", "updates", "expected_values", "expectation"),
[
pytest.param(
[
("begin", Timestamp("2024-01-01 00:00:00")),
("end", Timestamp("2024-01-02 00:00:00")),
],
("begin", Timestamp("2024-01-01 12:00:00")),
(Timestamp("2024-01-01 12:00:00"), Timestamp("2024-01-02 00:00:00")),
nullcontext(),
id="valid begin",
),
pytest.param(
[
("begin", Timestamp("2024-01-01 00:00:00")),
("end", Timestamp("2024-01-02 00:00:00")),
],
("end", Timestamp("2024-01-02 12:00:00")),
(Timestamp("2024-01-01 00:00:00"), Timestamp("2024-01-02 12:00:00")),
nullcontext(),
id="valid end",
),
pytest.param(
[
("begin", Timestamp("2024-01-01 00:00:00")),
("end", Timestamp("2024-01-02 00:00:00")),
],
("begin", Timestamp("2024-01-03 00:00:00")),
(Timestamp("2024-01-01 00:00:00"), Timestamp("2024-01-02 00:00:00")),
pytest.raises(ValueError, match="`end`.*must be greater than `begin`.*"),
id="invalid begin > end",
),
pytest.param(
[
("begin", Timestamp("2024-01-01 00:00:00")),
("end", Timestamp("2024-01-02 00:00:00")),
],
("end", Timestamp("2023-12-31 23:59:59")),
(Timestamp("2024-01-01 00:00:00"), Timestamp("2024-01-02 00:00:00")),
pytest.raises(ValueError, match="`end`.*must be greater than `begin`.*"),
id="invalid end < begin",
),
pytest.param(
[
("begin", Timestamp("2024-01-01 00:00:00")),
("end", Timestamp("2024-01-01 01:00:00")),
],
("begin", Timestamp("2024-01-01 01:00:00")),
(Timestamp("2024-01-01 00:00:00"), Timestamp("2024-01-01 01:00:00")),
pytest.raises(ValueError, match="`end`.*must be greater than `begin`.*"),
id="begin and end equals",
),
],
)
def test_event_begin_end_updates(
initial: list[tuple[str, Timestamp]],
updates: tuple[str, Timestamp],
expected_values: tuple[Timestamp, Timestamp],
expectation: AbstractContextManager[BaseException | None],
) -> None:
initial_dict = dict(initial)
cool_event = Event(begin=initial_dict["begin"], end=initial_dict["end"])

with expectation as e:
assert setattr(cool_event, updates[0], updates[1]) == e

assert cool_event.begin == expected_values[0]
assert cool_event.end == expected_values[1]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test looks a bit clumsy, with every parameter being passed independently and the Events being instantiated within the test function.
Moreover, you have both expected_values and the expectation error parameter: you can merge both in a single parameter using pytest conditional raising.

Example of a rework that looks a bit more straightforward to me:

@pytest.mark.parametrize(
    ("event", "updated_begin", "updated_end", "expected"),
    [
        pytest.param(
            Event(
                begin=Timestamp("2024-01-01 00:00:00"),
                end=Timestamp("2024-01-02 00:00:00"),
            ),
            Timestamp("2024-01-01 12:00:00"),
            None,
            nullcontext(
                Event(
                    begin=Timestamp("2024-01-01 12:00:00"),
                    end=Timestamp("2024-01-02 00:00:00"),
                )
            ),
            id="valid begin",
        ),
        pytest.param(
            Event(
                begin=Timestamp("2024-01-01 00:00:00"),
                end=Timestamp("2024-01-02 00:00:00"),
            ),
            None,
            Timestamp("2024-01-02 12:00:00"),
            nullcontext(
                Event(
                    begin=Timestamp("2024-01-01 00:00:00"),
                    end=Timestamp("2024-01-02 12:00:00"),
                )
            ),
            id="valid end",
        ),
        pytest.param(
            Event(
                begin=Timestamp("2024-01-01 00:00:00"),
                end=Timestamp("2024-01-02 00:00:00"),
            ),
            Timestamp("2024-01-03 00:00:00"),
            None,
            pytest.raises(ValueError, match="`end`.*must be greater than `begin`.*"),
            id="invalid begin > end",
        ),
        pytest.param(
            Event(
                begin=Timestamp("2024-01-01 00:00:00"),
                end=Timestamp("2024-01-02 00:00:00"),
            ),
            None,
            Timestamp("2023-12-31 23:59:59"),
            pytest.raises(ValueError, match="`end`.*must be greater than `begin`.*"),
            id="invalid end < begin",
        ),
        pytest.param(
            Event(
                begin=Timestamp("2024-01-01 00:00:00"),
                end=Timestamp("2024-01-01 01:00:00"),
            ),
            Timestamp("2024-01-01 01:00:00"),
            None,
            pytest.raises(ValueError, match="`end`.*must be greater than `begin`.*"),
            id="begin and end equals",
        ),
    ],
)
def test_event_begin_end_updates(
    event: Event,
    updated_begin: Timestamp | None,
    updated_end: Timestamp | None,
    expected: Event,
) -> None:
    def update_event(
        event: Event, begin: Timestamp | None, end: Timestamp | None
    ) -> Event:
        if begin:
            event.begin = begin
        if end:
            event.end = end
        return event

    with expected as e:
        assert update_event(event, updated_begin, updated_end) == e

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you didn't write a test function that tests the validity of the event within the constructor, it might be a good idea to do so 🥸

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

willem-dafoe

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uploading TheOfficeDwightSchruteGIF.gif…

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you didn't write a test function that tests the validity of the event within the constructor, it might be a good idea to do so 🥸

I'll do it if you want, it'll allow me to check on my GH logging

@Gautzilla
Copy link
Copy Markdown
Contributor

I opened this PR to add some tests to the Event class

@Gautzilla Gautzilla merged commit 1977e7a into Project-OSmOSE:main Sep 15, 2025
1 check passed
@mathieudpnt mathieudpnt deleted the hotfix/test-audiodataset branch September 15, 2025 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants