diff --git a/scylla-cql/src/frame/value_tests.rs b/scylla-cql/src/frame/value_tests.rs index f431aaacce..f15c5793fd 100644 --- a/scylla-cql/src/frame/value_tests.rs +++ b/scylla-cql/src/frame/value_tests.rs @@ -10,6 +10,8 @@ use super::value::{ CqlDate, CqlDuration, CqlTime, CqlTimestamp, LegacyBatchValues, LegacySerializedValues, MaybeUnset, SerializeValuesError, Unset, Value, ValueList, ValueTooBig, }; +#[cfg(test)] +use assert_matches::assert_matches; use bytes::BufMut; use std::collections::hash_map::DefaultHasher; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; @@ -1262,7 +1264,7 @@ fn serialized_values_value_list() { ser_values.add_value(&"qwertyuiop").unwrap(); let ser_ser_values: Cow = ser_values.serialized().unwrap(); - assert!(matches!(ser_ser_values, Cow::Borrowed(_))); + assert_matches!(ser_ser_values, Cow::Borrowed(_)); assert_eq!(&ser_values, ser_ser_values.as_ref()); } @@ -1272,7 +1274,7 @@ fn cow_serialized_values_value_list() { let cow_ser_values: Cow = Cow::Owned(LegacySerializedValues::new()); let serialized: Cow = cow_ser_values.serialized().unwrap(); - assert!(matches!(serialized, Cow::Borrowed(_))); + assert_matches!(serialized, Cow::Borrowed(_)); assert_eq!(cow_ser_values.as_ref(), serialized.as_ref()); } diff --git a/scylla-cql/src/types/serialize/row.rs b/scylla-cql/src/types/serialize/row.rs index c8f04cc6bc..2e14b75b3e 100644 --- a/scylla-cql/src/types/serialize/row.rs +++ b/scylla-cql/src/types/serialize/row.rs @@ -865,6 +865,7 @@ mod tests { }; use super::SerializedValues; + use assert_matches::assert_matches; use scylla_macros::SerializeRow; fn col_spec(name: &str, typ: ColumnType) -> ColumnSpec { @@ -1044,13 +1045,13 @@ mod tests { let err = do_serialize_err(v, &spec); let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::<()>()); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::WrongColumnCount { actual: 0, asked_for: 1, } - )); + ); // Non-unit tuple // Count mismatch @@ -1059,13 +1060,13 @@ mod tests { let err = do_serialize_err(v, &spec); let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::<(&str,)>()); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::WrongColumnCount { actual: 1, asked_for: 2, } - )); + ); // Serialization of one of the element fails let v = ("Ala ma kota", 123_i32); @@ -1086,13 +1087,13 @@ mod tests { let err = do_serialize_err(v, &spec); let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::>()); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::WrongColumnCount { actual: 1, asked_for: 2, } - )); + ); // Serialization of one of the element fails let v = vec!["Ala ma kota", "Kot ma pchły"]; @@ -1214,10 +1215,10 @@ mod tests { }; let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut row_writer).unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::ValueMissingForColumn { .. } - )); + ); let spec_duplicate_column = [ col("a", ColumnType::Text), @@ -1232,10 +1233,7 @@ mod tests { }; let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut row_writer).unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( - err.kind, - BuiltinTypeCheckErrorKind::NoColumnWithName { .. } - )); + assert_matches!(err.kind, BuiltinTypeCheckErrorKind::NoColumnWithName { .. }); let spec_wrong_type = [ col("a", ColumnType::Text), @@ -1248,10 +1246,10 @@ mod tests { }; let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut row_writer).unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinSerializationErrorKind::ColumnSerializationFailed { .. } - )); + ); } #[derive(SerializeRow)] @@ -1325,10 +1323,10 @@ mod tests { let ctx = RowSerializationContext { columns: &spec }; let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut writer).unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::ColumnNameMismatch { .. } - )); + ); let spec_without_c = [ col("a", ColumnType::Text), @@ -1341,10 +1339,10 @@ mod tests { }; let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut writer).unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::ValueMissingForColumn { .. } - )); + ); let spec_duplicate_column = [ col("a", ColumnType::Text), @@ -1359,10 +1357,7 @@ mod tests { }; let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut writer).unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( - err.kind, - BuiltinTypeCheckErrorKind::NoColumnWithName { .. } - )); + assert_matches!(err.kind, BuiltinTypeCheckErrorKind::NoColumnWithName { .. }); let spec_wrong_type = [ col("a", ColumnType::Text), @@ -1375,10 +1370,10 @@ mod tests { }; let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut writer).unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinSerializationErrorKind::ColumnSerializationFailed { .. } - )); + ); } #[test] diff --git a/scylla-cql/src/types/serialize/value.rs b/scylla-cql/src/types/serialize/value.rs index 6f917f8de4..b896b6a528 100644 --- a/scylla-cql/src/types/serialize/value.rs +++ b/scylla-cql/src/types/serialize/value.rs @@ -1536,6 +1536,7 @@ mod tests { }; use crate::types::serialize::{CellWriter, SerializationError}; + use assert_matches::assert_matches; use scylla_macros::SerializeCql; use super::{SerializeCql, UdtSerializationErrorKind, UdtTypeCheckErrorKind}; @@ -1628,12 +1629,12 @@ mod tests { let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::()); assert_eq!(err.got, ColumnType::Double); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::MismatchedType { expected: &[ColumnType::Int], - }, - )); + } + ); // str (and also Uuid) are interesting because they accept two types, // also check str here @@ -1642,12 +1643,12 @@ mod tests { let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::<&str>()); assert_eq!(err.got, ColumnType::Double); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::MismatchedType { expected: &[ColumnType::Ascii, ColumnType::Text], - }, - )); + } + ); // We'll skip testing for SizeOverflow as this would require producing // a value which is at least 2GB in size. @@ -1665,10 +1666,7 @@ mod tests { let err = get_ser_err(&err); assert_eq!(err.rust_name, std::any::type_name::()); assert_eq!(err.got, ColumnType::Decimal); - assert!(matches!( - err.kind, - BuiltinSerializationErrorKind::ValueOverflow, - )); + assert_matches!(err.kind, BuiltinSerializationErrorKind::ValueOverflow); } #[test] @@ -1679,10 +1677,10 @@ mod tests { let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::>()); assert_eq!(err.got, ColumnType::Double); - assert!(matches!( + assert_matches!( err.kind, - BuiltinTypeCheckErrorKind::SetOrListError(SetOrListTypeCheckErrorKind::NotSetOrList), - )); + BuiltinTypeCheckErrorKind::SetOrListError(SetOrListTypeCheckErrorKind::NotSetOrList) + ); // Trick: Unset is a ZST, so [Unset; 1 << 33] is a ZST, too. // While it's probably incorrect to use Unset in a collection, this @@ -1694,12 +1692,12 @@ mod tests { let err = get_ser_err(&err); assert_eq!(err.rust_name, std::any::type_name::<&[Unset]>()); assert_eq!(err.got, typ); - assert!(matches!( + assert_matches!( err.kind, BuiltinSerializationErrorKind::SetOrListError( SetOrListSerializationErrorKind::TooManyElements - ), - )); + ) + ); // Error during serialization of an element let v = vec![123_i32]; @@ -1715,12 +1713,12 @@ mod tests { panic!("unexpected error kind: {}", err.kind) }; let err = get_typeck_err(err); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::MismatchedType { expected: &[ColumnType::Int], } - )); + ); } #[test] @@ -1731,10 +1729,10 @@ mod tests { let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::>()); assert_eq!(err.got, ColumnType::Double); - assert!(matches!( + assert_matches!( err.kind, - BuiltinTypeCheckErrorKind::MapError(MapTypeCheckErrorKind::NotMap), - )); + BuiltinTypeCheckErrorKind::MapError(MapTypeCheckErrorKind::NotMap) + ); // It's not practical to check the TooManyElements error as it would // require allocating a huge amount of memory. @@ -1753,12 +1751,12 @@ mod tests { panic!("unexpected error kind: {}", err.kind) }; let err = get_typeck_err(err); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::MismatchedType { expected: &[ColumnType::Int], } - )); + ); // Error during serialization of a value let v = BTreeMap::from([(123_i32, 456_i32)]); @@ -1774,12 +1772,12 @@ mod tests { panic!("unexpected error kind: {}", err.kind) }; let err = get_typeck_err(err); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::MismatchedType { expected: &[ColumnType::Int], } - )); + ); } #[test] @@ -1790,10 +1788,10 @@ mod tests { let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::<(i32, i32, i32)>()); assert_eq!(err.got, ColumnType::Double); - assert!(matches!( + assert_matches!( err.kind, - BuiltinTypeCheckErrorKind::TupleError(TupleTypeCheckErrorKind::NotTuple), - )); + BuiltinTypeCheckErrorKind::TupleError(TupleTypeCheckErrorKind::NotTuple) + ); // The Rust tuple has more elements than the CQL type let v = (123_i32, 456_i32, 789_i32); @@ -1802,13 +1800,13 @@ mod tests { let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::<(i32, i32, i32)>()); assert_eq!(err.got, typ); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::TupleError(TupleTypeCheckErrorKind::WrongElementCount { actual: 3, asked_for: 2, - }), - )); + }) + ); // Error during serialization of one of the elements let v = (123_i32, "Ala ma kota", 789.0_f64); @@ -1824,12 +1822,12 @@ mod tests { panic!("unexpected error kind: {}", err.kind) }; let err = get_typeck_err(err); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::MismatchedType { expected: &[ColumnType::Double], } - )); + ); } #[test] @@ -1840,7 +1838,7 @@ mod tests { let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::()); assert_eq!(err.got, ColumnType::Counter); - assert!(matches!(err.kind, BuiltinTypeCheckErrorKind::NotEmptyable)); + assert_matches!(err.kind, BuiltinTypeCheckErrorKind::NotEmptyable); // Handle tuples and UDTs in separate tests, as they have some // custom logic @@ -1858,10 +1856,10 @@ mod tests { let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::()); assert_eq!(err.got, ColumnType::Double); - assert!(matches!( + assert_matches!( err.kind, - BuiltinTypeCheckErrorKind::TupleError(TupleTypeCheckErrorKind::NotTuple), - )); + BuiltinTypeCheckErrorKind::TupleError(TupleTypeCheckErrorKind::NotTuple) + ); // The Rust tuple has more elements than the CQL type let v = CqlValue::Tuple(vec![ @@ -1874,13 +1872,13 @@ mod tests { let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::()); assert_eq!(err.got, typ); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::TupleError(TupleTypeCheckErrorKind::WrongElementCount { actual: 3, asked_for: 2, - }), - )); + }) + ); // Error during serialization of one of the elements let v = CqlValue::Tuple(vec![ @@ -1900,12 +1898,12 @@ mod tests { panic!("unexpected error kind: {}", err.kind) }; let err = get_typeck_err(err); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::MismatchedType { expected: &[ColumnType::Double], } - )); + ); } #[test] @@ -1924,10 +1922,10 @@ mod tests { let err = get_typeck_err(&err); assert_eq!(err.rust_name, std::any::type_name::()); assert_eq!(err.got, ColumnType::Double); - assert!(matches!( + assert_matches!( err.kind, - BuiltinTypeCheckErrorKind::UdtError(UdtTypeCheckErrorKind::NotUdt), - )); + BuiltinTypeCheckErrorKind::UdtError(UdtTypeCheckErrorKind::NotUdt) + ); // Wrong type name let v = CqlValue::UserDefinedType { @@ -2027,12 +2025,12 @@ mod tests { }; assert_eq!(field_name, "c"); let err = get_typeck_err(err); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::MismatchedType { expected: &[ColumnType::Int], } - )); + ); } // Do not remove. It's not used in tests but we keep it here to check that @@ -2251,10 +2249,10 @@ mod tests { .serialize(&typ_not_udt, CellWriter::new(&mut data)) .unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::UdtError(UdtTypeCheckErrorKind::NotUdt) - )); + ); let typ_without_c = ColumnType::UserDefinedType { type_name: "typ".to_string(), @@ -2270,12 +2268,12 @@ mod tests { .serialize(&typ_without_c, CellWriter::new(&mut data)) .unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::UdtError( UdtTypeCheckErrorKind::ValueMissingForUdtField { .. } ) - )); + ); let typ_wrong_type = ColumnType::UserDefinedType { type_name: "typ".to_string(), @@ -2291,12 +2289,12 @@ mod tests { .serialize(&typ_wrong_type, CellWriter::new(&mut data)) .unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinSerializationErrorKind::UdtError( UdtSerializationErrorKind::FieldSerializationFailed { .. } ) - )); + ); } #[derive(SerializeCql)] @@ -2448,10 +2446,10 @@ mod tests { let err = <_ as SerializeCql>::serialize(&udt, &typ_not_udt, CellWriter::new(&mut data)) .unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::UdtError(UdtTypeCheckErrorKind::NotUdt) - )); + ); let typ = ColumnType::UserDefinedType { type_name: "typ".to_string(), @@ -2470,10 +2468,10 @@ mod tests { let err = <_ as SerializeCql>::serialize(&udt, &typ, CellWriter::new(&mut data)).unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::UdtError(UdtTypeCheckErrorKind::FieldNameMismatch { .. }) - )); + ); let typ_without_c = ColumnType::UserDefinedType { type_name: "typ".to_string(), @@ -2488,12 +2486,12 @@ mod tests { let err = <_ as SerializeCql>::serialize(&udt, &typ_without_c, CellWriter::new(&mut data)) .unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::UdtError( UdtTypeCheckErrorKind::ValueMissingForUdtField { .. } ) - )); + ); let typ_unexpected_field = ColumnType::UserDefinedType { type_name: "typ".to_string(), @@ -2509,12 +2507,12 @@ mod tests { <_ as SerializeCql>::serialize(&udt, &typ_unexpected_field, CellWriter::new(&mut data)) .unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinSerializationErrorKind::UdtError( UdtSerializationErrorKind::FieldSerializationFailed { .. } ) - )); + ); } #[derive(SerializeCql, Debug)] @@ -2669,10 +2667,10 @@ mod tests { .serialize(&typ_unexpected_field, CellWriter::new(&mut data)) .unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::UdtError(UdtTypeCheckErrorKind::NoSuchFieldInUdt { .. }) - )); + ); let typ_unexpected_field_middle = ColumnType::UserDefinedType { type_name: "typ".to_string(), @@ -2693,10 +2691,10 @@ mod tests { .serialize(&typ_unexpected_field_middle, CellWriter::new(&mut data)) .unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::UdtError(UdtTypeCheckErrorKind::NoSuchFieldInUdt { .. }) - )); + ); } #[derive(SerializeCql, Debug, PartialEq, Eq, Default)] @@ -2731,10 +2729,10 @@ mod tests { <_ as SerializeCql>::serialize(&udt, &typ_unexpected_field, CellWriter::new(&mut data)) .unwrap_err(); let err = err.0.downcast_ref::().unwrap(); - assert!(matches!( + assert_matches!( err.kind, BuiltinTypeCheckErrorKind::UdtError(UdtTypeCheckErrorKind::NoSuchFieldInUdt { .. }) - )); + ); } #[derive(SerializeCql, Debug)] diff --git a/scylla/src/history.rs b/scylla/src/history.rs index a43d46423a..f7c9acf6d9 100644 --- a/scylla/src/history.rs +++ b/scylla/src/history.rs @@ -465,6 +465,7 @@ mod tests { SpeculativeId, StructuredHistory, TimePoint, }; use crate::test_utils::create_new_session_builder; + use assert_matches::assert_matches; use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc}; use futures::StreamExt; use scylla_cql::{ @@ -642,10 +643,10 @@ mod tests { assert_eq!(history.queries.len(), 1); assert_eq!(history.queries[0].non_speculative_fiber.attempts.len(), 1); assert!(history.queries[0].speculative_fibers.is_empty()); - assert!(matches!( + assert_matches!( history.queries[0].non_speculative_fiber.attempts[0].result, Some(AttemptResult::Success(_)) - )); + ); let displayed = "Queries History: === Query #0 === diff --git a/scylla/src/transport/query_result.rs b/scylla/src/transport/query_result.rs index 98be873db4..e368a11745 100644 --- a/scylla/src/transport/query_result.rs +++ b/scylla/src/transport/query_result.rs @@ -267,6 +267,8 @@ mod tests { }; use std::convert::TryInto; + use assert_matches::assert_matches; + // Returns specified number of rows, each one containing one int32 value. // Values are 0, 1, 2, 3, 4, ... fn make_rows(rows_num: usize) -> Vec { @@ -483,10 +485,10 @@ mod tests { Ok((0,)) ); - assert!(matches!( + assert_matches!( make_string_rows_query_result(2).first_row_typed::<(i32,)>(), Err(FirstRowTypedError::FromRowError(_)) - )); + ); } #[test] @@ -539,10 +541,10 @@ mod tests { Ok(Some((0,))) ); - assert!(matches!( + assert_matches!( make_string_rows_query_result(1).maybe_first_row_typed::<(i32,)>(), Err(MaybeFirstRowTypedError::FromRowError(_)) - )) + ) } #[test] @@ -594,9 +596,9 @@ mod tests { Err(SingleRowTypedError::BadNumberOfRows(3)) ); - assert!(matches!( + assert_matches!( make_string_rows_query_result(1).single_row_typed::<(i32,)>(), Err(SingleRowTypedError::FromRowError(_)) - )); + ); } }