1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
mod derive_macros_integration {
mod value {
use bytes::Bytes;
use crate::deserialize::value::tests::{deserialize, udt_def_with_fields};
use crate::frame::response::result::{ColumnType, NativeType};
use crate::serialize::value::tests::do_serialize;
#[test]
fn derive_serialize_and_deserialize_value_loose_ordering() {
#[derive(
scylla_macros::DeserializeValue, scylla_macros::SerializeValue, PartialEq, Eq, Debug,
)]
#[scylla(crate = "crate")]
struct Udt<'a> {
a: &'a str,
#[scylla(skip)]
x: String,
#[scylla(allow_missing)]
b: Option<i32>,
#[scylla(default_when_null)]
c: i64,
}
let original_udt = Udt {
a: "The quick brown fox",
x: String::from("THIS SHOULD NOT BE (DE)SERIALIZED"),
b: Some(42),
c: 2137,
};
let tests = [
// All fields present
(
udt_def_with_fields([
("a", ColumnType::Native(NativeType::Text)),
("b", ColumnType::Native(NativeType::Int)),
("c", ColumnType::Native(NativeType::BigInt)),
]),
Udt {
x: String::new(),
..original_udt
},
),
//
// One field missing:
// - ignored during serialization,
// - default-initialized during deserialization.
(
udt_def_with_fields([
("a", ColumnType::Native(NativeType::Text)),
("c", ColumnType::Native(NativeType::BigInt)),
]),
Udt {
x: String::new(),
b: None,
..original_udt
},
),
//
// UDT fields switched - should still work.
(
udt_def_with_fields([
("b", ColumnType::Native(NativeType::Int)),
("a", ColumnType::Native(NativeType::Text)),
("c", ColumnType::Native(NativeType::BigInt)),
]),
Udt {
x: String::new(),
..original_udt
},
),
];
for (typ, expected_udt) in tests {
let serialized_udt = Bytes::from(do_serialize(&original_udt, &typ));
let deserialized_udt = deserialize::<Udt<'_>>(&typ, &serialized_udt).unwrap();
assert_eq!(deserialized_udt, expected_udt);
}
}
#[test]
fn derive_serialize_and_deserialize_value_strict_ordering() {
#[derive(
scylla_macros::DeserializeValue, scylla_macros::SerializeValue, PartialEq, Eq, Debug,
)]
#[scylla(crate = "crate", flavor = "enforce_order")]
struct Udt<'a> {
#[scylla(allow_missing)]
#[scylla(default_when_null)]
a: &'a str,
#[scylla(skip)]
x: String,
#[scylla(allow_missing)]
b: Option<i32>,
}
let original_udt = Udt {
a: "The quick brown fox",
x: String::from("THIS SHOULD NOT BE (DE)SERIALIZED"),
b: Some(42),
};
let tests = [
// All fields present
(
udt_def_with_fields([
("a", ColumnType::Native(NativeType::Text)),
("b", ColumnType::Native(NativeType::Int)),
]),
Udt {
x: String::new(),
..original_udt
},
),
//
// An excess field at the end of UDT
(
udt_def_with_fields([
("a", ColumnType::Native(NativeType::Text)),
("b", ColumnType::Native(NativeType::Int)),
("d", ColumnType::Native(NativeType::Boolean)),
]),
Udt {
x: String::new(),
..original_udt
},
),
//
// Missing non-required fields
(
udt_def_with_fields([("a", ColumnType::Native(NativeType::Text))]),
Udt {
x: String::new(),
b: None,
..original_udt
},
),
//
// An excess field at the end of UDT instead of non-required fields
(
udt_def_with_fields([("d", ColumnType::Native(NativeType::Boolean))]),
Udt {
x: String::new(),
a: "",
b: None,
},
),
];
for (typ, expected_udt) in tests {
let serialized_udt = Bytes::from(do_serialize(&original_udt, &typ));
let deserialized_udt = deserialize::<Udt<'_>>(&typ, &serialized_udt).unwrap();
assert_eq!(deserialized_udt, expected_udt);
}
}
#[test]
fn derive_ser_de_with_metadata_present_but_value_missing_during_deser() {
#[derive(scylla_macros::SerializeValue, Debug)]
#[scylla(crate = "crate")]
struct SerUdt<'a> {
a: &'a str,
}
#[derive(scylla_macros::DeserializeValue, Debug, PartialEq, Eq)]
#[scylla(crate = "crate")]
struct DeserUdt<'a> {
a: &'a str,
b: Option<i32>,
}
let ser_udt = SerUdt {
a: "The quick brown fox",
};
// Do not serialize `b` field.
let serialization_type =
udt_def_with_fields([("a", ColumnType::Native(NativeType::Text))]);
let expected_deserialized_udt = DeserUdt {
a: "The quick brown fox",
b: None,
};
// Deserialize `b` field. The metadata is present, but value is missing (i.e. `UdtIterator::next()` returns Ok(None)).
// This is possible, as specified by https://github.com/apache/cassandra/blob/4a80daf32eb4226d9870b914779a1fc007479da6/doc/native_protocol_v4.spec#L1003.
// It should fallback to null (b: None).
let deserialization_type = udt_def_with_fields([
("a", ColumnType::Native(NativeType::Text)),
("b", ColumnType::Native(NativeType::Int)),
]);
let serialized_udt = Bytes::from(do_serialize(&ser_udt, &serialization_type));
let deserialized_udt =
deserialize::<DeserUdt<'_>>(&deserialization_type, &serialized_udt).unwrap();
assert_eq!(deserialized_udt, expected_deserialized_udt);
}
}
mod row {
use bytes::Bytes;
use crate::deserialize::row::tests::deserialize;
use crate::deserialize::tests::spec;
use crate::frame::response::result::{ColumnType, NativeType};
use crate::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::Native(NativeType::Text)),
spec("b", ColumnType::Native(NativeType::Int)),
spec("c", ColumnType::Native(NativeType::BigInt)),
][..],
MyRow {
x: String::new(),
..original_row
},
),
//
// Columns switched - should still work.
(
&[
spec("b", ColumnType::Native(NativeType::Int)),
spec("a", ColumnType::Native(NativeType::Text)),
spec("c", ColumnType::Native(NativeType::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::Native(NativeType::Text)),
spec("b", ColumnType::Native(NativeType::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);
}
}
}
}