Skip to content

Commit

Permalink
added some error checking + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericapywang committed Jun 10, 2024
1 parent cd0dfcb commit 901f0d5
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 17 deletions.
105 changes: 88 additions & 17 deletions pinecone_sdk/src/models/create_index_request_params.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::utils::errors::PineconeError;

#[derive(Debug)]

#[derive(Debug, PartialEq)]
pub struct CreateIndexParams {
pub name: String,
pub dimension: u32,
Expand All @@ -12,7 +14,7 @@ impl CreateIndexParams {
CreateIndexParams {
name: name.to_string(),
dimension,
metric: metric.map(|x| x).unwrap_or(Metric::Cosine),
metric: metric.unwrap_or(Metric::Cosine),
spec,
}
}
Expand All @@ -24,30 +26,30 @@ impl CreateIndexParams {
}

pub struct CreateIndexParamsBuilder {
name: String,
dimension: u32,
name: Option<String>,
dimension: Option<u32>,
metric: Option<Metric>,
spec: Option<Spec>,
}

impl CreateIndexParamsBuilder {
pub fn new() -> CreateIndexParamsBuilder {
CreateIndexParamsBuilder {
name: "".to_string(),
dimension: 0,
name: None,
dimension: None,
metric: None,
spec: None,
}
}

pub fn with_name(mut self, name: &str) -> CreateIndexParamsBuilder {
self.name = name.to_string();
self.name = Some(name.to_string());
self
}

pub fn with_dimension(mut self, dimension: u32) -> CreateIndexParamsBuilder {
// want to eventually throw an error if dimension is 0?
self.dimension = dimension;
self.dimension = Some(dimension);
self
}

Expand All @@ -62,13 +64,26 @@ impl CreateIndexParamsBuilder {
}

// constructs CreateIndexParams from CreateIndexParamsBuilder fields
pub fn build(self) -> CreateIndexParams {
CreateIndexParams {
name: self.name,
dimension: self.dimension,
metric: self.metric.map(|x| x).unwrap_or(Metric::Cosine),
spec: self.spec.map(|x| x).unwrap(),
}
pub fn build(self) -> Result<CreateIndexParams, PineconeError> {
let name = match self.name {
Some(name) => name,
None => Err(PineconeError::MissingNameError)?,
};
let dimension = match self.dimension {
Some(dimension) => dimension,
None => Err(PineconeError::MissingDimensionError)?,
};
let spec = match self.spec {
Some(spec) => spec,
None => Err(PineconeError::MissingSpecError)?,
};

Ok(CreateIndexParams {
name,
dimension,
metric: self.metric.unwrap_or(Metric::Cosine),
spec,
})
}
}

Expand Down Expand Up @@ -134,7 +149,29 @@ mod tests {
cloud: Cloud::Aws,
region: "us-west-2".to_string(),
})
.build();
.build()
.unwrap();

assert_eq!(create_index_params.name, "test_index");
assert_eq!(create_index_params.dimension, 10);
assert_eq!(create_index_params.metric, Metric::Cosine);
assert_eq!(create_index_params.spec, Spec::Serverless {
cloud: Cloud::Aws,
region: "us-west-2".to_string(),
});
}

#[test]
fn test_builder_missing_metric() {
let create_index_params = CreateIndexParams::builder()
.with_name("test_index")
.with_dimension(10)
.with_spec(Spec::Serverless {
cloud: Cloud::Aws,
region: "us-west-2".to_string(),
})
.build()
.unwrap();

assert_eq!(create_index_params.name, "test_index");
assert_eq!(create_index_params.dimension, 10);
Expand All @@ -144,4 +181,38 @@ mod tests {
region: "us-west-2".to_string(),
});
}
}

#[test]
fn test_missing_name() {
let create_index_params = CreateIndexParams::builder()
.with_dimension(10)
.with_metric(Metric::Cosine)
.build();

assert!(create_index_params.is_err());
assert_eq!(create_index_params, Err(PineconeError::MissingNameError));
}

#[test]
fn test_missing_dimension() {
let create_index_params = CreateIndexParams::builder()
.with_name("test_index")
.with_metric(Metric::Cosine)
.build();

assert!(create_index_params.is_err());
assert_eq!(create_index_params, Err(PineconeError::MissingDimensionError));
}

#[test]
fn test_missing_spec() {
let create_index_params = CreateIndexParams::builder()
.with_name("test_index")
.with_dimension(10)
.build();

assert!(create_index_params.is_err());
assert_eq!(create_index_params, Err(PineconeError::MissingSpecError));
}

}
26 changes: 26 additions & 0 deletions pinecone_sdk/src/utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ pub enum PineconeError {
openapi_error: OpenAPIError<CreateIndexError>,
},

#[snafu(display("Index name missing."))]
MissingNameError,

#[snafu(display("Dimension missing."))]
MissingDimensionError,

#[snafu(display("Spec missing."))]
MissingSpecError,

#[snafu(display("Failed to parse headers: {}", json_error))]
InvalidHeadersError { json_error: serde_json::Error },

Expand All @@ -23,3 +32,20 @@ pub enum PineconeError {
#[snafu(display("Invalid region."))]
InvalidRegionError,
}

impl PartialEq for PineconeError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(PineconeError::APIKeyMissingError, PineconeError::APIKeyMissingError) => true,
(PineconeError::CreateIndexError { .. }, PineconeError::CreateIndexError { .. }) => true,
(PineconeError::MissingNameError, PineconeError::MissingNameError) => true,
(PineconeError::MissingDimensionError, PineconeError::MissingDimensionError) => true,
(PineconeError::MissingSpecError, PineconeError::MissingSpecError) => true,
(PineconeError::InvalidHeadersError { .. }, PineconeError::InvalidHeadersError { .. }) => true,
(PineconeError::InvalidCloudError { .. }, PineconeError::InvalidCloudError { .. }) => true,
(PineconeError::InvalidMetricError { .. }, PineconeError::InvalidMetricError { .. }) => true,
(PineconeError::InvalidRegionError, PineconeError::InvalidRegionError) => true,
_ => false,
}
}
}

0 comments on commit 901f0d5

Please sign in to comment.