Skip to content

Commit

Permalink
Refactor tests (#71)
Browse files Browse the repository at this point in the history
* fix test init

* fix example create
  • Loading branch information
ChuckHend authored Mar 4, 2024
1 parent 3edbd44 commit caae2ca
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 29 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ The following can be found within the this project's README, under [Hugging Face
Begin by creating a `producs` table with the dataset that comes included with `pg_vectorize`.

```sql
CREATE TABLE products AS
TABLE vectorize.example_products WITH DATA;
CREATE TABLE products (LIKE vectorize.example_products INCLUDING ALL);
INSERT INTO products SELECT * FROM vectorize.example_products;
```

You can then confirm everything is correct by running the following:
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ Follow the [installation](#installation) steps if you haven't already.
Setup a products table. Copy from the example data provided by the extension.

```sql
CREATE TABLE products AS
TABLE vectorize.example_products WITH DATA;
CREATE TABLE products (LIKE vectorize.example_products INCLUDING ALL);
INSERT INTO products SELECT * FROM vectorize.example_products;
```

```sql
Expand Down Expand Up @@ -166,8 +166,8 @@ SELECT pg_reload_conf();
Create an example table if it does not already exist.

```sql
CREATE TABLE products AS
TABLE vectorize.example_products WITH DATA;
CREATE TABLE products (LIKE vectorize.example_products INCLUDING ALL);
INSERT INTO products SELECT * FROM vectorize.example_products;
```

Initialize a table for RAG. We'll use an open source Sentence Transformer to generate embeddings.
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/openai_embeddings.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ SELECT pg_reload_conf();
Create an example table if it does not already exist.

```sql
CREATE TABLE products AS
TABLE vectorize.example_products WITH DATA;
CREATE TABLE products (LIKE vectorize.example_products INCLUDING ALL);
INSERT INTO products SELECT * FROM vectorize.example_products;
```

Then create the job.
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/sentence_transformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ SELECT pg_reload_conf();
Create an example table if it does not already exist.

```sql
CREATE TABLE products AS
TABLE vectorize.example_products WITH DATA;
CREATE TABLE products (LIKE vectorize.example_products INCLUDING ALL);
INSERT INTO products SELECT * FROM vectorize.example_products;
```

```sql
Expand Down
20 changes: 9 additions & 11 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ async fn test_scheduled_job() {
let conn = common::init_database().await;
let mut rng = rand::thread_rng();
let test_num = rng.gen_range(1..100000);
let test_table_name = common::init_test_table(test_num, &conn).await;
let test_table_name = format!("products_test_{}", test_num);
common::init_test_table(&test_table_name, &conn).await;
let job_name = format!("job_{}", test_num);

common::init_embedding_svc_url(&conn).await;
Expand Down Expand Up @@ -59,7 +60,8 @@ async fn test_realtime_job() {
common::init_embedding_svc_url(&conn).await;
let mut rng = rand::thread_rng();
let test_num = rng.gen_range(1..100000);
let test_table_name = common::init_test_table(test_num, &conn).await;
let test_table_name = format!("products_test_{}", test_num);
common::init_test_table(&test_table_name, &conn).await;
let job_name = format!("job_{}", test_num);

println!("test_table_name: {}", test_table_name);
Expand Down Expand Up @@ -124,7 +126,8 @@ async fn test_rag() {
common::init_embedding_svc_url(&conn).await;
let mut rng = rand::thread_rng();
let test_num = rng.gen_range(1..100000);
let test_table_name = common::init_test_table(test_num, &conn).await;
let test_table_name = format!("products_test_{}", test_num);
common::init_test_table(&test_table_name, &conn).await;
let agent_name = format!("agent_{}", test_num);

println!("test_table_name: {}", test_table_name);
Expand Down Expand Up @@ -157,7 +160,8 @@ async fn test_rag_alternate_schema() {
common::init_embedding_svc_url(&conn).await;
let mut rng = rand::thread_rng();
let test_num = rng.gen_range(1..100000);
let test_table_name = common::init_test_table(test_num, &conn).await;
let test_table_name = format!("products_test_{}", test_num);
common::init_test_table(&test_table_name, &conn).await;
let agent_name = format!("agent_{}", test_num);
println!("test_table_name: {}", test_table_name);
println!("agent_name: {}", agent_name);
Expand Down Expand Up @@ -191,14 +195,8 @@ async fn test_static() {
common::init_embedding_svc_url(&conn).await;
let mut rng = rand::thread_rng();
let test_table_name = "products_test_static";
common::init_test_table(&test_table_name, &conn).await;
let job_name = "static_test_job";
let query = format!(
"CREATE TABLE IF NOT EXISTS {test_table_name} AS TABLE vectorize.example_products WITH DATA;"
);
let _ = sqlx::query(&query)
.execute(&conn)
.await
.expect("failed to create static test table");

// initialize a job
let _ = sqlx::query(&format!(
Expand Down
28 changes: 20 additions & 8 deletions tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,26 @@ pub mod common {
Ok(options)
}

pub async fn init_test_table(test_num: i32, conn: &Pool<Postgres>) -> String {
let table = format!("product_{test_num}");
let q = format!("CREATE TABLE {table} AS TABLE vectorize.example_products WITH DATA");
let _ = sqlx::query(&q)
.execute(conn)
.await
.expect("failed to create test table");
table
pub async fn init_test_table(table: &str, conn: &Pool<Postgres>) {
let create = format!(
"CREATE TABLE IF NOT EXISTS {table} (LIKE vectorize.example_products INCLUDING ALL);"
);
let insert = format!(
"
DO $$
BEGIN
IF (SELECT COUNT(*) FROM {table}) = 0 THEN
INSERT INTO {table} SELECT * FROM vectorize.example_products;
END IF;
END $$;
"
);
for q in vec![create, insert] {
sqlx::query(&q)
.execute(conn)
.await
.expect("failed to create test table");
}
}

pub async fn row_count(fq_table_name: &str, conn: &Pool<Postgres>) -> i64 {
Expand Down

0 comments on commit caae2ca

Please sign in to comment.