Skip to content

Commit

Permalink
release 0.10.0 & improve README & add update_test_file (#132)
Browse files Browse the repository at this point in the history
* release 0.10.0 & improve README

Signed-off-by: xxchan <xxchan22f@gmail.com>

* fix halt for bin::update_test_file, and add runner::update_test_file

Signed-off-by: xxchan <xxchan22f@gmail.com>

* CHANGELOG

Signed-off-by: xxchan <xxchan22f@gmail.com>

Signed-off-by: xxchan <xxchan22f@gmail.com>
  • Loading branch information
xxchan authored Dec 16, 2022
1 parent cdc108a commit 29f219a
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 48 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.10.0] - 2022-12-15

- Improve the ability to unparse and update the test files. Mainly add `update_record_with_output` and `update_test_file` to the library.

More details:
* Add `impl Display` for `Record` (refactor `unparse`).
* Add `Record::Whitespace` so the whitespace in the original files can be reconstructed during `unparse`.
* Add tests for unparsing and updating records.
* Refactor and fix the behavior about newlines and `halt` for CLI options `--override` and `--format`.
- Fix: `hash-threshold` should be compared with the number of values instead of the number of rows.
- **Breaking change**: The type of `Validator` is changed from `fn(&Vec<String>, &Vec<String>) -> bool` to `fn(&[Vec<String>], &[String]) -> bool`. Also added a `default_validator`.

Thanks to the contributions of @alamb and @xudong963 .

## [0.9.0] - 2022-12-07

- Improve the format and color handling for errors.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
members = ["sqllogictest", "sqllogictest-bin", "examples/*", "tests"]

[workspace.package]
version = "0.9.0"
version = "0.10.0"
edition = "2021"
homepage = "https://github.com/risinglightdb/sqllogictest-rs"
keywords = ["sql", "database", "parser", "cli"]
Expand Down
4 changes: 0 additions & 4 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
MIT License

Copyright (c) 2021 Singularity Data Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
68 changes: 28 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

