Skip to content

Commit 8b93622

Browse files
committed
tests: Add more scalar type tests, normal uuid and string type tests
This commit adds additional test to test various simple types such as int and float, as well as one more uuid test and some simple string type tests
1 parent af38b24 commit 8b93622

File tree

1 file changed

+239
-0
lines changed

1 file changed

+239
-0
lines changed

scylla/tests/integration/serialization.rs

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,63 @@ fn assert_error_is_not_overflow_or_expected_non_null(error: &DeserializationErro
7878
}
7979
}
8080

81+
// Native type test, which expects value to have the display trait and be trivially copyable
82+
async fn run_native_serialize_test<T>(table_name: &str, type_name: &str, values: &[Option<T>])
83+
where
84+
T: SerializeValue
85+
+ DeserializeOwnedValue
86+
+ FromStr
87+
+ Debug
88+
+ Clone
89+
+ PartialEq
90+
+ Default
91+
+ Display
92+
+ Copy,
93+
{
94+
let session = prepare_test_table(table_name, type_name, true).await;
95+
tracing::debug!("Active keyspace: {}", session.get_keyspace().unwrap());
96+
97+
for original_value in values {
98+
session
99+
.query_unpaged(
100+
format!("INSERT INTO {} (id, val) VALUES (1, ?)", table_name),
101+
(&original_value,),
102+
)
103+
.await
104+
.unwrap();
105+
106+
let selected_value: Vec<T> = session
107+
.query_unpaged(format!("SELECT val FROM {}", table_name), &[])
108+
.await
109+
.unwrap()
110+
.into_rows_result()
111+
.unwrap()
112+
.rows::<(T,)>()
113+
.unwrap()
114+
.map(|row| match row {
115+
Ok((val,)) => val,
116+
Err(e) => {
117+
assert_error_is_not_expected_non_null(&e);
118+
T::default()
119+
}
120+
})
121+
.collect::<Vec<_>>();
122+
123+
for value in selected_value {
124+
tracing::debug!(
125+
"Received: {} | Expected: {}",
126+
value,
127+
if original_value.is_none() {
128+
T::default()
129+
} else {
130+
original_value.unwrap()
131+
}
132+
);
133+
assert_eq!(value, original_value.unwrap_or(T::default()));
134+
}
135+
}
136+
}
137+
81138
// Test for types that either don't have the Display trait or foreign types from other libraries
82139
async fn run_foreign_serialize_test<T>(table_name: &str, type_name: &str, values: &[Option<T>])
83140
where
@@ -266,6 +323,146 @@ async fn run_literal_input_maybe_empty_output_test<X>(
266323
}
267324
}
268325

