Skip to content

Commit

Permalink
add auto-tests for rust snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
generall committed Jun 19, 2024
1 parent f74f34a commit 3583f80
Show file tree
Hide file tree
Showing 81 changed files with 1,938 additions and 0 deletions.
72 changes: 72 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use std::fs;
use std::path::{Path, PathBuf};


fn generate_snippet_test(snippet: &str, name: &str, dir: &Path) -> () {

// Pad each line of the snippet with 8 spaces

let snippet = snippet.lines().map(|line| format!(" {}", line)).collect::<Vec<_>>().join("\n");

let test_snippet = format!(
r#"
#[tokio::test]
async fn test_{name}() {{
async fn {name}() -> Result<(), Box<dyn std::error::Error>> {{
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/{name}.rs` file
{snippet}
Ok(())
}}
let _ = {name}().await;
}}
"#
);

// Write the test snippet to the file
let mut test_snippet_path = PathBuf::from(dir);
test_snippet_path.push(format!("test_{}.rs", name));
fs::write(test_snippet_path, test_snippet).unwrap();
}


fn main() {
println!("cargo:rerun-if-changed=tests/snippets");

// Open all files in the `./tests/snippets` directory and save them to the `./tests/snippets_converted` directory
// Wrap text in the `tests/test_snippets.rs` file into { }
let snippets_dir = Path::new("./tests/snippets");
let tests_output_dir = Path::new("./tests/snippet_tests");

// Create the converted directory if it doesn't exist
if !tests_output_dir.exists() {
fs::create_dir_all(&tests_output_dir).unwrap();
}

let snippet_names: Vec<_> = fs::read_dir(snippets_dir)
.unwrap()
.into_iter().filter_map(|entry| {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
let snippet_name = path.file_stem().unwrap().to_str().unwrap().to_string();
Some((path, snippet_name))
} else {
None
}
}).collect();

for (path, name) in &snippet_names {
let content = fs::read_to_string(path).unwrap();
generate_snippet_test(&content, name, tests_output_dir);
}

// Generate `tests/snippet_tests/mod.rs` file
// For each file in `./tests/snippet_tests` directory, generate a line `mode {file_name};`

let mod_file = snippet_names.iter().map(|(_, name)| format!("mod test_{};", name)).collect::<Vec<_>>().join("\n");

let mut mod_file_path = PathBuf::from(tests_output_dir);
mod_file_path.push("mod.rs");
fs::write(mod_file_path, mod_file).unwrap();
}
39 changes: 39 additions & 0 deletions tests/snippet_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
mod test_create_snapshot;
mod test_set_payload;
mod test_overwrite_payload;
mod test_delete_payload;
mod test_count_points;
mod test_list_full_snapshots;
mod test_delete_collection;
mod test_get_collections;
mod test_discover_points;
mod test_delete_shard_key;
mod test_create_full_snapshot;
mod test_create_collection;
mod test_search_point_groups;
mod test_update_aliases;
mod test_delete_points;
mod test_get_points;
mod test_update_vectors;
mod test_create_field_index;
mod test_create_shard_key;
mod test_get_collections_aliases;
mod test_get_collection_aliases;
mod test_delete_snapshot;
mod test_delete_field_index;
mod test_clear_payload;
mod test_delete_vectors;
mod test_delete_full_snapshot;
mod test_get_collection;
mod test_recommend_batch_points;
mod test_recommend_point_groups;
mod test_batch_update;
mod test_recommend_points;
mod test_scroll_points;
mod test_search_points;
mod test_search_batch_points;
mod test_upsert_points;
mod test_collection_exists;
mod test_list_snapshots;
mod test_discover_batch_points;
mod test_update_collection;
63 changes: 63 additions & 0 deletions tests/snippet_tests/test_batch_update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

#[tokio::test]
async fn test_batch_update() {
async fn batch_update() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/batch_update.rs` file
use serde_json::json;
use qdrant_client::client::{ QdrantClient, Payload };
use std::collections::HashMap;
use qdrant_client::qdrant::{
points_selector::PointsSelectorOneOf,
points_update_operation::{
Operation, PointStructList, UpdateVectors, OverwritePayload
},
PointStruct, PointVectors, PointsIdsList, PointsSelector, PointsUpdateOperation,
};

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client
.batch_updates_blocking(
"{collection_name}",
&[
PointsUpdateOperation {
operation: Some(Operation::Upsert(PointStructList {
points: vec![PointStruct::new(
1,
vec![1.0, 2.0, 3.0, 4.0],
Payload::try_from(json!({})).unwrap(),
)],
..Default::default()
})),
},
PointsUpdateOperation {
operation: Some(Operation::UpdateVectors(UpdateVectors {
points: vec![PointVectors {
id: Some(1.into()),
vectors: Some(vec![1.0, 2.0, 3.0, 4.0].into()),
}],
..Default::default()
})),
},
PointsUpdateOperation {
operation: Some(Operation::OverwritePayload(OverwritePayload {
points_selector: Some(PointsSelector {
points_selector_one_of: Some(PointsSelectorOneOf::Points(
PointsIdsList {
ids: vec![1.into()],
},
)),
}),
payload: HashMap::from([("test_payload".to_string(), 1.into())]),
..Default::default()
})),
},
],
None,
)
.await?;
Ok(())
}
let _ = batch_update().await;
}
28 changes: 28 additions & 0 deletions tests/snippet_tests/test_clear_payload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#[tokio::test]
async fn test_clear_payload() {
async fn clear_payload() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/clear_payload.rs` file
use qdrant_client::{client::QdrantClient, qdrant::{
points_selector::PointsSelectorOneOf, PointsIdsList, PointsSelector,
}};

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client
.clear_payload(
"{collection_name}",
None,
Some(PointsSelector {
points_selector_one_of: Some(PointsSelectorOneOf::Points(PointsIdsList {
ids: vec![0.into(), 3.into(), 100.into()],
})),
}),
None,
)
.await?;
Ok(())
}
let _ = clear_payload().await;
}
15 changes: 15 additions & 0 deletions tests/snippet_tests/test_collection_exists.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#[tokio::test]
async fn test_collection_exists() {
async fn collection_exists() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/collection_exists.rs` file
use qdrant_client::client::QdrantClient;

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client.collection_exists("{collection_name}").await?;
Ok(())
}
let _ = collection_exists().await;
}
25 changes: 25 additions & 0 deletions tests/snippet_tests/test_count_points.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#[tokio::test]
async fn test_count_points() {
async fn count_points() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/count_points.rs` file
use qdrant_client::{client::QdrantClient, qdrant::{Condition, CountPoints, Filter}};

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client
.count(&CountPoints {
collection_name: "{collection_name}".to_string(),
filter: Some(Filter::must([Condition::matches(
"color",
"red".to_string(),
)])),
exact: Some(true),
..Default::default()
})
.await?;
Ok(())
}
let _ = count_points().await;
}
30 changes: 30 additions & 0 deletions tests/snippet_tests/test_create_collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#[tokio::test]
async fn test_create_collection() {
async fn create_collection() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/create_collection.rs` file
use qdrant_client::{
client::QdrantClient,
qdrant::{vectors_config::Config, CreateCollection, Distance, VectorParams, VectorsConfig},
};

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client
.create_collection(&CreateCollection {
collection_name: "{collection_name}".to_string(),
vectors_config: Some(VectorsConfig {
config: Some(Config::Params(VectorParams {
size: 100,
distance: Distance::Cosine.into(),
..Default::default()
})),
}),
..Default::default()
})
.await?;
Ok(())
}
let _ = create_collection().await;
}
23 changes: 23 additions & 0 deletions tests/snippet_tests/test_create_field_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#[tokio::test]
async fn test_create_field_index() {
async fn create_field_index() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/create_field_index.rs` file
use qdrant_client::{client::QdrantClient, qdrant::FieldType};

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client
.create_field_index(
"{collection_name}",
"{field_name}",
FieldType::Keyword,
None,
None,
)
.await?;
Ok(())
}
let _ = create_field_index().await;
}
15 changes: 15 additions & 0 deletions tests/snippet_tests/test_create_full_snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#[tokio::test]
async fn test_create_full_snapshot() {
async fn create_full_snapshot() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/create_full_snapshot.rs` file
use qdrant_client::client::QdrantClient;

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client.create_full_snapshot().await?;
Ok(())
}
let _ = create_full_snapshot().await;
}
23 changes: 23 additions & 0 deletions tests/snippet_tests/test_create_shard_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#[tokio::test]
async fn test_create_shard_key() {
async fn create_shard_key() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/create_shard_key.rs` file
use qdrant_client::{client::QdrantClient, qdrant::shard_key::Key};

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client
.create_shard_key(
"{collection_name}",
&Key::Keyword("{shard_key".to_string()),
None,
None,
&[],
)
.await?;
Ok(())
}
let _ = create_shard_key().await;
}
15 changes: 15 additions & 0 deletions tests/snippet_tests/test_create_snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#[tokio::test]
async fn test_create_snapshot() {
async fn create_snapshot() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/create_snapshot.rs` file
use qdrant_client::client::QdrantClient;

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client.create_snapshot("{collection_name}").await?;
Ok(())
}
let _ = create_snapshot().await;
}
15 changes: 15 additions & 0 deletions tests/snippet_tests/test_delete_collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#[tokio::test]
async fn test_delete_collection() {
async fn delete_collection() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/delete_collection.rs` file
use qdrant_client::client::QdrantClient;

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client.delete_collection("{collection_name}").await?;
Ok(())
}
let _ = delete_collection().await;
}
17 changes: 17 additions & 0 deletions tests/snippet_tests/test_delete_field_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#[tokio::test]
async fn test_delete_field_index() {
async fn delete_field_index() -> Result<(), Box<dyn std::error::Error>> {
// WARNING: This is a generated test snippet.
// Please, modify the snippet in the `../snippets/delete_field_index.rs` file
use qdrant_client::client::QdrantClient;

let client = QdrantClient::from_url("http://localhost:6334").build()?;

client
.delete_field_index("{collection_name}", "{field_name}", None)
.await?;
Ok(())
}
let _ = delete_field_index().await;
}
Loading

0 comments on commit 3583f80

Please sign in to comment.