Hotfix/test audiodataset#275
Conversation
| if begin > end: | ||
| msg = (f"`begin` ({begin}) must be smaller than `end`({end})") | ||
| raise ValueError(msg) |
There was a problem hiding this comment.
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)
Gautzilla
left a comment
There was a problem hiding this comment.
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)?
| self._begin = begin | ||
| self._end = end |
There was a problem hiding this comment.
Can't we directly use the setters to avoid testing the values twice?
|
|
||
| @begin.setter | ||
| def begin(self, value: Timestamp) -> None: | ||
| if hasattr(self, "_end") and value > self._end: |
|
|
||
| @end.setter | ||
| def end(self, value: Timestamp) -> None: | ||
| if hasattr(self, "_begin") and value < self._begin: |
| 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) |
| @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] | ||
|
|
There was a problem hiding this comment.
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) == eThere was a problem hiding this comment.
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 🥸
There was a problem hiding this comment.
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
|
I opened this PR to add some tests to the Event class |
add event init error tests




added a condition in
BaseDatasetclass and a test to raise an error ifbegin>end