Skip to content

Commit

Permalink
feat: add basic examples (#7)
Browse files Browse the repository at this point in the history
* add basic examples

* fix naming

* fix default value

* run examples in CI

* chore
  • Loading branch information
jiacai2050 authored Dec 26, 2022
1 parent b50e7c0 commit e96b85f
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ jobs:
run: |
make test
echo "Checking if ${LOCK_FILE} has changed..."
- name: Run examples
run: |
make basic-example
- name: Check Lock File
run: |
diff ${LOCK_FILE} ${LOCK_FILE}.bak
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ check-license:

clippy:
cd $(DIR); cargo clippy --all-targets --all-features --workspace -- -D warnings

basic-example:
cd $(DIR); cargo run --example basic -- examples/basic.toml
7 changes: 7 additions & 0 deletions examples/basic-case/simple/select.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
*
FROM
`table`;

ok

4 changes: 4 additions & 0 deletions examples/basic-case/simple/select.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
*
FROM
`table`;
64 changes: 64 additions & 0 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.

use std::{env, fmt::Display, process};

use async_trait::async_trait;
use sqlness::{Database, Environment, Runner, SqlnessError};

struct MyEnv;
struct MyDB;

#[async_trait]
impl Database for MyDB {
async fn query(&self, query: String) -> Box<dyn Display> {
// Implement query logic here
println!("Exec {}...", query);
return Box::new("ok".to_string());
}
}

impl MyDB {
fn new(_env: &str, _config: Option<String>) -> Self {
MyDB
}

fn stop(self) {
println!("MyDB stopped...");
}
}

#[async_trait]
impl Environment for MyEnv {
type DB = MyDB;

async fn start(&self, env: &str, config: Option<String>) -> Self::DB {
println!("MyEnv start, env:{}, config:{:?}", env, config);
MyDB::new(env, config)
}

async fn stop(&self, env: &str, database: Self::DB) {
println!("MyEnv stop, env:{}", env,);
database.stop();
}
}

#[tokio::main]
async fn main() -> Result<(), SqlnessError> {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
println!("Usage: {} config-path", args[0]);
process::exit(1);
}

let env = MyEnv;
let config_path = &args[1];
let runner = Runner::new(config_path, env)
.await
.expect("Create Runner failed");

println!("Run testcase...");

runner.run().await?;

Ok(())
}
1 change: 1 addition & 0 deletions examples/basic.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
case_dir = "examples/basic-case"
49 changes: 43 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,61 @@ use serde::{Deserialize, Serialize};
pub struct Config {
pub case_dir: String,
/// Default value: `sql`
#[builder(default = "String::from(\"sql\")")]
#[builder(default = "Config::default_test_case_extension()")]
#[serde(default = "Config::default_test_case_extension")]
pub test_case_extension: String,
/// Default value: `output`
#[builder(default = "String::from(\"output\")")]
#[builder(default = "Config::default_output_result_extension()")]
#[serde(default = "Config::default_output_result_extension")]
pub output_result_extension: String,
/// Default value: `result`
#[builder(default = "String::from(\"result\")")]
#[builder(default = "Config::default_expect_result_extension()")]
#[serde(default = "Config::default_expect_result_extension")]
pub expect_result_extension: String,
/// Default value: `-- SQLNESS`
#[builder(default = "String::from(\"-- SQLNESS\")")]
#[builder(default = "Config::default_interceptor_prefix()")]
#[serde(default = "Config::default_interceptor_prefix")]
pub interceptor_prefix: String,
/// Default value: `config.toml`
#[builder(default = "String::from(\"config.toml\")")]
#[builder(default = "Config::default_env_config_file()")]
#[serde(default = "Config::default_env_config_file")]
pub env_config_file: String,
/// Fail this run as soon as one case fails if true
#[builder(default = "true")]
#[serde(default = "Config::default_fail_fast")]
pub fail_fast: bool,
/// If specified, only run cases containing this string in their names.
#[builder(default = "String::new()")]
#[builder(default = "Config::default_test_filter()")]
#[serde(default = "Config::default_test_filter")]
pub test_filter: String,
}

impl Config {
fn default_test_case_extension() -> String {
"sql".to_string()
}

fn default_output_result_extension() -> String {
"output".to_string()
}

fn default_expect_result_extension() -> String {
"result".to_string()
}

fn default_interceptor_prefix() -> String {
"-- SQLNESS".to_string()
}

fn default_env_config_file() -> String {
"config.toml".to_string()
}

fn default_fail_fast() -> bool {
true
}

fn default_test_filter() -> String {
"".to_string()
}
}
4 changes: 2 additions & 2 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ impl<E: Environment> Runner<E> {
Ok(false) => {}
Err(e) => {
if self.config.fail_fast {
errors.push((case_name, e))
} else {
println!("Case {} failed with error {:?}", case_name, e);
println!("Stopping environment {} due to previous error.", env);
break;
} else {
errors.push((case_name, e))
}
}
}
Expand Down

0 comments on commit e96b85f

Please sign in to comment.