Skip to content

Commit cdcbad2

Browse files
committed
macros: add tests for {Ser,De}Row macros integration
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.
1 parent d553f94 commit cdcbad2

File tree

5 files changed

+108
-6
lines changed

5 files changed

+108
-6
lines changed

scylla-cql/src/types/deserialize/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ macro_rules! make_error_replace_rust_name {
323323
use make_error_replace_rust_name;
324324

325325
#[cfg(test)]
326-
mod tests {
326+
pub(crate) mod tests {
327327
use bytes::{Bytes, BytesMut};
328328

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

345-
pub(super) const fn spec<'a>(name: &'a str, typ: ColumnType<'a>) -> ColumnSpec<'a> {
345+
pub(crate) const fn spec<'a>(name: &'a str, typ: ColumnType<'a>) -> ColumnSpec<'a> {
346346
ColumnSpec::borrowed(name, typ, TableSpec::borrowed("ks", "tbl"))
347347
}
348348
}

scylla-cql/src/types/deserialize/row.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl Display for BuiltinDeserializationErrorKind {
496496

497497
#[cfg(test)]
498498
#[path = "row_tests.rs"]
499-
mod tests;
499+
pub(crate) mod tests;
500500

501501
/// ```compile_fail
502502
///

scylla-cql/src/types/deserialize/row_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ fn val_str(s: &str) -> Option<Vec<u8>> {
251251
Some(s.as_bytes().to_vec())
252252
}
253253

254-
fn deserialize<'frame, 'metadata, R>(
254+
pub(crate) fn deserialize<'frame, 'metadata, R>(
255255
specs: &'metadata [ColumnSpec<'metadata>],
256256
byts: &'frame Bytes,
257257
) -> Result<R, DeserializationError>

scylla-cql/src/types/serialize/row.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ mod doctests {
915915
}
916916

917917
#[cfg(test)]
918-
mod tests {
918+
pub(crate) mod tests {
919919
use std::borrow::Cow;
920920
use std::collections::BTreeMap;
921921

@@ -1037,7 +1037,7 @@ mod tests {
10371037
assert_eq!(typed_data, erased_data);
10381038
}
10391039

1040-
fn do_serialize<T: SerializeRow>(t: T, columns: &[ColumnSpec]) -> Vec<u8> {
1040+
pub(crate) fn do_serialize<T: SerializeRow>(t: T, columns: &[ColumnSpec]) -> Vec<u8> {
10411041
let ctx = RowSerializationContext { columns };
10421042
let mut ret = Vec::new();
10431043
let mut builder = RowWriter::new(&mut ret);

scylla-cql/src/types/types_tests.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,106 @@ mod derive_macros_integration {
149149
}
150150
}
151151
}
152+
153+
mod row {
154+
use bytes::Bytes;
155+
156+
use crate::frame::response::result::ColumnType;
157+
use crate::types::deserialize::row::tests::deserialize;
158+
use crate::types::deserialize::tests::spec;
159+
use crate::types::serialize::row::tests::do_serialize;
160+
161+
#[test]
162+
fn derive_serialize_and_deserialize_row_loose_ordering() {
163+
#[derive(
164+
scylla_macros::DeserializeRow, scylla_macros::SerializeRow, PartialEq, Eq, Debug,
165+
)]
166+
#[scylla(crate = "crate")]
167+
struct MyRow<'a> {
168+
a: &'a str,
169+
#[scylla(skip)]
170+
x: String,
171+
b: Option<i32>,
172+
c: i64,
173+
}
174+
175+
let original_row = MyRow {
176+
a: "The quick brown fox",
177+
x: String::from("THIS SHOULD NOT BE (DE)SERIALIZED"),
178+
b: Some(42),
179+
c: 2137,
180+
};
181+
182+
let tests = [
183+
// All columns present
184+
(
185+
&[
186+
spec("a", ColumnType::Text),
187+
spec("b", ColumnType::Int),
188+
spec("c", ColumnType::BigInt),
189+
][..],
190+
MyRow {
191+
x: String::new(),
192+
..original_row
193+
},
194+
),
195+
//
196+
// Columns switched - should still work.
197+
(
198+
&[
199+
spec("b", ColumnType::Int),
200+
spec("a", ColumnType::Text),
201+
spec("c", ColumnType::BigInt),
202+
],
203+
MyRow {
204+
x: String::new(),
205+
..original_row
206+
},
207+
),
208+
];
209+
for (typ, expected_row) in tests {
210+
let serialized_row = Bytes::from(do_serialize(&original_row, typ));
211+
let deserialized_row = deserialize::<MyRow<'_>>(typ, &serialized_row).unwrap();
212+
213+
assert_eq!(deserialized_row, expected_row);
214+
}
215+
}
216+
217+
#[test]
218+
fn derive_serialize_and_deserialize_row_strict_ordering() {
219+
#[derive(
220+
scylla_macros::DeserializeRow, scylla_macros::SerializeRow, PartialEq, Eq, Debug,
221+
)]
222+
#[scylla(crate = "crate", flavor = "enforce_order")]
223+
struct MyRow<'a> {
224+
a: &'a str,
225+
#[scylla(skip)]
226+
x: String,
227+
b: Option<i32>,
228+
}
229+
230+
let original_row = MyRow {
231+
a: "The quick brown fox",
232+
x: String::from("THIS SHOULD NOT BE (DE)SERIALIZED"),
233+
b: Some(42),
234+
};
235+
236+
let tests = [
237+
// All columns present
238+
(
239+
&[spec("a", ColumnType::Text), spec("b", ColumnType::Int)][..],
240+
MyRow {
241+
x: String::new(),
242+
..original_row
243+
},
244+
),
245+
];
246+
for (typ, expected_row) in tests {
247+
let serialized_row = Bytes::from(do_serialize(&original_row, typ));
248+
let deserialized_row = deserialize::<MyRow<'_>>(typ, &serialized_row).unwrap();
249+
250+
assert_eq!(deserialized_row, expected_row);
251+
}
252+
}
253+
}
152254
}

0 commit comments

Comments
 (0)