Skip to content

Commit

Permalink
Chore/unit tests (#63)
Browse files Browse the repository at this point in the history
* Add mockall

Add mockall

* Create dynamodb client trait and impl

* Update and test

* Cargo fmt

* Code review comment
  • Loading branch information
NChitty committed Jun 29, 2024
1 parent 9c440c8 commit e3f2208
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 18 deletions.
78 changes: 78 additions & 0 deletions lambda/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lambda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ aws-sdk-secretsmanager = "1.16.0"
axum = "0.7.3"
lambda_http = "0.9.0"
lambda_runtime = "0.9.0"
mockall = "0.12.1"
serde = { version = "1.0", features = ["derive"] }
serde_dynamo = { version = "4.2.13", features = ["aws-sdk-dynamodb+1"] }
serde_json = "1.0.111"
Expand Down
85 changes: 85 additions & 0 deletions lambda/src/aws_client/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::collections::HashMap;

use aws_config::SdkConfig;
use aws_sdk_dynamodb::error::SdkError;
use aws_sdk_dynamodb::operation::delete_item::{DeleteItemError, DeleteItemOutput};
use aws_sdk_dynamodb::operation::get_item::{GetItemError, GetItemOutput};
use aws_sdk_dynamodb::operation::put_item::{PutItemError, PutItemOutput};
use aws_sdk_dynamodb::types::AttributeValue;
use aws_sdk_dynamodb::Client;
use axum::async_trait;

#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait DynamoDbClient: Send + Sync {
async fn get_item(
&self,
table_name: &str,
key: HashMap<String, AttributeValue>,
) -> Result<GetItemOutput, GetItemError>;
async fn put_item(
&self,
table_name: &str,
item: HashMap<String, AttributeValue>,
) -> Result<PutItemOutput, PutItemError>;

async fn delete_item(
&self,
table_name: &str,
key: HashMap<String, AttributeValue>,
) -> Result<DeleteItemOutput, DeleteItemError>;
}

#[derive(Clone)]
pub struct DynamoDbClientImpl(Client);

impl DynamoDbClientImpl {
#[must_use]
pub fn new(sdk_config: &SdkConfig) -> Self { Self(Client::new(sdk_config)) }
}

#[async_trait]
impl DynamoDbClient for DynamoDbClientImpl {
async fn get_item(
&self,
table_name: &str,
key: HashMap<String, AttributeValue>,
) -> Result<GetItemOutput, GetItemError> {
self.0
.get_item()
.table_name(table_name)
.set_key(Some(key))
.send()
.await
.map_err(SdkError::into_service_error)
}

async fn put_item(
&self,
table_name: &str,
item: HashMap<String, AttributeValue>,
) -> Result<PutItemOutput, PutItemError> {
self.0
.put_item()
.table_name(table_name)
.set_item(Some(item))
.send()
.await
.map_err(SdkError::into_service_error)
}

async fn delete_item(
&self,
table_name: &str,
key: HashMap<String, AttributeValue>,
) -> Result<DeleteItemOutput, DeleteItemError> {
self.0
.delete_item()
.table_name(table_name)
.set_key(Some(key))
.condition_expression("attribute_exists(id)")
.send()
.await
.map_err(SdkError::into_service_error)
}
}
1 change: 1 addition & 0 deletions lambda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::future::Future;
use axum::http::StatusCode;
use uuid::Uuid;

pub mod aws_client;
pub mod recipe;
pub mod services;

Expand Down
Loading

0 comments on commit e3f2208

Please sign in to comment.