Skip to content

Commit 8d0943f

Browse files
aws-sdk-rust-ciDavidSoutherKPLauritzenford-at-aws
committed
[examples] Sync SDK examples from awsdocs/aws-doc-sdk-examples
Includes commit(s): 02819aebf320f98f0ac7f508a1bb73c569b3d6fe 2b96edaa6801292b9f0705b1f1e15ddd386ecf1b 1d851c3ae9fec718b2c7959f36d1193b3f8261dc c6d523c62d721609172d38cc2609f03d9ddf9e5b af69b9adfa59bfcc3d243de4c66efe9023af3382 7aa0394f9785da88c27b41c4895b972d5cc131d1 dbfcb34ed041e059f49ce74186308232c102919a 97a177aab8c3d2fef97416cb66e4b4d0da840138 Co-authored-by: David Souther <[email protected]> Co-authored-by: Kasper Primdal Lauritzen <[email protected]> Co-authored-by: ford prior <[email protected]>
1 parent 065a9b2 commit 8d0943f

File tree

13 files changed

+291
-53
lines changed

13 files changed

+291
-53
lines changed

examples/dynamodb/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ version = "0.54.1"
4242
path = "../../sdk/aws-smithy-types"
4343
version = "0.54.1"
4444

45+
[dependencies.sdk-examples-test-utils]
46+
path = "../test-utils"
47+
version = "0.1.0"
48+
4549
[dependencies.serde]
4650
version = "1.0"
4751
features = ["derive"]

examples/dynamodb/src/bin/list-more-tables.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ async fn main() -> Result<(), Error> {
2121
let config = make_config(Opt::from_args()).await?;
2222
let client = Client::new(&config);
2323

24-
list_tables_iterative(&client).await
24+
list_tables_iterative(&client).await?;
25+
Ok(())
2526
}

examples/dynamodb/src/bin/list-tables-local.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ async fn main() -> Result<(), Error> {
2020
.build();
2121

2222
let client = Client::from_conf(dynamodb_local_config);
23-
list_tables(&client).await
23+
list_tables(&client).await?;
24+
Ok(())
2425
}
2526
// snippet-end:[dynamodb.rust.list-tables-local]

examples/dynamodb/src/bin/list-tables.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ async fn main() -> Result<(), Error> {
2020

2121
let shared_config = make_config(Opt::from_args()).await?;
2222
let client = Client::new(&shared_config);
23-
list_tables(&client).await
23+
list_tables(&client).await?;
24+
Ok(())
2425
}

examples/dynamodb/src/scenario/add.rs

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@ pub struct Item {
1414
pub last: String,
1515
}
1616

