Skip to content

Commit

Permalink
Test Postgres more round-trippily
Browse files Browse the repository at this point in the history
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
  • Loading branch information
itowlson committed Oct 21, 2024
1 parent b17f132 commit 940387f
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions tests/test-components/components/outbound-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ impl Component {
ensure!(matches!(rowset.rows[0][0], postgres::DbValue::Str(ref s) if s == "rvarchar"));

let rowset = ensure_ok!(date_time_types(&conn));
ensure!(rowset.rows.iter().all(|r| r.len() == 3));
ensure_matches!(rowset.rows[0][0], postgres::DbValue::Date((y, m, d)) if y == 2525 && m == 12 && d == 25);
ensure!(rowset.rows.iter().all(|r| r.len() == 4));
ensure_matches!(rowset.rows[0][1], postgres::DbValue::Date((y, m, d)) if y == 2525 && m == 12 && d == 25);
ensure_matches!(rowset.rows[0][2], postgres::DbValue::Time((h, m, s, ns)) if h == 4 && m == 5 && s == 6 && ns == 789_000_000);
ensure_matches!(rowset.rows[0][3], postgres::DbValue::Datetime((y, _, _, h, _, _, ns)) if y == 1989 && h == 1 && ns == 0);
ensure_matches!(rowset.rows[1][1], postgres::DbValue::Date((y, m, d)) if y == 2525 && m == 12 && d == 25);
ensure_matches!(rowset.rows[1][2], postgres::DbValue::Time((h, m, s, ns)) if h == 14 && m == 15 && s == 16 && ns == 17);
ensure_matches!(rowset.rows[1][3], postgres::DbValue::Datetime((y, _, _, h, _, _, ns)) if y == 1989 && h == 1 && ns == 4);

let rowset = ensure_ok!(nullable(&conn));
ensure!(rowset.rows.iter().all(|r| r.len() == 1));
Expand Down Expand Up @@ -124,6 +129,7 @@ fn character_types(conn: &postgres::Connection) -> Result<postgres::RowSet, post
fn date_time_types(conn: &postgres::Connection) -> Result<postgres::RowSet, postgres::Error> {
let create_table_sql = r#"
CREATE TEMPORARY TABLE test_date_time_types (
index int2,
rdate date NOT NULL,
rtime time NOT NULL,
rtimestamp timestamp NOT NULL
Expand All @@ -132,24 +138,45 @@ fn date_time_types(conn: &postgres::Connection) -> Result<postgres::RowSet, post

conn.execute(create_table_sql, &[])?;

let insert_sql = r#"
// We will use this to test that we correctly decode "known good"
// Postgres database values. (This validates our decoding logic
// independently of our encoding logic.)
let insert_sql_pg_literals = r#"
INSERT INTO test_date_time_types
(rdate, rtime, rtimestamp)
(index, rdate, rtime, rtimestamp)
VALUES
(date '2525-12-25', time '04:05:06.789', timestamp '1989-11-24 01:02:03');
(1, date '2525-12-25', time '04:05:06.789', timestamp '1989-11-24 01:02:03');
"#;

conn.execute(insert_sql, &[])?;
conn.execute(insert_sql_pg_literals, &[])?;

// We will use this to test that we correctly encode Spin ParameterValue
// objects. (In conjunction with knowing that our decode logic is good,
// this validates our encode logic.)
let insert_sql_spin_parameters = r#"
INSERT INTO test_date_time_types
(index, rdate, rtime, rtimestamp)
VALUES
(2, $1, $2, $3);
"#;

let date_pv = postgres::ParameterValue::Date((2525, 12, 25));
let time_pv = postgres::ParameterValue::Time((14, 15, 16, 17));
let ts_pv = postgres::ParameterValue::Datetime((1989, 11, 24, 1, 2, 3, 4));
conn.execute(insert_sql_spin_parameters, &[date_pv, time_pv, ts_pv])?;

let sql = r#"
SELECT
index,
rdate,
rtime,
rtimestamp
FROM test_date_time_types;
FROM test_date_time_types
ORDER BY index;
"#;

conn.query(sql, &[])

}

fn nullable(conn: &postgres::Connection) -> Result<postgres::RowSet, postgres::Error> {
Expand Down

0 comments on commit 940387f

Please sign in to comment.