1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::bucket::GridFSBucket;
use bson::Document;
use mongodb::error::Result;
impl GridFSBucket {
pub async fn drop(&self) -> Result<()> {
let dboptions = self.options.clone().unwrap_or_default();
let bucket_name = dboptions.bucket_name;
let file_collection = bucket_name.clone() + ".files";
let files = self.db.collection::<Document>(&file_collection);
files.drop(None).await?;
let chunk_collection = bucket_name + ".chunks";
let chunks = self.db.collection::<Document>(&chunk_collection);
chunks.drop(None).await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::GridFSBucket;
use crate::{options::GridFSBucketOptions, GridFSError};
use mongodb::Client;
use mongodb::Database;
use uuid::Uuid;
fn db_name_new() -> String {
"test_".to_owned()
+ Uuid::new_v4()
.to_hyphenated()
.encode_lower(&mut Uuid::encode_buffer())
}
#[tokio::test]
async fn drop_bucket() -> Result<(), GridFSError> {
let client = Client::with_uri_str(
&std::env::var("MONGO_URI").unwrap_or("mongodb://localhost:27017/".to_string()),
)
.await?;
let dbname = db_name_new();
let db: Database = client.database(&dbname);
let bucket = &GridFSBucket::new(db.clone(), Some(GridFSBucketOptions::default()));
bucket
.clone()
.upload_from_stream("test.txt", "test data".as_bytes(), None)
.await?;
let coll_list = db.list_collection_names(None).await?;
assert!(coll_list.contains(&"fs.files".to_string()));
assert!(coll_list.contains(&"fs.chunks".to_string()));
bucket.drop().await?;
let coll_list = db.list_collection_names(None).await?;
assert!(coll_list.is_empty());
db.drop(None).await?;
Ok(())
}
}