|
| 1 | +mod derive_macros_integration { |
| 2 | + mod value { |
| 3 | + use bytes::Bytes; |
| 4 | + |
| 5 | + use crate::frame::response::result::ColumnType; |
| 6 | + use crate::types::deserialize::value::tests::{deserialize, udt_def_with_fields}; |
| 7 | + use crate::types::serialize::value::tests::do_serialize; |
| 8 | + |
| 9 | + #[test] |
| 10 | + fn derive_serialize_and_deserialize_value_loose_ordering() { |
| 11 | + #[derive( |
| 12 | + scylla_macros::DeserializeValue, scylla_macros::SerializeValue, PartialEq, Eq, Debug, |
| 13 | + )] |
| 14 | + #[scylla(crate = "crate")] |
| 15 | + struct Udt<'a> { |
| 16 | + a: &'a str, |
| 17 | + #[scylla(skip)] |
| 18 | + x: String, |
| 19 | + #[scylla(allow_missing)] |
| 20 | + b: Option<i32>, |
| 21 | + #[scylla(default_when_null)] |
| 22 | + c: i64, |
| 23 | + } |
| 24 | + |
| 25 | + let original_udt = Udt { |
| 26 | + a: "The quick brown fox", |
| 27 | + x: String::from("THIS SHOULD NOT BE (DE)SERIALIZED"), |
| 28 | + b: Some(42), |
| 29 | + c: 2137, |
| 30 | + }; |
| 31 | + |
| 32 | + let tests = [ |
| 33 | + // All fields present |
| 34 | + ( |
| 35 | + udt_def_with_fields([ |
| 36 | + ("a", ColumnType::Text), |
| 37 | + ("b", ColumnType::Int), |
| 38 | + ("c", ColumnType::BigInt), |
| 39 | + ]), |
| 40 | + Udt { |
| 41 | + x: String::new(), |
| 42 | + ..original_udt |
| 43 | + }, |
| 44 | + ), |
| 45 | + // |
| 46 | + // One field missing: |
| 47 | + // - ignored during serialization, |
| 48 | + // - default-initialized during deserialization. |
| 49 | + ( |
| 50 | + udt_def_with_fields([("a", ColumnType::Text), ("c", ColumnType::BigInt)]), |
| 51 | + Udt { |
| 52 | + x: String::new(), |
| 53 | + b: None, |
| 54 | + ..original_udt |
| 55 | + }, |
| 56 | + ), |
| 57 | + // |
| 58 | + // UDT fields switched - should still work. |
| 59 | + ( |
| 60 | + udt_def_with_fields([ |
| 61 | + ("b", ColumnType::Int), |
| 62 | + ("a", ColumnType::Text), |
| 63 | + ("c", ColumnType::BigInt), |
| 64 | + ]), |
| 65 | + Udt { |
| 66 | + x: String::new(), |
| 67 | + ..original_udt |
| 68 | + }, |
| 69 | + ), |
| 70 | + ]; |
| 71 | + for (typ, expected_udt) in tests { |
| 72 | + let serialized_udt = Bytes::from(do_serialize(&original_udt, &typ)); |
| 73 | + let deserialized_udt = deserialize::<Udt<'_>>(&typ, &serialized_udt).unwrap(); |
| 74 | + |
| 75 | + assert_eq!(deserialized_udt, expected_udt); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn derive_serialize_and_deserialize_value_strict_ordering() { |
| 81 | + #[derive( |
| 82 | + scylla_macros::DeserializeValue, scylla_macros::SerializeValue, PartialEq, Eq, Debug, |
| 83 | + )] |
| 84 | + #[scylla(crate = "crate", flavor = "enforce_order")] |
| 85 | + struct Udt<'a> { |
| 86 | + #[scylla(allow_missing)] |
| 87 | + #[scylla(default_when_null)] |
| 88 | + a: &'a str, |
| 89 | + #[scylla(skip)] |
| 90 | + x: String, |
| 91 | + #[scylla(allow_missing)] |
| 92 | + b: Option<i32>, |
| 93 | + } |
| 94 | + |
| 95 | + let original_udt = Udt { |
| 96 | + a: "The quick brown fox", |
| 97 | + x: String::from("THIS SHOULD NOT BE (DE)SERIALIZED"), |
| 98 | + b: Some(42), |
| 99 | + }; |
| 100 | + |
| 101 | + let tests = [ |
| 102 | + // All fields present |
| 103 | + ( |
| 104 | + udt_def_with_fields([("a", ColumnType::Text), ("b", ColumnType::Int)]), |
| 105 | + Udt { |
| 106 | + x: String::new(), |
| 107 | + ..original_udt |
| 108 | + }, |
| 109 | + ), |
| 110 | + // |
| 111 | + // An excess field at the end of UDT |
| 112 | + ( |
| 113 | + udt_def_with_fields([ |
| 114 | + ("a", ColumnType::Text), |
| 115 | + ("b", ColumnType::Int), |
| 116 | + ("d", ColumnType::Boolean), |
| 117 | + ]), |
| 118 | + Udt { |
| 119 | + x: String::new(), |
| 120 | + ..original_udt |
| 121 | + }, |
| 122 | + ), |
| 123 | + // |
| 124 | + // Missing non-required fields |
| 125 | + ( |
| 126 | + udt_def_with_fields([("a", ColumnType::Text)]), |
| 127 | + Udt { |
| 128 | + x: String::new(), |
| 129 | + b: None, |
| 130 | + ..original_udt |
| 131 | + }, |
| 132 | + ), |
| 133 | + // |
| 134 | + // An excess field at the end of UDT instead of non-required fields |
| 135 | + ( |
| 136 | + udt_def_with_fields([("d", ColumnType::Boolean)]), |
| 137 | + Udt { |
| 138 | + x: String::new(), |
| 139 | + a: "", |
| 140 | + b: None, |
| 141 | + }, |
| 142 | + ), |
| 143 | + ]; |
| 144 | + for (typ, expected_udt) in tests { |
| 145 | + let serialized_udt = Bytes::from(do_serialize(&original_udt, &typ)); |
| 146 | + let deserialized_udt = deserialize::<Udt<'_>>(&typ, &serialized_udt).unwrap(); |
| 147 | + |
| 148 | + assert_eq!(deserialized_udt, expected_udt); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments