Initial SQLite functionality (#5182)

* Add SQLite functionality to open

* Add in-memory SQLite tests

* clippy fixes

* Fix up old SQLite-related tests
This commit is contained in:
Reilly Wood
2022-04-13 20:15:02 -07:00
committed by GitHub
parent 87c684c7da
commit c150e11cb4
9 changed files with 312 additions and 151 deletions

View File

@ -93,7 +93,7 @@ fn md5_works_with_file() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db | hash md5
open sample.db --raw | hash md5
"#
)
);
@ -106,7 +106,7 @@ fn sha256_works_with_file() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db | hash sha256
open sample.db --raw | hash sha256
"#
)
);

View File

@ -84,63 +84,38 @@ fn parses_more_bson_complexity() {
// sample.db has the following format:
//
// ━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━
// # │ table_name │ table_values
// ───┼────────────┼──────────────
// 0 │ strings │ [6 items]
// 1 │ ints │ [5 items]
// 2 │ floats │ [4 items]
// ━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━
// ╭─────────┬────────────────╮
// │ strings[table 6 rows] │
// │ ints │ [table 5 rows] │
// │ floats │ [table 4 rows] │
// ╰─────────┴────────────────╯
//
// In this case, this represents a sqlite database
// with three tables named `strings`, `ints`, and `floats`.
// The table_values represent the values for the tables:
//
// ━━━━┯━━━━━━━┯━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// # │ x │ y │ z │ f
// ────┼───────┼──────────┼──────┼──────────────────────────────────────────────────────────────────────
// 0 │ hello │ <binary> │ │
// 1 │ hello │ <binary> │ │
// 2 │ hello │ <binary> │ │
// 3 │ hello │ <binary> │ │
// 4 │ world │ <binary> │ │
// 5 │ world │ <binary> │ │
// 6 │ │ │ 1 │
// 7 │ │ │ 42 │
// 8 │ │ │ 425 │
// 9 │ │ │ 4253 │
// 10 │ │ │ │
// 11 │ │ │ │ 3.400000000000000
// 12 │ │ │ │ 3.141592650000000
// 13 │ │ │ │ 23.00000000000000
// 14 │ │ │ │ this string that doesn't really belong here but sqlite is what it is
// ━━━━┷━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
//
// We can see here that each table has different columns. `strings` has `x` and `y`, while
// `ints` has just `z`, and `floats` has only the column `f`. This means, in general, when working
// Each table has different columns. `strings` has `x` and `y`, while
// `ints` has just `z`, and `floats` has only the column `f`. In general, when working
// with sqlite, one will want to select a single table, e.g.:
//
// open sample.db | select 1 | get table_values
// ━━━┯━━━━━━
// # │ z
// ───┼──────
// 0 │ 1
// 1 │ 42
// 2 │ 425
// 3 │ 4253
// 4 │
// ━━━┷━━━━━━
// open sample.db | get ints
// ╭───┬──────╮
// # │ z │
// ───┼──────
// 0 │ 1
// 1 │ 42
// 2 │ 425
// 3 │ 4253
// 4 │
// ╰───┴──────╯
#[cfg(feature = "sqlite")]
#[test]
fn parses_sqlite() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| get table_values
| select 2
| get x
| get strings
| get x.0
"#
));

View File

@ -1,6 +1,4 @@
use nu_test_support::nu;
#[cfg(feature = "sqlite")]
use nu_test_support::pipeline;
#[test]
@ -43,36 +41,16 @@ fn where_not_in_table() {
assert_eq!(actual.out, "4");
}
#[cfg(feature = "sqlite")]
#[test]
fn explicit_block_condition() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| where table_name == ints
| get table_values
| first 4
| where {= $it.z > 4200}
| get z
"#
));
assert_eq!(actual.out, "4253");
}
#[cfg(feature = "sqlite")]
#[test]
fn binary_operator_comparisons() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| where table_name == ints
| get table_values
| get ints
| first 4
| where z > 4200
| get z
| get z.0
"#
));
@ -82,11 +60,10 @@ fn binary_operator_comparisons() {
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| where table_name == ints
| get table_values
| get ints
| first 4
| where z >= 4253
| get z
| get z.0
"#
));
@ -96,11 +73,10 @@ fn binary_operator_comparisons() {
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| where table_name == ints
| get table_values
| get ints
| first 4
| where z < 10
| get z
| get z.0
"#
));
@ -110,11 +86,10 @@ fn binary_operator_comparisons() {
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| where table_name == ints
| get table_values
| get ints
| first 4
| where z <= 1
| get z
| get z.0
"#
));
@ -124,8 +99,7 @@ fn binary_operator_comparisons() {
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| where table_name == ints
| get table_values
| get ints
| where z != 1
| first 1
| get z
@ -135,15 +109,13 @@ fn binary_operator_comparisons() {
assert_eq!(actual.out, "42");
}
#[cfg(feature = "sqlite")]
#[test]
fn contains_operator() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| where table_name == strings
| get table_values
| get strings
| where x =~ ell
| length
"#
@ -155,8 +127,7 @@ fn contains_operator() {
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| where table_name == strings
| get table_values
| get strings
| where x !~ ell
| length
"#

View File

@ -7,7 +7,6 @@ mod json;
mod markdown;
mod nuon;
mod ods;
mod sqlite;
mod ssv;
mod toml;
mod tsv;

View File

@ -1,36 +0,0 @@
#[cfg(feature = "sqlite")]
use nu_test_support::{nu, pipeline};
#[cfg(feature = "sqlite")]
#[test]
fn table_to_sqlite_and_back_into_table() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| to sqlite
| from sqlite
| get table_values
| select 2
| get x
"#
));
assert_eq!(actual.out, "hello");
}
#[cfg(feature = "sqlite")]
#[test]
fn table_to_sqlite_and_back_into_table_select_table() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
open sample.db
| to sqlite
| from sqlite -t [strings]
| get table_names
"#
));
assert_eq!(actual.out, "strings");
}