Skip to content

Commit

Permalink
DeserializeValue: default_when_null 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 deserialization when the field contains null and
the corresponding Rust type expects non-null value.
  • Loading branch information
wprzytula committed Jun 27, 2024
1 parent 9330d41 commit bc17dce
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions scylla-cql/src/types/deserialize/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,11 @@ pub(super) mod tests {
self
}

fn null_field(mut self) -> Self {
append_null(&mut self.buf);
self
}

fn finalize(&self) -> Bytes {
make_bytes(&self.buf)
}
Expand Down Expand Up @@ -2482,6 +2487,7 @@ pub(super) mod tests {
x: String,
#[scylla(allow_missing)]
b: Option<i32>,
#[scylla(default_when_null)]
c: i64,
}

Expand Down Expand Up @@ -2510,6 +2516,30 @@ pub(super) mod tests {
);
}

// The last two UDT field are missing in serialized form - it should treat it
// as if there were nulls at the end.
{
let udt = UdtSerializer::new()
.field("The quick brown fox".as_bytes())
.finalize();
let typ = udt_def_with_fields([
("a", ColumnType::Text),
("b", ColumnType::Int),
("c", ColumnType::BigInt),
]);

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

// UDT fields switched - should still work.
{
let udt = UdtSerializer::new()
Expand All @@ -2535,6 +2565,25 @@ pub(super) mod tests {
);
}

// Only field 'a' is present
{
let udt = UdtSerializer::new()
.field("The quick brown fox".as_bytes())
.finalize();
let typ = udt_def_with_fields([("a", ColumnType::Text), ("c", ColumnType::BigInt)]);

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

// Wrong column type
{
let typ = udt_def_with_fields([("a", ColumnType::Text)]);
Expand All @@ -2553,6 +2602,7 @@ pub(super) mod tests {
#[derive(scylla_macros::DeserializeValue, PartialEq, Eq, Debug)]
#[scylla(crate = "crate", enforce_order, forbid_excess_udt_fields)]
struct Udt<'a> {
#[scylla(default_when_null)]
a: &'a str,
#[scylla(skip)]
x: String,
Expand Down Expand Up @@ -2638,6 +2688,25 @@ pub(super) mod tests {
}
);
}

// The first field is null, but `default_when_null` prevents failure.
{
let udt = UdtSerializer::new()
.null_field()
.field(&42i32.to_be_bytes())
.finalize();
let typ = udt_def_with_fields([("a", ColumnType::Text), ("b", ColumnType::Int)]);

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

#[test]
Expand Down Expand Up @@ -3305,6 +3374,7 @@ pub(super) mod tests {
x: String,
#[scylla(allow_missing)]
b: Option<i32>,
#[scylla(default_when_null)]
c: bool,
}

Expand Down

0 comments on commit bc17dce

Please sign in to comment.