-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from thedodd/28-find-one-and-do
28 implement find_one_and_* methods.
- Loading branch information
Showing
7 changed files
with
204 additions
and
19 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -2,14 +2,14 @@ | |
authors = ["Anthony Dodd <[email protected]>"] | ||
categories = ["database", "data-structures"] | ||
description = "An ODM for MongoDB built upon the mongo rust driver." | ||
documentation = "https://github.com/thedodd/wither" | ||
documentation = "https://docs.rs/wither" | ||
homepage = "https://github.com/thedodd/wither" | ||
keywords = ["mongodb", "odm", "orm"] | ||
license = "Apache-2.0" | ||
name = "wither" | ||
readme = "../README.md" | ||
repository = "https://github.com/thedodd/wither" | ||
version = "0.8.0-beta.0" | ||
version = "0.8.0" | ||
|
||
[dependencies] | ||
chrono = "0.4" | ||
|
@@ -20,7 +20,7 @@ serde_derive = "1" | |
|
||
[dev-dependencies] | ||
lazy_static = "1" | ||
wither_derive = { version = "0.8.0-beta.0", path = "../wither_derive" } | ||
wither_derive = { version = "0.8.0", path = "../wither_derive" } | ||
|
||
[features] | ||
docinclude = [] # Used only for activating `doc(include="...")` on nightly. | ||
|
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,39 @@ | ||
### underlying driver | ||
If at any point in time you need direct access to the [underlying driver](https://docs.rs/mongodb/latest/mongodb/), it is always available. All of the `Model` interface methods take a handle to the database, which is part of the underlying driver. You can then use the [`Model::COLLECTION_NAME`](https://docs.rs/wither/latest/wither/model/trait.Model.html#associatedconstant.COLLECTION_NAME) to ensure you are accessing the correct collection. You can also use the various model convenience methods for serialization, such as the [`Model::instance_from_document`](https://docs.rs/wither/latest/wither/model/trait.Model.html#method.instance_from_document) method. | ||
|
||
```rust | ||
# #[macro_use] | ||
# extern crate mongodb; | ||
# extern crate serde; | ||
# #[macro_use(Serialize, Deserialize)] | ||
# extern crate serde_derive; | ||
# extern crate wither; | ||
# #[macro_use(Model)] | ||
# extern crate wither_derive; | ||
# | ||
# use wither::prelude::*; | ||
# use mongodb::{ | ||
# Document, ThreadedClient, | ||
# coll::options::IndexModel, | ||
# db::ThreadedDatabase, | ||
# }; | ||
# | ||
#[derive(Model, Serialize, Deserialize)] | ||
struct MyModel { | ||
#[serde(rename="_id", skip_serializing_if="Option::is_none")] | ||
pub id: Option<mongodb::oid::ObjectId>, | ||
|
||
#[model(index(index="dsc", unique="true"))] | ||
pub email: String, | ||
} | ||
|
||
fn main() { | ||
// Your DB handle. This is part of the underlying driver. | ||
let db = mongodb::Client::with_uri("mongodb://localhost:27017/").unwrap().db("mydb"); | ||
|
||
// Use the driver directly, but rely on your model for getting the collection name. | ||
let coll = db.collection(MyModel::COLLECTION_NAME); | ||
|
||
// Now you can use the raw collection interface to your heart's content. | ||
} | ||
``` |
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 |
---|---|---|
|
@@ -126,6 +126,114 @@ fn model_find_one_should_fetch_the_model_instance_matching_given_filter() { | |
assert_eq!(&user_from_db.email, &user.email); | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
// Model::find_one_and_delete //////////////////////////////////////////////// | ||
|
||
#[test] | ||
fn model_find_one_and_delete_should_delete_the_target_doc() { | ||
let fixture = Fixture::new().with_dropped_database().with_synced_models(); | ||
let db = fixture.get_db(); | ||
let mut user = User{id: None, email: "[email protected]".to_string()}; | ||
let mut user2 = User{id: None, email: "[email protected]".to_string()}; | ||
|
||
user.save(db.clone(), None).expect("Expected a successful save operation."); | ||
user2.save(db.clone(), None).expect("Expected a successful save operation."); | ||
let output = User::find_one_and_delete(db.clone(), doc!{"email": "[email protected]"}, None) | ||
.expect("Expected a operation.").unwrap(); | ||
|
||
assert_eq!(&output.email, &user.email); | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
// Model::find_one_and_replace /////////////////////////////////////////////// | ||
|
||
#[test] | ||
fn model_find_one_and_replace_should_replace_the_target_doc_and_return_new_doc() { | ||
let fixture = Fixture::new().with_dropped_database().with_synced_models(); | ||
let db = fixture.get_db(); | ||
let mut user = User{id: None, email: "[email protected]".to_string()}; | ||
let mut user2 = User{id: None, email: "[email protected]".to_string()}; | ||
let mut opts = FindOneAndUpdateOptions::new(); | ||
opts.return_document = Some(ReturnDocument::After); | ||
|
||
user.save(db.clone(), None).expect("Expected a successful save operation."); | ||
user2.save(db.clone(), None).expect("Expected a successful save operation."); | ||
let output = User::find_one_and_replace( | ||
db.clone(), | ||
doc!{"email": "[email protected]"}, | ||
doc!{"email": "[email protected]"}, | ||
Some(opts), | ||
).expect("Expected a operation.").unwrap(); | ||
|
||
assert_eq!(&output.email, "[email protected]"); | ||
} | ||
|
||
#[test] | ||
fn model_find_one_and_replace_should_replace_the_target_doc_and_return_old_doc() { | ||
let fixture = Fixture::new().with_dropped_database().with_synced_models(); | ||
let db = fixture.get_db(); | ||
let mut user = User{id: None, email: "[email protected]".to_string()}; | ||
let mut user2 = User{id: None, email: "[email protected]".to_string()}; | ||
let mut opts = FindOneAndUpdateOptions::new(); | ||
opts.return_document = Some(ReturnDocument::Before); | ||
|
||
user.save(db.clone(), None).expect("Expected a successful save operation."); | ||
user2.save(db.clone(), None).expect("Expected a successful save operation."); | ||
let output = User::find_one_and_replace( | ||
db.clone(), | ||
doc!{"email": "[email protected]"}, | ||
doc!{"email": "[email protected]"}, | ||
Some(opts), | ||
).expect("Expected a operation.").unwrap(); | ||
|
||
assert_eq!(&output.email, "[email protected]"); | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
// Model::find_one_and_update //////////////////////////////////////////////// | ||
|
||
#[test] | ||
fn model_find_one_and_update_should_update_target_document_and_return_new() { | ||
let fixture = Fixture::new().with_dropped_database().with_synced_models(); | ||
let db = fixture.get_db(); | ||
let mut user = User{id: None, email: "[email protected]".to_string()}; | ||
let mut user2 = User{id: None, email: "[email protected]".to_string()}; | ||
let mut opts = FindOneAndUpdateOptions::new(); | ||
opts.return_document = Some(ReturnDocument::After); | ||
|
||
user.save(db.clone(), None).expect("Expected a successful save operation."); | ||
user2.save(db.clone(), None).expect("Expected a successful save operation."); | ||
let output = User::find_one_and_update( | ||
db.clone(), | ||
doc!{"email": "[email protected]"}, | ||
doc!{"$set": doc!{"email": "[email protected]"}}, | ||
Some(opts), | ||
).expect("Expected a operation.").unwrap(); | ||
|
||
assert_eq!(&output.email, "[email protected]"); | ||
} | ||
|
||
#[test] | ||
fn model_find_one_and_update_should_update_target_document_and_return_old() { | ||
let fixture = Fixture::new().with_dropped_database().with_synced_models(); | ||
let db = fixture.get_db(); | ||
let mut user = User{id: None, email: "[email protected]".to_string()}; | ||
let mut user2 = User{id: None, email: "[email protected]".to_string()}; | ||
let mut opts = FindOneAndUpdateOptions::new(); | ||
opts.return_document = Some(ReturnDocument::Before); | ||
|
||
user.save(db.clone(), None).expect("Expected a successful save operation."); | ||
user2.save(db.clone(), None).expect("Expected a successful save operation."); | ||
let output = User::find_one_and_update( | ||
db.clone(), | ||
doc!{"email": "[email protected]"}, | ||
doc!{"$set": doc!{"email": "[email protected]"}}, | ||
Some(opts), | ||
).expect("Expected a operation.").unwrap(); | ||
|
||
assert_eq!(&output.email, "[email protected]"); | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
// Model.update ////////////////////////////////////////////////////////////// | ||
|
||
|
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 |
---|---|---|
|
@@ -2,14 +2,14 @@ | |
authors = ["Anthony Dodd <[email protected]>"] | ||
categories = ["database", "data-structures"] | ||
description = "An ODM for MongoDB built upon the mongo rust driver." | ||
documentation = "https://github.com/thedodd/wither" | ||
documentation = "https://docs.rs/wither" | ||
homepage = "https://github.com/thedodd/wither" | ||
keywords = ["mongodb", "odm", "orm"] | ||
license = "Apache-2.0" | ||
name = "wither_derive" | ||
readme = "README.md" | ||
repository = "https://github.com/thedodd/wither" | ||
version = "0.8.0-beta.0" | ||
version = "0.8.0" | ||
|
||
[lib] | ||
proc-macro = true | ||
|