17+
#[derive(Debug, PartialEq)]
18+
pub struct ItemOut {
19+
pub p_type: Option<AttributeValue>,
20+
pub age: Option<AttributeValue>,
21+
pub username: Option<AttributeValue>,
22+
pub first_name: Option<AttributeValue>,
23+
pub last_name: Option<AttributeValue>,
24+
}
25+
1726
// Add an item to a table.
1827
// snippet-start:[dynamodb.rust.add-item]
19-
pub async fn add_item(client: &Client, item: Item, table: &String) -> Result<(), Error> {
28+
pub async fn add_item(client: &Client, item: Item, table: &String) -> Result<ItemOut, Error> {
2029
let user_av = AttributeValue::S(item.username);
2130
let type_av = AttributeValue::S(item.p_type);
2231
let age_av = AttributeValue::S(item.age);
@@ -38,15 +47,71 @@ pub async fn add_item(client: &Client, item: Item, table: &String) -> Result<(),
3847

3948
let attributes = resp.attributes().unwrap();
4049

50+
let username = attributes.get("username").cloned();
51+
let first_name = attributes.get("first_name").cloned();
52+
let last_name = attributes.get("last_name").cloned();
53+
let age = attributes.get("age").cloned();
54+
let p_type = attributes.get("p_type").cloned();
55+
4156
println!(
4257
"Added user {:?}, {:?} {:?}, age {:?} as {:?} user",
43-
attributes.get("username"),
44-
attributes.get("first_name"),
45-
attributes.get("last_name"),
46-
attributes.get("age"),
47-
attributes.get("p_type")
58+
username, first_name, last_name, age, p_type
4859
);
4960

50-
Ok(())
61+
Ok(ItemOut {
62+
p_type,
63+
age,
64+
username,
65+
first_name,
66+
last_name,
67+
})
5168
}
5269
// snippet-end:[dynamodb.rust.add-item]
70+
71+
// snippet-start:[dynamodb.rust.add-item.test]
72+
#[cfg(test)]
73+
mod test {
74+
use aws_sdk_dynamodb::model::AttributeValue;
75+
use sdk_examples_test_utils::single_shot_client;
76+
77+
use super::{add_item, Item, ItemOut};
78+
79+
#[tokio::test]
80+
async fn test_add_item() {
81+
let client = single_shot_client! {
82+
sdk: aws_sdk_dynamodb,
83+
status: 200,
84+
response: r#"{"Attributes": {
85+
"p_type": {"S": "Brown"},
86+
"age": {"N": "27"},
87+
"username": {"S": "testuser"},
88+
"first_name": {"S": "Test"},
89+
"last_name": {"S": "User"}
90+
}}"#};
91+
92+
let item = Item {
93+
username: "testuser".into(),
94+
p_type: "Brown".into(),
95+
age: "27".into(),
96+
first: "Test".into(),
97+
last: "User".into(),
98+
};
99+
100+
let resp = add_item(&client, item, &"test_table".to_string()).await;
101+
102+
assert!(resp.is_ok(), "{:?}", resp);
103+
let out = resp.unwrap();
104+
105+
assert_eq!(
106+
out,
107+
ItemOut {
108+
p_type: Some(AttributeValue::S("Brown".to_string())),
109+
age: Some(AttributeValue::N("27".to_string())),
110+
username: Some(AttributeValue::S("testuser".to_string())),
111+
first_name: Some(AttributeValue::S("Test".to_string())),
112+
last_name: Some(AttributeValue::S("User".to_string()))
113+
}
114+
)
115+
}
116+
}
117+
// snippet-end:[dynamodb.rust.add-item.test]

examples/dynamodb/src/scenario/create.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ use crate::scenario::error::Error;
77
use aws_sdk_dynamodb::model::{
88
AttributeDefinition, KeySchemaElement, KeyType, ProvisionedThroughput, ScalarAttributeType,
99
};
10+
use aws_sdk_dynamodb::output::CreateTableOutput;
1011
use aws_sdk_dynamodb::Client;
1112