[![Crate](https://img.shields.io/crates/v/sqllogictest.svg)](https://crates.io/crates/sqllogictest)
[![Docs](https://docs.rs/sqllogictest/badge.svg)](https://docs.rs/sqllogictest)
[![CI](https://github.com/singularity-data/sqllogictest-rs/workflows/CI/badge.svg?branch=main)](https://github.com/singularity-data/sqllogictest-rs/actions)
[![CI](https://github.com/risinglightdb/sqllogictest-rs/workflows/CI/badge.svg?branch=main)](https://github.com/risinglightdb/sqllogictest-rs/actions)

[Sqllogictest][Sqllogictest] is a testing framework to verify the correctness of an SQL database.

This crate implements a sqllogictest parser and runner in Rust.
This repository provides two crates:
- `sqllogictest` is a library containing sqllogictest parser and runner.
- `sqllogictest-bin` is a CLI tool to run sqllogictests.

[Sqllogictest]: https://www.sqlite.org/sqllogictest/doc/trunk/about.wiki

## Using as Library
## Use the library

Add the following lines to your `Cargo.toml` file:
To add the dependency to your project:

```toml
[dependencies]
sqllogictest = "0.9"
```sh
cargo add sqllogictest
```

Implement `DB` trait for your database structure:
Expand All @@ -26,38 +27,32 @@ struct Database {...}

impl sqllogictest::DB for Database {
type Error = ...;
fn run(&mut self, sql: &str) -> Result<String, Self::Error> {
fn run(&mut self, sql: &str) -> Result<sqllogictest::DBOutput, Self::Error> {
...
}
}
```

It should take an SQL query string as input, and output the query result as a string.
The runner verifies the results by comparing the string after normalization.

Finally, create a `Runner` on your database instance, and then run the script:
Then create a `Runner` on your database instance, and run the tests:

```rust
let mut tester = sqllogictest::Runner::new(Database::new());
let script = std::fs::read_to_string("script.slt").unwrap();
tester.run_script(&script);
let db = Database {...};
let mut tester = sqllogictest::Runner::new(db);
tester.run_file("script.slt").unwrap();
```

You can also parse the script and execute the records separately:

```rust
let records = sqllogictest::parse(&script).unwrap();
let records = sqllogictest::parse_file("script.slt").unwrap();
for record in records {
tester.run(record);
tester.run(record).unwrap();
}
```

## Use the CLI tool

## Using as CLI

This crate can also be used as a command-line tool.

To install the binary, the `bin` feature is required:
To install the binary:

```sh
cargo install sqllogictest-bin
Expand All @@ -71,9 +66,11 @@ sqllogictest './test/**/*.slt'

This command will run scripts in `test` directory against postgres with default connection settings.

You can find more options in `sqllogictest --help`.
You can find more options in `sqllogictest --help` .

Note that only postgres is supported now.
> **Note**
>
> Currently only postgres is supported in the CLI tool.
## `.slt` Test File Format Cookbook

Expand Down Expand Up @@ -111,19 +108,13 @@ statement error Multiple object drop not supported
DROP VIEW foo, bar;
```

## Used by

## License

Licensed under either of
- [RisingLight](https://github.com/risinglightdb/risinglight): An OLAP database system for educational purpose
- [RisingWave](https://github.com/risingwavelabs/risingwave): The next-generation streaming database in the cloud
- [DataFusion](https://github.com/apache/arrow-datafusion): Apache Arrow DataFusion SQL Query Engine

* Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

## Contribution
## Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
Expand All @@ -132,9 +123,6 @@ dual licensed as above, without any additional terms or conditions.
Contributors should add a Signed-off-by line for [Developer Certificate of Origin](https://github.com/probot/dco#how-it-works)
in their commits. Use `git commit -s` to sign off commits.

## Publish the crate
## License

```
cargo publish -p sqllogictest
cargo publish -p sqllogictest-bin
```
This project is available under the terms of either the [Apache 2.0 license](LICENSE-APACHE) or the [MIT license](LICENSE-MIT).
3 changes: 2 additions & 1 deletion sqllogictest-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ rand = "0.8"
rust_decimal = { version = "1.7.0", features = ["tokio-pg"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sqllogictest = { path = "../sqllogictest", version = "0.9" }
sqllogictest = { path = "../sqllogictest", version = "0.10" }
tempfile = "3"
thiserror = "1"
tokio = { version = "1", features = [
Expand All @@ -43,3 +43,4 @@ tokio = { version = "1", features = [
] }
tokio-postgres = { version = "0.7" }
tokio-util = { version = "0.7", features = ["codec"] }
fs-err = "2.9.0"
3 changes: 3 additions & 0 deletions sqllogictest-bin/src/bin/sqllogictest.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! We use `bin/sqllogictest.rs` instead of `main.rs` so that the installed binary
//! is named `sqllogictest` instead of `sqllogictest-bin`.
use anyhow::Result;

#[tokio::main]
Expand Down
20 changes: 18 additions & 2 deletions sqllogictest-bin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod engines;

use fs_err::{File, OpenOptions};
use std::collections::BTreeMap;
use std::fs::{File, OpenOptions};
use std::io::{stdout, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -403,6 +403,7 @@ async fn connect_and_run_test_file(
Ok(result)
}

/// Different from [`Runner::run_file_async`], we re-implement it here to print some progress information.
async fn run_test_file<T: std::io::Write, D: AsyncDB>(
out: &mut T,
mut runner: Runner<D>,
Expand Down Expand Up @@ -504,6 +505,7 @@ fn finish_test_file<T: std::io::Write>(
Ok::<_, anyhow::Error>(())
}

/// Different from [`sqllogictest::update_test_file`], we re-implement it here to print some progress information.
async fn update_test_file<T: std::io::Write, D: AsyncDB>(
out: &mut T,
mut runner: Runner<D>,
Expand Down Expand Up @@ -567,7 +569,7 @@ async fn update_test_file<T: std::io::Write, D: AsyncDB>(
}
}

std::fs::rename(outfilename, filename)?;
fs_err::rename(outfilename, filename)?;

Ok(())
}
Expand All @@ -576,19 +578,22 @@ async fn update_test_file<T: std::io::Write, D: AsyncDB>(
filename: String,
outfilename: PathBuf,
outfile: File,
halt: bool,
}
let (outfilename, outfile) = create_outfile(filename)?;
let mut stack = vec![Item {
filename: filename.to_string_lossy().to_string(),
outfilename,
outfile,
halt: false,
}];

for record in records {
let Item {
filename,
outfilename,
outfile,
halt,
} = stack.last_mut().unwrap();

match &record {
Expand All @@ -598,6 +603,7 @@ async fn update_test_file<T: std::io::Write, D: AsyncDB>(
filename: filename.clone(),
outfilename,
outfile,
halt: false,
});

begin_times.push(Instant::now());
Expand All @@ -621,6 +627,15 @@ async fn update_test_file<T: std::io::Write, D: AsyncDB>(
finish_test_file(out, &mut begin_times, &mut did_pop, file)?;
}
_ => {
if *halt {
writeln!(outfile, "{}", record)?;
continue;
}
if matches!(record, Record::Halt { .. }) {
*halt = true;
writeln!(outfile, "{}", record)?;
continue;
}
update_record(outfile, &mut runner, record, format)
.await
.context(format!("failed to run `{}`", style(filename).bold()))?;
Expand All @@ -639,6 +654,7 @@ async fn update_test_file<T: std::io::Write, D: AsyncDB>(
filename,
outfilename,
outfile,
halt: _,
} = stack.last_mut().unwrap();
override_with_outfile(filename, outfilename, outfile)?;

Expand Down
1 change: 1 addition & 0 deletions sqllogictest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ libtest-mimic = "0.6"
regex = "1.7.0"
owo-colors = "3.5.0"
md5 = "0.7.0"
fs-err = "2.9.0"
Loading

0 comments on commit 29f219a

Please sign in to comment.