|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use bson::{doc, Bson}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + error::ErrorKind, |
| 7 | + options::{Acknowledgment, InsertOneOptions, WriteConcern}, |
| 8 | + test::{TestClient, LOCK}, |
| 9 | +}; |
| 10 | + |
| 11 | +#[test] |
| 12 | +fn write_concern_is_acknowledged() { |
| 13 | + let w_1 = WriteConcern::builder() |
| 14 | + .w(Acknowledgment::Nodes(1)) |
| 15 | + .journal(false) |
| 16 | + .build(); |
| 17 | + assert!(w_1.is_acknowledged()); |
| 18 | + |
| 19 | + let w_majority = WriteConcern::builder() |
| 20 | + .w(Acknowledgment::Majority) |
| 21 | + .journal(false) |
| 22 | + .build(); |
| 23 | + assert!(w_majority.is_acknowledged()); |
| 24 | + |
| 25 | + let w_0 = WriteConcern::builder() |
| 26 | + .w(Acknowledgment::Nodes(0)) |
| 27 | + .journal(false) |
| 28 | + .build(); |
| 29 | + assert!(!w_0.is_acknowledged()); |
| 30 | + |
| 31 | + let w_0 = WriteConcern::builder().w(Acknowledgment::Nodes(0)).build(); |
| 32 | + assert!(!w_0.is_acknowledged()); |
| 33 | + |
| 34 | + let empty = WriteConcern::builder().build(); |
| 35 | + assert!(empty.is_acknowledged()); |
| 36 | + |
| 37 | + let empty = WriteConcern::builder().journal(false).build(); |
| 38 | + assert!(empty.is_acknowledged()); |
| 39 | + |
| 40 | + let empty = WriteConcern::builder().journal(true).build(); |
| 41 | + assert!(empty.is_acknowledged()); |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn write_concern_deserialize() { |
| 46 | + let w_1 = doc! { "w": 1 }; |
| 47 | + let wc: WriteConcern = bson::from_bson(Bson::Document(w_1)).unwrap(); |
| 48 | + assert_eq!( |
| 49 | + wc, |
| 50 | + WriteConcern { |
| 51 | + w: Acknowledgment::Nodes(1).into(), |
| 52 | + w_timeout: None, |
| 53 | + journal: None |
| 54 | + } |
| 55 | + ); |
| 56 | + |
| 57 | + let w_majority = doc! { "w": "majority" }; |
| 58 | + let wc: WriteConcern = bson::from_bson(Bson::Document(w_majority)).unwrap(); |
| 59 | + assert_eq!( |
| 60 | + wc, |
| 61 | + WriteConcern { |
| 62 | + w: Acknowledgment::Majority.into(), |
| 63 | + w_timeout: None, |
| 64 | + journal: None |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + let w_timeout = doc! { "w": "majority", "wtimeout": 100 }; |
| 69 | + let wc: WriteConcern = bson::from_bson(Bson::Document(w_timeout)).unwrap(); |
| 70 | + assert_eq!( |
| 71 | + wc, |
| 72 | + WriteConcern { |
| 73 | + w: Acknowledgment::Majority.into(), |
| 74 | + w_timeout: Duration::from_millis(100).into(), |
| 75 | + journal: None |
| 76 | + } |
| 77 | + ); |
| 78 | + |
| 79 | + let journal = doc! { "w": "majority", "j": true }; |
| 80 | + let wc: WriteConcern = bson::from_bson(Bson::Document(journal)).unwrap(); |
| 81 | + assert_eq!( |
| 82 | + wc, |
| 83 | + WriteConcern { |
| 84 | + w: Acknowledgment::Majority.into(), |
| 85 | + w_timeout: None, |
| 86 | + journal: true.into() |
| 87 | + } |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg_attr(feature = "tokio-runtime", tokio::test)] |
| 92 | +#[cfg_attr(feature = "async-std-runtime", async_std::test)] |
| 93 | +#[function_name::named] |
| 94 | +async fn inconsistent_write_concern_rejected() { |
| 95 | + let _guard = LOCK.run_concurrently().await; |
| 96 | + |
| 97 | + let client = TestClient::new().await; |
| 98 | + let db = client.database(function_name!()); |
| 99 | + let error = db |
| 100 | + .run_command( |
| 101 | + doc! { |
| 102 | + "insert": function_name!(), |
| 103 | + "documents": [ {} ], |
| 104 | + "writeConcern": { "w": 0, "j": true } |
| 105 | + }, |
| 106 | + None, |
| 107 | + ) |
| 108 | + .await |
| 109 | + .expect_err("insert should fail"); |
| 110 | + assert!(matches!(error.kind.as_ref(), ErrorKind::ArgumentError { .. })); |
| 111 | + |
| 112 | + let coll = db.collection(function_name!()); |
| 113 | + let wc = WriteConcern { |
| 114 | + w: Acknowledgment::Nodes(0).into(), |
| 115 | + journal: true.into(), |
| 116 | + w_timeout: None, |
| 117 | + }; |
| 118 | + let options = InsertOneOptions::builder().write_concern(wc).build(); |
| 119 | + let error = coll |
| 120 | + .insert_one(doc! {}, options) |
| 121 | + .await |
| 122 | + .expect_err("insert should fail"); |
| 123 | + assert!(matches!(error.kind.as_ref(), ErrorKind::ArgumentError { .. })); |
| 124 | +} |
0 commit comments