Skip to content

Commit

Permalink
[Rust]: Add more SoS tags to test samples (#5624)
Browse files Browse the repository at this point in the history
Add further SoS tags for rust testing samples
  • Loading branch information
shepazon authored Nov 8, 2023
1 parent 20ed184 commit 8d51b94
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
18 changes: 17 additions & 1 deletion rust_dev_preview/examples/testing/src/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* SPDX-License-Identifier: Apache-2.0.
*/

// snippet-start:[testing.rust.replay-uses]
use aws_sdk_s3 as s3;
// snippet-end:[testing.rust.replay-uses]

#[allow(dead_code)]
// snippet-start:[testing.rust.replay]
Expand Down Expand Up @@ -42,6 +44,7 @@ pub async fn determine_prefix_file_size(

#[allow(dead_code)]
// snippet-start:[testing.rust.replay-tests]
// snippet-start:[testing.rust.replay-make-credentials]
fn make_s3_test_credentials() -> s3::config::Credentials {
s3::config::Credentials::new(
"ATESTCLIENT",
Expand All @@ -51,9 +54,12 @@ fn make_s3_test_credentials() -> s3::config::Credentials {
"",
)
}
// snippet-end:[testing.rust.replay-make-credentials]

// snippet-start:[testing.rust.replay-test-module]
#[cfg(test)]
mod test {
// snippet-start:[testing.rust.replay-test-single]
use super::*;
use aws_sdk_s3 as s3;
use aws_smithy_runtime::client::http::test_util::{ReplayEvent, StaticReplayClient};
Expand Down Expand Up @@ -90,9 +96,12 @@ mod test {
assert_eq!(7, size);
replay_client.assert_requests_match(&[]);
}
// snippet-end:[testing.rust.replay-test-single]

// snippet-start:[testing.rust.replay-test-multiple]
#[tokio::test]
async fn test_multiple_pages() {
// snippet-start:[testing.rust.replay-create-replay]
let page_1 = ReplayEvent::new(
http::Request::builder()
.method("GET")
Expand All @@ -116,22 +125,29 @@ mod test {
.unwrap(),
);
let replay_client = StaticReplayClient::new(vec![page_1, page_2]);
// snippet-end:[testing.rust.replay-create-replay]
// snippet-start:[testing.rust.replay-create-client]
let client: s3::Client = s3::Client::from_conf(
s3::Config::builder()
.credentials_provider(make_s3_test_credentials())
.region(s3::config::Region::new("us-east-1"))
.http_client(replay_client.clone())
.build(),
);
// snippet-end:[testing.rust.replay-create-client]

// Run the code we want to test with it
// snippet-start:[testing.rust.replay-test-and-verify]
let size = determine_prefix_file_size(client, "test-bucket", "test-prefix")
.await
.unwrap();

assert_eq!(19, size);

replay_client.assert_requests_match(&[]);
// snippet-end:[testing.rust.replay-test-and-verify]
}
// snippet-end:[testing.rust.replay-tests]
// snippet-end:[testing.rust.replay-test-multiple]
}
// snippet-end:[testing.rust.replay-tests]
// snippet-end:[testing.rust.replay-test-module]
18 changes: 16 additions & 2 deletions rust_dev_preview/examples/testing/src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
* SPDX-License-Identifier: Apache-2.0.
*/

// snippet-start:[testing.rust.wrapper]
// snippet-start:[testing.rust.wrapper-uses]
use aws_sdk_s3 as s3;
#[allow(unused_imports)]
use mockall::automock;

use s3::operation::list_objects_v2::{ListObjectsV2Error, ListObjectsV2Output};
// snippet-end:[testing.rust.wrapper-uses]

// snippet-start:[testing.rust.wrapper]
// snippet-start:[testing.rust.wrapper-which-impl]
#[cfg(test)]
pub use MockS3Impl as S3;
#[cfg(not(test))]
pub use S3Impl as S3;
// snippet-end:[testing.rust.wrapper-which-impl]

// snippet-start:[testing.rust.wrapper-impl]
#[allow(dead_code)]
pub struct S3Impl {
inner: s3::Client,
Expand Down Expand Up @@ -43,7 +48,9 @@ impl S3Impl {
.await
}
}
// snippet-end:[testing.rust.wrapper-impl]

// snippet-start:[testing.rust.wrapper-func]
#[allow(dead_code)]
pub async fn determine_prefix_file_size(
// Now we take a reference to our trait object instead of the S3 client
Expand Down Expand Up @@ -72,14 +79,17 @@ pub async fn determine_prefix_file_size(
}
Ok(total_size_bytes)
}
// snippet-end:[testing.rust.wrapper-func]
// snippet-end:[testing.rust.wrapper]

// snippet-start:[testing.rust.wrapper-test-mod]
#[cfg(test)]
mod test {
// snippet-start:[testing.rust.wrapper-tests]
use super::*;
use mockall::predicate::eq;

// snippet-start:[testing.rust.wrapper-tests]
// snippet-start:[testing.rust.wrapper-test-single]
#[tokio::test]
async fn test_single_page() {
let mut mock = MockS3Impl::default();
Expand All @@ -103,7 +113,9 @@ mod test {
// Verify we got the correct total size back
assert_eq!(7, size);
}
// snippet-end:[testing.rust.wrapper-test-single]

// snippet-start:[testing.rust.wrapper-test-multiple]
#[tokio::test]
async fn test_multiple_pages() {
// Create the Mock instance with two pages of objects now
Expand Down Expand Up @@ -143,5 +155,7 @@ mod test {

assert_eq!(19, size);
}
// snippet-end:[testing.rust.wrapper-test-multiple]
// snippet-end:[testing.rust.wrapper-tests]
}
// snippet-end:[testing.rust.wrapper-test-mod]

0 comments on commit 8d51b94

Please sign in to comment.