Debian Codegraph @

src/uuid/test.rs

raw
1 use crate::{
2 from_document,
3 from_slice,
4 spec::BinarySubtype,
5 uuid::{Uuid, UuidRepresentation},
6 Binary,
7 Bson,
8 Document,
9 };
10 use serde::{Deserialize, Serialize};
11 use serde_json::json;
12
13 #[derive(Debug, Serialize, Deserialize, PartialEq)]
14 struct U {
15 uuid: Uuid,
16 }
17
18 #[test]
19 fn into_bson() {
20 let uuid = Uuid::new();
21
22 let bson: Bson = uuid.into();
23 let binary = Binary {
24 bytes: uuid.bytes().to_vec(),
25 subtype: BinarySubtype::Uuid,
26 };
27
28 assert_eq!(bson, Bson::Binary(binary));
29 }
30
31 #[test]
32 fn raw_serialization() {
33 let u = U { uuid: Uuid::new() };
34 let bytes = crate::to_vec(&u).unwrap();
35
36 let doc: Document = crate::from_slice(bytes.as_slice()).unwrap();
37 assert_eq!(doc, doc! { "uuid": u.uuid });
38
39 let u_roundtrip: U = crate::from_slice(bytes.as_slice()).unwrap();
40 assert_eq!(u_roundtrip, u);
41 }
42
43 #[test]
44 fn bson_serialization() {
45 let u = U { uuid: Uuid::new() };
46 let correct = doc! {
47 "uuid": Binary {
48 bytes: u.uuid.bytes().to_vec(),
49 subtype: BinarySubtype::Uuid
50 }
51 };
52
53 assert_eq!(doc! { "uuid": u.uuid }, correct);
54
55 let doc = crate::to_document(&u).unwrap();
56 assert_eq!(doc, correct);
57
58 let u_roundtrip: U = crate::from_document(doc).unwrap();
59 assert_eq!(u_roundtrip, u);
60 }
61
62 #[test]
63 fn json() {
64 let u = U { uuid: Uuid::new() };
65
66 let json = serde_json::to_value(&u).unwrap();
67 assert_eq!(json, json!({ "uuid": u.uuid.to_string() }));
68
69 let u_roundtrip_json: U = serde_json::from_value(json).unwrap();
70 assert_eq!(u_roundtrip_json, u);
71 }
72
73 #[test]
74 fn wrong_subtype() {
75 let generic = doc! {
76 "uuid": Binary {
77 bytes: Uuid::new().bytes().to_vec(),
78 subtype: BinarySubtype::Generic
79 }
80 };
81 crate::from_document::<U>(generic.clone()).unwrap_err();
82 let generic_bytes = crate::to_vec(&generic).unwrap();
83 crate::from_slice::<U>(&generic_bytes).unwrap_err();
84
85 let old = doc! {
86 "uuid": Binary {
87 bytes: Uuid::new().bytes().to_vec(),
88 subtype: BinarySubtype::UuidOld
89 }
90 };
91 crate::from_document::<U>(old.clone()).unwrap_err();
92 let old_bytes = crate::to_vec(&old).unwrap();
93 crate::from_slice::<U>(&old_bytes).unwrap_err();
94
95 let other = doc! {
96 "uuid": Binary {
97 bytes: Uuid::new().bytes().to_vec(),
98 subtype: BinarySubtype::UserDefined(100)
99 }
100 };
101 crate::from_document::<U>(other.clone()).unwrap_err();
102 let other_bytes = crate::to_vec(&other).unwrap();
103 crate::from_slice::<U>(&other_bytes).unwrap_err();
104 }
105
106 #[test]
107 fn test_binary_constructors() {
108 let uuid = crate::Uuid::parse_str("00112233445566778899AABBCCDDEEFF").unwrap();
109 let bin = Binary::from_uuid(uuid);
110 assert_eq!(bin.bytes, uuid.bytes());
111 assert_eq!(bin.subtype, BinarySubtype::Uuid);
112
113 let bin = Binary::from_uuid_with_representation(uuid, UuidRepresentation::Standard);
114 assert_eq!(bin.bytes, uuid.bytes());
115 assert_eq!(bin.subtype, BinarySubtype::Uuid);
116
117 let bin = Binary::from_uuid_with_representation(uuid, UuidRepresentation::JavaLegacy);
118 assert_eq!(
119 bin.bytes,
120 Uuid::parse_str("7766554433221100FFEEDDCCBBAA9988")
121 .unwrap()
122 .bytes()
123 );
124 assert_eq!(bin.subtype, BinarySubtype::UuidOld);
125
126 let bin = Binary::from_uuid_with_representation(uuid, UuidRepresentation::CSharpLegacy);
127 assert_eq!(
128 bin.bytes,
129 Uuid::parse_str("33221100554477668899AABBCCDDEEFF")
130 .unwrap()
131 .bytes()
132 );
133 assert_eq!(bin.subtype, BinarySubtype::UuidOld);
134
135 // Same byte ordering as standard representation
136 let bin = Binary::from_uuid_with_representation(uuid, UuidRepresentation::PythonLegacy);
137 assert_eq!(
138 bin.bytes,
139 Uuid::parse_str("00112233445566778899AABBCCDDEEFF")
140 .unwrap()
141 .bytes()
142 );
143 assert_eq!(bin.subtype, BinarySubtype::UuidOld);
144 }
145
146 #[test]
147 fn test_binary_to_uuid_standard_rep() {
148 let uuid = crate::Uuid::parse_str("00112233445566778899AABBCCDDEEFF").unwrap();
149 let bin = Binary::from_uuid(uuid);
150
151 assert_eq!(bin.to_uuid().unwrap(), uuid);
152 assert_eq!(
153 bin.to_uuid_with_representation(UuidRepresentation::Standard)
154 .unwrap(),
155 uuid
156 );
157
158 assert!(bin
159 .to_uuid_with_representation(UuidRepresentation::CSharpLegacy)
160 .is_err());
161 assert!(bin
162 .to_uuid_with_representation(UuidRepresentation::PythonLegacy)
163 .is_err());
164 assert!(bin
165 .to_uuid_with_representation(UuidRepresentation::PythonLegacy)
166 .is_err());
167 }
168
169 #[test]
170 fn test_binary_to_uuid_explicitly_standard_rep() {
171 let uuid = crate::Uuid::parse_str("00112233445566778899AABBCCDDEEFF").unwrap();
172 let bin = Binary::from_uuid_with_representation(uuid, UuidRepresentation::Standard);
173
174 assert_eq!(bin.to_uuid().unwrap(), uuid);
175 assert_eq!(
176 bin.to_uuid_with_representation(UuidRepresentation::Standard)
177 .unwrap(),
178 uuid
179 );
180
181 assert!(bin
182 .to_uuid_with_representation(UuidRepresentation::CSharpLegacy)
183 .is_err());
184 assert!(bin
185 .to_uuid_with_representation(UuidRepresentation::PythonLegacy)
186 .is_err());
187 assert!(bin
188 .to_uuid_with_representation(UuidRepresentation::PythonLegacy)
189 .is_err());
190 }
191
192 #[test]
193 fn test_binary_to_uuid_java_rep() {
194 let uuid = crate::Uuid::parse_str("00112233445566778899AABBCCDDEEFF").unwrap();
195 let bin = Binary::from_uuid_with_representation(uuid, UuidRepresentation::JavaLegacy);
196
197 assert!(bin.to_uuid().is_err());
198 assert!(bin
199 .to_uuid_with_representation(UuidRepresentation::Standard)
200 .is_err());
201
202 assert_eq!(
203 bin.to_uuid_with_representation(UuidRepresentation::JavaLegacy)
204 .unwrap(),
205 uuid
206 );
207 }
208
209 #[test]
210 fn test_binary_to_uuid_csharp_legacy_rep() {
211 let uuid = crate::Uuid::parse_str("00112233445566778899AABBCCDDEEFF").unwrap();
212 let bin = Binary::from_uuid_with_representation(uuid, UuidRepresentation::CSharpLegacy);
213
214 assert!(bin.to_uuid().is_err());
215 assert!(bin
216 .to_uuid_with_representation(UuidRepresentation::Standard)
217 .is_err());
218
219 assert_eq!(
220 bin.to_uuid_with_representation(UuidRepresentation::CSharpLegacy)
221 .unwrap(),
222 uuid
223 );
224 }
225
226 #[test]
227 fn test_binary_to_uuid_python_legacy_rep() {
228 let uuid = crate::Uuid::parse_str("00112233445566778899AABBCCDDEEFF").unwrap();
229 let bin = Binary::from_uuid_with_representation(uuid, UuidRepresentation::PythonLegacy);
230
231 assert!(bin.to_uuid().is_err());
232 assert!(bin
233 .to_uuid_with_representation(UuidRepresentation::Standard)
234 .is_err());
235
236 assert_eq!(
237 bin.to_uuid_with_representation(UuidRepresentation::PythonLegacy)
238 .unwrap(),
239 uuid
240 );
241 }
242
243 #[cfg(feature = "uuid-0_8")]
244 #[test]
245 fn interop_0_8() {
246 let uuid = crate::Uuid::new();
247 let uuid_uuid = uuid.to_uuid_0_8();
248 assert_eq!(uuid.to_string(), uuid_uuid.to_string());
249 assert_eq!(&uuid.bytes(), uuid_uuid.as_bytes());
250
251 let back: crate::Uuid = uuid_uuid.into();
252 assert_eq!(back, uuid);
253
254 let d_bson = doc! { "uuid": uuid };
255 let d_uuid = doc! { "uuid": uuid_uuid };
256 assert_eq!(d_bson, d_uuid);
257 }
258
259 #[cfg(feature = "uuid-1")]
260 #[test]
261 fn interop_1() {
262 let uuid = crate::Uuid::new();
263 let uuid_uuid = uuid.to_uuid_1();
264 assert_eq!(uuid.to_string(), uuid_uuid.to_string());
265 assert_eq!(&uuid.bytes(), uuid_uuid.as_bytes());
266
267 let back: crate::Uuid = uuid_uuid.into();
268 assert_eq!(back, uuid);
269
270 let d_bson = doc! { "uuid": uuid };
271 let d_uuid = doc! { "uuid": uuid_uuid };
272 assert_eq!(d_bson, d_uuid);
273 }
274
275 #[test]
276 fn deserialize_uuid_from_string() {
277 #[derive(Deserialize)]
278 struct UuidWrapper {
279 uuid: Uuid,
280 }
281
282 let uuid = Uuid::new();
283
284 let doc = doc! { "uuid": uuid.to_string() };
285 let wrapper: UuidWrapper = from_document(doc).expect("failed to deserialize document");
286 assert_eq!(wrapper.uuid, uuid);
287
288 let raw_doc = rawdoc! { "uuid": uuid.to_string() };
289 let wrapper: UuidWrapper =
290 from_slice(raw_doc.as_bytes()).expect("failed to deserialize raw document");
291 assert_eq!(wrapper.uuid, uuid);
292 }