Skip to content

Commit

Permalink
macros: add tests for {Ser,De}Row macros integration
Browse files Browse the repository at this point in the history
Even though these macros are less complex and feature fewer switches and
flavors than their {Ser,De}Value counterparts, tests are added that
verify that when a row struct is derived both SerializeRow and
DeserializeRow:
- attributes are parsed correctly and trait impls are generated without
  errors,
- after serialization and deserialization, the result is what is
  expected.
  • Loading branch information
wprzytula committed Nov 13, 2024
1 parent 4f2649d commit 3711afc
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
4 changes: 2 additions & 2 deletions scylla-cql/src/types/deserialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ macro_rules! make_error_replace_rust_name {
use make_error_replace_rust_name;

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use bytes::{Bytes, BytesMut};

use crate::frame::response::result::{ColumnSpec, ColumnType, TableSpec};
Expand All @@ -342,7 +342,7 @@ mod tests {
bytes.freeze()
}

pub(super) const fn spec<'a>(name: &'a str, typ: ColumnType<'a>) -> ColumnSpec<'a> {
pub(crate) const fn spec<'a>(name: &'a str, typ: ColumnType<'a>) -> ColumnSpec<'a> {
ColumnSpec::borrowed(name, typ, TableSpec::borrowed("ks", "tbl"))
}
}
2 changes: 1 addition & 1 deletion scylla-cql/src/types/deserialize/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ impl Display for BuiltinDeserializationErrorKind {

#[cfg(test)]
#[path = "row_tests.rs"]
mod tests;
pub(crate) mod tests;

/// ```compile_fail
///
Expand Down
2 changes: 1 addition & 1 deletion scylla-cql/src/types/deserialize/row_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ fn val_str(s: &str) -> Option<Vec<u8>> {
Some(s.as_bytes().to_vec())
}

fn deserialize<'frame, 'metadata, R>(
pub(crate) fn deserialize<'frame, 'metadata, R>(
specs: &'metadata [ColumnSpec<'metadata>],
byts: &'frame Bytes,
) -> Result<R, DeserializationError>
Expand Down
4 changes: 2 additions & 2 deletions scylla-cql/src/types/serialize/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ mod doctests {
}

#[cfg(test)]
mod tests {
pub(crate) mod tests {
use std::borrow::Cow;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -1037,7 +1037,7 @@ mod tests {
assert_eq!(typed_data, erased_data);
}

fn do_serialize<T: SerializeRow>(t: T, columns: &[ColumnSpec]) -> Vec<u8> {
pub(crate) fn do_serialize<T: SerializeRow>(t: T, columns: &[ColumnSpec]) -> Vec<u8> {
let ctx = RowSerializationContext { columns };
let mut ret = Vec::new();
let mut builder = RowWriter::new(&mut ret);
Expand Down
102 changes: 102 additions & 0 deletions scylla-cql/src/types/types_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,106 @@ mod derive_macros_integration {
}
}
}

mod row {
use bytes::Bytes;

use crate::frame::response::result::ColumnType;
use crate::types::deserialize::row::tests::deserialize;
use crate::types::deserialize::tests::spec;
use crate::types::serialize::row::tests::do_serialize;

#[test]
fn derive_serialize_and_deserialize_row_loose_ordering() {
#[derive(
scylla_macros::DeserializeRow, scylla_macros::SerializeRow, PartialEq, Eq, Debug,
)]
#[scylla(crate = "crate")]
struct MyRow<'a> {
a: &'a str,
#[scylla(skip)]
x: String,
b: Option<i32>,
c: i64,
}

let original_row = MyRow {
a: "The quick brown fox",
x: String::from("THIS SHOULD NOT BE (DE)SERIALIZED"),
b: Some(42),
c: 2137,
};

let tests = [
// All columns present
(
&[
spec("a", ColumnType::Text),
spec("b", ColumnType::Int),
spec("c", ColumnType::BigInt),
][..],
MyRow {
x: String::new(),
..original_row
},
),
//
// Columns switched - should still work.
(
&[
spec("b", ColumnType::Int),
spec("a", ColumnType::Text),
spec("c", ColumnType::BigInt),
],
MyRow {
x: String::new(),
..original_row
},
),
];
for (typ, expected_row) in tests {
let serialized_row = Bytes::from(do_serialize(&original_row, typ));
let deserialized_row = deserialize::<MyRow<'_>>(typ, &serialized_row).unwrap();

assert_eq!(deserialized_row, expected_row);
}
}

#[test]
fn derive_serialize_and_deserialize_row_strict_ordering() {
#[derive(
scylla_macros::DeserializeRow, scylla_macros::SerializeRow, PartialEq, Eq, Debug,
)]
#[scylla(crate = "crate", flavor = "enforce_order")]
struct MyRow<'a> {
a: &'a str,
#[scylla(skip)]
x: String,
b: Option<i32>,
}

let original_row = MyRow {
a: "The quick brown fox",
x: String::from("THIS SHOULD NOT BE (DE)SERIALIZED"),
b: Some(42),
};

let tests = [
// All columns present
(
&[spec("a", ColumnType::Text), spec("b", ColumnType::Int)][..],
MyRow {
x: String::new(),
..original_row
},
),
];
for (typ, expected_row) in tests {
let serialized_row = Bytes::from(do_serialize(&original_row, typ));
let deserialized_row = deserialize::<MyRow<'_>>(typ, &serialized_row).unwrap();

assert_eq!(deserialized_row, expected_row);
}
}
}
}

0 comments on commit 3711afc

Please sign in to comment.