From d2c60b8e24a37c11f3199f9358b813fe357b20f6 Mon Sep 17 00:00:00 2001 From: Floris Date: Wed, 10 Jul 2024 17:35:27 +0200 Subject: [PATCH] fix: abstracted all tests into seperate methods with files --- Cargo.toml | 5 +- src/lib.rs | 1 + src/nightly.rs | 11 + src/request/headers.rs | 52 ++ src/request/mod.rs | 8 + src/select.rs | 16 +- src/tests/base.rs | 544 ++---------------- src/tests/methods/EACH_TEST_GOES_HERE.md | 0 src/tests/methods/delete.rs | 38 ++ src/tests/methods/init.rs | 12 + src/tests/methods/insert.rs | 44 ++ src/tests/methods/insert_if_unique_numeric.rs | 43 ++ src/tests/methods/insert_if_unique_string.rs | 42 ++ src/tests/methods/insert_numeric.rs | 41 ++ src/tests/methods/insert_string.rs | 41 ++ src/tests/methods/select.rs | 36 ++ src/tests/methods/select_filter.rs | 40 ++ src/tests/methods/select_stacked_queries.rs | 48 ++ src/tests/methods/select_with_columns.rs | 42 ++ src/tests/methods/select_with_count.rs | 36 ++ .../methods/select_with_count_and_filter.rs | 41 ++ src/tests/methods/update_with_column.rs | 47 ++ src/tests/methods/upsert_numeric.rs | 49 ++ src/tests/methods/upsert_string.rs | 49 ++ src/tests/mod.rs | 19 + src/tests/readme.md | 10 + 26 files changed, 810 insertions(+), 505 deletions(-) create mode 100644 src/request/headers.rs create mode 100644 src/request/mod.rs create mode 100644 src/tests/methods/EACH_TEST_GOES_HERE.md create mode 100644 src/tests/methods/delete.rs create mode 100644 src/tests/methods/init.rs create mode 100644 src/tests/methods/insert.rs create mode 100644 src/tests/methods/insert_if_unique_numeric.rs create mode 100644 src/tests/methods/insert_if_unique_string.rs create mode 100644 src/tests/methods/insert_numeric.rs create mode 100644 src/tests/methods/insert_string.rs create mode 100644 src/tests/methods/select.rs create mode 100644 src/tests/methods/select_filter.rs create mode 100644 src/tests/methods/select_stacked_queries.rs create mode 100644 src/tests/methods/select_with_columns.rs create mode 100644 src/tests/methods/select_with_count.rs create mode 100644 src/tests/methods/select_with_count_and_filter.rs create mode 100644 src/tests/methods/update_with_column.rs create mode 100644 src/tests/methods/upsert_numeric.rs create mode 100644 src/tests/methods/upsert_string.rs create mode 100644 src/tests/readme.md diff --git a/Cargo.toml b/Cargo.toml index 2f9225d..20de0ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,16 +17,17 @@ serde_json = "1.0.111" tokio = { version = "1.37.0", features = ["full"] } dotenv = "0.15.0" anyhow = "1.0.86" -regex = { version = "1.10.5", optional = true } +regex = { version = "1.10.5", optional = false } +#[cfg(feature = "nightly")] [features] nightly = [] storage = [] rustls = [] -# default = ["nightly", "storage", "realtime"] +default = ["nightly", "storage"] # # # # # realtime = [] diff --git a/src/lib.rs b/src/lib.rs index 237dafc..d7908e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -344,6 +344,7 @@ pub mod select; pub mod success; pub mod tests; pub mod update; +pub mod request; pub mod graphql; pub mod nightly; diff --git a/src/nightly.rs b/src/nightly.rs index 8595d69..a5d8776 100644 --- a/src/nightly.rs +++ b/src/nightly.rs @@ -12,4 +12,15 @@ pub fn print_nightly_warning() { println!("\x1b[34;1mWarning: This is a nightly build and may contain bugs.\x1b[0m"); println!("\x1b[34;1mFeatures 'Force rustls' and 'Graphql support' are currently under nightly mode.\x1b[0m"); println!("\x1b[34;1mTo disable this message, set the environment variable SUPABASE_RS_NO_NIGHTLY_MSG to 'true'.\x1b[0m"); +} + +pub fn print_if_dev( + message: &str +) { + dotenv().ok(); + if env::var("SUPABASE_RS_DEV").unwrap_or_else(|_| "false".to_string()) == "true" { + return; + } + println!("\x1b[34m{}\x1b[0m", message); + } \ No newline at end of file diff --git a/src/request/headers.rs b/src/request/headers.rs new file mode 100644 index 0000000..1b82406 --- /dev/null +++ b/src/request/headers.rs @@ -0,0 +1,52 @@ +use std::collections::HashMap; + +use crate::request::Headers; + + +impl Headers { + pub fn new() -> Self { + Headers { + headers: HashMap::new(), + } + } + + pub fn insert(&mut self, key: &str, value: &str) { + self.headers.insert(key.to_string(), value.to_string()); + } + + pub fn get_headers(&self) -> HashMap { + self.headers.clone() + } +} + + +impl Default for Headers { + fn default() -> Self { + let mut headers = Headers::new(); + headers.insert(HeadersTypes::ClientInfo.as_str(), "supabase-rs/0.3.3"); + headers.insert(HeadersTypes::ContentType.as_str(), "application/json"); + Headers { + headers: headers.get_headers(), + } + } +} + +pub enum HeadersTypes { + ApiKey, + Authorization, + ContentType, + Prefer, + ClientInfo, +} + +impl HeadersTypes { + pub fn as_str(&self) -> &str { + match self { + HeadersTypes::ApiKey => "apikey", + HeadersTypes::Authorization => "Authorization", + HeadersTypes::ContentType => "Content-Type", + HeadersTypes::Prefer => "prefer", + HeadersTypes::ClientInfo => "x_client_info", + } + } +} diff --git a/src/request/mod.rs b/src/request/mod.rs new file mode 100644 index 0000000..e018e27 --- /dev/null +++ b/src/request/mod.rs @@ -0,0 +1,8 @@ +pub mod headers; + +use std::collections::HashMap; + + +pub struct Headers { + pub headers: HashMap, +} \ No newline at end of file diff --git a/src/select.rs b/src/select.rs index 7c8e23d..7e9c843 100644 --- a/src/select.rs +++ b/src/select.rs @@ -128,12 +128,17 @@ #![allow(rustdoc::invalid_rust_codeblocks)] use crate::SupabaseClient; -use reqwest; -use reqwest::Client; -use reqwest::Response; +use crate::query::QueryBuilder; + +use reqwest::{Client, Response}; +use reqwest::header::HeaderMap; use serde_json::{json, Value}; -use crate::query::QueryBuilder; + + + +#[cfg(feature = "nightly")] +use crate::nightly::print_if_dev; impl SupabaseClient { /// Initializes a `QueryBuilder` for a specified table. @@ -167,7 +172,8 @@ impl SupabaseClient { // Build the client and the endpoint let endpoint: String = format!("{}/rest/v1/{}?{}", self.url, table_name, query_string); - println!("Endpoint: {}", endpoint); + #[cfg(feature = "nightly")] + println!("\x1b[33mEndpoint: {}\x1b[0m", endpoint); #[cfg(feature = "rustls")] let client = Client::builder().use_rustls_tls().build().unwrap(); diff --git a/src/tests/base.rs b/src/tests/base.rs index 4bc840d..1e8ca43 100644 --- a/src/tests/base.rs +++ b/src/tests/base.rs @@ -20,6 +20,10 @@ //! are working as expected. //! + + + + #[cfg(test)] mod methods { use crate::SupabaseClient; @@ -27,570 +31,114 @@ mod methods { use serde_json::{json, Value}; use std::env::var; - /// Initializes the Supabase client by loading environment variables. - async fn init() -> Result> { - dotenv().ok(); - - let supabase_url = var("SUPABASE_URL")?; - let supabase_key = var("SUPABASE_KEY")?; - - Ok(SupabaseClient::new(supabase_url, supabase_key)) - } + // import local method tests + use crate::tests::methods::{ + init::init, + insert::insert as test_insert, + insert_string::insert_string as test_insert_string, + insert_numeric::insert_numeric as test_insert_numeric, + insert_if_unique_string::insert_if_unique_string as test_insert_if_unique_string, + insert_if_unique_numeric::insert_if_unique_numeric as test_insert_if_unique_numeric, + select::select as test_select, + select_filter::select_filter as test_select_filter, + select_with_columns::select_with_columns as test_select_with_columns, + select_with_count::select_with_count as test_select_with_count, + select_with_count_and_filter::select_with_count_and_filter as test_select_with_count_and_filter, + delete::delete as test_delete, + upsert_string::upsert_string as test_upsert_string, + upsert_numeric::upsert_numeric as test_upsert_numeric, + update_with_column::update_with_column as test_update_with_column, + select_stacked_queries::select_stacked_queries as test_select_stacked_queries, + }; /// Tests the `insert` method of `SupabaseClient`. #[tokio::test] async fn insert() { - /// Performs an insert operation in an isolated scope. - async fn insert_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - let response_inner: Result = supabase_client - .insert( - "test", - json!({ - "dog": "what da dog doing" - }), - ) - .await; - - match response_inner { - Ok(response_inner) => Ok(()), - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - - let response: Result<(), String> = insert_inner(supabase_client).await; - - assert!(response.is_ok()); + test_insert().await; } - /// Tests the `insert` method of `SupabaseClient`. + /// Tests the `insert` with a string method of `SupabaseClient`. #[tokio::test] async fn insert_string() { - /// Performs an insert operation in an isolated scope. - async fn insert_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - let response_inner: Result = supabase_client - .insert( - "test", - json!({ - "dog": "what da dog doing2" - }), - ) - .await; - - match response_inner { - Ok(response_inner) => Ok(()), - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = insert_inner(supabase_client).await; - - assert!(response.is_ok()); + test_insert_string().await; } - /// Tests the `insert` method of `SupabaseClient`. + /// Tests the `insert` with a number method of `SupabaseClient`. #[tokio::test] async fn insert_numeric() { - /// Performs an insert operation in an isolated scope. - async fn insert_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - let response_inner: Result = supabase_client - .insert( - "test", - json!({ - "dog": 1234 - }), - ) - .await; - - match response_inner { - Ok(response_inner) => Ok(()), - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = insert_inner(supabase_client).await; - - assert!(response.is_ok()); + test_insert_numeric().await; } /// Tests the `insert_if_unique` method of `SupabaseClient`. #[tokio::test] async fn insert_if_unique_string() { - /// Performs an insert_if_unique operation in an isolated scope. - async fn insert_if_unique_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - let random_string: String = rand::random::().to_string(); - - let response_inner: Result = supabase_client - .insert_if_unique( - "test", - json!({ - "dog": random_string, - }), - ) - .await; - - match response_inner { - Ok(response_inner) => Ok(()), - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = insert_if_unique_inner(supabase_client).await; - - assert!(response.is_ok()); + test_insert_if_unique_string().await; } /// Tests the `insert_if_unique` method of `SupabaseClient`. #[tokio::test] async fn insert_if_unique_numeric() { - /// Performs an insert_if_unique operation in an isolated scope. - async fn insert_if_unique_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - let random_number: u64 = rand::random::(); - - let response_inner: Result = supabase_client - .insert_if_unique( - "test", - json!({ - "dog": random_number, - }), - ) - .await; - - match response_inner { - Ok(response_inner) => Ok(()), - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = insert_if_unique_inner(supabase_client).await; - - assert!(response.is_ok()); + test_insert_if_unique_numeric().await; } /// Tests the `select` method of `SupabaseClient`. #[tokio::test] async fn select() { - /// Performs a select operation in an isolated scope. - async fn select_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - - let response_inner: Result, String> = - supabase_client.select("test").execute().await; - - match response_inner { - Ok(response_inner) => Ok(()), - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = select_inner(supabase_client).await; - - assert!(response.is_ok()); + test_select().await; } /// Tests the `select_filter` method of `SupabaseClient`. #[tokio::test] async fn select_filter() { - /// Performs a select_filter operation in an isolated scope. - async fn select_filter_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - - let response_inner: Result, String> = supabase_client - .select("test") - .eq("dog", "what da dog doing") - .execute() - .await; - - match response_inner { - Ok(response_inner) => Ok(()), - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = select_filter_inner(supabase_client).await; - - assert!(response.is_ok()); + test_select_filter().await; } /// Tests the `select_filter` method of `SupabaseClient`. #[tokio::test] async fn select_with_columns() { - /// Performs a select_with_columns operation in an isolated scope. - async fn select_filter_columns_inner( - supabase_client: SupabaseClient, - ) -> Result<(), String> { - // Usage example - - let response_inner: Result, String> = supabase_client - .select("test") - .columns(["dog"].to_vec()) - .eq("dog", "what da dog doing") - .execute() - .await; - - match response_inner { - Ok(response_inner) => Ok(()), - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = select_filter_columns_inner(supabase_client).await; - - assert!(response.is_ok()); + test_select_with_columns().await; } /// Tests the `select_filter` method of `SupabaseClient`. #[tokio::test] async fn select_with_count() { - /// Performs a select_filter operation in an isolated scope. - async fn select_with_count_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - - let response_inner: Result, String> = - supabase_client.select("test").count().execute().await; - - match response_inner { - Ok(response_inner) => Ok(()), - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = select_with_count_inner(supabase_client).await; - - assert!(response.is_ok()); + test_select_with_count().await; } - // /// Tests the `select_filter` method of `SupabaseClient`. + /// Tests the `select_filter` method of `SupabaseClient`. // #[tokio::test] // async fn select_with_count_and_filter() { - - // /// Performs a select_filter operation in an isolated scope. - // async fn select_with_count_and_filter_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // // Usage example - - // let response_inner: Result, String> = supabase_client - // .select("test") - // .eq("dog", "what da dog doing") - // .count() - // .execute() - // .await; - // println!("{:?}", response_inner); - - // match response_inner { - // Ok(response_inner) => { - // println!("Response: {:?}", response_inner); - // Ok(()) - // } - // Err(error) => { - // println!("Error: {:?}", error); - // Err(error) - // } - // } - // } - - // let supabase_client: SupabaseClient = match init().await { - // Ok(client) => client, - // Err(e) => { - // eprintln!("\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", e); - // return; - // } - // }; - // let response: Result<(), String> = select_with_count_and_filter_inner(supabase_client).await; - // println!("{:?}", response); - - // assert_eq!(response.is_ok(), true); + // test_select_with_count_and_filter().await; // } /// Tests the `select_filter` method of `SupabaseClient`. #[tokio::test] async fn delete() { - /// Performs a select_filter operation in an isolated scope. - async fn delete_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - - let response_inner: Result<(), String> = - supabase_client.delete("test", "1476105020679346924").await; - - match response_inner { - Ok(response_inner) => Ok(()), - - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = delete_inner(supabase_client).await; - - assert!(response.is_ok()); + test_delete().await; } /// Tests the `upsert` method of `SupabaseClient`. #[tokio::test] async fn upsert_string() { - /// Performs a select_filter operation in an isolated scope. - async fn upsert_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - - let id: String = "8826759220049045588".to_string(); - let email: String = "floris@xylex.ai".to_string(); - - // Usage example - let response_inner = supabase_client - .upsert( - "test", - &id, - json!({ - "email": email - }), - ) - .await; - - match response_inner { - Ok(response_inner) => Ok(()), - - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = upsert_inner(supabase_client).await; - - assert!(response.is_ok()); + test_upsert_string().await; } /// Tests the `upsert` method of `SupabaseClient`. #[tokio::test] async fn upsert_numeric() { - /// Performs a select_filter operation in an isolated scope. - async fn upsert_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - - let id: String = "8826759220049045588".to_string(); - let email: i64 = 1234; - - // Usage example - let response_inner = supabase_client - .upsert( - "test", - &id, - json!({ - "email": email - }), - ) - .await; - - match response_inner { - Ok(response_inner) => Ok(()), - - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = upsert_inner(supabase_client).await; - - assert!(response.is_ok()); + test_upsert_numeric().await; } /// Tests the `update_with_column` method of `SupabaseClient` with a string value. #[tokio::test] async fn update_with_column() { - /// Performs a select_filter operation in an isolated scope. - async fn update_inner(supabase_client: SupabaseClient) -> Result<(), String> { - // Usage example - - let id: String = "what da dog doing".to_string(); - let email: i64 = 1234; - - let updated_body: Value = json!({ - "dog4": "what da dog doing" - }); - - // Usage example - let response_inner: Result = supabase_client - .update_with_column_name("test", "dog", &id, updated_body) - .await; - - match response_inner { - Ok(response_inner) => Ok(()), - - Err(error) => { - println!("Error: {:?}", error); - Err(error) - } - } - } - - let supabase_client: SupabaseClient = match init().await { - Ok(client) => client, - Err(e) => { - eprintln!( - "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", - e - ); - return; - } - }; - let response: Result<(), String> = update_inner(supabase_client).await; + test_update_with_column().await; + } - assert!(response.is_ok()); + /// Tests the `select_stacked_queries` method of `SupabaseClient`. + /// This test is used to test the chaining of multiple `eq` methods in a single query. + #[tokio::test] + async fn select_stacked_queries() { + test_select_stacked_queries().await; } } diff --git a/src/tests/methods/EACH_TEST_GOES_HERE.md b/src/tests/methods/EACH_TEST_GOES_HERE.md new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/methods/delete.rs b/src/tests/methods/delete.rs new file mode 100644 index 0000000..af66b49 --- /dev/null +++ b/src/tests/methods/delete.rs @@ -0,0 +1,38 @@ + +use serde_json::{json, Value}; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn delete() { + /// Performs a select_filter operation in an isolated scope. + async fn delete_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + + let response_inner: Result<(), String> = + supabase_client.delete("test", "1476105020679346924").await; + + match response_inner { + Ok(response_inner) => Ok(()), + + Err(error) => { + println!("Error: {:?}", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = delete_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/init.rs b/src/tests/methods/init.rs new file mode 100644 index 0000000..a8ec72d --- /dev/null +++ b/src/tests/methods/init.rs @@ -0,0 +1,12 @@ +use crate::SupabaseClient; +use std::env::var; +use dotenv::dotenv; + +pub async fn init() -> Result> { + dotenv().ok(); + + let supabase_url: String = var("SUPABASE_URL")?; + let supabase_key: String = var("SUPABASE_KEY")?; + + Ok(SupabaseClient::new(supabase_url, supabase_key)) +} \ No newline at end of file diff --git a/src/tests/methods/insert.rs b/src/tests/methods/insert.rs new file mode 100644 index 0000000..fe64cc3 --- /dev/null +++ b/src/tests/methods/insert.rs @@ -0,0 +1,44 @@ +use serde_json::json; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn insert() { + /// Performs an insert operation in an isolated scope. + async fn insert_inner( + supabase_client: SupabaseClient + ) -> Result<(), String> { + // Usage example + let response_inner: Result = supabase_client + .insert( + "test", + json!({ + "dog": "what da dog doing" + }), + ) + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + Err(error) => { + eprintln!("\x1b[31mError: {:?}\x1b[0m", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + + let response: Result<(), String> = insert_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/insert_if_unique_numeric.rs b/src/tests/methods/insert_if_unique_numeric.rs new file mode 100644 index 0000000..5e032e5 --- /dev/null +++ b/src/tests/methods/insert_if_unique_numeric.rs @@ -0,0 +1,43 @@ +use serde_json::json; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn insert_if_unique_numeric() { + /// Performs an insert_if_unique operation in an isolated scope. + async fn insert_if_unique_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + let random_number: u64 = rand::random::(); + + let response_inner: Result = supabase_client + .insert_if_unique( + "test", + json!({ + "dog": random_number, + }), + ) + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + Err(error) => { + println!("Error: {:?}", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = insert_if_unique_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/insert_if_unique_string.rs b/src/tests/methods/insert_if_unique_string.rs new file mode 100644 index 0000000..4776c97 --- /dev/null +++ b/src/tests/methods/insert_if_unique_string.rs @@ -0,0 +1,42 @@ +use serde_json::json; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + +pub async fn insert_if_unique_string() { + /// Performs an insert_if_unique operation in an isolated scope. + async fn insert_if_unique_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + let random_string: String = rand::random::().to_string(); + + let response_inner: Result = supabase_client + .insert_if_unique( + "test", + json!({ + "dog": random_string, + }), + ) + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + Err(error) => { + println!("Error: {:?}", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = insert_if_unique_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/insert_numeric.rs b/src/tests/methods/insert_numeric.rs new file mode 100644 index 0000000..d759510 --- /dev/null +++ b/src/tests/methods/insert_numeric.rs @@ -0,0 +1,41 @@ +use serde_json::json; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn insert_numeric() { + /// Performs an insert operation in an isolated scope. + async fn insert_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + let response_inner: Result = supabase_client + .insert( + "test", + json!({ + "dog": 1234 + }), + ) + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + Err(error) => { + eprintln!("\x1b[31mError: {:?}\x1b[0m", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = insert_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/insert_string.rs b/src/tests/methods/insert_string.rs new file mode 100644 index 0000000..a19fa1e --- /dev/null +++ b/src/tests/methods/insert_string.rs @@ -0,0 +1,41 @@ +use serde_json::json; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn insert_string() { + /// Performs an insert operation in an isolated scope. + async fn insert_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + let response_inner: Result = supabase_client + .insert( + "test", + json!({ + "dog": "what da dog doing2" + }), + ) + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + Err(error) => { + eprintln!("\x1b[31mError: {:?}\x1b[0m", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = insert_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/select.rs b/src/tests/methods/select.rs new file mode 100644 index 0000000..9845d3e --- /dev/null +++ b/src/tests/methods/select.rs @@ -0,0 +1,36 @@ +use serde_json::{json, Value}; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn select() { + /// Performs a select operation in an isolated scope. + async fn select_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + + let response_inner: Result, String> = + supabase_client.select("test").execute().await; + + match response_inner { + Ok(response_inner) => Ok(()), + Err(error) => { + println!("Error: {:?}", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = select_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/select_filter.rs b/src/tests/methods/select_filter.rs new file mode 100644 index 0000000..6e1e920 --- /dev/null +++ b/src/tests/methods/select_filter.rs @@ -0,0 +1,40 @@ + +use serde_json::{json, Value}; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn select_filter() { + /// Performs a select_filter operation in an isolated scope. + async fn select_filter_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + + let response_inner: Result, String> = supabase_client + .select("test") + .eq("dog", "what da dog doing") + .execute() + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + Err(error) => { + println!("Error: {:?}", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = select_filter_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/select_stacked_queries.rs b/src/tests/methods/select_stacked_queries.rs new file mode 100644 index 0000000..1a3ac23 --- /dev/null +++ b/src/tests/methods/select_stacked_queries.rs @@ -0,0 +1,48 @@ + +use serde_json::{json, Value}; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn select_stacked_queries() { + /// Performs a select_filter operation in an isolated scope. + async fn upsert_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + + let id: String = "8826759220049045588".to_string(); + let email: String = "floris@xylex.ai".to_string(); + + // Usage example + let response_inner = supabase_client + .select("test") + .eq("dog", "what da dog doing") + .eq("dog", "what da dog doing2") + .eq("dog", "what da dog doing3") + .eq("dog", "what da dog doing4") + .execute() + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + + Err(error) => { + eprintln!("\x1b[31mError: {:?}\x1b[0m", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = upsert_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/select_with_columns.rs b/src/tests/methods/select_with_columns.rs new file mode 100644 index 0000000..c222212 --- /dev/null +++ b/src/tests/methods/select_with_columns.rs @@ -0,0 +1,42 @@ +use serde_json::{json, Value}; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn select_with_columns() { + /// Performs a select_with_columns operation in an isolated scope. + async fn select_filter_columns_inner( + supabase_client: SupabaseClient, + ) -> Result<(), String> { + // Usage example + + let response_inner: Result, String> = supabase_client + .select("test") + .columns(["dog"].to_vec()) + .eq("dog", "what da dog doing") + .execute() + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + Err(error) => { + println!("Error: {:?}", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = select_filter_columns_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/select_with_count.rs b/src/tests/methods/select_with_count.rs new file mode 100644 index 0000000..3efec25 --- /dev/null +++ b/src/tests/methods/select_with_count.rs @@ -0,0 +1,36 @@ +use serde_json::{json, Value}; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn select_with_count() { + /// Performs a select_filter operation in an isolated scope. + async fn select_with_count_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + + let response_inner: Result, String> = + supabase_client.select("test").count().execute().await; + + match response_inner { + Ok(response_inner) => Ok(()), + Err(error) => { + println!("Error: {:?}", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = select_with_count_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/select_with_count_and_filter.rs b/src/tests/methods/select_with_count_and_filter.rs new file mode 100644 index 0000000..a6d8158 --- /dev/null +++ b/src/tests/methods/select_with_count_and_filter.rs @@ -0,0 +1,41 @@ + +use serde_json::{json, Value}; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn select_with_count_and_filter() { + + /// Performs a select_filter operation in an isolated scope. + async fn select_with_count_and_filter_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + + let response_inner: Result, String> = supabase_client + .select("test") + .eq("dog", "what da dog doing") + .count() + .execute() + .await; + + match response_inner { + Ok(response_inner) => { + Ok(()) + } + Err(error) => { + eprintln!("\x1b[31mError: {:?}\x1b[0m", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!("\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", e); + return; + } + }; + let response: Result<(), String> = select_with_count_and_filter_inner(supabase_client).await; + + assert_eq!(response.is_ok(), true); +} \ No newline at end of file diff --git a/src/tests/methods/update_with_column.rs b/src/tests/methods/update_with_column.rs new file mode 100644 index 0000000..6c2d8ff --- /dev/null +++ b/src/tests/methods/update_with_column.rs @@ -0,0 +1,47 @@ + +use serde_json::{json, Value}; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn update_with_column() { + /// Performs a select_filter operation in an isolated scope. + async fn update_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + + let id: String = "what da dog doing".to_string(); + let email: i64 = 1234; + + let updated_body: Value = json!({ + "dog4": "what da dog doing" + }); + + // Usage example + let response_inner: Result = supabase_client + .update_with_column_name("test", "dog", &id, updated_body) + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + + Err(error) => { + println!("Error: {:?}", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = update_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/upsert_numeric.rs b/src/tests/methods/upsert_numeric.rs new file mode 100644 index 0000000..7820716 --- /dev/null +++ b/src/tests/methods/upsert_numeric.rs @@ -0,0 +1,49 @@ + +use serde_json::{json, Value}; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn upsert_numeric() { + /// Performs a select_filter operation in an isolated scope. + async fn upsert_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + + let id: String = "8826759220049045588".to_string(); + let email: i64 = 1234; + + // Usage example + let response_inner = supabase_client + .upsert( + "test", + &id, + json!({ + "email": email + }), + ) + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + + Err(error) => { + println!("Error: {:?}", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = upsert_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/methods/upsert_string.rs b/src/tests/methods/upsert_string.rs new file mode 100644 index 0000000..a43d83d --- /dev/null +++ b/src/tests/methods/upsert_string.rs @@ -0,0 +1,49 @@ + +use serde_json::{json, Value}; +use crate::SupabaseClient; +use crate::tests::methods::init::init; + + +pub async fn upsert_string() { + /// Performs a select_filter operation in an isolated scope. + async fn upsert_inner(supabase_client: SupabaseClient) -> Result<(), String> { + // Usage example + + let id: String = "8826759220049045588".to_string(); + let email: String = "floris@xylex.ai".to_string(); + + // Usage example + let response_inner = supabase_client + .upsert( + "test", + &id, + json!({ + "email": email + }), + ) + .await; + + match response_inner { + Ok(response_inner) => Ok(()), + + Err(error) => { + eprintln!("\x1b[31mError: {:?}\x1b[0m", error); + Err(error) + } + } + } + + let supabase_client: SupabaseClient = match init().await { + Ok(client) => client, + Err(e) => { + eprintln!( + "\x1b[31mFailed to initialize Supabase client: {:?}\x1b[0m", + e + ); + return; + } + }; + let response: Result<(), String> = upsert_inner(supabase_client).await; + + assert!(response.is_ok()); +} \ No newline at end of file diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 6cf245d..a69722e 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1 +1,20 @@ pub mod base; + +pub mod methods { + pub mod delete; + pub mod init; + pub mod insert; + pub mod insert_if_unique_numeric; + pub mod insert_if_unique_string; + pub mod insert_numeric; + pub mod insert_string; + pub mod select; + pub mod select_filter; + pub mod select_with_columns; + pub mod select_with_count; + pub mod select_with_count_and_filter; + pub mod upsert_numeric; + pub mod upsert_string; + pub mod update_with_column; + pub mod select_stacked_queries; +} \ No newline at end of file diff --git a/src/tests/readme.md b/src/tests/readme.md new file mode 100644 index 0000000..7cb6c2c --- /dev/null +++ b/src/tests/readme.md @@ -0,0 +1,10 @@ +# adding tests + +base.rs will contain a call to each test in the /methods dir + +to add a new test: +- add a new `METHOD_NAME.rs` in /methods +- add the `.rs` file to `mod.rs` +- import the test in `base.rs` +- run the test in the `methods` mod under a `#[tokio::test]` macro +- ?? success