-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make create job atomic * changelog * comment * fix prettier * fix tests and upsert logic * update: lint fix --------- Co-authored-by: Heemank Verma <heemankv@gmail.com>
- Loading branch information
1 parent
3b04d66
commit 2b957be
Showing
7 changed files
with
145 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use color_eyre::eyre::{eyre, Result}; | ||
use mongodb::bson::{Bson, Document}; | ||
use serde::Serialize; | ||
|
||
pub trait ToDocument { | ||
fn to_document(&self) -> Result<Document>; | ||
} | ||
|
||
impl<T: Serialize> ToDocument for T { | ||
fn to_document(&self) -> Result<Document> { | ||
match mongodb::bson::to_bson(self)? { | ||
Bson::Document(document) => Ok(document), | ||
_ => Err(eyre!("Failed to convert to Document")), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use mongodb::bson::{Bson, Document}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::*; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct TestStruct { | ||
id: i32, | ||
name: String, | ||
optional_field: Option<String>, | ||
} | ||
|
||
#[test] | ||
fn test_to_document() { | ||
let test_struct = TestStruct { id: 1, name: "Test".to_string(), optional_field: None }; | ||
|
||
let document = test_struct.to_document().expect("Failed to convert to Document"); | ||
|
||
let mut expected_document = Document::new(); | ||
expected_document.insert("id", 1); | ||
expected_document.insert("name", "Test"); | ||
expected_document.insert("optional_field", Bson::Null); | ||
|
||
assert_eq!(document, expected_document); | ||
} | ||
|
||
#[test] | ||
fn test_to_document_fail() { | ||
let non_document_value = 1; | ||
|
||
let result = non_document_value.to_document(); | ||
|
||
assert!(result.is_err() && result.unwrap_err().to_string().contains("Failed to convert to Document")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters