mirror of
https://github.com/nushell/nushell
synced 2025-01-27 20:35:43 +00:00
add some tests for stor create
(#11194)
# Description This is a PR to start adding a few tests to the `stor` commands. It refactors the `stor create` command so it's easier to write tests. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
This commit is contained in:
parent
80881c75f9
commit
e5d9f405db
1 changed files with 158 additions and 72 deletions
|
@ -62,6 +62,18 @@ impl Command for StorCreate {
|
||||||
let columns: Option<Record> = call.get_flag(engine_state, stack, "columns")?;
|
let columns: Option<Record> = call.get_flag(engine_state, stack, "columns")?;
|
||||||
let db = Box::new(SQLiteDatabase::new(std::path::Path::new(MEMORY_DB), None));
|
let db = Box::new(SQLiteDatabase::new(std::path::Path::new(MEMORY_DB), None));
|
||||||
|
|
||||||
|
process(table_name, span, &db, columns)?;
|
||||||
|
// dbg!(db.clone());
|
||||||
|
Ok(Value::custom_value(db, span).into_pipeline_data())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process(
|
||||||
|
table_name: Option<String>,
|
||||||
|
span: Span,
|
||||||
|
db: &SQLiteDatabase,
|
||||||
|
columns: Option<Record>,
|
||||||
|
) -> Result<(), ShellError> {
|
||||||
if table_name.is_none() {
|
if table_name.is_none() {
|
||||||
return Err(ShellError::MissingParameter {
|
return Err(ShellError::MissingParameter {
|
||||||
param_name: "requires at table name".into(),
|
param_name: "requires at table name".into(),
|
||||||
|
@ -129,14 +141,12 @@ impl Command for StorCreate {
|
||||||
None => {
|
None => {
|
||||||
return Err(ShellError::MissingParameter {
|
return Err(ShellError::MissingParameter {
|
||||||
param_name: "requires at least one column".into(),
|
param_name: "requires at least one column".into(),
|
||||||
span: call.head,
|
span,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// dbg!(db.clone());
|
Ok(())
|
||||||
Ok(Value::custom_value(db, span).into_pipeline_data())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -149,4 +159,80 @@ mod test {
|
||||||
|
|
||||||
test_examples(StorCreate {})
|
test_examples(StorCreate {})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process_with_valid_parameters() {
|
||||||
|
let table_name = Some("test_table".to_string());
|
||||||
|
let span = Span::unknown();
|
||||||
|
let db = Box::new(SQLiteDatabase::new(std::path::Path::new(MEMORY_DB), None));
|
||||||
|
let mut columns = Record::new();
|
||||||
|
columns.insert(
|
||||||
|
"int_column".to_string(),
|
||||||
|
Value::test_string("int".to_string()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = process(table_name, span, &db, Some(columns));
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process_with_missing_table_name() {
|
||||||
|
let table_name = None;
|
||||||
|
let span = Span::unknown();
|
||||||
|
let db = Box::new(SQLiteDatabase::new(std::path::Path::new(MEMORY_DB), None));
|
||||||
|
let mut columns = Record::new();
|
||||||
|
columns.insert(
|
||||||
|
"int_column".to_string(),
|
||||||
|
Value::test_string("int".to_string()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = process(table_name, span, &db, Some(columns));
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(result
|
||||||
|
.unwrap_err()
|
||||||
|
.to_string()
|
||||||
|
.contains("requires at table name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process_with_missing_columns() {
|
||||||
|
let table_name = Some("test_table".to_string());
|
||||||
|
let span = Span::unknown();
|
||||||
|
let db = Box::new(SQLiteDatabase::new(std::path::Path::new(MEMORY_DB), None));
|
||||||
|
|
||||||
|
let result = process(table_name, span, &db, None);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(result
|
||||||
|
.unwrap_err()
|
||||||
|
.to_string()
|
||||||
|
.contains("requires at least one column"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_process_with_unsupported_column_data_type() {
|
||||||
|
let table_name = Some("test_table".to_string());
|
||||||
|
let span = Span::unknown();
|
||||||
|
let db = Box::new(SQLiteDatabase::new(std::path::Path::new(MEMORY_DB), None));
|
||||||
|
let mut columns = Record::new();
|
||||||
|
let column_datatype = "bogus_data_type".to_string();
|
||||||
|
columns.insert(
|
||||||
|
"column0".to_string(),
|
||||||
|
Value::test_string(column_datatype.clone()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = process(table_name, span, &db, Some(columns));
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
|
||||||
|
let expected_err = ShellError::UnsupportedInput {
|
||||||
|
msg: "unsupported column data type".into(),
|
||||||
|
input: format!("{:?}", column_datatype.clone()),
|
||||||
|
msg_span: Span::test_data(),
|
||||||
|
input_span: Span::test_data(),
|
||||||
|
};
|
||||||
|
assert_eq!(result.unwrap_err().to_string(), expected_err.to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue