Skip to content

Commit 09db999

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 aae68f2 commit 09db999

File tree

1 file changed

+235
-0
lines changed

1 file changed

+235
-0
lines changed

scylla/tests/integration/serialization.rs

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,60 @@ fn handle_incorrect_dates(error: &DeserializationError) -> bool {
9797
}
9898
}
9999

100+
// Native type test, which expects value to have the display trait and be trivially copyable
101+
async fn run_native_serialize_test<T>(table_name: &str, type_name: &str, values: Vec<Option<T>>)
102+
where
103+
T: SerializeValue
104+
+ DeserializeOwnedValue
105+
+ FromStr
106+
+ Debug
107+
+ Clone
108+
+ PartialEq
109+
+ Default
110+
+ Display
111+
+ Copy,
112+
{
113+
let session = init_test(table_name, type_name).await;
114+
tracing::debug!("Active keyspace: {}", session.get_keyspace().unwrap());
115+
116+
for original_value in values {
117+
session
118+
.query_unpaged(
119+
format!("INSERT INTO {} (id, val) VALUES (1, ?)", table_name),
120+
(&original_value,),
121+
)
122+
.await
123+
.unwrap();
124+
125+
let selected_value: Vec<T> = session
126+
.query_unpaged(format!("SELECT val FROM {}", table_name), &[])
127+
.await
128+
.unwrap()
129+
.into_rows_result()
130+
.unwrap()
131+
.rows::<(T,)>()
132+
.unwrap()
133+
.map(|row| match row {
134+
Ok((val,)) => val,
135+
Err(e) => handle_deserialize_error(&e, || T::default()),
136+
})
137+
.collect::<Vec<_>>();
138+
139+
for value in selected_value {
140+
tracing::debug!(
141+
"Received: {} | Expected: {}",
142+
value,
143+
if original_value.is_none() {
144+
T::default()
145+
} else {
146+
original_value.unwrap()
147+
}
148+
);
149+
assert_eq!(value, original_value.unwrap_or(T::default()));
150+
}
151+
}
152+
}
153+
100154
// Test for types that either don't have the Display trait or foreign types from other libraries
101155
async fn run_foreign_serialize_test<T>(table_name: &str, type_name: &str, values: Vec<Option<T>>)
102156
where
@@ -277,6 +331,146 @@ async fn run_literal_input_maybe_empty_output_test<X>(
277331
}
278332
}
279333

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

282476
#[tokio::test]
@@ -1242,6 +1436,19 @@ async fn test_serialize_deserialize_offset_date_time_03_high_precision_rounding_
12421436

12431437
//UUID Types
12441438

1439+
#[tokio::test]
1440+
async fn test_serialize_deserialize_uuid() {
1441+
setup_tracing();
1442+
const TABLE_NAME: &str = "uuid_serialization_test";
1443+
1444+
let tests = (0..100)
1445+
.map(|_| uuid::Uuid::new_v4())
1446+
.map(|new_uuid| (new_uuid.to_string(), new_uuid))
1447+
.collect();
1448+
1449+
run_literal_and_bound_value_test(TABLE_NAME, "uuid", tests, false).await;
1450+
}
1451+
12451452
#[tokio::test]
12461453
async fn test_serialize_deserialize_timeuuid() {
12471454
setup_tracing();
@@ -1350,6 +1557,34 @@ async fn test_serialize_deserialize_timeuuid_ordering() {
13501557
}
13511558
}
13521559

1560+
#[tokio::test]
1561+
async fn test_serialize_deserialize_strings_varchar() {
1562+
let table_name = "varchar_serialization_test";
1563+
1564+
let test_cases = vec![
1565+
("Hello, World!", String::from("Hello, World!")),
1566+
("Hello, Мир", String::from("Hello, Мир")), // multi-byte string
1567+
("🦀A🦀B🦀C", String::from("🦀A🦀B🦀C")),
1568+
("おはようございます", String::from("おはようございます")),
1569+
];
1570+
1571+
run_literal_and_bound_value_test(table_name, "varchar", test_cases, true).await;
1572+
}
1573+
1574+
#[tokio::test]
1575+
async fn test_serialize_deserialize_strings_ascii() {
1576+
let table_name = "ascii_serialization_test";
1577+
1578+
let test_cases = vec![
1579+
("Hello, World!", String::from("Hello, World!")),
1580+
(
1581+
str::from_utf8(&[0x0, 0x7f]).unwrap(),
1582+
String::from_utf8(vec![0x0, 0x7f]).unwrap(),
1583+
), // min/max ASCII values
1584+
];
1585+
1586+
run_literal_and_bound_value_test(table_name, "varchar", test_cases, true).await;
1587+
}
13531588

13541589
// UDT Types
13551590

0 commit comments

Comments
 (0)