Skip to content

Commit c2e2c97

Browse files
committed
serialization: Test with custom errors
We need to verify that function changing rust_name in builtin errors does not panic for custom errors.
1 parent c5fa6db commit c2e2c97

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

scylla-cql/src/serialize/value_tests.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::serialize::value::{
55
SetOrListSerializationErrorKind, SetOrListTypeCheckErrorKind, TupleSerializationErrorKind,
66
TupleTypeCheckErrorKind, UdtSerializationErrorKind, UdtTypeCheckErrorKind,
77
};
8+
use crate::serialize::writers::WrittenCellProof;
89
use crate::serialize::{CellWriter, SerializationError};
910
use crate::value::{
1011
Counter, CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlTimeuuid, CqlValue, CqlVarint,
@@ -20,8 +21,31 @@ use std::str::FromStr;
2021
use std::sync::Arc;
2122

2223
use assert_matches::assert_matches;
24+
use thiserror::Error;
2325
use uuid::Uuid;
2426

27+
#[derive(PartialEq, Eq, PartialOrd, Ord)]
28+
struct SerializeWithCustomError;
29+
30+
#[derive(Error, Debug)]
31+
#[error("Custom serialization error")]
32+
struct CustomSerializationError;
33+
34+
impl SerializeValue for SerializeWithCustomError {
35+
fn serialize<'b>(
36+
&self,
37+
_typ: &ColumnType,
38+
_writer: CellWriter<'b>,
39+
) -> Result<WrittenCellProof<'b>, SerializationError> {
40+
Err(SerializationError::new(CustomSerializationError))
41+
}
42+
}
43+
44+
#[cfg(feature = "secrecy-08")]
45+
impl secrecy_08::Zeroize for SerializeWithCustomError {
46+
fn zeroize(&mut self) {}
47+
}
48+
2549
#[test]
2650
fn test_dyn_serialize_value() {
2751
let v: i32 = 123;
@@ -123,31 +147,51 @@ fn verify_typeck_error_in_wrapper<T: SerializeValue>(v: T) {
123147
);
124148
}
125149

150+
fn verify_custom_error_in_wrapper<T: SerializeValue>(v: T) {
151+
let err = do_serialize_err::<T>(v, &ColumnType::Native(NativeType::BigInt));
152+
err.0
153+
.downcast_ref::<CustomSerializationError>()
154+
.expect("CustomSerializationError");
155+
}
156+
126157
#[cfg(feature = "secrecy-08")]
127158
#[test]
128159
fn test_secrecy_08_errors() {
129160
use secrecy_08::Secret;
130161
verify_typeck_error_in_wrapper::<Secret<i32>>(Secret::new(123));
162+
verify_custom_error_in_wrapper::<Secret<SerializeWithCustomError>>(Secret::new(
163+
SerializeWithCustomError,
164+
));
131165
}
132166

133167
#[test]
134168
fn test_option_errors() {
135169
verify_typeck_error_in_wrapper::<Option<i32>>(Some(123));
170+
verify_custom_error_in_wrapper::<Option<SerializeWithCustomError>>(Some(
171+
SerializeWithCustomError,
172+
));
136173
}
137174

138175
#[test]
139176
fn test_maybe_unset_errors() {
140177
verify_typeck_error_in_wrapper::<MaybeUnset<i32>>(MaybeUnset::Set(123));
178+
verify_custom_error_in_wrapper::<MaybeUnset<SerializeWithCustomError>>(MaybeUnset::Set(
179+
SerializeWithCustomError,
180+
));
141181
}
142182

143183
#[test]
144184
fn test_ref_errors() {
145185
verify_typeck_error_in_wrapper::<&i32>(&123_i32);
186+
verify_custom_error_in_wrapper::<&SerializeWithCustomError>(&SerializeWithCustomError);
146187
}
147188

148189
#[test]
149190
fn test_box_errors() {
150191
verify_typeck_error_in_wrapper::<Box<i32>>(Box::new(123));
192+
verify_custom_error_in_wrapper::<Box<SerializeWithCustomError>>(Box::new(
193+
SerializeWithCustomError,
194+
));
151195
}
152196

