Skip to content

Commit

Permalink
Test missing doc id case for insert and replace
Browse files Browse the repository at this point in the history
  • Loading branch information
Westwooo committed Sep 25, 2024
1 parent ca2c69c commit 771c73b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/cli/doc_upsert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub fn run_kv_mutations(
} else {
failed += 1;
let mut missing_reason = HashSet::new();
missing_reason.insert("missing doc id".into());
missing_reason.insert("Missing doc id".into());
fail_reasons.extend(missing_reason);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/doc_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ fn import_missing_id() {
assert_eq!(0, json["success"]);
assert_eq!(1, json["processed"]);
assert_eq!(1, json["failed"]);
assert_eq!("missing doc id", json["failures"]);
assert_eq!("Missing doc id", json["failures"]);
});
}
16 changes: 16 additions & 0 deletions tests/doc_insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,19 @@ fn error_on_insert_twice() {
assert_eq!("Key already exists", json["failures"]);
});
}

#[test]
#[cfg_attr(not(feature = "key_value"), ignore)]
fn insert_missing_id() {
CBPlayground::setup("insert_missing_id", None, None, |dirs, sandbox| {
let content = r#"{foo: bar, fizz: buzz}"#;
let out = cbsh!(cwd: dirs.test(), support::cb_pipeline(format!("{} | wrap content | insert not_id 123 | doc insert | first | to json", content)));
assert_eq!("", out.err);

let json = sandbox.parse_out_to_json(out.out).unwrap();
assert_eq!(0, json["success"]);
assert_eq!(1, json["processed"]);
assert_eq!(1, json["failed"]);
assert_eq!("Missing doc id", json["failures"]);
});
}
70 changes: 70 additions & 0 deletions tests/doc_replace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
mod common;

use crate::common::{new_doc_id, playground::CBPlayground, support};

#[test]
#[cfg_attr(not(feature = "key_value"), ignore)]
fn replace_a_document() {
CBPlayground::setup("replace_a_document", None, None, |dirs, sandbox| {
let key = new_doc_id();
let content = r#"{foo: bar, fizz: buzz}"#;
let out = cbsh!(cwd: dirs.test(), support::cb_pipeline(format!("{} | wrap content | insert id {} | doc insert | first | to json", content, key)));
assert_eq!("", out.err);
let json = sandbox.parse_out_to_json(out.out).unwrap();
assert_eq!(1, json["success"]);
assert_eq!(1, json["processed"]);
assert_eq!(0, json["failed"]);
assert_eq!("", json["failures"]);

let replacement_content = r#"{foo: fizz, bar: buzz}"#;
let out = cbsh!(cwd: dirs.test(), support::cb_pipeline(format!("{} | wrap content | insert id {} | doc replace | first | to json", replacement_content, key)));
assert_eq!("", out.err);
let json = sandbox.parse_out_to_json(out.out).unwrap();
assert_eq!(1, json["success"]);
assert_eq!(1, json["processed"]);
assert_eq!(0, json["failed"]);
assert_eq!("", json["failures"]);

let out = cbsh!(cwd: dirs.test(), support::cb_pipeline(format!("doc get {} | first | to json", key)));
assert_eq!("", out.err);
let json = sandbox.parse_out_to_json(out.out).unwrap();
assert_eq!(
"{\"foo\":\"fizz\",\"bar\":\"buzz\"}",
json["content"].to_string()
);
});
}

#[test]
#[cfg_attr(not(feature = "key_value"), ignore)]
fn replace_missing_doc() {
CBPlayground::setup("replace_missing_doc", None, None, |dirs, sandbox| {
let key = new_doc_id();
let content = r#"{foo: bar, fizz: buzz}"#;
let out = cbsh!(cwd: dirs.test(), support::cb_pipeline(format!("{} | wrap content | insert id {} | doc replace | first | to json", content, key)));
assert_eq!("", out.err);
let json = sandbox.parse_out_to_json(out.out).unwrap();

assert_eq!(0, json["success"]);
assert_eq!(1, json["processed"]);
assert_eq!(1, json["failed"]);
assert_eq!("Key not found", json["failures"]);
});
}

#[test]
#[cfg_attr(not(feature = "key_value"), ignore)]
fn replace_missing_doc_id() {
CBPlayground::setup("replace_missing_doc_id", None, None, |dirs, sandbox| {
let key = new_doc_id();
let content = r#"{foo: bar, fizz: buzz}"#;
let out = cbsh!(cwd: dirs.test(), support::cb_pipeline(format!("{} | wrap content | insert not_id {} | doc replace | first | to json", content, key)));
assert_eq!("", out.err);
let json = sandbox.parse_out_to_json(out.out).unwrap();

assert_eq!(0, json["success"]);
assert_eq!(1, json["processed"]);
assert_eq!(1, json["failed"]);
assert_eq!("Missing doc id", json["failures"]);
});
}
2 changes: 1 addition & 1 deletion tests/doc_upsert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ fn upsert_missing_id() {
assert_eq!(0, json["success"]);
assert_eq!(1, json["processed"]);
assert_eq!(1, json["failed"]);
assert_eq!("missing doc id", json["failures"]);
assert_eq!("Missing doc id", json["failures"]);
});
}

0 comments on commit 771c73b

Please sign in to comment.