Skip to content

Commit db6bee0

Browse files
piodulwprzytula
andcommitted
caching_session: modernize tests
Adjusts the CachingSession tests to use the new deserialization interface. Co-authored-by: Wojciech Przytuła <[email protected]>
1 parent 001b5bb commit db6bee0

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

scylla/src/transport/caching_session.rs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,22 @@ where
314314
mod tests {
315315
use crate::query::Query;
316316
use crate::statement::PagingState;
317-
use crate::test_utils::{
318-
create_new_session_builder, scylla_supports_tablets_legacy, setup_tracing,
319-
};
317+
use crate::test_utils::{create_new_session_builder, scylla_supports_tablets, setup_tracing};
320318
use crate::transport::partitioner::PartitionerName;
319+
use crate::transport::session::Session;
321320
use crate::utils::test_utils::unique_keyspace_name;
322321
use crate::{
323322
batch::{Batch, BatchStatement},
324323
prepared_statement::PreparedStatement,
325-
LegacyCachingSession, LegacySession,
324+
CachingSession,
326325
};
327326
use futures::TryStreamExt;
327+
use scylla_cql::frame::response::result::Row;
328328
use std::collections::BTreeSet;
329329

330-
async fn new_for_test(with_tablet_support: bool) -> LegacySession {
330+
async fn new_for_test(with_tablet_support: bool) -> Session {
331331
let session = create_new_session_builder()
332-
.build_legacy()
332+
.build()
333333
.await
334334
.expect("Could not create session");
335335
let ks = unique_keyspace_name();
@@ -338,7 +338,7 @@ mod tests {
338338
"CREATE KEYSPACE IF NOT EXISTS {ks}
339339
WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}"
340340
);
341-
if !with_tablet_support && scylla_supports_tablets_legacy(&session).await {
341+
if !with_tablet_support && scylla_supports_tablets(&session).await {
342342
create_ks += " AND TABLETS = {'enabled': false}";
343343
}
344344

@@ -366,8 +366,8 @@ mod tests {
366366
session
367367
}
368368

369-
async fn create_caching_session() -> LegacyCachingSession {
370-
let session = LegacyCachingSession::from(new_for_test(true).await, 2);
369+
async fn create_caching_session() -> CachingSession {
370+
let session = CachingSession::from(new_for_test(true).await, 2);
371371

372372
// Add a row, this makes it easier to check if the caching works combined with the regular execute fn on Session
373373
session
@@ -428,17 +428,20 @@ mod tests {
428428
.execute_unpaged("select * from test_table", &[])
429429
.await
430430
.unwrap();
431+
let result_rows = result.into_rows_result().unwrap().unwrap();
431432

432433
assert_eq!(1, session.cache.len());
433-
assert_eq!(1, result.rows_num().unwrap());
434+
assert_eq!(1, result_rows.rows_num());
434435

435436
let result = session
436437
.execute_unpaged("select * from test_table", &[])
437438
.await
438439
.unwrap();
439440

441+
let result_rows = result.into_rows_result().unwrap().unwrap();
442+
440443
assert_eq!(1, session.cache.len());
441-
assert_eq!(1, result.rows_num().unwrap());
444+
assert_eq!(1, result_rows.rows_num());
442445
}
443446

444447
/// Checks that caching works with execute_iter
@@ -452,7 +455,10 @@ mod tests {
452455
let iter = session
453456
.execute_iter("select * from test_table", &[])
454457
.await
455-
.unwrap();
458+
.unwrap()
459+
.rows_stream::<Row>()
460+
.unwrap()
461+
.into_stream();
456462

457463
let rows = iter.try_collect::<Vec<_>>().await.unwrap().len();
458464

@@ -474,18 +480,21 @@ mod tests {
474480
.unwrap();
475481

476482
assert_eq!(1, session.cache.len());
477-
assert_eq!(1, result.rows_num().unwrap());
483+
assert_eq!(1, result.into_rows_result().unwrap().unwrap().rows_num());
478484
}
479485

480486
async fn assert_test_batch_table_rows_contain(
481-
sess: &LegacyCachingSession,
487+
sess: &CachingSession,
482488
expected_rows: &[(i32, i32)],
483489
) {
484490
let selected_rows: BTreeSet<(i32, i32)> = sess
485491
.execute_unpaged("SELECT a, b FROM test_batch_table", ())
486492
.await
487493
.unwrap()
488-
.rows_typed::<(i32, i32)>()
494+
.into_rows_result()
495+
.unwrap()
496+
.unwrap()
497+
.rows::<(i32, i32)>()
489498
.unwrap()
490499
.map(|r| r.unwrap())
491500
.collect();
@@ -524,18 +533,18 @@ mod tests {
524533
}
525534
}
526535

527-
let _session: LegacyCachingSession<std::collections::hash_map::RandomState> =
528-
LegacyCachingSession::from(new_for_test(true).await, 2);
529-
let _session: LegacyCachingSession<CustomBuildHasher> =
530-
LegacyCachingSession::from(new_for_test(true).await, 2);
531-
let _session: LegacyCachingSession<CustomBuildHasher> =
532-
LegacyCachingSession::with_hasher(new_for_test(true).await, 2, Default::default());
536+
let _session: CachingSession<std::collections::hash_map::RandomState> =
537+
CachingSession::from(new_for_test(true).await, 2);
538+
let _session: CachingSession<CustomBuildHasher> =
539+
CachingSession::from(new_for_test(true).await, 2);
540+
let _session: CachingSession<CustomBuildHasher> =
541+
CachingSession::with_hasher(new_for_test(true).await, 2, Default::default());
533542
}
534543

535544
#[tokio::test]
536545
async fn test_batch() {
537546
setup_tracing();
538-
let session: LegacyCachingSession = create_caching_session().await;
547+
let session: CachingSession = create_caching_session().await;
539548

540549
session
541550
.execute_unpaged(
@@ -658,8 +667,7 @@ mod tests {
658667
#[tokio::test]
659668
async fn test_parameters_caching() {
660669
setup_tracing();
661-
let session: LegacyCachingSession =
662-
LegacyCachingSession::from(new_for_test(true).await, 100);
670+
let session: CachingSession = CachingSession::from(new_for_test(true).await, 100);
663671

664672
session
665673
.execute_unpaged("CREATE TABLE tbl (a int PRIMARY KEY, b int)", ())
@@ -695,7 +703,11 @@ mod tests {
695703
.execute_unpaged("SELECT b, WRITETIME(b) FROM tbl", ())
696704
.await
697705
.unwrap()
698-
.rows_typed_or_empty::<(i32, i64)>()
706+
.into_rows_result()
707+
.unwrap()
708+
.unwrap()
709+
.rows::<(i32, i64)>()
710+
.unwrap()
699711
.collect::<Result<Vec<_>, _>>()
700712
.unwrap();
701713

@@ -712,8 +724,7 @@ mod tests {
712724
}
713725

714726
// This test uses CDC which is not yet compatible with Scylla's tablets.
715-
let session: LegacyCachingSession =
716-
LegacyCachingSession::from(new_for_test(false).await, 100);
727+
let session: CachingSession = CachingSession::from(new_for_test(false).await, 100);
717728

718729
session
719730
.execute_unpaged(

scylla/src/utils/test_utils.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use scylla_cql::frame::response::result::Row;
2+
13
#[cfg(test)]
24
use crate::transport::session_builder::{GenericSessionBuilder, SessionBuilderKind};
3-
use crate::LegacySession;
5+
use crate::{LegacySession, Session};
46
#[cfg(test)]
57
use std::{num::NonZeroU32, time::Duration};
68
use std::{
@@ -106,6 +108,23 @@ pub async fn scylla_supports_tablets_legacy(session: &LegacySession) -> bool {
106108
result.single_row().is_ok()
107109
}
108110

111+
pub async fn scylla_supports_tablets(session: &Session) -> bool {
112+
let result = session
113+
.query_unpaged(
114+
"select column_name from system_schema.columns where
115+
keyspace_name = 'system_schema'
116+
and table_name = 'scylla_keyspaces'
117+
and column_name = 'initial_tablets'",
118+
&[],
119+
)
120+
.await
121+
.unwrap()
122+
.into_rows_result()
123+
.unwrap();
124+
125+
result.map_or(false, |rows_result| rows_result.single_row::<Row>().is_ok())
126+
}
127+
109128
#[cfg(test)]
110129
pub(crate) fn setup_tracing() {
111130
let _ = tracing_subscriber::fmt::fmt()

0 commit comments

Comments
 (0)