Added tests, Fixed microseconds in ToSql implementations #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I took a stab at making some tests as per sfackler#1164 (comment).
Tests:
Added tests in /tokio-postgres/tests/test/types/jiff_01.rs
for civil::Date, civil::DateTime, civil::Time, Timestamp
Fixes:
Changed Span::new().microseconds(x) to Span::new().try_microseconds(x) in the FromSql implementations. The former panics, the latter returns a Result.
There was a misuse of Span when trying to get the number of microseconds since the epoch in the ToSql implementations.
For example, the following code:
returns the value of the field microseconds in the span, not the total number of microseconds if you convert all of the days, hours, minutes, etc to microseconds and sum them.
What we want is:
Aside:
——
Fwiw @BurntSushi, it’s clear to me now and the docs were helpful once I knew to look, but initially I found this confusing.
This is a common operation so might I suggest including an example of getting the total seconds of a Span, or similar, in the Usage section of the Readme.
I would also have a preference for adding methods to Span for something like .total_microseconds(), .total_seconds(), etc.
Maybe I’m showing my ignorance of standards/common expectations here, but at a glance I might also opt for .set_microseconds(x) over .microseconds(x) just to disambiguate it. If one were to not be prepended, I would actually have defaulted to having the getter be .microseconds() rather than the setter. Though I do see what you’re doing with .try_microseconds(). I personally would probably never use the panic version of the methods but can understand others preferring it. I wonder if people might naively not realize the non-try version panics.
As a final commentary (sorry), I would personally rather have the fields be public and get/set them directly. I see what you’re doing with the min and max ranges (which is nice!) so perhaps that just wouldn’t be clean enough to do. But generally I’d rather do span.microseconds = x, span.microseconds = x.into(), or even span.microseconds = SpanMicroseconds::try_new(x)?, and then be able to get with a simple span.microseconds. Probably my weakest suggestion here given I have a bias against getters and setters unless absolutely necessary. Having set_, get_, and total_ would probably be sufficient to make things very clear for the user.
Separately, a method or other mechanism for checking what type of Error is returned would be helpful. One of my tests uses a very dirty method of
(I’m enjoying the library so far and appreciate the evident thought and work you put into this. Just my 2c for whatever it’s worth while the library is still being developed).
——