Debian Codegraph @

third_party/rust/serde_with/tests/chrono_0_4.rs

raw
1 //! Test Cases
2
3 extern crate alloc;
4
5 mod utils;
6
7 use crate::utils::{
8 check_deserialization, check_error_deserialization, check_serialization, is_equal,
9 };
10 use alloc::collections::BTreeMap;
11 use chrono_0_4::{DateTime, Duration, Local, NaiveDateTime, Utc};
12 use core::str::FromStr;
13 use expect_test::expect;
14 use serde::{Deserialize, Serialize};
15 use serde_with::{
16 formats::Flexible, serde_as, DurationMicroSeconds, DurationMicroSecondsWithFrac,
17 DurationMilliSeconds, DurationMilliSecondsWithFrac, DurationNanoSeconds,
18 DurationNanoSecondsWithFrac, DurationSeconds, DurationSecondsWithFrac, TimestampMicroSeconds,
19 TimestampMicroSecondsWithFrac, TimestampMilliSeconds, TimestampMilliSecondsWithFrac,
20 TimestampNanoSeconds, TimestampNanoSecondsWithFrac, TimestampSeconds, TimestampSecondsWithFrac,
21 };
22
23 fn new_datetime(secs: i64, nsecs: u32) -> DateTime<Utc> {
24 DateTime::from_timestamp(secs, nsecs).unwrap()
25 }
26
27 fn unix_epoch_local() -> DateTime<Local> {
28 DateTime::from_timestamp(0, 0)
29 .unwrap()
30 .with_timezone(&Local)
31 }
32
33 fn unix_epoch_naive() -> NaiveDateTime {
34 DateTime::from_timestamp(0, 0).unwrap().naive_utc()
35 }
36
37 #[test]
38 fn json_datetime_from_any_to_string_deserialization() {
39 #[derive(Debug, PartialEq, Deserialize)]
40 struct S(
41 #[serde(with = "serde_with::chrono_0_4::datetime_utc_ts_seconds_from_any")] DateTime<Utc>,
42 );
43
44 // just integers
45 check_deserialization(
46 vec![
47 S(new_datetime(1_478_563_200, 0)),
48 S(new_datetime(0, 0)),
49 S(new_datetime(-86000, 0)),
50 ],
51 r#"[
52 1478563200,
53 0,
54 -86000
55 ]"#,
56 );
57
58 // floats, shows precision errors in sub-second part
59 check_deserialization(
60 vec![
61 S(new_datetime(1_478_563_200, 122_999_906)),
62 S(new_datetime(0, 0)),
63 S(new_datetime(-86000, 998_999_999)),
64 ],
65 r#"[
66 1478563200.123,
67 0.000,
68 -86000.999
69 ]"#,
70 );
71
72 // string representation of floats
73 check_deserialization(
74 vec![
75 S(new_datetime(1_478_563_200, 123_000_000)),
76 S(new_datetime(0, 0)),
77 S(new_datetime(-86000, 999_000_000)),
78 ],
79 r#"[
80 "1478563200.123",
81 "0.000",
82 "-86000.999"
83 ]"#,
84 );
85 }
86
87 #[test]
88 fn test_chrono_naive_date_time() {
89 #[serde_as]
90 #[derive(Debug, Serialize, Deserialize, PartialEq)]
91 pub struct S(#[serde_as(as = "DateTime<Utc>")] NaiveDateTime);
92
93 is_equal(
94 S(NaiveDateTime::from_str("1994-11-05T08:15:30").unwrap()),
95 expect![[r#""1994-11-05T08:15:30Z""#]],
96 );
97 }
98
99 #[test]
100 fn test_chrono_option_naive_date_time() {
101 #[serde_as]
102 #[derive(Debug, Serialize, Deserialize, PartialEq)]
103 pub struct S(#[serde_as(as = "Option<DateTime<Utc>>")] Option<NaiveDateTime>);
104
105 is_equal(
106 S(NaiveDateTime::from_str("1994-11-05T08:15:30").ok()),
107 expect![[r#""1994-11-05T08:15:30Z""#]],
108 );
109 }
110
111 #[test]
112 fn test_chrono_vec_option_naive_date_time() {
113 #[serde_as]
114 #[derive(Debug, Serialize, Deserialize, PartialEq)]
115 pub struct S(#[serde_as(as = "Vec<Option<DateTime<Utc>>>")] Vec<Option<NaiveDateTime>>);
116
117 is_equal(
118 S(vec![
119 NaiveDateTime::from_str("1994-11-05T08:15:30").ok(),
120 NaiveDateTime::from_str("1994-11-05T08:15:31").ok(),
121 ]),
122 expect![[r#"
123 [
124 "1994-11-05T08:15:30Z",
125 "1994-11-05T08:15:31Z"
126 ]"#]],
127 );
128 }
129
130 #[test]
131 fn test_chrono_btreemap_naive_date_time() {
132 #[serde_as]
133 #[derive(Debug, Serialize, Deserialize, PartialEq)]
134 pub struct S(#[serde_as(as = "BTreeMap<_, DateTime<Utc>>")] BTreeMap<i32, NaiveDateTime>);
135
136 is_equal(
137 S(BTreeMap::from_iter(vec![
138 (1, NaiveDateTime::from_str("1994-11-05T08:15:30").unwrap()),
139 (2, NaiveDateTime::from_str("1994-11-05T08:15:31").unwrap()),
140 ])),
141 expect![[r#"
142 {
143 "1": "1994-11-05T08:15:30Z",
144 "2": "1994-11-05T08:15:31Z"
145 }"#]],
146 );
147 }
148
149 #[test]
150 fn test_chrono_duration_seconds() {
151 let zero = Duration::zero();
152 let one_second = Duration::seconds(1);
153 let half_second = Duration::nanoseconds(500_000_000);
154 let minus_one_second = zero - one_second;
155 let minus_half_second = zero - half_second;
156
157 #[serde_as]
158 #[derive(Debug, Serialize, Deserialize, PartialEq)]
159 struct StructIntStrict(#[serde_as(as = "DurationSeconds<i64>")] Duration);
160
161 is_equal(StructIntStrict(zero), expect![[r#"0"#]]);
162 is_equal(StructIntStrict(one_second), expect![[r#"1"#]]);
163 is_equal(StructIntStrict(minus_one_second), expect![[r#"-1"#]]);
164 check_serialization(StructIntStrict(half_second), expect![[r#"1"#]]);
165 check_serialization(StructIntStrict(minus_half_second), expect![[r#"-1"#]]);
166 check_error_deserialization::<StructIntStrict>(
167 r#""1""#,
168 expect![[r#"invalid type: string "1", expected i64 at line 1 column 3"#]],
169 );
170 check_error_deserialization::<StructIntStrict>(
171 r#"9223372036854775808"#,
172 expect![[
173 r#"invalid value: integer `9223372036854775808`, expected i64 at line 1 column 19"#
174 ]],
175 );
176
177 #[serde_as]
178 #[derive(Debug, Serialize, Deserialize, PartialEq)]
179 struct StructIntFlexible(#[serde_as(as = "DurationSeconds<i64, Flexible>")] Duration);
180
181 is_equal(StructIntFlexible(zero), expect![[r#"0"#]]);
182 is_equal(StructIntFlexible(one_second), expect![[r#"1"#]]);
183 check_serialization(StructIntFlexible(half_second), expect![[r#"1"#]]);
184 check_serialization(StructIntFlexible(minus_half_second), expect![[r#"-1"#]]);
185 check_deserialization(StructIntFlexible(half_second), r#""0.5""#);
186 check_deserialization(StructIntFlexible(minus_half_second), r#""-0.5""#);
187 check_deserialization(StructIntFlexible(one_second), r#""1""#);
188 check_deserialization(StructIntFlexible(minus_one_second), r#""-1""#);
189 check_deserialization(StructIntFlexible(zero), r#""0""#);
190 check_error_deserialization::<StructIntFlexible>(
191 r#""a""#,
192 expect![[
193 r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"#
194 ]],
195 );
196
197 #[serde_as]
198 #[derive(Debug, Serialize, Deserialize, PartialEq)]
199 struct Structf64Strict(#[serde_as(as = "DurationSeconds<f64>")] Duration);
200
201 is_equal(Structf64Strict(zero), expect![[r#"0.0"#]]);
202 is_equal(Structf64Strict(one_second), expect![[r#"1.0"#]]);
203 is_equal(Structf64Strict(minus_one_second), expect![[r#"-1.0"#]]);
204 check_serialization(Structf64Strict(half_second), expect![[r#"1.0"#]]);
205 check_serialization(Structf64Strict(minus_half_second), expect![[r#"-1.0"#]]);
206 check_deserialization(Structf64Strict(one_second), r#"0.5"#);
207 check_deserialization(Structf64Strict(minus_one_second), r#"-0.5"#);
208 check_error_deserialization::<Structf64Strict>(
209 r#""1""#,
210 expect![[r#"invalid type: string "1", expected f64 at line 1 column 3"#]],
211 );
212
213 #[serde_as]
214 #[derive(Debug, Serialize, Deserialize, PartialEq)]
215 struct Structf64Flexible(#[serde_as(as = "DurationSeconds<f64, Flexible>")] Duration);
216
217 is_equal(Structf64Flexible(zero), expect![[r#"0.0"#]]);
218 is_equal(Structf64Flexible(one_second), expect![[r#"1.0"#]]);
219 is_equal(Structf64Flexible(minus_one_second), expect![[r#"-1.0"#]]);
220 check_serialization(Structf64Flexible(half_second), expect![[r#"1.0"#]]);
221 check_serialization(Structf64Flexible(minus_half_second), expect![[r#"-1.0"#]]);
222 check_deserialization(Structf64Flexible(half_second), r#""0.5""#);
223 check_deserialization(Structf64Flexible(minus_half_second), r#""-0.5""#);
224 check_deserialization(Structf64Flexible(one_second), r#""1""#);
225 check_deserialization(Structf64Flexible(minus_one_second), r#""-1""#);
226 check_deserialization(Structf64Flexible(zero), r#""0""#);
227 check_error_deserialization::<Structf64Flexible>(
228 r#""a""#,
229 expect![[
230 r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"#
231 ]],
232 );
233
234 #[serde_as]
235 #[derive(Debug, Serialize, Deserialize, PartialEq)]
236 struct StructStringStrict(#[serde_as(as = "DurationSeconds<String>")] Duration);
237
238 is_equal(StructStringStrict(zero), expect![[r#""0""#]]);
239 is_equal(StructStringStrict(one_second), expect![[r#""1""#]]);
240 is_equal(StructStringStrict(minus_one_second), expect![[r#""-1""#]]);
241 check_serialization(StructStringStrict(half_second), expect![[r#""1""#]]);
242 check_serialization(StructStringStrict(minus_half_second), expect![[r#""-1""#]]);
243 check_error_deserialization::<StructStringStrict>(
244 r#"1"#,
245 expect![[
246 r#"invalid type: integer `1`, expected a string containing a number at line 1 column 1"#
247 ]],
248 );
249 check_error_deserialization::<StructStringStrict>(
250 r#"-1"#,
251 expect![[
252 r#"invalid type: integer `-1`, expected a string containing a number at line 1 column 2"#
253 ]],
254 );
255
256 #[serde_as]
257 #[derive(Debug, Serialize, Deserialize, PartialEq)]
258 struct StructStringFlexible(#[serde_as(as = "DurationSeconds<String, Flexible>")] Duration);
259
260 is_equal(StructStringFlexible(zero), expect![[r#""0""#]]);
261 is_equal(StructStringFlexible(one_second), expect![[r#""1""#]]);
262 is_equal(StructStringFlexible(minus_one_second), expect![[r#""-1""#]]);
263 check_serialization(StructStringFlexible(half_second), expect![[r#""1""#]]);
264 check_deserialization(StructStringFlexible(half_second), r#""0.5""#);
265 check_deserialization(StructStringFlexible(one_second), r#""1""#);
266 check_deserialization(StructStringFlexible(zero), r#""0""#);
267 check_error_deserialization::<StructStringFlexible>(
268 r#""a""#,
269 expect![[
270 r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"#
271 ]],
272 );
273 }
274
275 #[test]
276 fn test_chrono_duration_seconds_with_frac() {
277 let zero = Duration::zero();
278 let one_second = Duration::seconds(1);
279 let half_second = Duration::nanoseconds(500_000_000);
280 let minus_one_second = zero - one_second;
281 let minus_half_second = zero - half_second;
282
283 #[serde_as]
284 #[derive(Debug, Serialize, Deserialize, PartialEq)]
285 struct Structf64Strict(#[serde_as(as = "DurationSecondsWithFrac<f64>")] Duration);
286
287 is_equal(Structf64Strict(zero), expect![[r#"0.0"#]]);
288 is_equal(Structf64Strict(one_second), expect![[r#"1.0"#]]);
289 is_equal(Structf64Strict(minus_one_second), expect![[r#"-1.0"#]]);
290 is_equal(Structf64Strict(half_second), expect![[r#"0.5"#]]);
291 is_equal(Structf64Strict(minus_half_second), expect![[r#"-0.5"#]]);
292 check_error_deserialization::<Structf64Strict>(
293 r#""1""#,
294 expect![[r#"invalid type: string "1", expected f64 at line 1 column 3"#]],
295 );
296
297 #[serde_as]
298 #[derive(Debug, Serialize, Deserialize, PartialEq)]
299 struct Structf64Flexible(#[serde_as(as = "DurationSecondsWithFrac<f64, Flexible>")] Duration);
300
301 is_equal(Structf64Flexible(zero), expect![[r#"0.0"#]]);
302 is_equal(Structf64Flexible(one_second), expect![[r#"1.0"#]]);
303 is_equal(Structf64Flexible(minus_one_second), expect![[r#"-1.0"#]]);
304 is_equal(Structf64Flexible(minus_half_second), expect![[r#"-0.5"#]]);
305 check_deserialization(Structf64Flexible(one_second), r#""1""#);
306 check_deserialization(Structf64Flexible(minus_one_second), r#""-1""#);
307 check_deserialization(Structf64Flexible(half_second), r#""0.5""#);
308 check_deserialization(Structf64Flexible(zero), r#""0""#);
309 check_error_deserialization::<Structf64Flexible>(
310 r#""a""#,
311 expect![[
312 r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"#
313 ]],
314 );
315
316 #[serde_as]
317 #[derive(Debug, Serialize, Deserialize, PartialEq)]
318 struct StructStringStrict(#[serde_as(as = "DurationSecondsWithFrac<String>")] Duration);
319
320 is_equal(StructStringStrict(zero), expect![[r#""0""#]]);
321 is_equal(StructStringStrict(one_second), expect![[r#""1""#]]);
322 is_equal(StructStringStrict(minus_one_second), expect![[r#""-1""#]]);
323 is_equal(StructStringStrict(half_second), expect![[r#""0.5""#]]);
324 is_equal(
325 StructStringStrict(minus_half_second),
326 expect![[r#""-0.5""#]],
327 );
328 is_equal(
329 StructStringStrict(minus_half_second),
330 expect![[r#""-0.5""#]],
331 );
332 check_error_deserialization::<StructStringStrict>(
333 r#"1"#,
334 expect![[r#"invalid type: integer `1`, expected a string at line 1 column 1"#]],
335 );
336 check_error_deserialization::<StructStringStrict>(
337 r#"-1"#,
338 expect![[r#"invalid type: integer `-1`, expected a string at line 1 column 2"#]],
339 );
340
341 #[serde_as]
342 #[derive(Debug, Serialize, Deserialize, PartialEq)]
343 struct StructStringFlexible(
344 #[serde_as(as = "DurationSecondsWithFrac<String, Flexible>")] Duration,
345 );
346
347 is_equal(StructStringFlexible(zero), expect![[r#""0""#]]);
348 is_equal(StructStringFlexible(one_second), expect![[r#""1""#]]);
349 is_equal(StructStringFlexible(minus_one_second), expect![[r#""-1""#]]);
350 is_equal(StructStringFlexible(half_second), expect![[r#""0.5""#]]);
351 is_equal(
352 StructStringFlexible(minus_half_second),
353 expect![[r#""-0.5""#]],
354 );
355 check_deserialization(StructStringFlexible(one_second), r#""1""#);
356 check_deserialization(StructStringFlexible(zero), r#""0""#);
357 check_error_deserialization::<StructStringFlexible>(
358 r#""a""#,
359 expect![[
360 r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"#
361 ]],
362 );
363 }
364
365 #[test]
366 fn test_chrono_timestamp_seconds() {
367 let zero = new_datetime(0, 0);
368 let one_second = zero + Duration::seconds(1);
369 let half_second = zero + Duration::nanoseconds(500_000_000);
370 let minus_one_second = zero - Duration::seconds(1);
371 let minus_half_second = zero - Duration::nanoseconds(500_000_000);
372
373 #[serde_as]
374 #[derive(Debug, Serialize, Deserialize, PartialEq)]
375 struct StructIntStrict(#[serde_as(as = "TimestampSeconds")] DateTime<Utc>);
376
377 is_equal(StructIntStrict(zero), expect![[r#"0"#]]);
378 is_equal(StructIntStrict(one_second), expect![[r#"1"#]]);
379 is_equal(StructIntStrict(minus_one_second), expect![[r#"-1"#]]);
380 check_serialization(StructIntStrict(half_second), expect![[r#"1"#]]);
381 check_serialization(StructIntStrict(minus_half_second), expect![[r#"-1"#]]);
382 check_error_deserialization::<StructIntStrict>(
383 r#""1""#,
384 expect![[r#"invalid type: string "1", expected i64 at line 1 column 3"#]],
385 );
386 check_error_deserialization::<StructIntStrict>(
387 r#"0.123"#,
388 expect![[r#"invalid type: floating point `0.123`, expected i64 at line 1 column 5"#]],
389 );
390
391 #[serde_as]
392 #[derive(Debug, Serialize, Deserialize, PartialEq)]
393 struct StructIntFlexible(#[serde_as(as = "TimestampSeconds<i64, Flexible>")] DateTime<Utc>);
394
395 is_equal(StructIntFlexible(zero), expect![[r#"0"#]]);
396 is_equal(StructIntFlexible(one_second), expect![[r#"1"#]]);
397 is_equal(StructIntFlexible(minus_one_second), expect![[r#"-1"#]]);
398 check_serialization(StructIntFlexible(half_second), expect![[r#"1"#]]);
399 check_serialization(StructIntFlexible(minus_half_second), expect![[r#"-1"#]]);
400 check_deserialization(StructIntFlexible(one_second), r#""1""#);
401 check_deserialization(StructIntFlexible(one_second), r#"1.0"#);
402 check_deserialization(StructIntFlexible(minus_half_second), r#""-0.5""#);
403 check_deserialization(StructIntFlexible(half_second), r#"0.5"#);
404 check_error_deserialization::<StructIntFlexible>(
405 r#""a""#,
406 expect![[
407 r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"#
408 ]],
409 );
410
411 #[serde_as]
412 #[derive(Debug, Serialize, Deserialize, PartialEq)]
413 struct Structf64Strict(#[serde_as(as = "TimestampSeconds<f64>")] DateTime<Utc>);
414
415 is_equal(Structf64Strict(zero), expect![[r#"0.0"#]]);
416 is_equal(Structf64Strict(one_second), expect![[r#"1.0"#]]);
417 is_equal(Structf64Strict(minus_one_second), expect![[r#"-1.0"#]]);
418 check_serialization(Structf64Strict(half_second), expect![[r#"1.0"#]]);
419 check_serialization(Structf64Strict(minus_half_second), expect![[r#"-1.0"#]]);
420 check_deserialization(Structf64Strict(one_second), r#"0.5"#);
421 check_error_deserialization::<Structf64Strict>(
422 r#""1""#,
423 expect![[r#"invalid type: string "1", expected f64 at line 1 column 3"#]],
424 );
425
426 #[serde_as]
427 #[derive(Debug, Serialize, Deserialize, PartialEq)]
428 struct Structf64Flexible(#[serde_as(as = "TimestampSeconds<f64, Flexible>")] DateTime<Utc>);
429
430 is_equal(Structf64Flexible(zero), expect![[r#"0.0"#]]);
431 is_equal(Structf64Flexible(one_second), expect![[r#"1.0"#]]);
432 is_equal(Structf64Flexible(minus_one_second), expect![[r#"-1.0"#]]);
433 check_serialization(Structf64Flexible(half_second), expect![[r#"1.0"#]]);
434 check_serialization(Structf64Flexible(minus_half_second), expect![[r#"-1.0"#]]);
435 check_deserialization(Structf64Flexible(one_second), r#""1""#);
436 check_deserialization(Structf64Flexible(one_second), r#"1.0"#);
437 check_deserialization(Structf64Flexible(minus_half_second), r#""-0.5""#);
438 check_deserialization(Structf64Flexible(half_second), r#"0.5"#);
439 check_error_deserialization::<Structf64Flexible>(
440 r#""a""#,
441 expect![[
442 r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"#
443 ]],
444 );
445
446 #[serde_as]
447 #[derive(Debug, Serialize, Deserialize, PartialEq)]
448 struct StructStringStrict(#[serde_as(as = "TimestampSeconds<String>")] DateTime<Utc>);
449
450 is_equal(StructStringStrict(zero), expect![[r#""0""#]]);
451 is_equal(StructStringStrict(one_second), expect![[r#""1""#]]);
452 is_equal(StructStringStrict(minus_one_second), expect![[r#""-1""#]]);
453 check_serialization(StructStringStrict(half_second), expect![[r#""1""#]]);
454 check_serialization(StructStringStrict(minus_half_second), expect![[r#""-1""#]]);
455 check_deserialization(StructStringStrict(one_second), r#""1""#);
456 check_error_deserialization::<StructStringStrict>(
457 r#""0.5""#,
458 expect![[r#"invalid digit found in string at line 1 column 5"#]],
459 );
460 check_error_deserialization::<StructStringStrict>(
461 r#""-0.5""#,
462 expect![[r#"invalid digit found in string at line 1 column 6"#]],
463 );
464 check_error_deserialization::<StructStringStrict>(
465 r#"1"#,
466 expect![[
467 r#"invalid type: integer `1`, expected a string containing a number at line 1 column 1"#
468 ]],
469 );
470 check_error_deserialization::<StructStringStrict>(
471 r#"0.1"#,
472 expect![[
473 r#"invalid type: floating point `0.1`, expected a string containing a number at line 1 column 3"#
474 ]],
475 );
476
477 #[serde_as]
478 #[derive(Debug, Serialize, Deserialize, PartialEq)]
479 struct StructStringFlexible(
480 #[serde_as(as = "TimestampSeconds<String, Flexible>")] DateTime<Utc>,
481 );
482
483 is_equal(StructStringFlexible(zero), expect![[r#""0""#]]);
484 is_equal(StructStringFlexible(one_second), expect![[r#""1""#]]);
485 is_equal(StructStringFlexible(minus_one_second), expect![[r#""-1""#]]);
486 check_serialization(StructStringFlexible(half_second), expect![[r#""1""#]]);
487 check_serialization(
488 StructStringFlexible(minus_half_second),
489 expect![[r#""-1""#]],
490 );
491 check_deserialization(StructStringFlexible(one_second), r#"1"#);
492 check_deserialization(StructStringFlexible(one_second), r#"1.0"#);
493 check_deserialization(StructStringFlexible(minus_half_second), r#""-0.5""#);
494 check_deserialization(StructStringFlexible(half_second), r#"0.5"#);
495 check_error_deserialization::<StructStringFlexible>(
496 r#""a""#,
497 expect![[
498 r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"#
499 ]],
500 );
501 }
502
503 #[test]
504 fn test_chrono_timestamp_seconds_with_frac() {
505 let zero = new_datetime(0, 0);
506 let one_second = zero + Duration::seconds(1);
507 let half_second = zero + Duration::nanoseconds(500_000_000);
508 let minus_one_second = zero - Duration::seconds(1);
509 let minus_half_second = zero - Duration::nanoseconds(500_000_000);
510
511 #[serde_as]
512 #[derive(Debug, Serialize, Deserialize, PartialEq)]
513 struct Structf64Strict(#[serde_as(as = "TimestampSecondsWithFrac<f64>")] DateTime<Utc>);
514
515 is_equal(Structf64Strict(zero), expect![[r#"0.0"#]]);
516 is_equal(Structf64Strict(one_second), expect![[r#"1.0"#]]);
517 is_equal(Structf64Strict(minus_one_second), expect![[r#"-1.0"#]]);
518 is_equal(Structf64Strict(half_second), expect![[r#"0.5"#]]);
519 is_equal(Structf64Strict(minus_half_second), expect![[r#"-0.5"#]]);
520 check_error_deserialization::<Structf64Strict>(
521 r#""1""#,
522 expect![[r#"invalid type: string "1", expected f64 at line 1 column 3"#]],
523 );
524
525 #[serde_as]
526 #[derive(Debug, Serialize, Deserialize, PartialEq)]
527 struct Structf64Flexible(
528 #[serde_as(as = "TimestampSecondsWithFrac<f64, Flexible>")] DateTime<Utc>,
529 );
530
531 is_equal(Structf64Flexible(zero), expect![[r#"0.0"#]]);
532 is_equal(Structf64Flexible(one_second), expect![[r#"1.0"#]]);
533 is_equal(Structf64Flexible(minus_one_second), expect![[r#"-1.0"#]]);
534 is_equal(Structf64Flexible(half_second), expect![[r#"0.5"#]]);
535 is_equal(Structf64Flexible(minus_half_second), expect![[r#"-0.5"#]]);
536 check_deserialization(Structf64Flexible(one_second), r#""1""#);
537 check_deserialization(Structf64Flexible(minus_half_second), r#""-0.5""#);
538 check_error_deserialization::<Structf64Flexible>(
539 r#""a""#,
540 expect![[
541 r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"#
542 ]],
543 );
544
545 #[serde_as]
546 #[derive(Debug, Serialize, Deserialize, PartialEq)]
547 struct StructStringStrict(#[serde_as(as = "TimestampSecondsWithFrac<String>")] DateTime<Utc>);
548
549 is_equal(StructStringStrict(zero), expect![[r#""0""#]]);
550 is_equal(StructStringStrict(one_second), expect![[r#""1""#]]);
551 is_equal(StructStringStrict(minus_one_second), expect![[r#""-1""#]]);
552 is_equal(StructStringStrict(half_second), expect![[r#""0.5""#]]);
553 is_equal(
554 StructStringStrict(minus_half_second),
555 expect![[r#""-0.5""#]],
556 );
557 check_error_deserialization::<StructStringStrict>(
558 r#"1"#,
559 expect![[r#"invalid type: integer `1`, expected a string at line 1 column 1"#]],
560 );
561 check_error_deserialization::<StructStringStrict>(
562 r#"0.1"#,
563 expect![[r#"invalid type: floating point `0.1`, expected a string at line 1 column 3"#]],
564 );
565
566 #[serde_as]
567 #[derive(Debug, Serialize, Deserialize, PartialEq)]
568 struct StructStringFlexible(
569 #[serde_as(as = "TimestampSecondsWithFrac<String, Flexible>")] DateTime<Utc>,
570 );
571
572 is_equal(StructStringFlexible(zero), expect![[r#""0""#]]);
573 is_equal(StructStringFlexible(one_second), expect![[r#""1""#]]);
574 is_equal(StructStringFlexible(minus_one_second), expect![[r#""-1""#]]);
575 is_equal(StructStringFlexible(half_second), expect![[r#""0.5""#]]);
576 is_equal(
577 StructStringFlexible(minus_half_second),
578 expect![[r#""-0.5""#]],
579 );
580 check_deserialization(StructStringFlexible(one_second), r#"1"#);
581 check_deserialization(StructStringFlexible(one_second), r#"1.0"#);
582 check_deserialization(StructStringFlexible(half_second), r#"0.5"#);
583 check_error_deserialization::<StructStringFlexible>(
584 r#""a""#,
585 expect![[
586 r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"#
587 ]],
588 );
589 }
590
591 macro_rules! smoketest {
592 ($($valuety:ty, $adapter:literal, $value:expr, $expect:tt;)*) => {
593 $({
594 #[serde_as]
595 #[derive(Debug, Serialize, Deserialize, PartialEq)]
596 struct S(#[serde_as(as = $adapter)] $valuety);
597 #[allow(unused_braces)]
598 is_equal(S($value), $expect);
599 })*
600 };
601 }
602
603 #[test]
604 fn test_duration_smoketest() {
605 let zero = Duration::seconds(0);
606 let one_second = Duration::seconds(1);
607
608 smoketest! {
609 Duration, "DurationSeconds<i64>", one_second, {expect![[r#"1"#]]};
610 Duration, "DurationSeconds<f64>", one_second, {expect![[r#"1.0"#]]};
611 Duration, "DurationMilliSeconds<i64>", one_second, {expect![[r#"1000"#]]};
612 Duration, "DurationMilliSeconds<f64>", one_second, {expect![[r#"1000.0"#]]};
613 Duration, "DurationMicroSeconds<i64>", one_second, {expect![[r#"1000000"#]]};
614 Duration, "DurationMicroSeconds<f64>", one_second, {expect![[r#"1000000.0"#]]};
615 Duration, "DurationNanoSeconds<i64>", one_second, {expect![[r#"1000000000"#]]};
616 Duration, "DurationNanoSeconds<f64>", one_second, {expect![[r#"1000000000.0"#]]};
617 };
618
619 smoketest! {
620 Duration, "DurationSecondsWithFrac", one_second, {expect![[r#"1.0"#]]};
621 Duration, "DurationSecondsWithFrac<String>", one_second, {expect![[r#""1""#]]};
622 Duration, "DurationMilliSecondsWithFrac", one_second, {expect![[r#"1000.0"#]]};
623 Duration, "DurationMilliSecondsWithFrac<String>", one_second, {expect![[r#""1000""#]]};
624 Duration, "DurationMicroSecondsWithFrac", one_second, {expect![[r#"1000000.0"#]]};
625 Duration, "DurationMicroSecondsWithFrac<String>", one_second, {expect![[r#""1000000""#]]};
626 Duration, "DurationNanoSecondsWithFrac", one_second, {expect![[r#"1000000000.0"#]]};
627 Duration, "DurationNanoSecondsWithFrac<String>", one_second, {expect![[r#""1000000000""#]]};
628 };
629
630 smoketest! {
631 Duration, "DurationSecondsWithFrac", zero, {expect![[r#"0.0"#]]};
632 Duration, "DurationSecondsWithFrac", zero + Duration::nanoseconds(500_000_000), {expect![[r#"0.5"#]]};
633 Duration, "DurationSecondsWithFrac", zero + Duration::seconds(1), {expect![[r#"1.0"#]]};
634 Duration, "DurationSecondsWithFrac", zero - Duration::nanoseconds(500_000_000), {expect![[r#"-0.5"#]]};
635 Duration, "DurationSecondsWithFrac", zero - Duration::seconds(1), {expect![[r#"-1.0"#]]};
636 };
637 }
638
639 #[test]
640 fn test_datetime_utc_smoketest() {
641 let zero = new_datetime(0, 0);
642 let one_second = zero + Duration::seconds(1);
643
644 smoketest! {
645 DateTime<Utc>, "TimestampSeconds<i64>", one_second, {expect![[r#"1"#]]};
646 DateTime<Utc>, "TimestampSeconds<f64>", one_second, {expect![[r#"1.0"#]]};
647 DateTime<Utc>, "TimestampMilliSeconds<i64>", one_second, {expect![[r#"1000"#]]};
648 DateTime<Utc>, "TimestampMilliSeconds<f64>", one_second, {expect![[r#"1000.0"#]]};
649 DateTime<Utc>, "TimestampMicroSeconds<i64>", one_second, {expect![[r#"1000000"#]]};
650 DateTime<Utc>, "TimestampMicroSeconds<f64>", one_second, {expect![[r#"1000000.0"#]]};
651 DateTime<Utc>, "TimestampNanoSeconds<i64>", one_second, {expect![[r#"1000000000"#]]};
652 DateTime<Utc>, "TimestampNanoSeconds<f64>", one_second, {expect![[r#"1000000000.0"#]]};
653 };
654
655 smoketest! {
656 DateTime<Utc>, "TimestampSecondsWithFrac", one_second, {expect![[r#"1.0"#]]};
657 DateTime<Utc>, "TimestampSecondsWithFrac<String>", one_second, {expect![[r#""1""#]]};
658 DateTime<Utc>, "TimestampMilliSecondsWithFrac", one_second, {expect![[r#"1000.0"#]]};
659 DateTime<Utc>, "TimestampMilliSecondsWithFrac<String>", one_second, {expect![[r#""1000""#]]};
660 DateTime<Utc>, "TimestampMicroSecondsWithFrac", one_second, {expect![[r#"1000000.0"#]]};
661 DateTime<Utc>, "TimestampMicroSecondsWithFrac<String>", one_second, {expect![[r#""1000000""#]]};
662 DateTime<Utc>, "TimestampNanoSecondsWithFrac", one_second, {expect![[r#"1000000000.0"#]]};
663 DateTime<Utc>, "TimestampNanoSecondsWithFrac<String>", one_second, {expect![[r#""1000000000""#]]};
664 };
665
666 smoketest! {
667 DateTime<Utc>, "TimestampSecondsWithFrac", zero, {expect![[r#"0.0"#]]};
668 DateTime<Utc>, "TimestampSecondsWithFrac", zero + Duration::nanoseconds(500_000_000), {expect![[r#"0.5"#]]};
669 DateTime<Utc>, "TimestampSecondsWithFrac", zero + Duration::seconds(1), {expect![[r#"1.0"#]]};
670 DateTime<Utc>, "TimestampSecondsWithFrac", zero - Duration::nanoseconds(500_000_000), {expect![[r#"-0.5"#]]};
671 DateTime<Utc>, "TimestampSecondsWithFrac", zero - Duration::seconds(1), {expect![[r#"-1.0"#]]};
672 };
673 }
674
675 #[test]
676 fn test_datetime_local_smoketest() {
677 let zero = unix_epoch_local();
678 let one_second = zero + Duration::seconds(1);
679
680 smoketest! {
681 DateTime<Local>, "TimestampSeconds<i64>", one_second, {expect![[r#"1"#]]};
682 DateTime<Local>, "TimestampSeconds<f64>", one_second, {expect![[r#"1.0"#]]};
683 DateTime<Local>, "TimestampMilliSeconds<i64>", one_second, {expect![[r#"1000"#]]};
684 DateTime<Local>, "TimestampMilliSeconds<f64>", one_second, {expect![[r#"1000.0"#]]};
685 DateTime<Local>, "TimestampMicroSeconds<i64>", one_second, {expect![[r#"1000000"#]]};
686 DateTime<Local>, "TimestampMicroSeconds<f64>", one_second, {expect![[r#"1000000.0"#]]};
687 DateTime<Local>, "TimestampNanoSeconds<i64>", one_second, {expect![[r#"1000000000"#]]};
688 DateTime<Local>, "TimestampNanoSeconds<f64>", one_second, {expect![[r#"1000000000.0"#]]};
689 };
690
691 smoketest! {
692 DateTime<Local>, "TimestampSecondsWithFrac", one_second, {expect![[r#"1.0"#]]};
693 DateTime<Local>, "TimestampSecondsWithFrac<String>", one_second, {expect![[r#""1""#]]};
694 DateTime<Local>, "TimestampMilliSecondsWithFrac", one_second, {expect![[r#"1000.0"#]]};
695 DateTime<Local>, "TimestampMilliSecondsWithFrac<String>", one_second, {expect![[r#""1000""#]]};
696 DateTime<Local>, "TimestampMicroSecondsWithFrac", one_second, {expect![[r#"1000000.0"#]]};
697 DateTime<Local>, "TimestampMicroSecondsWithFrac<String>", one_second, {expect![[r#""1000000""#]]};
698 DateTime<Local>, "TimestampNanoSecondsWithFrac", one_second, {expect![[r#"1000000000.0"#]]};
699 DateTime<Local>, "TimestampNanoSecondsWithFrac<String>", one_second, {expect![[r#""1000000000""#]]};
700 };
701
702 smoketest! {
703 DateTime<Local>, "TimestampSecondsWithFrac", zero, {expect![[r#"0.0"#]]};
704 DateTime<Local>, "TimestampSecondsWithFrac", zero + Duration::nanoseconds(500_000_000), {expect![[r#"0.5"#]]};
705 DateTime<Local>, "TimestampSecondsWithFrac", zero + Duration::seconds(1), {expect![[r#"1.0"#]]};
706 DateTime<Local>, "TimestampSecondsWithFrac", zero - Duration::nanoseconds(500_000_000), {expect![[r#"-0.5"#]]};
707 DateTime<Local>, "TimestampSecondsWithFrac", zero - Duration::seconds(1), {expect![[r#"-1.0"#]]};
708 };
709 }
710
711 #[test]
712 fn test_naive_datetime_smoketest() {
713 let zero = unix_epoch_naive();
714 let one_second = zero + Duration::seconds(1);
715
716 smoketest! {
717 NaiveDateTime, "TimestampSeconds<i64>", one_second, {expect![[r#"1"#]]};
718 NaiveDateTime, "TimestampSeconds<f64>", one_second, {expect![[r#"1.0"#]]};
719 NaiveDateTime, "TimestampMilliSeconds<i64>", one_second, {expect![[r#"1000"#]]};
720 NaiveDateTime, "TimestampMilliSeconds<f64>", one_second, {expect![[r#"1000.0"#]]};
721 NaiveDateTime, "TimestampMicroSeconds<i64>", one_second, {expect![[r#"1000000"#]]};
722 NaiveDateTime, "TimestampMicroSeconds<f64>", one_second, {expect![[r#"1000000.0"#]]};
723 NaiveDateTime, "TimestampNanoSeconds<i64>", one_second, {expect![[r#"1000000000"#]]};
724 NaiveDateTime, "TimestampNanoSeconds<f64>", one_second, {expect![[r#"1000000000.0"#]]};
725 };
726
727 smoketest! {
728 NaiveDateTime, "TimestampSecondsWithFrac", one_second, {expect![[r#"1.0"#]]};
729 NaiveDateTime, "TimestampSecondsWithFrac<String>", one_second, {expect![[r#""1""#]]};
730 NaiveDateTime, "TimestampMilliSecondsWithFrac", one_second, {expect![[r#"1000.0"#]]};
731 NaiveDateTime, "TimestampMilliSecondsWithFrac<String>", one_second, {expect![[r#""1000""#]]};
732 NaiveDateTime, "TimestampMicroSecondsWithFrac", one_second, {expect![[r#"1000000.0"#]]};
733 NaiveDateTime, "TimestampMicroSecondsWithFrac<String>", one_second, {expect![[r#""1000000""#]]};
734 NaiveDateTime, "TimestampNanoSecondsWithFrac", one_second, {expect![[r#"1000000000.0"#]]};
735 NaiveDateTime, "TimestampNanoSecondsWithFrac<String>", one_second, {expect![[r#""1000000000""#]]};
736 };
737
738 smoketest! {
739 NaiveDateTime, "TimestampSecondsWithFrac", zero, {expect![[r#"0.0"#]]};
740 NaiveDateTime, "TimestampSecondsWithFrac", zero + Duration::nanoseconds(500_000_000), {expect![[r#"0.5"#]]};
741 NaiveDateTime, "TimestampSecondsWithFrac", zero + Duration::seconds(1), {expect![[r#"1.0"#]]};
742 NaiveDateTime, "TimestampSecondsWithFrac", zero - Duration::nanoseconds(500_000_000), {expect![[r#"-0.5"#]]};
743 NaiveDateTime, "TimestampSecondsWithFrac", zero - Duration::seconds(1), {expect![[r#"-1.0"#]]};
744 };
745 }