Skip to content

Commit

Permalink
DeserializeValue: allow_missing tests
Browse files Browse the repository at this point in the history
New tests are added for both flavours that show how the attribute
allows for successful type check and deserialization when the
corresponding field is missing from the CQL UDT definition.

Co-authored-by: Piotr Dulikowski <[email protected]>
  • Loading branch information
wprzytula and piodul committed Jun 27, 2024
1 parent debb600 commit cef8406
Showing 1 changed file with 64 additions and 14 deletions.
78 changes: 64 additions & 14 deletions scylla-cql/src/types/deserialize/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2480,6 +2480,7 @@ pub(super) mod tests {
a: &'a str,
#[scylla(skip)]
x: String,
#[scylla(allow_missing)]
b: Option<i32>,
c: i64,
}
Expand Down Expand Up @@ -2555,6 +2556,7 @@ pub(super) mod tests {
a: &'a str,
#[scylla(skip)]
x: String,
#[scylla(allow_missing)]
b: Option<i32>,
}

Expand Down Expand Up @@ -2620,6 +2622,22 @@ pub(super) mod tests {
let typ = udt_def_with_fields([("b", ColumnType::Int)]);
Udt::type_check(&typ).unwrap_err();
}

// Missing non-required column
{
let udt = UdtSerializer::new().field(b"kotmaale").finalize();
let typ = udt_def_with_fields([("a", ColumnType::Text)]);

let udt = deserialize::<Udt<'_>>(&typ, &udt).unwrap();
assert_eq!(
udt,
Udt {
a: "kotmaale",
x: String::new(),
b: None,
}
);
}
}

#[test]
Expand Down Expand Up @@ -3285,6 +3303,7 @@ pub(super) mod tests {
a: &'a str,
#[scylla(skip)]
x: String,
#[scylla(allow_missing)]
b: Option<i32>,
c: bool,
}
Expand Down Expand Up @@ -3321,7 +3340,7 @@ pub(super) mod tests {
else {
panic!("unexpected error kind: {:?}", err.kind)
};
assert_eq!(missing_fields.as_slice(), &["a", "b"]);
assert_eq!(missing_fields.as_slice(), &["a"]);
}

// excess fields in UDT
Expand Down Expand Up @@ -3407,6 +3426,43 @@ pub(super) mod tests {
assert_eq!(err.cql_type, typ);
assert_matches!(err.kind, BuiltinDeserializationErrorKind::ExpectedNonNull);
}

// UDT field deserialization failed
{
let typ =
udt_def_with_fields([("a", ColumnType::Ascii), ("c", ColumnType::Boolean)]);

let udt_bytes = UdtSerializer::new()
.field("alamakota".as_bytes())
.field(&42_i16.to_be_bytes())
.finalize();

let err = deserialize::<Udt>(&typ, &udt_bytes).unwrap_err();

let err = get_deser_err(&err);
assert_eq!(err.rust_name, std::any::type_name::<Udt>());
assert_eq!(err.cql_type, typ);
let BuiltinDeserializationErrorKind::UdtError(
UdtDeserializationErrorKind::FieldDeserializationFailed {
ref field_name,
ref err,
},
) = err.kind
else {
panic!("unexpected error kind: {:?}", err.kind)
};
assert_eq!(field_name.as_str(), "c");
let err = get_deser_err(err);
assert_eq!(err.rust_name, std::any::type_name::<bool>());
assert_eq!(err.cql_type, ColumnType::Boolean);
assert_matches!(
err.kind,
BuiltinDeserializationErrorKind::ByteLengthMismatch {
expected: 1,
got: 2,
}
);
}
}
}

Expand All @@ -3419,6 +3475,7 @@ pub(super) mod tests {
#[scylla(skip)]
x: String,
b: Option<i32>,
#[scylla(allow_missing)]
c: bool,
}

Expand Down Expand Up @@ -3453,7 +3510,7 @@ pub(super) mod tests {
else {
panic!("unexpected error kind: {:?}", err.kind)
};
assert_eq!(required_fields.as_slice(), &["a", "b", "c"]);
assert_eq!(required_fields.as_slice(), &["a", "b"]);
assert_eq!(present_fields.as_slice(), &["a".to_string()]);
}

Expand All @@ -3462,8 +3519,7 @@ pub(super) mod tests {
let typ = udt_def_with_fields([
("a", ColumnType::Text),
("b", ColumnType::Int),
("c", ColumnType::Boolean),
("d", ColumnType::Counter),
("d", ColumnType::Boolean),
]);
let err = Udt::type_check(&typ).unwrap_err();
let err = get_typeck_err_inner(err.0.as_ref());
Expand All @@ -3480,11 +3536,8 @@ pub(super) mod tests {

// UDT fields switched - field name mismatch
{
let typ = udt_def_with_fields([
("b", ColumnType::Int),
("a", ColumnType::Text),
("c", ColumnType::Boolean),
]);
let typ =
udt_def_with_fields([("b", ColumnType::Int), ("a", ColumnType::Text)]);
let err = Udt::type_check(&typ).unwrap_err();
let err = get_typeck_err_inner(err.0.as_ref());
assert_eq!(err.rust_name, std::any::type_name::<Udt>());
Expand All @@ -3506,11 +3559,8 @@ pub(super) mod tests {

// UDT fields incompatible types - field type check failed
{
let typ = udt_def_with_fields([
("a", ColumnType::Blob),
("b", ColumnType::Int),
("c", ColumnType::Boolean),
]);
let typ =
udt_def_with_fields([("a", ColumnType::Blob), ("b", ColumnType::Int)]);
let err = Udt::type_check(&typ).unwrap_err();
let err = get_typeck_err_inner(err.0.as_ref());
assert_eq!(err.rust_name, std::any::type_name::<Udt>());
Expand Down

0 comments on commit cef8406

Please sign in to comment.