|
1 | 1 | #![allow(clippy::blacklisted_name)] |
2 | 2 |
|
3 | | -use bson::{bson, doc, Bson, Decoder, Encoder}; |
| 3 | +use bson::{bson, doc, spec::BinarySubtype, Bson, Decoder, Encoder}; |
4 | 4 | use serde::{Deserialize, Serialize}; |
5 | 5 | use serde_derive::{Deserialize, Serialize}; |
6 | 6 |
|
@@ -140,6 +140,48 @@ fn test_compat_u2f() { |
140 | 140 | assert_eq!(de_foo, foo); |
141 | 141 | } |
142 | 142 |
|
| 143 | +#[test] |
| 144 | +fn test_binary_generic_roundtrip() { |
| 145 | + #[derive(Serialize, Deserialize, Debug, PartialEq)] |
| 146 | + pub struct Foo { |
| 147 | + data: Bson, |
| 148 | + } |
| 149 | + |
| 150 | + let x = Foo { |
| 151 | + data: Bson::Binary(BinarySubtype::Generic, b"12345abcde".to_vec()), |
| 152 | + }; |
| 153 | + |
| 154 | + let b = bson::to_bson(&x).unwrap(); |
| 155 | + assert_eq!( |
| 156 | + b.as_document().unwrap(), |
| 157 | + &doc! {"data": Bson::Binary(BinarySubtype::Generic, b"12345abcde".to_vec())} |
| 158 | + ); |
| 159 | + |
| 160 | + let f = bson::from_bson::<Foo>(b).unwrap(); |
| 161 | + assert_eq!(x, f); |
| 162 | +} |
| 163 | + |
| 164 | +#[test] |
| 165 | +fn test_binary_non_generic_roundtrip() { |
| 166 | + #[derive(Serialize, Deserialize, Debug, PartialEq)] |
| 167 | + pub struct Foo { |
| 168 | + data: Bson, |
| 169 | + } |
| 170 | + |
| 171 | + let x = Foo { |
| 172 | + data: Bson::Binary(BinarySubtype::BinaryOld, b"12345abcde".to_vec()), |
| 173 | + }; |
| 174 | + |
| 175 | + let b = bson::to_bson(&x).unwrap(); |
| 176 | + assert_eq!( |
| 177 | + b.as_document().unwrap(), |
| 178 | + &doc! {"data": Bson::Binary(BinarySubtype::BinaryOld, b"12345abcde".to_vec())} |
| 179 | + ); |
| 180 | + |
| 181 | + let f = bson::from_bson::<Foo>(b).unwrap(); |
| 182 | + assert_eq!(x, f); |
| 183 | +} |
| 184 | + |
143 | 185 | #[test] |
144 | 186 | fn test_byte_vec() { |
145 | 187 | #[derive(Serialize, Debug, Eq, PartialEq)] |
|
0 commit comments