Skip to content

Commit c223d1a

Browse files
Move test_deserialize_empty_collections to cql_types.rs
1 parent e2a08ab commit c223d1a

File tree

2 files changed

+68
-67
lines changed

2 files changed

+68
-67
lines changed

scylla/tests/integration/cql_types.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use scylla::serialize::value::SerializeValue;
44
use scylla::value::{Counter, CqlDate, CqlTime, CqlTimestamp, CqlTimeuuid, CqlValue, CqlVarint};
55
use scylla::{DeserializeValue, SerializeValue};
66
use std::cmp::PartialEq;
7+
use std::collections::{HashMap, HashSet};
78
use std::fmt::Debug;
89
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
910
use std::str::FromStr;
@@ -1863,3 +1864,67 @@ async fn test_udt_with_missing_field() {
18631864
)
18641865
.await;
18651866
}
1867+
1868+
/// ScyllaDB does not distinguish empty collections from nulls. That is, INSERTing an empty collection
1869+
/// is equivalent to nullifying the corresponding column.
1870+
/// As pointed out in [#1001](https://github.com/scylladb/scylla-rust-driver/issues/1001), it's a nice
1871+
/// QOL feature to be able to deserialize empty CQL collections to empty Rust collections instead of
1872+
/// `None::<RustCollection>`. This test checks that.
1873+
#[tokio::test]
1874+
async fn test_deserialize_empty_collections() {
1875+
// Setup session.
1876+
let ks = unique_keyspace_name();
1877+
let session = create_new_session_builder().build().await.unwrap();
1878+
session.ddl(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}", ks)).await.unwrap();
1879+
session.use_keyspace(&ks, true).await.unwrap();
1880+
1881+
async fn deserialize_empty_collection<
1882+
Collection: Default + DeserializeOwnedValue + SerializeValue,
1883+
>(
1884+
session: &Session,
1885+
collection_name: &str,
1886+
collection_type_params: &str,
1887+
) -> Collection {
1888+
// Create a table for the given collection type.
1889+
let table_name = "test_empty_".to_owned() + collection_name;
1890+
let query = format!(
1891+
"CREATE TABLE {} (n int primary key, c {}<{}>)",
1892+
table_name, collection_name, collection_type_params
1893+
);
1894+
session.ddl(query).await.unwrap();
1895+
1896+
// Populate the table with an empty collection, effectively inserting null as the collection.
1897+
session
1898+
.query_unpaged(
1899+
format!("INSERT INTO {} (n, c) VALUES (?, ?)", table_name,),
1900+
(0, Collection::default()),
1901+
)
1902+
.await
1903+
.unwrap();
1904+
1905+
let query_rows_result = session
1906+
.query_unpaged(format!("SELECT c FROM {}", table_name), ())
1907+
.await
1908+
.unwrap()
1909+
.into_rows_result()
1910+
.unwrap();
1911+
let (collection,) = query_rows_result.first_row::<(Collection,)>().unwrap();
1912+
1913+
// Drop the table
1914+
collection
1915+
}
1916+
1917+
let list = deserialize_empty_collection::<Vec<i32>>(&session, "list", "int").await;
1918+
assert!(list.is_empty());
1919+
1920+
let set = deserialize_empty_collection::<HashSet<i64>>(&session, "set", "bigint").await;
1921+
assert!(set.is_empty());
1922+
1923+
let map = deserialize_empty_collection::<HashMap<bool, CqlVarint>>(
1924+
&session,
1925+
"map",
1926+
"boolean, varint",
1927+
)
1928+
.await;
1929+
assert!(map.is_empty());
1930+
}

scylla/tests/integration/session.rs

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::utils::DeserializeOwnedValue;
21
use crate::utils::{
32
create_new_session_builder, scylla_supports_tablets, setup_tracing, supports_feature,
43
unique_keyspace_name, PerformDDL,
@@ -27,9 +26,10 @@ use scylla::statement::Consistency;
2726
use scylla_cql::frame::request::query::{PagingState, PagingStateResponse};
2827
use scylla_cql::serialize::row::{SerializeRow, SerializedValues};
2928
use scylla_cql::serialize::value::SerializeValue;
30-
use scylla_cql::value::{CqlVarint, Row};
29+
use scylla_cql::value::Row;
30+
use std::collections::BTreeSet;
3131
use std::collections::{BTreeMap, HashMap};
32-
use std::collections::{BTreeSet, HashSet};
32+
use std::collections::HashSet;
3333
use std::sync::atomic::{AtomicBool, Ordering};
3434
use std::sync::{Arc, Mutex};
3535
use tokio::net::TcpListener;
@@ -2854,70 +2854,6 @@ async fn test_manual_primary_key_computation() {
28542854
}
28552855
}
28562856

2857-
/// ScyllaDB does not distinguish empty collections from nulls. That is, INSERTing an empty collection
2858-
/// is equivalent to nullifying the corresponding column.
2859-
/// As pointed out in [#1001](https://github.com/scylladb/scylla-rust-driver/issues/1001), it's a nice
2860-
/// QOL feature to be able to deserialize empty CQL collections to empty Rust collections instead of
2861-
/// `None::<RustCollection>`. This test checks that.
2862-
#[tokio::test]
2863-
async fn test_deserialize_empty_collections() {
2864-
// Setup session.
2865-
let ks = unique_keyspace_name();
2866-
let session = create_new_session_builder().build().await.unwrap();
2867-
session.ddl(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}", ks)).await.unwrap();
2868-
session.use_keyspace(&ks, true).await.unwrap();
2869-
2870-
async fn deserialize_empty_collection<
2871-
Collection: Default + DeserializeOwnedValue + SerializeValue,
2872-
>(
2873-
session: &Session,
2874-
collection_name: &str,
2875-
collection_type_params: &str,
2876-
) -> Collection {
2877-
// Create a table for the given collection type.
2878-
let table_name = "test_empty_".to_owned() + collection_name;
2879-
let query = format!(
2880-
"CREATE TABLE {} (n int primary key, c {}<{}>)",
2881-
table_name, collection_name, collection_type_params
2882-
);
2883-
session.ddl(query).await.unwrap();
2884-
2885-
// Populate the table with an empty collection, effectively inserting null as the collection.
2886-
session
2887-
.query_unpaged(
2888-
format!("INSERT INTO {} (n, c) VALUES (?, ?)", table_name,),
2889-
(0, Collection::default()),
2890-
)
2891-
.await
2892-
.unwrap();
2893-
2894-
let query_rows_result = session
2895-
.query_unpaged(format!("SELECT c FROM {}", table_name), ())
2896-
.await
2897-
.unwrap()
2898-
.into_rows_result()
2899-
.unwrap();
2900-
let (collection,) = query_rows_result.first_row::<(Collection,)>().unwrap();
2901-
2902-
// Drop the table
2903-
collection
2904-
}
2905-
2906-
let list = deserialize_empty_collection::<Vec<i32>>(&session, "list", "int").await;
2907-
assert!(list.is_empty());
2908-
2909-
let set = deserialize_empty_collection::<HashSet<i64>>(&session, "set", "bigint").await;
2910-
assert!(set.is_empty());
2911-
2912-
let map = deserialize_empty_collection::<HashMap<bool, CqlVarint>>(
2913-
&session,
2914-
"map",
2915-
"boolean, varint",
2916-
)
2917-
.await;
2918-
assert!(map.is_empty());
2919-
}
2920-
29212857
#[cfg(cassandra_tests)]
29222858
#[tokio::test]
29232859
async fn test_vector_type_metadata() {

0 commit comments

Comments
 (0)