Skip to content

Commit 3da897d

Browse files
authored
Merge pull request #1403 from spoorn/iss-1399
DeserializeRow: default_when_null attribute
2 parents 68dd529 + 1c85e58 commit 3da897d

File tree

5 files changed

+185
-38
lines changed

5 files changed

+185
-38
lines changed

scylla-cql/src/deserialize/row_tests.rs

Lines changed: 84 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,42 +101,64 @@ fn test_struct_deserialization_loose_ordering() {
101101
b: Option<i32>,
102102
#[scylla(skip)]
103103
c: String,
104+
#[scylla(default_when_null)]
105+
d: i32,
106+
#[scylla(default_when_null)]
107+
e: &'a str,
104108
}
105109

106110
// Original order of columns
107111
let specs = &[
108112
spec("a", ColumnType::Native(NativeType::Text)),
109113
spec("b", ColumnType::Native(NativeType::Int)),
114+
spec("d", ColumnType::Native(NativeType::Int)),
115+
spec("e", ColumnType::Native(NativeType::Text)),
110116
];
111-
let byts = serialize_cells([val_str("abc"), val_int(123)]);
117+
let byts = serialize_cells([val_str("abc"), val_int(123), None, val_str("def")]);
112118
let row = deserialize::<MyRow<'_>>(specs, &byts).unwrap();
113119
assert_eq!(
114120
row,
115121
MyRow {
116122
a: "abc",
117123
b: Some(123),
118124
c: String::new(),
125+
d: 0,
126+
e: "def",
119127
}
120128
);
121129

122130
// Different order of columns - should still work
123131
let specs = &[
132+
spec("e", ColumnType::Native(NativeType::Text)),
124133
spec("b", ColumnType::Native(NativeType::Int)),
134+
spec("d", ColumnType::Native(NativeType::Int)),
125135
spec("a", ColumnType::Native(NativeType::Text)),
126136
];
127-
let byts = serialize_cells([val_int(123), val_str("abc")]);
137+
let byts = serialize_cells([None, val_int(123), None, val_str("abc")]);
128138
let row = deserialize::<MyRow<'_>>(specs, &byts).unwrap();
129139
assert_eq!(
130140
row,
131141
MyRow {
132142
a: "abc",
133143
b: Some(123),
134144
c: String::new(),
145+
d: 0,
146+
e: "",
135147
}
136148
);
137149

138150
// Missing column
139-
let specs = &[spec("a", ColumnType::Native(NativeType::Text))];
151+
let specs = &[
152+
spec("a", ColumnType::Native(NativeType::Text)),
153+
spec("e", ColumnType::Native(NativeType::Text)),
154+
];
155+
MyRow::type_check(specs).unwrap_err();
156+
157+
// Missing both default_when_null column
158+
let specs = &[
159+
spec("a", ColumnType::Native(NativeType::Text)),
160+
spec("b", ColumnType::Native(NativeType::Int)),
161+
];
140162
MyRow::type_check(specs).unwrap_err();
141163

142164
// Wrong column type
@@ -156,33 +178,53 @@ fn test_struct_deserialization_strict_ordering() {
156178
b: Option<i32>,
157179
#[scylla(skip)]
158180
c: String,
181+
#[scylla(default_when_null)]
182+
d: i32,
183+
#[scylla(default_when_null)]
184+
e: &'a str,
159185
}
160186

161187
// Correct order of columns
162188
let specs = &[
163189
spec("a", ColumnType::Native(NativeType::Text)),
164190
spec("b", ColumnType::Native(NativeType::Int)),
191+
spec("d", ColumnType::Native(NativeType::Int)),
192+
spec("e", ColumnType::Native(NativeType::Text)),
165193
];
166-
let byts = serialize_cells([val_str("abc"), val_int(123)]);
194+
let byts = serialize_cells([val_str("abc"), val_int(123), None, val_str("def")]);
167195
let row = deserialize::<MyRow<'_>>(specs, &byts).unwrap();
168196
assert_eq!(
169197
row,
170198
MyRow {
171199
a: "abc",
172200
b: Some(123),
173201
c: String::new(),
202+
d: 0,
203+
e: "def",
174204
}
175205
);
176206

177207
// Wrong order of columns
178208
let specs = &[
179209
spec("b", ColumnType::Native(NativeType::Int)),
180210
spec("a", ColumnType::Native(NativeType::Text)),
211+
spec("d", ColumnType::Native(NativeType::Int)),
212+
spec("e", ColumnType::Native(NativeType::Text)),
181213
];
182214
MyRow::type_check(specs).unwrap_err();
183215

