Skip to content

Commit

Permalink
test: add integration test for export
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-sakamoto committed Oct 9, 2023
1 parent d546a36 commit 95c25f9
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 9 deletions.
4 changes: 2 additions & 2 deletions tests/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async fn test_bootstrap() -> Result<(), Box<dyn std::error::Error>> {
for (args, expected_json) in test_cases {
let mut c = tm.command()?;
let cmd = c.args(&args);
util::assert_eq_json(cmd, expected_json);
util::assert_eq_cmd_json(cmd, expected_json);
}

tm.cleanup(vec!["Forum", "ProductCatalog", "Reply", "Thread"])
Expand All @@ -134,7 +134,7 @@ async fn test_bootstrap_movie() -> Result<(), Box<dyn std::error::Error>> {
"1933",
"King Kong",
]);
util::assert_eq_json(
util::assert_eq_cmd_json(
cmd,
r#"
{
Expand Down
122 changes: 122 additions & 0 deletions tests/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pub mod util;

use assert_cmd::prelude::*; // Add methods on commands
use predicates::prelude::*; // Used for writing assertions
use tempfile::tempdir;

#[tokio::test]
async fn test_export_non_existent_table() -> Result<(), Box<dyn std::error::Error>> {
let tm = util::setup().await?;
let mut c = tm.command()?;
let cmd = c.args(&[
"--region",
"local",
"--table",
"dummy-table-doent-exist",
"export",
"--output-file",
"a",
]);
cmd.assert().failure().stderr(predicate::str::contains(
// The error message is different between DynamoDB local and real service.
// It should be "Requested resource not found: Table: table not found" actually.
"Cannot do operations on a non-existent table",
));
Ok(())
}

#[tokio::test]
async fn test_export_empty_table() -> Result<(), Box<dyn std::error::Error>> {
let mut tm = util::setup().await?;
let table_name = tm.create_temporary_table("pk", None).await?;

let base_dir = tempdir()?;
let temp_path = base_dir.path().join(&table_name);

let mut c = tm.command()?;
let cmd = c.args(&[
"--region",
"local",
"--table",
&table_name,
"export",
"--output-file",
temp_path.to_str().unwrap(),
]);
cmd.assert().failure().stderr(
"thread 'main' panicked at 'attempt to subtract with overflow', src/transfer.rs:461:20
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
",
);
Ok(())
}

#[tokio::test]
async fn test_export_with_items() -> Result<(), Box<dyn std::error::Error>> {
let mut tm = util::setup().await?;
let table_name = tm
.create_temporary_table_with_items(
"pk",
Some("sk,N"),
vec![
util::TemporaryItem::new("abc", Some("1"), None),
util::TemporaryItem::new("abc", Some("2"), Some(r#"{"a": 1, "b": 2}"#)),
util::TemporaryItem::new("def", Some("3"), None),
],
)
.await?;

let base_dir = tempdir()?;
let temp_path = base_dir.path().join(&table_name);

let mut c = tm.command()?;
let cmd = c.args(&[
"--region",
"local",
"--table",
&table_name,
"export",
"--output-file",
temp_path.to_str().unwrap(),
]);
cmd.assert().success();

let export_content = std::fs::read_to_string(temp_path)?;
util::assert_eq_json(
&export_content,
r#"[
{
"sk": 1,
"pk": "abc"
},
{
"pk": "abc",
"sk": 2,
"a": 1,
"b": 2
},
{
"pk": "def",
"sk": 3
}
]"#,
);

Ok(())
}
8 changes: 4 additions & 4 deletions tests/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn test_get_item() -> Result<(), Box<dyn std::error::Error>> {

let mut c = tm.command()?;
let cmd = c.args(&["--region", "local", "--table", &table_name, "get", "42"]);
util::assert_eq_json(
util::assert_eq_cmd_json(
cmd,
r#"{
"flag": true,
Expand All @@ -86,7 +86,7 @@ async fn test_get_item_output_json() -> Result<(), Box<dyn std::error::Error>> {
"-o",
"json",
]);
util::assert_eq_json(
util::assert_eq_cmd_json(
cmd,
r#"{
"flag": true,
Expand All @@ -113,7 +113,7 @@ async fn test_get_item_output_yaml() -> Result<(), Box<dyn std::error::Error>> {
"-o",
"yaml",
]);
util::assert_eq_yaml(
util::assert_eq_cmd_yaml(
cmd,
r#"---
flag: true
Expand All @@ -140,7 +140,7 @@ async fn test_get_item_output_raw() -> Result<(), Box<dyn std::error::Error>> {
"-o",
"raw",
]);
util::assert_eq_json(
util::assert_eq_cmd_json(
cmd,
r#"{
"flag": {
Expand Down
10 changes: 7 additions & 3 deletions tests/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,22 @@ pub async fn cleanup_config(dummy_dir: &str) -> io::Result<()> {
remove_dir_all(dummy_dir)
}

pub fn assert_eq_json(cmd: &mut Command, expected: &str) {
pub fn assert_eq_cmd_json(cmd: &mut Command, expected: &str) {
cmd.assert().success();
let stdout = cmd.output().unwrap().stdout;
let output = String::from_utf8(stdout).unwrap();

assert_eq_json(&output, expected)
}

pub fn assert_eq_json(content: &str, expected: &str) {
assert_eq!(
output.parse::<serde_json::Value>().unwrap(),
content.parse::<serde_json::Value>().unwrap(),
expected.parse::<serde_json::Value>().unwrap(),
)
}

pub fn assert_eq_yaml(cmd: &mut Command, expected: &str) {
pub fn assert_eq_cmd_yaml(cmd: &mut Command, expected: &str) {
cmd.assert().success();
let stdout = cmd.output().unwrap().stdout;
let output = String::from_utf8(stdout).unwrap();
Expand Down

0 comments on commit 95c25f9

Please sign in to comment.