1213
// Create a table.
1314
// snippet-start:[dynamodb.rust.create-table]
14-
pub async fn create_table(client: &Client, table: &str, key: &str) -> Result<(), Error> {
15+
pub async fn create_table(
16+
client: &Client,
17+
table: &str,
18+
key: &str,
19+
) -> Result<CreateTableOutput, Error> {
1520
let a_name: String = key.into();
1621
let table_name: String = table.into();
1722

@@ -40,9 +45,9 @@ pub async fn create_table(client: &Client, table: &str, key: &str) -> Result<(),
4045
.await;
4146

4247
match create_table_response {
43-
Ok(_) => {
48+
Ok(out) => {
4449
println!("Added table {} with key {}", table, key);
45-
Ok(())
50+
Ok(out)
4651
}
4752
Err(e) => {
4853
eprintln!("Got an error creating table:");
@@ -52,3 +57,40 @@ pub async fn create_table(client: &Client, table: &str, key: &str) -> Result<(),
5257
}
5358
}
5459
// snippet-end:[dynamodb.rust.create-table]
60+
61+
#[cfg(test)]
62+
mod test {
63+
use sdk_examples_test_utils::single_shot_client;
64+
65+
use super::create_table;
66+
67+
// snippet-start:[dynamodb.rust.create-table.test]
68+
#[tokio::test]
69+
async fn test_create_table() {
70+
let client = single_shot_client! {
71+
sdk: aws_sdk_dynamodb,
72+
status: 200,
73+
response: r#""#
74+
};
75+
76+
let resp = create_table(&client, "test_table", "test_key").await;
77+
78+
assert!(resp.is_ok(), "{resp:?}");
79+
}
80+
// snippet-end:[dynamodb.rust.create-table.test]
81+
82+
// snippet-start:[dynamodb.rust.create-table.test_err]
83+
#[tokio::test]
84+
async fn test_create_table_err() {
85+
let client = single_shot_client! {
86+
sdk: aws_sdk_dynamodb,
87+
status: 400,
88+
response: r#""#
89+
};
90+
91+
let resp = create_table(&client, "test_table", "test_key").await;
92+
93+
assert!(resp.is_err(), "{resp:?}");
94+
}
95+
// snippet-end:[dynamodb.rust.create-table.test_err]
96+
}

examples/dynamodb/src/scenario/delete.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
*/
55

66
use crate::scenario::error::Error;
7-
use aws_sdk_dynamodb::{model::AttributeValue, Client};
7+
use aws_sdk_dynamodb::{
8+
model::AttributeValue,
9+
output::{DeleteItemOutput, DeleteTableOutput},
10+
Client,
11+
};
812

913
// Deletes an item from the table.
1014
// snippet-start:[dynamodb.rust.delete-item]
@@ -13,17 +17,17 @@ pub async fn delete_item(
1317
table: &str,
1418
key: &str,
1519
value: &str,
16-
) -> Result<(), Error> {
20+
) -> Result<DeleteItemOutput, Error> {
1721
match client
1822
.delete_item()
1923
.table_name(table)
2024
.key(key, AttributeValue::S(value.into()))
2125
.send()
2226
.await
2327
{
24-
Ok(_) => {
28+
Ok(out) => {
2529
println!("Deleted item from table");
26-
Ok(())
30+
Ok(out)
2731
}
2832
Err(e) => Err(Error::unhandled(e)),
2933
}
@@ -32,11 +36,36 @@ pub async fn delete_item(
3236

3337
// Delete a table.
3438
// snippet-start:[dynamodb.rust.delete-table]
35-
pub async fn delete_table(client: &Client, table: &str) -> Result<(), Error> {
36-
client.delete_table().table_name(table).send().await?;
39+
pub async fn delete_table(client: &Client, table: &str) -> Result<DeleteTableOutput, Error> {
40+
let resp = client.delete_table().table_name(table).send().await;
3741

38-
println!("Deleted table");
39-
40-
Ok(())
42+
match resp {
43+
Ok(out) => {
44+
println!("Deleted table");
45+
Ok(out)
46+
}
47+
Err(e) => Err(Error::Unhandled(e.into())),
48+
}
4149
}
4250
// snippet-end:[dynamodb.rust.delete-table]
51+
52+
#[cfg(test)]
53+
mod test {
54+
use super::delete_item;
55+
use sdk_examples_test_utils::single_shot_client;
56+
57+
// snippet-start:[dynamodb.rust.delete-item.test_err]
58+
#[tokio::test]
59+
async fn test_delete_item_err() {
60+
let client = single_shot_client! {
61+
sdk: aws_sdk_dynamodb,
62+
status: 500,
63+
response: r#""#
64+
};
65+
66+
let resp = delete_item(&client, "test_table", "id", "test").await;
67+
68+
assert!(resp.is_err(), "{resp:?}");
69+
}
70+
// snippet-end:[dynamodb.rust.delete-item.test_err]
71+
}

0 commit comments

Comments
 (0)