Skip to content
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
3 changes: 2 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[alias]
xtask = "run --package xtask --"
xtask = "run --package xtask --"
test-clean = "run --package xtask -- test-clean"
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ sha2 = "0.10"
thiserror = "2"
url = "2"
web-time = "=1.1.0"
# dev dependencies
tokio = { version = "1.5", features = ["macros", "rt", "rt-multi-thread"] }

22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,26 @@ docker run --rm \
--additional-properties useSingleRequestParameter=true
```

### Testing

Make sure you have a Typesense server up and running:

```bash
docker compose up
```

Then run this command in the root folder to run the integration tests:

```bash
cargo test-clean -- --all-features
```

This is an alias command which will run a script to clean up your Typesense server after the tests finish. You can pass any arguments of `cargo test` after the `--`.

To run test for wasm (chrome, headless):

```bash
cargo test-clean --wasm
```

If you'd like to contribute, please join our [Slack Community](https://join.slack.com/t/typesense-community/shared_invite/zt-mx4nbsbn-AuOL89O7iBtvkz136egSJg) and say hello!
2 changes: 1 addition & 1 deletion typesense/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ trybuild = "1.0.42"

# native-only dev deps
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.5", features = ["macros", "rt", "rt-multi-thread"] }
tokio = { workspace = true}
wiremock = "0.6"

# wasm test deps
Expand Down
2 changes: 2 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ clap = { workspace = true }
reqwest = { version = "0.12", features = ["blocking"] } # "blocking" is simpler for scripts
serde = { workspace = true }
serde_yaml = { workspace = true }
typesense = { path = "../typesense"}
tokio = { workspace = true}
22 changes: 22 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ use clap::{Parser, ValueEnum};
use std::{env, fs, process::Command};
mod add_vendor_attributes;
mod preprocess_openapi;
mod test_clean;
mod vendor_attributes;

use preprocess_openapi::preprocess_openapi_file;
use test_clean::test_clean;

const SPEC_URL: &str =
"https://raw.githubusercontent.com/typesense/typesense-api-spec/master/openapi.yml";
Expand All @@ -27,6 +30,14 @@ struct Cli {
/// The list of tasks to run in sequence.
#[arg(required = true, value_enum)]
tasks: Vec<Task>,

/// Flag to run tests for wasm.
#[arg(long)]
wasm: bool,

/// Arguments to forward to cargo test
#[arg(last(true))]
test_args: Vec<String>,
}

#[derive(ValueEnum, Clone, Debug)]
Expand All @@ -38,18 +49,29 @@ enum Task {
Fetch,
/// Preprocesses fetched OpenAPI spec file into a new one
Preprocess,
/// Clean up test artifacts, e.g., collections
TestClean,
}

fn main() -> Result<()> {
let cli = Cli::parse();

let rt = tokio::runtime::Runtime::new().unwrap();

for task in cli.tasks {
println!("▶️ Running task: {:?}", task);
match task {
Task::CodeGen => task_codegen()?,
Task::Fetch => task_fetch_api_spec()?,
Task::Preprocess => preprocess_openapi_file(INPUT_SPEC_FILE, OUTPUT_PREPROCESSED_FILE)
.expect("Preprocess failed, aborting!"),
Task::TestClean => {
let test_args = cli.test_args.clone();
let is_wasm = cli.wasm;
rt.block_on(async move {
test_clean(is_wasm, test_args).await;
});
}
}
}
Ok(())
Expand Down
68 changes: 68 additions & 0 deletions xtask/src/test_clean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::time::Duration;
use typesense::{Client, ExponentialBackoff, models::GetCollectionsParameters};

async fn clean_test_artifacts() {
let client = Client::builder()
.nodes(vec!["http://localhost:8108"])
.api_key("xyz")
.healthcheck_interval(Duration::from_secs(5))
.retry_policy(ExponentialBackoff::builder().build_with_max_retries(3))
.connection_timeout(Duration::from_secs(3))
.build()
.expect("Failed to create Typesense client");

let collections = client
.collections()
.retrieve(GetCollectionsParameters::new())
.await
.expect("Get all collections failed!");

println!("Cleaning up test collections...");

let mut collection_count = 0;

for collection in collections.iter() {
if !collection.name.starts_with("test_") {
continue;
}

if let Err(err) = client
.collection_schemaless(&collection.name)
.delete()
.await
{
eprintln!("Failed to delete {}: {}", collection.name, err);
} else {
collection_count += 1;
println!("Deleted {}", collection.name);
}
}
println!("Deleted {} test collections.", collection_count);
println!("✅ Cleanup complete.");
}

pub async fn test_clean(is_wasm: bool, args: Vec<String>) {
let status = if is_wasm {
println!("Running wasm-pack test...");
std::process::Command::new("wasm-pack")
.arg("test")
.arg("--headless")
.arg("--chrome")
.args(&args)
.arg("typesense")
.status()
.expect("Failed to run wasm-pack test")
} else {
println!("Running cargo test with arguments: {}", args.join(" "));
std::process::Command::new("cargo")
.arg("test")
.args(&args)
.status()
.expect("Failed to run cargo test")
};

clean_test_artifacts().await;

// Propagate cargo test exit code
std::process::exit(status.code().unwrap_or(1));
}
Loading