326+
// Scalar types
327+
328+
#[tokio::test]
329+
async fn test_serialize_deserialize_bigint() {
330+
setup_tracing();
331+
const TABLE_NAME: &str = "bigint_serialization_test";
332+
333+
let test_cases = [
334+
Some(i64::MIN),
335+
Some(i64::MAX),
336+
Some(1),
337+
Some(-1),
338+
Some(0),
339+
None,
340+
];
341+
342+
run_native_serialize_test(TABLE_NAME, "bigint", &test_cases).await;
343+
}
344+
345+
#[tokio::test]
346+
async fn test_serialize_deserialize_int() {
347+
setup_tracing();
348+
const TABLE_NAME: &str = "int_serialization_test";
349+
350+
let test_cases = [
351+
Some(i32::MIN),
352+
Some(i32::MAX),
353+
Some(1),
354+
Some(-1),
355+
Some(0),
356+
None,
357+
];
358+
359+
run_native_serialize_test(TABLE_NAME, "int", &test_cases).await;
360+
}
361+
362+
#[tokio::test]
363+
async fn test_serialize_deserialize_smallint() {
364+
setup_tracing();
365+
const TABLE_NAME: &str = "smallint_serialization_test";
366+
367+
let test_cases = [
368+
Some(i16::MIN),
369+
Some(i16::MAX),
370+
Some(1),
371+
Some(-1),
372+
Some(0),
373+
None,
374+
];
375+
376+
run_native_serialize_test(TABLE_NAME, "smallint", &test_cases).await;
377+
}
378+
379+
#[tokio::test]
380+
async fn test_serialize_deserialize_tinyint() {
381+
setup_tracing();
382+
const TABLE_NAME: &str = "tinyint_serialization_test";
383+
384+
let test_cases = [
385+
Some(i8::MIN),
386+
Some(i8::MAX),
387+
Some(1),
388+
Some(-1),
389+
Some(0),
390+
None,
391+
];
392+
393+
run_native_serialize_test(TABLE_NAME, "tinyint", &test_cases).await;
394+
}
395+
396+
#[tokio::test]
397+
async fn test_serialize_deserialize_float() {
398+
setup_tracing();
399+
const TABLE_NAME: &str = "float_serialization_test";
400+
401+
let test_cases = [
402+
Some(f32::MIN),
403+
Some(f32::MAX),
404+
Some(1.0),
405+
Some(-1.0),
406+
Some(0.0),
407+
None,
408+
];
409+
410+
run_native_serialize_test(TABLE_NAME, "float", &test_cases).await;
411+
}
412+
413+
#[tokio::test]
414+
async fn test_serialize_deserialize_bool() {
415+
setup_tracing();
416+
const TABLE_NAME: &str = "bool_serialization_test";
417+
418+
let test_cases = [Some(true), Some(false), None];
419+
420+
run_native_serialize_test(TABLE_NAME, "boolean", &test_cases).await;
421+
}
422+
423+
#[tokio::test]
424+
async fn test_serialize_deserialize_double() {
425+
setup_tracing();
426+
const TABLE_NAME: &str = "double_serialization_test";
427+
428+
let test_cases = [
429+
Some(f64::MIN),
430+
Some(f64::MAX),
431+
Some(1.0),
432+
Some(-1.0),
433+
Some(0.0),
434+
None,
435+
];
436+
437+
run_native_serialize_test(TABLE_NAME, "double", &test_cases).await;
438+
}
439+
440+
#[tokio::test]
441+
async fn test_serialize_deserialize_double_literal() {
442+
setup_tracing();
443+
const TABLE_NAME: &str = "double_serialization_test";
444+
445+
let test_cases: Vec<(String, f64)> = [f64::MIN, f64::MAX, 1.0, -1.0, 0.0, 0.1]
446+
.iter()
447+
.map(|val| (val.to_string(), *val))
448+
.collect();
449+
450+
run_literal_and_bound_value_test(TABLE_NAME, "double", test_cases.as_slice(), false).await;
451+
}
452+
453+
#[tokio::test]
454+
async fn test_serialize_deserialize_float_literal() {
455+
setup_tracing();
456+
const TABLE_NAME: &str = "float_serialization_test";
457+
458+
let test_cases: Vec<(String, f32)> = [f32::MIN, f32::MAX, 1.0, -1.0, 0.0, 0.1]
459+
.iter()
460+
.map(|val| (val.to_string(), *val))
461+
.collect();
462+
463+
run_literal_and_bound_value_test(TABLE_NAME, "float", test_cases.as_slice(), false).await;
464+
}
465+
269466
// Arbitrary precision types
270467

271468
#[tokio::test]
@@ -1222,6 +1419,19 @@ async fn test_serialize_deserialize_offset_date_time_03_high_precision_rounding_
12221419

12231420
//UUID Types
12241421

1422+
#[tokio::test]
1423+
async fn test_serialize_deserialize_uuid() {
1424+
setup_tracing();
1425+
const TABLE_NAME: &str = "uuid_serialization_test";
1426+
1427+
let tests: Vec<(String, uuid::Uuid)> = (0..100)
1428+
.map(|_| uuid::Uuid::new_v4())
1429+
.map(|new_uuid| (new_uuid.to_string(), new_uuid))
1430+
.collect();
1431+
1432+
run_literal_and_bound_value_test(TABLE_NAME, "uuid", tests.as_slice(), false).await;
1433+
}
1434+
12251435
#[tokio::test]
12261436
async fn test_serialize_deserialize_timeuuid() {
12271437
setup_tracing();
@@ -1330,6 +1540,35 @@ async fn test_serialize_deserialize_timeuuid_ordering() {
13301540
}
13311541
}
13321542

1543+
#[tokio::test]
1544+
async fn test_serialize_deserialize_strings_varchar() {
1545+
let table_name = "varchar_serialization_test";
1546+
1547+
let test_cases: Vec<(&str, String)> = vec![
1548+
("Hello, World!", String::from("Hello, World!")),
1549+
("Hello, Мир", String::from("Hello, Мир")), // multi-byte string
1550+
("🦀A🦀B🦀C", String::from("🦀A🦀B🦀C")),
1551+
("おはようございます", String::from("おはようございます")),
1552+
];
1553+
1554+
run_literal_and_bound_value_test(table_name, "varchar", test_cases.as_slice(), true).await;
1555+
}
1556+
1557+
#[tokio::test]
1558+
async fn test_serialize_deserialize_strings_ascii() {
1559+
let table_name = "ascii_serialization_test";
1560+
1561+
let test_cases: Vec<(&str, String)> = vec![
1562+
("Hello, World!", String::from("Hello, World!")),
1563+
(
1564+
str::from_utf8(&[0x0, 0x7f]).unwrap(),
1565+
String::from_utf8(vec![0x0, 0x7f]).unwrap(),
1566+
), // min/max ASCII values
1567+
];
1568+
1569+
run_literal_and_bound_value_test(table_name, "varchar", test_cases.as_slice(), true).await;
1570+
}
1571+
13331572
// UDT Types
13341573

13351574
#[tokio::test]

0 commit comments

Comments
 (0)