| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
//! Utilities for tests to make IncomingBsos and Content from test data. |
| 6 |
use super::{IncomingBso, IncomingEnvelope, OutgoingBso}; |
| 7 |
use crate::{Guid, ServerTimestamp}; |
| 8 |
|
| 9 |
/// Tests often want an IncomingBso to test, and the easiest way is often to |
| 10 |
/// create an OutgoingBso convert it back to an incoming. |
| 11 |
impl OutgoingBso { |
| 12 |
// These functions would ideally consume `self` and avoid the clones, but |
| 13 |
// this is more convenient for some tests and the extra overhead doesn't |
| 14 |
// really matter for tests. |
| 15 |
/// When a test has an [OutgoingBso] and wants it as an [IncomingBso] |
| 16 |
pub fn to_test_incoming(&self) -> IncomingBso { |
| 17 |
self.to_test_incoming_ts(ServerTimestamp::default()) |
| 18 |
} |
| 19 |
|
| 20 |
/// When a test has an [OutgoingBso] and wants it as an [IncomingBso] with a specific timestamp. |
| 21 |
pub fn to_test_incoming_ts(&self, ts: ServerTimestamp) -> IncomingBso { |
| 22 |
IncomingBso { |
| 23 |
envelope: IncomingEnvelope { |
| 24 |
id: self.envelope.id.clone(), |
| 25 |
modified: ts, |
| 26 |
sortindex: self.envelope.sortindex, |
| 27 |
ttl: self.envelope.ttl, |
| 28 |
}, |
| 29 |
payload: self.payload.clone(), |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/// When a test has an [OutgoingBso] and wants it as an [IncomingBso] with a specific T. |
| 34 |
pub fn to_test_incoming_t<T: for<'de> serde::Deserialize<'de>>(&self) -> T { |
| 35 |
self.to_test_incoming().into_content().content().unwrap() |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/// Helpers to create an IncomingBso from some T |
| 40 |
impl IncomingBso { |
| 41 |
/// When a test has an T and wants it as an [IncomingBso] |
| 42 |
pub fn from_test_content<T: serde::Serialize>(json: T) -> Self { |
| 43 |
// Go via an OutgoingBso |
| 44 |
OutgoingBso::from_content_with_id(json) |
| 45 |
.unwrap() |
| 46 |
.to_test_incoming() |
| 47 |
} |
| 48 |
|
| 49 |
/// When a test has an T and wants it as an [IncomingBso] with a specific timestamp. |
| 50 |
pub fn from_test_content_ts<T: serde::Serialize>(json: T, ts: ServerTimestamp) -> Self { |
| 51 |
// Go via an OutgoingBso |
| 52 |
OutgoingBso::from_content_with_id(json) |
| 53 |
.unwrap() |
| 54 |
.to_test_incoming_ts(ts) |
| 55 |
} |
| 56 |
|
| 57 |
/// When a test wants a new incoming tombstone. |
| 58 |
pub fn new_test_tombstone(guid: Guid) -> Self { |
| 59 |
OutgoingBso::new_tombstone(guid.into()).to_test_incoming() |
| 60 |
} |
| 61 |
} |