-
-
Notifications
You must be signed in to change notification settings - Fork 256
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
feat(pgrx): add chrono feature for easy conversions #1603
Open
t3hmrman
wants to merge
3
commits into
pgcentralfoundation:develop
Choose a base branch
from
t3hmrman:feat(pgrx)=add-chrono-feature
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+469
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,43 @@ | ||
# pgrx-tests | ||
|
||
Test framework for [`pgrx`](https://crates.io/crates/pgrx/). | ||
Test framework for [`pgrx`](https://crates.io/crates/pgrx/). | ||
|
||
Meant to be used as one of your `[dev-dependencies]` when using `pgrx`. | ||
Meant to be used as one of your `[dev-dependencies]` when using `pgrx`. | ||
|
||
## Running tests | ||
|
||
When running tests defined in this crate, you will have to pass along featurs as you would normally pass to `pgrx`. | ||
|
||
For example if you simply want to run a test by name on PG16: | ||
|
||
```console | ||
cargo test --features=pg16 name_of_your_test | ||
``` | ||
|
||
A slightly more complicated example which runs al tests with start with `test_pgrx_chrono_roundtrip` and enables the required features for those tests to run: | ||
|
||
```console | ||
cargo test --features "pg16,proptest,chrono" test_pgrx_chrono_roundtrip | ||
``` | ||
|
||
## FAQ / Common Issues | ||
|
||
### Different `cargo pgrx` version | ||
|
||
In local testing, if the version of `cargo-pgrx` differs from the ones the tests attempt to use, an error results. | ||
|
||
If you run into this issue, make sure to install the *local* version of `cargo-pgrx` rather than the officially released version, temporarily while you run your tests. | ||
|
||
From the `pgrx-tests` folder, you would run: | ||
|
||
```console | ||
cargo install --path ../cargo-pgrx | ||
``` | ||
|
||
### `The specified pg_config binary, ... does not exist` | ||
|
||
If you get this error, and were trying to test against PG16 (as in the example from the [running tests section](#running-tests) above) you should re-initialize pgrx: | ||
|
||
```console | ||
cargo pgrx init --pg16 download | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! Property based testing for the `chrono` features of `cargo-pgrx` | ||
//! | ||
//! The code in here is inspired by the src/tests/proptests.rs | ||
//! | ||
#![cfg(all(feature = "chrono", feature = "proptest"))] | ||
|
||
/** | ||
|
||
Macro for generating tests that attempt to convert a [`chrono`] data type to a [`pgrx`] data type | ||
|
||
This macro takes a pgrx type, the chrono type it converts to, and a generator for the pgrx type. | ||
|
||
For example: | ||
|
||
```rust,ignore | ||
proptest_pgrx_to_chrono_type! { | ||
pgrx::DateTime, chrono::NaiveDateTime, prop::num::i32::ANY.prop_map(Date::saturating_from_raw), | ||
} | ||
``` | ||
|
||
Tests generated by this macro will: | ||
- Create the pgrx type via the given generator | ||
- Convert the pgrx type to a chrono type | ||
- Convert the chrono type into a new pgrx type | ||
- Equality check the first and created pgrx types | ||
|
||
**/ | ||
macro_rules! proptest_pgrx_to_chrono_type { | ||
(pgrx: $pgrx_ty:ty, chrono: $chrono_ty:ty, gen: $generator:expr) => { | ||
paste::paste! { | ||
#[ | ||
doc = concat!( | ||
"pgrx type [", | ||
stringify!($pgrx_ty), | ||
"] should roundtrip to chrono type [", | ||
stringify!($chrono_ty), | ||
"]", | ||
) | ||
] | ||
#[pgrx::pg_test] | ||
fn [<test_pgrx_chrono_roundtrip_ $pgrx_ty:lower _ $chrono_ty:lower>]() -> std::result::Result<(), pgrx::DateTimeConversionError> { | ||
use proptest::prelude::prop; | ||
use proptest::strategy::Strategy; | ||
use crate::proptest::PgTestRunner; | ||
|
||
let mut proptest = PgTestRunner::default(); | ||
let generator = $generator; | ||
proptest.run( | ||
&generator, | ||
|pgrx_date| { | ||
// TODO(fix): this NaiveDate conversion fails | ||
let converted = $chrono_ty::try_from(pgrx_date)?; | ||
// let reverted = $pgrx_ty::try_from(converted)?; | ||
// eprintln!("pgrx_date: {pgrx_date:#?}"); | ||
// eprintln!("reverted: {reverted:#?}"); | ||
//assert_eq!(pgrx_date, reverted, "original value matches roundtripped value"); | ||
assert_eq!(pgrx_date, pgrx_date, "original value matches roundtripped value"); | ||
Ok(()) | ||
}) | ||
.unwrap(); | ||
Ok(()) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
#[cfg(any(test, feature = "pg_test"))] | ||
#[pgrx::pg_schema] | ||
mod tests { | ||
#[allow(unused_imports)] | ||
use crate as pgrx_tests; | ||
#[allow(unused_imports)] | ||
use chrono::NaiveDate; | ||
#[allow(unused_imports)] | ||
use pgrx::Date; | ||
|
||
proptest_pgrx_to_chrono_type! { | ||
pgrx: Date, | ||
chrono: NaiveDate, | ||
gen: prop::num::i32::ANY.prop_map(Date::saturating_from_raw) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//! Tests for the `chrono` features of `cargo-pgrx` | ||
//! | ||
#![cfg(feature = "chrono")] | ||
|
||
#[cfg(any(test, feature = "pg_test"))] | ||
#[pgrx::pg_schema] | ||
mod tests { | ||
#[allow(unused_imports)] | ||
use crate as pgrx_tests; | ||
|
||
use std::result::Result; | ||
|
||
use chrono::{Datelike as _, Timelike as _, Utc}; | ||
|
||
use pgrx::pg_test; | ||
use pgrx::DateTimeConversionError; | ||
|
||
// Utility class for errors | ||
type DtcResult<T> = Result<T, DateTimeConversionError>; | ||
|
||
/// Ensure simple conversion ([`pgrx::Date`] -> [`chrono::NaiveDate`]) works | ||
#[pg_test] | ||
fn chrono_simple_date_conversion() -> DtcResult<()> { | ||
let original = pgrx::Date::new(1970, 1, 1)?; | ||
let d = chrono::NaiveDate::try_from(original)?; | ||
assert_eq!(d.year(), original.year(), "year matches"); | ||
assert_eq!(d.month(), 1, "month matches"); | ||
assert_eq!(d.day(), 1, "day matches"); | ||
let backwards = pgrx::Date::try_from(d)?; | ||
assert_eq!(backwards, original); | ||
Ok(()) | ||
} | ||
|
||
/// Ensure simple conversion ([`pgrx::Time`] -> [`chrono::NaiveTime`]) works | ||
#[pg_test] | ||
fn chrono_simple_time_conversion() -> DtcResult<()> { | ||
let original = pgrx::Time::new(12, 1, 59.0000001)?; | ||
let d = chrono::NaiveTime::try_from(original)?; | ||
assert_eq!(d.hour(), 12, "hours match"); | ||
assert_eq!(d.minute(), 1, "minutes match"); | ||
assert_eq!(d.second(), 59, "seconds match"); | ||
assert_eq!(d.nanosecond(), 0, "nanoseconds are zero (pg only supports microseconds)"); | ||
let backwards = pgrx::Time::try_from(d)?; | ||
assert_eq!(backwards, original); | ||
Ok(()) | ||
} | ||
|
||
/// Ensure simple conversion ([`pgrx::Timestamp`] -> [`chrono::NaiveDateTime`]) works | ||
#[pg_test] | ||
fn chrono_simple_timestamp_conversion() -> DtcResult<()> { | ||
let original = pgrx::Timestamp::new(1970, 1, 1, 1, 1, 1.0)?; | ||
let d = chrono::NaiveDateTime::try_from(original)?; | ||
assert_eq!(d.hour(), 1, "hours match"); | ||
assert_eq!(d.minute(), 1, "minutes match"); | ||
assert_eq!(d.second(), 1, "seconds match"); | ||
assert_eq!(d.nanosecond(), 0, "nanoseconds are zero (pg only supports microseconds)"); | ||
let backwards = pgrx::Timestamp::try_from(d)?; | ||
assert_eq!(backwards, original, "NaiveDateTime -> Timestamp return conversion failed"); | ||
Ok(()) | ||
} | ||
|
||
/// Ensure simple conversion ([`pgrx::TimestampWithTimeZone`] -> [`chrono::DateTime<Utc>`]) works | ||
#[pg_test] | ||
fn chrono_simple_datetime_with_time_zone_conversion() -> DtcResult<()> { | ||
let original = pgrx::TimestampWithTimeZone::with_timezone(1970, 1, 1, 1, 1, 1.0, "utc")?; | ||
let d = chrono::DateTime::<Utc>::try_from(original)?; | ||
assert_eq!(d.hour(), 1, "hours match"); | ||
assert_eq!(d.minute(), 1, "minutes match"); | ||
assert_eq!(d.second(), 1, "seconds match"); | ||
assert_eq!(d.nanosecond(), 0, "nanoseconds are zero (pg only supports microseconds)"); | ||
let backwards = pgrx::TimestampWithTimeZone::try_from(d)?; | ||
assert_eq!(backwards, original); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you, in addition to these simpler tests, set up a proptest like in https://github.com/pgcentralfoundation/pgrx/blob/77a28242fd89372a6fd7cc4ded1fcae9a63319f8/pgrx-tests/src/tests/proptests.rs so that these conversions are fairly invariant?
It doesn't have to roundtrip stuff through SPI, since that's not really what we'd be testing, so it doesn't need to use the exact pattern I used there. It can do something simpler with just handling https://github.com/pgcentralfoundation/pgrx/blob/77a28242fd89372a6fd7cc4ded1fcae9a63319f8/pgrx-tests/src/proptest.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely -- yeah I didn't think the roundtripping was necessary, but a prop test would definitely make a ton of sense here.