Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor test_select_by_time function #82

Open
DaniBodor opened this issue Jul 20, 2023 · 0 comments
Open

Refactor test_select_by_time function #82

DaniBodor opened this issue Jul 20, 2023 · 0 comments
Assignees
Labels
refactoring Improve clarity or readability of code

Comments

@DaniBodor
Copy link
Member

DaniBodor commented Jul 20, 2023

Currently just one long code block. It feels like this could be made more modular in a nice loop structure, but I couldn't think of the right way of doing it (and ChatGPT didnt either).

def test_select_by_time(
draeger_data2: DraegerSequence,
):
# TODO (#82): this function is kinda ugly. Would be nice to refactor it
# but I am struggling to think of a logical way to loop through.
data = draeger_data2
t22 = 55825.268
t52 = 55826.768
ms = 0.001
# test illegal
with pytest.warns(UserWarning):
_ = data.select_by_time()
with pytest.warns(UserWarning):
_ = data.select_by_time(None, None)
with pytest.warns(UserWarning):
_ = data.select_by_time(None)
with pytest.warns(UserWarning):
_ = data.select_by_time(end = None)
# test start only
start_slices = [
# (time, expectation if inclusive=True, expectation if inclusive=False)
(t22, 22, 23),
(t22 - ms, 22, 22),
(t22 + ms, 23, 23),
]
for start_slicing in start_slices:
sliced = data.select_by_time(
start = start_slicing[0], start_inclusive=True)
assert len(sliced) == len(data) - start_slicing[1]
sliced = data.select_by_time(start = start_slicing[0], start_inclusive=False)
assert len(sliced) == len(data) - start_slicing[2]
# test end only
end_slices = [
# (time, expectation if inclusive=True, expectation if inclusive=False)
(t52, 52, 51),
(t52 - ms, 51, 51),
(t52 + ms, 52, 52),
]
for end_slicing in end_slices:
sliced = data.select_by_time(end = end_slicing[0], end_inclusive=True)
assert len(sliced) == end_slicing[1]
sliced = data.select_by_time(end = end_slicing[0], end_inclusive=False)
assert len(sliced) == end_slicing[2]
# test start and end
for start_slicing in start_slices:
for end_slicing in end_slices:
# True/True
sliced = data.select_by_time(start = start_slicing[0],
end = end_slicing[0],
start_inclusive=True,
end_inclusive=True)
assert len(sliced) == end_slicing[1]-start_slicing[1]
# False/True
sliced = data.select_by_time(start = start_slicing[0],
end = end_slicing[0],
start_inclusive=False,
end_inclusive=True)
assert len(sliced) == end_slicing[1]-start_slicing[2]
# True/False
sliced = data.select_by_time(start = start_slicing[0],
end = end_slicing[0],
start_inclusive=True,
end_inclusive=False)
assert len(sliced) == end_slicing[2]-start_slicing[1]
# False/False
sliced = data.select_by_time(start = start_slicing[0],
end = end_slicing[0],
start_inclusive=False,
end_inclusive=False)
assert len(sliced) == end_slicing[2]-start_slicing[2]

@DaniBodor DaniBodor added the refactoring Improve clarity or readability of code label Sep 19, 2023
@psomhorst psomhorst added this to the Next release: 1.5.0 milestone Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Improve clarity or readability of code
Projects
None yet
Development

No branches or pull requests

2 participants