From aa2ff61d466caa31e8812c1d9150b02dd5b003c5 Mon Sep 17 00:00:00 2001 From: muzarski Date: Thu, 1 Feb 2024 17:37:55 +0100 Subject: [PATCH] tests: add test case for result_metadata update --- scylla/src/transport/session_test.rs | 81 ++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/scylla/src/transport/session_test.rs b/scylla/src/transport/session_test.rs index b6c9c20ba4..73623ccb4d 100644 --- a/scylla/src/transport/session_test.rs +++ b/scylla/src/transport/session_test.rs @@ -1968,6 +1968,87 @@ async fn test_unprepared_reprepare_in_execute() { assert_eq!(all_rows, vec![(1, 2, 3), (1, 3, 2)]); } +#[tokio::test] +async fn test_use_prepared_result_metadata() { + let _ = tracing_subscriber::fmt::try_init(); + + let session = create_new_session_builder() + .skip_prepared_statement_result_metadata(true) + .build() + .await + .unwrap(); + let ks = unique_keyspace_name(); + + session.query(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}", ks), &[]).await.unwrap(); + session.use_keyspace(ks, false).await.unwrap(); + + session + .query( + "CREATE TABLE IF NOT EXISTS tab (a int PRIMARY KEY, b int, c int)", + &[], + ) + .await + .unwrap(); + + let insert_a_b_c = session + .prepare("INSERT INTO tab (a, b, c) VALUES (?, ?, ?)") + .await + .unwrap(); + let select_star = session.prepare("SELECT * FROM tab").await.unwrap(); + + let select_col_types = select_star + .get_result_metadata() + .col_specs + .iter() + .map(|c| c.typ.clone()) + .collect::>(); + + assert_eq!( + vec![ColumnType::Int, ColumnType::Int, ColumnType::Int], + select_col_types + ); + + session.execute(&insert_a_b_c, (1, 2, 3)).await.unwrap(); + session.execute(&insert_a_b_c, (3, 4, -1)).await.unwrap(); + + session + .query("ALTER TABLE tab ALTER c TYPE blob", &[]) + .await + .unwrap(); + + // The type of `c` should change to blob after reprepare. + let all_rows: Vec<(i32, i32, Vec)> = session + .execute(&select_star, &[]) + .await + .unwrap() + .rows_typed::<(i32, i32, Vec)>() + .unwrap() + .map(|r| r.unwrap()) + .collect(); + + // Third column deserialized as raw bytes. + assert_eq!( + vec![ + (1, 2, vec![0x00, 0x00, 0x00, 0x03]), + (3, 4, vec![0xff, 0xff, 0xff, 0xff]) + ], + all_rows + ); + + // Verify that result_metadata of the SELECT query got updated. + let select_col_types = select_star + .get_result_metadata() + .col_specs + .iter() + .map(|c| c.typ.clone()) + .collect::>(); + + assert_eq!( + vec![ColumnType::Int, ColumnType::Int, ColumnType::Blob], + select_col_types + ); +} + #[tokio::test] async fn test_unusual_valuelists() { let _ = tracing_subscriber::fmt::try_init();