184216
// Missing column
185-
let specs = &[spec("a", ColumnType::Native(NativeType::Text))];
217+
let specs = &[
218+
spec("a", ColumnType::Native(NativeType::Text)),
219+
spec("e", ColumnType::Native(NativeType::Text)),
220+
];
221+
MyRow::type_check(specs).unwrap_err();
222+
223+
// Missing both default_when_null column
224+
let specs = &[
225+
spec("a", ColumnType::Native(NativeType::Text)),
226+
spec("b", ColumnType::Native(NativeType::Int)),
227+
];
186228
MyRow::type_check(specs).unwrap_err();
187229

188230
// Wrong column type
@@ -476,6 +518,8 @@ fn test_struct_deserialization_errors() {
476518
b: Option<i32>,
477519
#[scylla(rename = "c")]
478520
d: bool,
521+
#[scylla(default_when_null)]
522+
e: Option<i32>,
479523
}
480524

481525
// Type check errors
@@ -496,7 +540,7 @@ fn test_struct_deserialization_errors() {
496540
else {
497541
panic!("unexpected error kind: {:?}", err.kind)
498542
};
499-
assert_eq!(missing_fields.as_slice(), &["c"]);
543+
assert_eq!(missing_fields.as_slice(), &["c", "e"]);
500544
}
501545

502546
// Duplicated column
@@ -588,6 +632,7 @@ fn test_struct_deserialization_errors() {
588632
spec("c", ColumnType::Native(NativeType::Boolean)),
589633
spec("a", ColumnType::Native(NativeType::Blob)),
590634
spec("b", ColumnType::Native(NativeType::Int)),
635+
spec("e", ColumnType::Native(NativeType::Int)),
591636
];
592637

