Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add integration tests for scan #217

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 155 additions & 2 deletions tests/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

pub mod util;

use assert_cmd::prelude::*; // Add methods on commands
use predicates::prelude::*; // Used for writing assertions
use crate::util::TemporaryItem;
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::test]
async fn test_scan_non_existent_table() -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -71,3 +74,153 @@ async fn test_simple_scan() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

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

let mut c = tm.command()?;
let scan_cmd = c.args(["--region", "local", "--table", &table_name, "scan"]);
let scan_exec = scan_cmd
.assert()
.success()
.stdout(predicate::str::contains("1"));

let scan_consistent_cmd = scan_cmd.args(["--consistent-read"]);
scan_consistent_cmd
.assert()
.success()
.stdout(scan_exec.get_output().stdout.to_owned());

Ok(())
}

#[tokio::test]
async fn test_index_scan() -> Result<(), Box<dyn std::error::Error>> {
ryota-sakamoto marked this conversation as resolved.
Show resolved Hide resolved
let mut tm = util::setup().await?;
let table_name = tm
.create_temporary_table_with_items(
"pk,S",
None,
[TemporaryItem::new("1", None, Some("{'sk':'1'}"))],
)
.await?;

let mut create_idx_cmd = tm.command()?;
create_idx_cmd
.args([
"--region",
"local",
"--table",
&table_name,
"admin",
"create",
"index",
"idx",
"--keys",
"sk,S",
])
.assert()
.success();

// This sleep is required to prevent InternalFailure
sleep(Duration::from_secs(1)).await;

let mut scan_cmd = tm.command()?;
let scan_exec = scan_cmd
.args(["--region", "local", "--table", &table_name, "scan"])
.assert()
.success()
.stdout(predicate::str::contains("sk"));

let mut scan_idx_cmd = tm.command()?;
scan_idx_cmd
.args([
"--region",
"local",
"--table",
&table_name,
"scan",
"--index",
"idx",
])
.assert()
.success()
.stdout(scan_exec.get_output().stdout.to_owned());

Ok(())
}

#[tokio::test]
async fn test_scan_with_attributes() -> Result<(), Box<dyn std::error::Error>> {
let mut tm = util::setup().await?;
let table_name = tm
.create_temporary_table_with_items(
"pk,S",
None,
[TemporaryItem::new(
"1",
None,
Some("{'opt1':'1','opt2':'2'}"),
)],
)
.await?;

let mut scan_cmd = tm.command()?;
scan_cmd
.args([
"--region",
"local",
"--table",
&table_name,
"scan",
"-a",
"opt1",
])
.assert()
.success()
.stdout(predicate::str::contains("opt1").and(predicate::str::contains("opt2").not()));

Ok(())
}

#[tokio::test]
async fn test_scan_with_limits() -> Result<(), Box<dyn std::error::Error>> {
let mut tm = util::setup().await?;
let table_name = tm
.create_temporary_table_with_items(
"pk,S",
None,
[
TemporaryItem::new("opt1", None, None),
TemporaryItem::new("opt2", None, None),
],
)
.await?;

let mut scan_cmd = tm.command()?;
scan_cmd
.args([
"--region",
"local",
"--table",
&table_name,
"scan",
"--limit",
"1",
])
.assert()
.success()
.stdout(
predicate::str::contains("opt1")
.and(predicate::str::contains("opt2").not())
.or(predicate::str::contains("opt1")
.not()
.and(predicate::str::contains("opt2"))),
);

Ok(())
}
9 changes: 6 additions & 3 deletions tests/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ impl<'a> TestManager<'a> {
}

/// Create temporary table with items via `create_temporary_table`.
pub async fn create_temporary_table_with_items(
pub async fn create_temporary_table_with_items<ItemIter>(
&mut self,
pk: &'static str,
sk: Option<&'static str>,
items: Vec<TemporaryItem>,
) -> Result<String, Box<dyn std::error::Error>> {
items: ItemIter,
) -> Result<String, Box<dyn std::error::Error>>
where
ItemIter: IntoIterator<Item = TemporaryItem>,
{
let table_name = self.create_temporary_table(pk, sk).await?;

for ti in items {
Expand Down
Loading