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

[Rust]: Add more SoS tags to test samples #5624

Merged
merged 8 commits into from
Nov 8, 2023
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
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]
Loading