593638
let err = MyRow::deserialize(ColumnIterator::new(
@@ -615,13 +660,15 @@ fn test_struct_deserialization_errors() {
615660
spec("b", ColumnType::Native(NativeType::Int)),
616661
spec("a", ColumnType::Native(NativeType::Ascii)),
617662
spec("c", ColumnType::Native(NativeType::Boolean)),
663+
spec("e", ColumnType::Native(NativeType::Int)),
618664
];
619665

620666
let row_bytes = serialize_cells(
621667
[
622668
&0_i32.to_be_bytes(),
623669
"alamakota".as_bytes(),
624670
&42_i16.to_be_bytes(),
671+
&13_i32.to_be_bytes(),
625672
]
626673
.map(Some),
627674
);
@@ -664,6 +711,8 @@ fn test_struct_deserialization_errors() {
664711
x: String,
665712
b: Option<i32>,
666713
c: bool,
714+
#[scylla(default_when_null)]
715+
d: Option<i32>,
667716
}
668717

669718
// Type check errors
@@ -682,7 +731,7 @@ fn test_struct_deserialization_errors() {
682731
else {
683732
panic!("unexpected error kind: {:?}", err.kind)
684733
};
685-
assert_eq!(rust_cols, 3);
734+
assert_eq!(rust_cols, 4);
686735
assert_eq!(cql_cols, 1);
687736
}
688737

@@ -692,7 +741,8 @@ fn test_struct_deserialization_errors() {
692741
spec("a", ColumnType::Native(NativeType::Text)),
693742
spec("b", ColumnType::Native(NativeType::Int)),
694743
spec("c", ColumnType::Native(NativeType::Boolean)),
695-
spec("d", ColumnType::Native(NativeType::Counter)),
744+
spec("d", ColumnType::Native(NativeType::Int)),
745+
spec("e", ColumnType::Native(NativeType::Counter)),
696746
];
697747
let err = MyRow::type_check(&specs).unwrap_err();
698748
let err = get_typck_err_inner(err.0.as_ref());
@@ -705,16 +755,17 @@ fn test_struct_deserialization_errors() {
705755
else {
706756
panic!("unexpected error kind: {:?}", err.kind)
707757
};
708-
assert_eq!(rust_cols, 3);
709-
assert_eq!(cql_cols, 4);
758+
assert_eq!(rust_cols, 4);
759+
assert_eq!(cql_cols, 5);
710760
}
711761

712762
// Renamed column name mismatch
713763
{
714764
let specs = [
715765
spec("a", ColumnType::Native(NativeType::Text)),
716766
spec("b", ColumnType::Native(NativeType::Int)),
717-
spec("d", ColumnType::Native(NativeType::Boolean)),
767+
spec("e", ColumnType::Native(NativeType::Boolean)),
768+
spec("d", ColumnType::Native(NativeType::Int)),
718769
];
719770
let err = MyRow::type_check(&specs).unwrap_err();
720771
let err = get_typck_err_inner(err.0.as_ref());
@@ -731,7 +782,7 @@ fn test_struct_deserialization_errors() {
731782
assert_eq!(field_index, 3);
732783
assert_eq!(rust_column_name, "c");
733784
assert_eq!(column_index, 2);
734-
assert_eq!(db_column_name.as_str(), "d");
785+
assert_eq!(db_column_name.as_str(), "e");
735786
}
736787

737788
// Columns switched - column name mismatch
@@ -740,6 +791,7 @@ fn test_struct_deserialization_errors() {
740791
spec("b", ColumnType::Native(NativeType::Int)),
741792
spec("a", ColumnType::Native(NativeType::Text)),
742793
spec("c", ColumnType::Native(NativeType::Boolean)),
794+
spec("d", ColumnType::Native(NativeType::Int)),
743795
];
744796
let err = MyRow::type_check(&specs).unwrap_err();
745797
let err = get_typck_err_inner(err.0.as_ref());
@@ -766,6 +818,7 @@ fn test_struct_deserialization_errors() {
766818
spec("a", ColumnType::Native(NativeType::Blob)),
767819
spec("b", ColumnType::Native(NativeType::Int)),
768820
spec("c", ColumnType::Native(NativeType::Boolean)),
821+
spec("d", ColumnType::Native(NativeType::Int)),
769822
];
770823
let err = MyRow::type_check(&specs).unwrap_err();
771824
let err = get_typck_err_inner(err.0.as_ref());
@@ -804,6 +857,7 @@ fn test_struct_deserialization_errors() {
804857
spec("a", ColumnType::Native(NativeType::Text)),
805858
spec("b", ColumnType::Native(NativeType::Int)),
806859
spec("c", ColumnType::Native(NativeType::Boolean)),
860+
spec("d", ColumnType::Native(NativeType::Int)),
807861
];
808862

809863
let err = MyRow::deserialize(ColumnIterator::new(
@@ -831,10 +885,17 @@ fn test_struct_deserialization_errors() {
831885
spec("a", ColumnType::Native(NativeType::Text)),
832886
spec("b", ColumnType::Native(NativeType::Int)),
833887
spec("c", ColumnType::Native(NativeType::Boolean)),
888+
spec("d", ColumnType::Native(NativeType::Int)),
834889
];
835890

836891
let row_bytes = serialize_cells(
837-
[(&b"alamakota"[..]), &42_i32.to_be_bytes(), &[true as u8]].map(Some),
892+
[
893+
(&b"alamakota"[..]),
894+
&42_i32.to_be_bytes(),
895+
&[true as u8],
896+
&13_i32.to_be_bytes(),
897+
]
898+
.map(Some),
838899
);
839900

840901
let row_bytes_too_short = row_bytes.slice(..row_bytes.len() - 1);
@@ -852,8 +913,8 @@ fn test_struct_deserialization_errors() {
852913
else {
853914
panic!("unexpected error kind: {:?}", err.kind)
854915
};
855-
assert_eq!(column_index, 2);
856-
assert_eq!(column_name, "c");
916+
assert_eq!(column_index, 3);
917+
assert_eq!(column_name, "d");
857918
}
858919

859920
// Column deserialization failed
@@ -862,10 +923,17 @@ fn test_struct_deserialization_errors() {
862923
spec("a", ColumnType::Native(NativeType::Text)),
863924
spec("b", ColumnType::Native(NativeType::Int)),
864925
spec("c", ColumnType::Native(NativeType::Boolean)),
926+
spec("d", ColumnType::Native(NativeType::Int)),
865927
];
866928

867929
let row_bytes = serialize_cells(
868-
[&b"alamakota"[..], &42_i64.to_be_bytes(), &[true as u8]].map(Some),
930+
[
931+
&b"alamakota"[..],
932+
&42_i64.to_be_bytes(),
933+
&[true as u8],
934+
&13_i32.to_be_bytes(),
935+
]
936+
.map(Some),
869937
);
870938

871939
let err = deserialize::<MyRow>(&specs, &row_bytes).unwrap_err();

0 commit comments

Comments
 (0)