153197
#[cfg(feature = "bigdecimal-04")]
@@ -221,6 +265,25 @@ fn test_set_or_list_errors() {
221265
expected: &[ColumnType::Native(NativeType::Int)],
222266
}
223267
);
268+
269+
// Test serialization with custom error
270+
let err = do_serialize_err(
271+
vec![SerializeWithCustomError],
272+
&ColumnType::Collection {
273+
frozen: false,
274+
typ: CollectionType::Set(Box::new(ColumnType::Native(NativeType::Double))),
275+
},
276+
);
277+
let err = get_ser_err(&err);
278+
let BuiltinSerializationErrorKind::SetOrListError(
279+
SetOrListSerializationErrorKind::ElementSerializationFailed(err),
280+
) = &err.kind
281+
else {
282+
panic!("unexpected error kind: {}", err.kind)
283+
};
284+
err.0
285+
.downcast_ref::<CustomSerializationError>()
286+
.expect("CustomSerializationError");
224287
}
225288

226289
#[test]
@@ -292,6 +355,51 @@ fn test_map_errors() {
292355
expected: &[ColumnType::Native(NativeType::Int)],
293356
}
294357
);
358+
359+
// Test serialization with custom error
360+
// Value
361+
let err = do_serialize_err(
362+
BTreeMap::from([(123_i32, SerializeWithCustomError)]),
363+
&ColumnType::Collection {
364+
frozen: false,
365+
typ: CollectionType::Map(
366+
Box::new(ColumnType::Native(NativeType::Int)),
367+
Box::new(ColumnType::Native(NativeType::Int)),
368+
),
369+
},
370+
);
371+
let err = get_ser_err(&err);
372+
let BuiltinSerializationErrorKind::MapError(
373+
MapSerializationErrorKind::ValueSerializationFailed(err),
374+
) = &err.kind
375+
else {
376+
panic!("unexpected error kind: {}", err.kind)
377+
};
378+
err.0
379+
.downcast_ref::<CustomSerializationError>()
380+
.expect("CustomSerializationError");
381+
382+
// Key
383+
let err = do_serialize_err(
384+
BTreeMap::from([(SerializeWithCustomError, 123_i32)]),
385+
&ColumnType::Collection {
386+
frozen: false,
387+
typ: CollectionType::Map(
388+
Box::new(ColumnType::Native(NativeType::Int)),
389+
Box::new(ColumnType::Native(NativeType::Int)),
390+
),
391+
},
392+
);
393+
let err = get_ser_err(&err);
394+
let BuiltinSerializationErrorKind::MapError(MapSerializationErrorKind::KeySerializationFailed(
395+
err,
396+
)) = &err.kind
397+
else {
398+
panic!("unexpected error kind: {}", err.kind)
399+
};
400+
err.0
401+
.downcast_ref::<CustomSerializationError>()
402+
.expect("CustomSerializationError");
295403
}
296404

297405
#[test]
@@ -346,6 +454,25 @@ fn test_tuple_errors() {
346454
expected: &[ColumnType::Native(NativeType::Double)],
347455
}
348456
);
457+
458+
// Test serialization with custom error
459+
let err = do_serialize_err(
460+
(SerializeWithCustomError, SerializeWithCustomError),
461+
&ColumnType::Tuple(vec![
462+
ColumnType::Native(NativeType::Double),
463+
ColumnType::Native(NativeType::Double),
464+
]),
465+
);
466+
let err = get_ser_err(&err);
467+
let BuiltinSerializationErrorKind::TupleError(
468+
TupleSerializationErrorKind::ElementSerializationFailed { index: 0, err },
469+
) = &err.kind
470+
else {
471+
panic!("unexpected error kind: {}", err.kind)
472+
};
473+
err.0
474+
.downcast_ref::<CustomSerializationError>()
475+
.expect("CustomSerializationError");
349476
}
350477

351478
#[test]

0 commit comments

Comments
 (0)