Skip to content

Commit

Permalink
added builder and more tests for Pinecone
Browse files Browse the repository at this point in the history
  • Loading branch information
ericapywang committed Jun 10, 2024
1 parent 901f0d5 commit 1bc27f1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 14 deletions.
25 changes: 13 additions & 12 deletions pinecone_sdk/src/models/create_index_request_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ pub enum Cloud {
#[cfg(test)]
mod tests {
use super::*;
use tokio;

#[test]
fn test_create_index_params() {
#[tokio::test]
async fn test_create_index_params() {
let create_index_params = CreateIndexParams::new("test_index", 10, None, Spec::Serverless {
cloud: Cloud::Aws,
region: "us-west-2".to_string(),
Expand All @@ -139,8 +140,8 @@ mod tests {
});
}

#[test]
fn test_create_index_params_builder() {
#[tokio::test]
async fn test_create_index_params_builder() {
let create_index_params = CreateIndexParams::builder()
.with_name("test_index")
.with_dimension(10)
Expand All @@ -161,8 +162,8 @@ mod tests {
});
}

#[test]
fn test_builder_missing_metric() {
#[tokio::test]
async fn test_builder_missing_metric() {
let create_index_params = CreateIndexParams::builder()
.with_name("test_index")
.with_dimension(10)
Expand All @@ -182,8 +183,8 @@ mod tests {
});
}

#[test]
fn test_missing_name() {
#[tokio::test]
async fn test_missing_name() {
let create_index_params = CreateIndexParams::builder()
.with_dimension(10)
.with_metric(Metric::Cosine)
Expand All @@ -193,8 +194,8 @@ mod tests {
assert_eq!(create_index_params, Err(PineconeError::MissingNameError));
}

#[test]
fn test_missing_dimension() {
#[tokio::test]
async fn test_missing_dimension() {
let create_index_params = CreateIndexParams::builder()
.with_name("test_index")
.with_metric(Metric::Cosine)
Expand All @@ -204,8 +205,8 @@ mod tests {
assert_eq!(create_index_params, Err(PineconeError::MissingDimensionError));
}

#[test]
fn test_missing_spec() {
#[tokio::test]
async fn test_missing_spec() {
let create_index_params = CreateIndexParams::builder()
.with_name("test_index")
.with_dimension(10)
Expand Down
82 changes: 80 additions & 2 deletions pinecone_sdk/src/pinecone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,65 @@ impl Pinecone {
})
}

pub fn builder() -> PineconeBuilder {
PineconeBuilder::new()
}

pub fn openapi_config(&self) -> &Configuration {
&self.openapi_config
}
}

pub struct PineconeBuilder {
api_key: Option<String>,
control_plane_host: Option<String>,
additional_headers: Option<HashMap<String, String>>,
source_tag: Option<String>,
}

impl PineconeBuilder {
pub fn new() -> PineconeBuilder {
PineconeBuilder {
api_key: None,
control_plane_host: None,
additional_headers: None,
source_tag: None,
}
}

pub fn with_api_key(mut self, api_key: &str) -> PineconeBuilder {
self.api_key = Some(api_key.to_string());
self
}

pub fn with_control_plane_host(mut self, control_plane_host: &str) -> PineconeBuilder {
self.control_plane_host = Some(control_plane_host.to_string());
self
}

pub fn with_additional_headers(
mut self,
additional_headers: HashMap<String, String>,
) -> PineconeBuilder {
self.additional_headers = Some(additional_headers);
self
}

pub fn with_source_tag(mut self, source_tag: &str) -> PineconeBuilder {
self.source_tag = Some(source_tag.to_string());
self
}

pub fn build(self) -> Result<Pinecone, PineconeError> {
Pinecone::new(
self.api_key,
self.control_plane_host,
self.additional_headers,
self.source_tag,
)
}
}

#[cfg(test)]
mod tests {
use std::env;
Expand Down Expand Up @@ -147,10 +201,10 @@ mod tests {
);

assert!(pinecone.is_err());
assert!(matches!(
assert_eq!(
pinecone.err().unwrap(),
PineconeError::APIKeyMissingError
));
);
}

#[tokio::test]
Expand Down Expand Up @@ -344,6 +398,30 @@ mod tests {
pinecone.as_ref().unwrap().config.additional_headers,
mock_arg_headers.clone()
);
}

#[tokio::test]
async fn test_builder() {
let pinecone = Pinecone::builder()
.with_api_key("mock-api-key")
.build();

assert_eq!(pinecone.unwrap().config.api_key, "mock-api-key");
}

#[tokio::test]
async fn test_builder_all_params() {
let pinecone = Pinecone::builder()
.with_api_key("mock-api-key")
.with_additional_headers(HashMap::from([("header1".to_string(), "value1".to_string())]))
.with_control_plane_host("mock-controller-host")
.with_source_tag("mock-source-tag")
.build()
.unwrap();

assert_eq!(pinecone.config.api_key, "mock-api-key");
assert_eq!(pinecone.config.additional_headers, HashMap::from([("header1".to_string(), "value1".to_string())]));
assert_eq!(pinecone.config.controller_url, "mock-controller-host");
assert_eq!(pinecone.config.source_tag, Some("mock-source-tag".to_string()));
}
}

0 comments on commit 1bc27f1

Please sign in to comment.