Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/unit tests #63

Merged
merged 5 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) }
NChitty marked this conversation as resolved.
Show resolved Hide resolved
}

#[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
Loading