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

[feature] an assertion that compares numbers within a delta #92

Closed
masukomi opened this issue Dec 17, 2022 · 2 comments
Closed

[feature] an assertion that compares numbers within a delta #92

masukomi opened this issue Dec 17, 2022 · 2 comments

Comments

@masukomi
Copy link

Use case:

I'm testing a timestamp that's been inserted in the db, but don't have a good way to "freeze time". So, I need to insert the record, get the current epoch time (presumably from date) then compare the two, but the second may have changed between the creation and the acquisition of the new date.

So, i want the ability to assert within a delta.

There are, one presumes, many cases where you know a good approximation of a correct value but not the exact value. Just like the need for regexp when you don't know the exact string that will come back, but do know its form.

Proposal

assert_within_delta <expected num> <actual num> <max delta>

Given an expectation of 5 and a delta of 2 this would match 3, 4, 5, 6, and 7

There should probably be a positive and negative variant.

assert_within_positive_delta <expected_num> <actual_num> <max_delta>

Given an expectation of 5 and a delta of 2 this would match 5, 6, and 7.

assert_within_negative_delta <expected_num> <actual_num> <max_delta>

Given an expectation of 5 and a delta of 2 this would match 3, 4, and 5.

pgrange added a commit that referenced this issue Jan 2, 2023
```
assert_within_delta <expected num> <actual num> <max delta>
```

Given an expectation of 5 and a delta of 2 this would match 3, 4, 5, 6, and 7
@pgrange
Copy link
Owner

pgrange commented Jan 2, 2023

Hello @masukomi and happy new year. Also, sorry for the delay.

Thank you for using bash_unit and for this nicely shaped proposal.

Adding an assertion function in bash_unit itself is a tough one as we will have to maintain it forever then. But why not, your remark makes sense, it's quite close to asserting against regexp.

I've implemented your proposed assert_within_delta in PR #93 and would appreciate your feedback. In particular, you can look at the doc here.

I guess we can keep in mind assert_within_positive_delta and assert_within_negative_delta but postpone there implementation for later if somebody else express the need for them. Is is my understanding that you would only need assert_within_delta for your current use case. Correct?

@masukomi
Copy link
Author

masukomi commented Jan 2, 2023

But why not, your remark makes sense, it's quite close to asserting against regexp.

yay 🎉 :D

Is is my understanding that you would only need assert_within_delta for your current use case. Correct?

correct. i'm doing time related tests so it'd always be in the same direction.

I think the positive and negative are "nice-to-haves" rather than truly necessary for most folks.

@pgrange pgrange closed this as completed in 4411bc6 Mar 3, 2023
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

No branches or pull requests

2 participants