Skip to content

Commit

Permalink
tests: add timeuuid runtime ordering test
Browse files Browse the repository at this point in the history
  • Loading branch information
muzarski committed Jan 19, 2024
1 parent 8656d6f commit 5ebd7bc
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions scylla/src/transport/cql_types_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::transport::session::Session;
use crate::utils::test_utils::unique_keyspace_name;
use bigdecimal::BigDecimal;
use num_bigint::BigInt;
use rand::seq::SliceRandom;
use scylla_cql::frame::value::CqlTimeuuid;
use scylla_cql::types::serialize::value::SerializeCql;
use scylla_macros::SerializeCql;
Expand Down Expand Up @@ -1149,6 +1150,76 @@ async fn test_timeuuid() {
}
}

#[tokio::test]
async fn test_timeuuid_ordering() {
let session: Session = create_new_session_builder().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 tab (p int, t timeuuid, PRIMARY KEY (p, t))",
(),
)
.await
.unwrap();

// Timeuuid values, sorted in the same order as Scylla/Cassandra sorts them.
let sorted_timeuuid_vals: Vec<CqlTimeuuid> = vec![
CqlTimeuuid::from_str("00000000-0000-1000-8080-808080808080").unwrap(),
CqlTimeuuid::from_str("00000000-0000-1000-ffff-ffffffffffff").unwrap(),
CqlTimeuuid::from_str("00000000-0000-1000-0000-000000000000").unwrap(),
CqlTimeuuid::from_str("fed35080-0efb-11ee-a1ca-00006490e9a4").unwrap(),
CqlTimeuuid::from_str("00000257-0efc-11ee-9547-00006490e9a6").unwrap(),
CqlTimeuuid::from_str("ffffffff-ffff-1fff-ffff-ffffffffffef").unwrap(),
CqlTimeuuid::from_str("ffffffff-ffff-1fff-ffff-ffffffffffff").unwrap(),
CqlTimeuuid::from_str("ffffffff-ffff-1fff-0000-000000000000").unwrap(),
CqlTimeuuid::from_str("ffffffff-ffff-1fff-7f7f-7f7f7f7f7f7f").unwrap(),
];

let mut shuffled_timeuuid_vals = sorted_timeuuid_vals.clone();
let mut rng = rand::thread_rng();
shuffled_timeuuid_vals.shuffle(&mut rng);

// Verify that Scylla really sorts timeuuids as defined in sorted_timeuuid_vals
let prepared = session
.prepare("INSERT INTO tab (p, t) VALUES (0, ?)")
.await
.unwrap();
for timeuuid_val in &shuffled_timeuuid_vals {
session.execute(&prepared, (timeuuid_val,)).await.unwrap();
}

let scylla_order_timeuuids: Vec<CqlTimeuuid> = session
.query("SELECT t FROM tab WHERE p = 0", ())
.await
.unwrap()
.rows_typed::<(CqlTimeuuid,)>()
.unwrap()
.map(|r| r.unwrap().0)
.collect();

assert_eq!(sorted_timeuuid_vals, scylla_order_timeuuids);

// Test if rust timeuuid values are sorted in the same way as in Scylla
let mut rust_sorted_timeuuids: Vec<CqlTimeuuid> = shuffled_timeuuid_vals.clone();
rust_sorted_timeuuids.sort();

assert_eq!(sorted_timeuuid_vals, rust_sorted_timeuuids);
}

#[tokio::test]
async fn test_inet() {
let session: Session = init_test("inet_tests", "inet").await;
Expand Down

0 comments on commit 5ebd7bc

Please sign in to comment.