diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2d1dc85..03350bf 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -100,7 +100,7 @@ jobs: echo "Installed cargo lambda version:" cargo lambda --version - - name: Run integration tests + - name: Run unit and integration tests run: just test # deploy: diff --git a/Cargo.lock b/Cargo.lock index e5b7a7d..2609780 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,9 +118,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.87" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10f00e1f6e58a40e807377c75c6a7f97bf9044fab57816f2414e6f5f4499d7b8" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" [[package]] name = "arrayvec" @@ -274,9 +274,9 @@ dependencies = [ [[package]] name = "aws-sdk-dynamodb" -version = "1.45.0" +version = "1.47.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7f3d9e807092149e3df266e3f4d9760dac439b90f82d8438e5b2c0bbe62007f" +checksum = "8abc61e7374a01ecebcc3fd465655a2e1b83455f15b26716bfac05c35d25fa1b" dependencies = [ "aws-credential-types", "aws-runtime", @@ -297,9 +297,9 @@ dependencies = [ [[package]] name = "aws-sdk-eventbridge" -version = "1.44.0" +version = "1.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb6cea1cc285c6ee3df24cd1b0e9da4b194ab30ea43dde116557dfe2d64de6e1" +checksum = "9a02ac04d349fe763d3982cfe8f6433f8b55e6d213d69ffd4fd4e17561704337" dependencies = [ "aws-credential-types", "aws-runtime", @@ -319,9 +319,9 @@ dependencies = [ [[package]] name = "aws-sdk-sso" -version = "1.42.0" +version = "1.43.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27bf24cd0d389daa923e974b0e7c38daf308fc21e963c049f57980235017175e" +checksum = "70a9d27ed1c12b1140c47daf1bc541606c43fdafd918c4797d520db0043ceef2" dependencies = [ "aws-credential-types", "aws-runtime", @@ -341,9 +341,9 @@ dependencies = [ [[package]] name = "aws-sdk-ssooidc" -version = "1.43.0" +version = "1.44.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b43b3220f1c46ac0e9dcc0a97d94b93305dacb36d1dd393996300c6b9b74364" +checksum = "44514a6ca967686cde1e2a1b81df6ef1883d0e3e570da8d8bc5c491dcb6fc29b" dependencies = [ "aws-credential-types", "aws-runtime", @@ -363,9 +363,9 @@ dependencies = [ [[package]] name = "aws-sdk-sts" -version = "1.42.0" +version = "1.43.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1c46924fb1add65bba55636e12812cae2febf68c0f37361766f627ddcca91ce" +checksum = "cd7a4d279762a35b9df97209f6808b95d4fe78547fe2316b4d200a0283960c5a" dependencies = [ "aws-credential-types", "aws-runtime", @@ -577,9 +577,9 @@ dependencies = [ [[package]] name = "axum" -version = "0.7.5" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a6c9af12842a67734c9a2e355436e5d03b22383ed60cf13cd0c18fbfe3dcbcf" +checksum = "8f43644eed690f5374f1af436ecd6aea01cd201f6fbdf0178adaf6907afb2cec" dependencies = [ "async-trait", "axum-core", @@ -604,7 +604,7 @@ dependencies = [ "serde_urlencoded", "sync_wrapper 1.0.1", "tokio", - "tower", + "tower 0.5.1", "tower-layer", "tower-service", "tracing", @@ -612,9 +612,9 @@ dependencies = [ [[package]] name = "axum-core" -version = "0.4.3" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3" +checksum = "5e6b8ba012a258d63c9adfa28b9ddcf66149da6f986c5b5452e629d5ee64bf00" dependencies = [ "async-trait", "bytes", @@ -625,7 +625,7 @@ dependencies = [ "mime", "pin-project-lite", "rustversion", - "sync_wrapper 0.1.2", + "sync_wrapper 1.0.1", "tower-layer", "tower-service", "tracing", @@ -633,11 +633,10 @@ dependencies = [ [[package]] name = "axum-macros" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00c055ee2d014ae5981ce1016374e8213682aa14d9bf40e48ab48b5f3ef20eaa" +checksum = "57d123550fa8d071b7255cb0cc04dc302baa6c8c4a79f55701552684d8399bce" dependencies = [ - "heck 0.4.1", "proc-macro2", "quote", "syn 2.0.77", @@ -811,9 +810,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.7.1" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" +checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" dependencies = [ "serde", ] @@ -830,9 +829,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.18" +version = "1.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476" +checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" dependencies = [ "jobserver", "libc", @@ -891,9 +890,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.17" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac" +checksum = "b0956a43b323ac1afaffc053ed5c4b7c1f1800bacd1683c353aabbb752515dd3" dependencies = [ "clap_builder", "clap_derive", @@ -901,9 +900,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.17" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" +checksum = "4d72166dd41634086d5803a47eb71ae740e61d84709c36f3c34110173db3961b" dependencies = [ "anstream", "anstyle", @@ -913,11 +912,11 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.13" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" dependencies = [ - "heck 0.5.0", + "heck", "proc-macro2", "quote", "syn 2.0.77", @@ -1091,16 +1090,22 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "downcast" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1" + [[package]] name = "drive-deposits-cal-types" -version = "0.20.0" +version = "0.30.0" dependencies = [ "anyhow", "chrono", "chrono-tz", "drive-deposits-event-source", "drive-deposits-proto-grpc-types", - "heck 0.5.0", + "heck", "once_cell", "pretty_assertions", "rust_decimal", @@ -1116,7 +1121,7 @@ dependencies = [ [[package]] name = "drive-deposits-check-cmd" -version = "0.20.0" +version = "0.30.0" dependencies = [ "anyhow", "assert_cmd", @@ -1139,7 +1144,7 @@ dependencies = [ [[package]] name = "drive-deposits-event-source" -version = "0.20.0" +version = "0.30.0" dependencies = [ "anyhow", "aws-config", @@ -1157,7 +1162,7 @@ dependencies = [ [[package]] name = "drive-deposits-grpc-server" -version = "0.20.0" +version = "0.30.0" dependencies = [ "drive-deposits-cal-types", "drive-deposits-event-source", @@ -1173,7 +1178,7 @@ dependencies = [ [[package]] name = "drive-deposits-lambda-db-types" -version = "0.20.0" +version = "0.30.0" dependencies = [ "aws-sdk-dynamodb", "drive-deposits-rest-types", @@ -1187,7 +1192,7 @@ dependencies = [ [[package]] name = "drive-deposits-lambda-dynamodb-reader" -version = "0.20.0" +version = "0.30.0" dependencies = [ "aws-config", "aws-sdk-dynamodb", @@ -1204,7 +1209,7 @@ dependencies = [ [[package]] name = "drive-deposits-logs-lambda-target" -version = "0.20.0" +version = "0.30.0" dependencies = [ "aws-config", "aws-sdk-dynamodb", @@ -1221,10 +1226,10 @@ dependencies = [ [[package]] name = "drive-deposits-proto-grpc-types" -version = "0.20.0" +version = "0.30.0" dependencies = [ "drive-deposits-rest-types", - "heck 0.5.0", + "heck", "prost", "tonic", "tonic-build", @@ -1233,17 +1238,19 @@ dependencies = [ [[package]] name = "drive-deposits-rest-gateway-server" -version = "0.20.0" +version = "0.30.0" dependencies = [ "axum", "drive-deposits-proto-grpc-types", "drive-deposits-rest-types", + "mockall", + "pretty_assertions", "serde", "serde_json", "thiserror", "tokio", "tonic", - "tower", + "tower 0.4.13", "tower-http", "tracing", "tracing-subscriber", @@ -1252,7 +1259,7 @@ dependencies = [ [[package]] name = "drive-deposits-rest-types" -version = "0.20.0" +version = "0.30.0" dependencies = [ "chrono", "chrono-tz", @@ -1383,6 +1390,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fragile" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c2141d6d6c8512188a7891b4b01590a45f6dac67afb4f255c4124dbb86d4eaa" + [[package]] name = "funty" version = "2.0.0" @@ -1579,12 +1592,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "heck" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" - [[package]] name = "heck" version = "0.5.0" @@ -1785,16 +1792,16 @@ dependencies = [ "pin-project-lite", "socket2", "tokio", - "tower", + "tower 0.4.13", "tower-service", "tracing", ] [[package]] name = "iana-time-zone" -version = "0.1.60" +version = "0.1.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -1853,9 +1860,9 @@ dependencies = [ [[package]] name = "iri-string" -version = "0.7.3" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f80d71a680f471ab23df28c5cb3e6a5598f96ab5570a2e320b73a44c46db440" +checksum = "9c25163201be6ded9e686703e85532f8f852ea1f92ba625cb3c51f7fe6d07a4a" dependencies = [ "memchr", "serde", @@ -1950,7 +1957,7 @@ dependencies = [ "serde_path_to_error", "tokio", "tokio-stream", - "tower", + "tower 0.4.13", "tower-layer", "tracing", ] @@ -1970,7 +1977,7 @@ dependencies = [ "hyper 1.4.1", "hyper-util", "tokio", - "tower", + "tower 0.4.13", "tower-service", "tracing", "tracing-subscriber", @@ -2068,6 +2075,32 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "mockall" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4c28b3fb6d753d28c20e826cd46ee611fda1cf3cde03a443a974043247c065a" +dependencies = [ + "cfg-if", + "downcast", + "fragile", + "mockall_derive", + "predicates", + "predicates-tree", +] + +[[package]] +name = "mockall_derive" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "341014e7f530314e9a1fdbc7400b244efea7122662c96bfa248c31da5bfb2020" +dependencies = [ + "cfg-if", + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "multimap" version = "0.10.0" @@ -2339,9 +2372,9 @@ dependencies = [ [[package]] name = "pretty_assertions" -version = "1.4.0" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d" dependencies = [ "diff", "yansi", @@ -2416,7 +2449,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8650aabb6c35b860610e9cff5dc1af886c9e25073b7b1712a68972af4281302" dependencies = [ "bytes", - "heck 0.5.0", + "heck", "itertools", "log", "multimap", @@ -2530,9 +2563,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.3" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" dependencies = [ "bitflags", ] @@ -2694,9 +2727,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.36" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f55e80d50763938498dd5ebb18647174e0c76dc38c5505294bb224624f30f36" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags", "errno", @@ -2820,9 +2853,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.11.1" +version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" +checksum = "ea4a292869320c0272d7bc55a5a6aafaff59b4f63404a003887b679a2e05b4b6" dependencies = [ "core-foundation-sys", "libc", @@ -3044,7 +3077,7 @@ version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" dependencies = [ - "heck 0.5.0", + "heck", "proc-macro2", "quote", "rustversion", @@ -3275,9 +3308,9 @@ checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" [[package]] name = "toml_edit" -version = "0.22.20" +version = "0.22.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +checksum = "3b072cee73c449a636ffd6f32bd8de3a9f7119139aff882f44943ce2986dc5cf" dependencies = [ "indexmap 2.5.0", "toml_datetime", @@ -3308,7 +3341,7 @@ dependencies = [ "socket2", "tokio", "tokio-stream", - "tower", + "tower 0.4.13", "tower-layer", "tower-service", "tracing", @@ -3372,6 +3405,22 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2873938d487c3cfb9aed7546dc9f2711d867c9f90c46b889989a2cb84eba6b4f" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper 0.1.2", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "tower-http" version = "0.5.2" @@ -3396,7 +3445,7 @@ dependencies = [ "pin-project-lite", "tokio", "tokio-util", - "tower", + "tower 0.4.13", "tower-layer", "tower-service", "tracing", @@ -3525,9 +3574,9 @@ checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "unicode-normalization" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" dependencies = [ "tinyvec", ] @@ -3850,9 +3899,9 @@ checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4" [[package]] name = "yansi" -version = "0.5.1" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" [[package]] name = "zerocopy" diff --git a/README.md b/README.md index 5da3786..a81a8cc 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,10 @@ - [Bridging Synchronous and Asynchronous Components In DriveDeposits](#bridging-synchronous-and-asynchronous-components-in-drivedeposits) - [Deployment](#deployment) - [Hybrid Integration Testing Tool](#hybrid-integration-testing-tool) +- [Running Tests](#running-tests) + - [Integration tests](#integration-tests) + - [Unit and Integration tests](#unit-and-integration-tests) + - [End-to-End tests](#end-to-end-tests) - [Data population](#data-population) - [Querying with custom domain](#querying-with-custom-domain) - [Development Tool: cargo lambda](#development-tool-cargo-lambda) @@ -318,10 +322,22 @@ Key features: .cargo/config.toml has alias for command line [drive-deposits-check-cmd](drive-deposits-check-cmd) so can be run using `cargo ddcheck` For help see `cargo ddcheck -- --help` +[Back to Table of Contents](#table-of-contents) + +### Running Tests + ##### Integration tests +`just test-intg` + +##### Unit and Integration tests + `just test` +##### End-to-End tests + +`just test-e2e` + [Back to Table of Contents](#table-of-contents) ### Data population @@ -342,7 +358,7 @@ Key features: `just post-calculate-portfolio-valid-greater-amount` -##### Alternatively Using check command without servers +##### Alternatively, Using check command without servers `just run-drive-deposits-check-cmd-valid-send-events` diff --git a/drive-deposits-cal-types/Cargo.toml b/drive-deposits-cal-types/Cargo.toml index 4fdcc35..dcbdb37 100644 --- a/drive-deposits-cal-types/Cargo.toml +++ b/drive-deposits-cal-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drive-deposits-cal-types" -version = "0.20.0" +version = "0.30.0" edition = "2021" [dependencies] diff --git a/drive-deposits-cal-types/tests/helper/enable_tracing.rs b/drive-deposits-cal-types/tests/helper/enable_tracing.rs index 62d16a0..2354eb2 100644 --- a/drive-deposits-cal-types/tests/helper/enable_tracing.rs +++ b/drive-deposits-cal-types/tests/helper/enable_tracing.rs @@ -1,8 +1,9 @@ use anyhow::Result; use once_cell::sync::OnceCell; use tracing::{debug, Span}; -use tracing_subscriber::{EnvFilter, registry}; +use tracing_subscriber::fmt::format::FmtSpan; use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::registry; use tracing_subscriber::util::SubscriberInitExt; static INIT: OnceCell<()> = OnceCell::new(); @@ -10,13 +11,10 @@ static INIT: OnceCell<()> = OnceCell::new(); pub fn setup_tracing_subscriber() -> Result<()> { let rust_log = std::env::var("RUST_LOG")?; println!("RUST_LOG is {}", rust_log); - // can change level to debug to see all debug messages in tests - // or to info not to see - // for test span added test=debug here - registry() - .with(EnvFilter::try_from_default_env()?.add_directive("test=debug".parse()?)) - .with(tracing_subscriber::fmt::layer()) - .init(); + let fmt_layer = tracing_subscriber::fmt::layer() + .with_test_writer() + .with_span_events(FmtSpan::FULL); + registry().with(fmt_layer).try_init()?; debug!("tracing subscriber initialized"); Ok(()) } diff --git a/drive-deposits-check-cmd/Cargo.toml b/drive-deposits-check-cmd/Cargo.toml index 26b0fa6..6d9a8fe 100644 --- a/drive-deposits-check-cmd/Cargo.toml +++ b/drive-deposits-check-cmd/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drive-deposits-check-cmd" -version = "0.20.0" +version = "0.30.0" edition = "2021" [dependencies] diff --git a/drive-deposits-check-cmd/tests/enable_tracing.rs b/drive-deposits-check-cmd/tests/enable_tracing.rs index 95ad1bc..2354eb2 100644 --- a/drive-deposits-check-cmd/tests/enable_tracing.rs +++ b/drive-deposits-check-cmd/tests/enable_tracing.rs @@ -1,24 +1,21 @@ use anyhow::Result; use once_cell::sync::OnceCell; use tracing::{debug, Span}; -use tracing_subscriber::{EnvFilter, registry}; +use tracing_subscriber::fmt::format::FmtSpan; use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::registry; use tracing_subscriber::util::SubscriberInitExt; static INIT: OnceCell<()> = OnceCell::new(); pub fn setup_tracing_subscriber() -> Result<()> { - // can change level to debug to see all debug messages in tests - // or to info not to see - // for test span added test=debug here - registry() - // the test=debug is for debug! statements inside tests itself - .with(EnvFilter::try_from_default_env()?.add_directive("test=debug".parse()?)) - .with(tracing_subscriber::fmt::layer()) - .init(); - debug!("tracing subscriber initialized"); let rust_log = std::env::var("RUST_LOG")?; - debug!("RUST_LOG is {}", rust_log); + println!("RUST_LOG is {}", rust_log); + let fmt_layer = tracing_subscriber::fmt::layer() + .with_test_writer() + .with_span_events(FmtSpan::FULL); + registry().with(fmt_layer).try_init()?; + debug!("tracing subscriber initialized"); Ok(()) } diff --git a/drive-deposits-event-source/Cargo.toml b/drive-deposits-event-source/Cargo.toml index 0cd28f3..3b520e4 100644 --- a/drive-deposits-event-source/Cargo.toml +++ b/drive-deposits-event-source/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drive-deposits-event-source" -version = "0.20.0" +version = "0.30.0" edition = "2021" diff --git a/drive-deposits-grpc-server/Cargo.toml b/drive-deposits-grpc-server/Cargo.toml index cb199d8..1d867d3 100644 --- a/drive-deposits-grpc-server/Cargo.toml +++ b/drive-deposits-grpc-server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drive-deposits-grpc-server" -version = "0.20.0" +version = "0.30.0" edition = "2021" [dependencies] diff --git a/drive-deposits-lambda-db-types/Cargo.toml b/drive-deposits-lambda-db-types/Cargo.toml index 4cfb260..6b807f7 100644 --- a/drive-deposits-lambda-db-types/Cargo.toml +++ b/drive-deposits-lambda-db-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drive-deposits-lambda-db-types" -version = "0.20.0" +version = "0.30.0" edition = "2021" [dependencies] diff --git a/drive-deposits-lambda-dynamodb-reader/Cargo.toml b/drive-deposits-lambda-dynamodb-reader/Cargo.toml index e7136cb..4c19741 100644 --- a/drive-deposits-lambda-dynamodb-reader/Cargo.toml +++ b/drive-deposits-lambda-dynamodb-reader/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drive-deposits-lambda-dynamodb-reader" -version = "0.20.0" +version = "0.30.0" edition = "2021" [dependencies] diff --git a/drive-deposits-logs-lambda-target/Cargo.toml b/drive-deposits-logs-lambda-target/Cargo.toml index 833e591..d6858c9 100644 --- a/drive-deposits-logs-lambda-target/Cargo.toml +++ b/drive-deposits-logs-lambda-target/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drive-deposits-logs-lambda-target" -version = "0.20.0" +version = "0.30.0" edition = "2021" [dependencies] diff --git a/drive-deposits-proto-grpc-types/Cargo.toml b/drive-deposits-proto-grpc-types/Cargo.toml index 23fe98c..72deb9f 100644 --- a/drive-deposits-proto-grpc-types/Cargo.toml +++ b/drive-deposits-proto-grpc-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drive-deposits-proto-grpc-types" -version = "0.20.0" +version = "0.30.0" edition = "2021" [dependencies] diff --git a/drive-deposits-rest-gateway-server/Cargo.toml b/drive-deposits-rest-gateway-server/Cargo.toml index c6ecb6b..091241f 100644 --- a/drive-deposits-rest-gateway-server/Cargo.toml +++ b/drive-deposits-rest-gateway-server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drive-deposits-rest-gateway-server" -version = "0.20.0" +version = "0.30.0" edition = "2021" [dependencies] @@ -16,9 +16,14 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } # tower and tower-http tower = { version = "0.4.13", features = ["full"] } tower-http = { version = "0.5.2", features = ["full"] } +mockall = "0.13.0" # workspace member depdenencies drive-deposits-rest-types = { path = "../drive-deposits-rest-types" } # proto generated dependency here the drive-deposits-proto-grpc-types is still package # name so with dashes drive-deposits-proto-grpc-types = { path = "../drive-deposits-proto-grpc-types" } + + +[dev-dependencies] +pretty_assertions = "1.4.1" \ No newline at end of file diff --git a/drive-deposits-rest-gateway-server/src/drive_deposits_client.rs b/drive-deposits-rest-gateway-server/src/drive_deposits_client.rs index 126f7b1..31d0ccf 100644 --- a/drive-deposits-rest-gateway-server/src/drive_deposits_client.rs +++ b/drive-deposits-rest-gateway-server/src/drive_deposits_client.rs @@ -1,20 +1,50 @@ -use axum::Json; +use axum::{async_trait, Json}; +use mockall::automock; use tracing::{debug, debug_span, error, info}; use app_error::Error as AppError; -use drive_deposits_proto_grpc_types::generated::drive_deposits_service_client::DriveDepositsServiceClient; -use drive_deposits_rest_types::rest_types::CalculatePortfolioResponse; +use drive_deposits_proto_grpc_types::generated::{ + drive_deposits_service_client::DriveDepositsServiceClient, + CalculatePortfolioRequest as GrpcCalculatePortfolioRequest, + CalculatePortfolioResponse as GrpcCalculatePortfolioResponse, +}; +use drive_deposits_rest_types::rest_types::{ + CalculatePortfolioRequest as RestCalculatePortfolioRequest, + CalculatePortfolioResponse as RestCalculatePortfolioResponse, +}; use request_error::ValidateCalculateRequest; +use tonic::transport::Channel; mod app_error; - mod request_error; +#[automock] +#[async_trait] +pub trait CalculatePortfolioClient { + async fn calculate_portfolio_request( + mut self, + request: tonic::Request, + ) -> Result, tonic::Status>; +} + +#[async_trait] +impl CalculatePortfolioClient for DriveDepositsServiceClient { + // Differentiating the trait method name from the method being called on self helps avoid confusion and potential infinite recursion + async fn calculate_portfolio_request( + mut self, + request: tonic::Request, + ) -> Result, tonic::Status> { + // Call the actual gRPC method instead of recursively calling itself: using associated style + // DriveDepositsServiceClient::calculate_portfolio(&mut self, request).await + self.calculate_portfolio(request).await + } +} + // #[debug_handler] pub async fn calculate_portfolio( ValidateCalculateRequest(rest_delta_request): ValidateCalculateRequest, -) -> Result, AppError> { - let span = debug_span!("rest_calculate_portfolio"); +) -> Result, AppError> { + let span = debug_span!("calculate_portfolio"); span.in_scope(|| { debug!( "calculate_portfolio request incoming is : {:#?}", @@ -22,15 +52,30 @@ pub async fn calculate_portfolio( ) }); - let mut client = DriveDepositsServiceClient::connect("http://[::1]:50052") + let client = DriveDepositsServiceClient::connect("http://[::1]:50052") .await .inspect_err(|err| { span.in_scope(|| error!("grpc client connection error: {:?}", err)); })?; + calculate_portfolio_with_client(rest_delta_request, client).await +} + +pub async fn calculate_portfolio_with_client( + rest_delta_request: RestCalculatePortfolioRequest, + client: impl CalculatePortfolioClient, +) -> Result, AppError> { + let span = debug_span!("calculate_portfolio_with_client"); + span.in_scope(|| { + debug!( + "calculate_portfolio request incoming is : {:#?}", + rest_delta_request + ) + }); + let grpc_delta_request = span.in_scope(|| rest_delta_request.into()); let grpc_request = tonic::Request::new(grpc_delta_request); - let grpc_response = client.calculate_portfolio(grpc_request).await?; + let grpc_response = client.calculate_portfolio_request(grpc_request).await?; span.in_scope(|| { info!( @@ -46,3 +91,83 @@ pub async fn calculate_portfolio( Ok(Json(rest_response)) }) } + +#[cfg(test)] +mod tests { + use super::calculate_portfolio_with_client; + use crate::drive_deposits_client::MockCalculatePortfolioClient; + use drive_deposits_proto_grpc_types::generated::{ + Bank as GrpcBank, CalculatePortfolioResponse as GrpcCalculatePortfolioResponse, + Outcome as GrpcOutcome, + }; + use drive_deposits_rest_types::rest_types::{ + Bank as RestBank, CalculatePortfolioRequest as RestCalculatePortfolioRequest, + NewDelta as RestNewDelta, Outcome as RestOutcome, + }; + use pretty_assertions::assert_eq; + use tracing_subscriber::fmt::format::FmtSpan; + + fn init_test_subscriber() { + let subscriber = tracing_subscriber::fmt() + .with_test_writer() + .with_span_events(FmtSpan::FULL) + .finish(); + tracing::subscriber::set_global_default(subscriber) + .expect("setting default subscriber failed"); + } + + #[tokio::test] + async fn test_calculate_portfolio_with_client() { + init_test_subscriber(); + let mut mock_client = MockCalculatePortfolioClient::new(); + + let test_grpc_bank = GrpcBank { + name: "name".to_string(), + uuid: "uuid".to_string(), + bank_tz: "bank_tz".to_string(), + deposits: vec![], + outcome: Some(GrpcOutcome { + delta: None, + maturity: None, + errors: vec![], + }), + }; + mock_client + .expect_calculate_portfolio_request() + .returning(move |_grpc_request| { + let grpc_response = GrpcCalculatePortfolioResponse { + uuid: "uuid".to_string(), + banks: vec![test_grpc_bank.clone()], + outcome: None, + created_at: "created_at".to_string(), + }; + Ok(tonic::Response::new(grpc_response)) + }); + + let rest_request = RestCalculatePortfolioRequest { + new_banks: vec![], + new_delta: RestNewDelta { + period: "1".to_string(), + period_unit: "Month".to_string(), + }, + }; + let result = calculate_portfolio_with_client(rest_request, mock_client).await; + + assert!(result.is_ok()); + let response = result.unwrap(); + assert_eq!(response.0.uuid, "uuid"); + let expected_bank = RestBank { + name: "name".to_string(), + uuid: "uuid".to_string(), + bank_tz: "bank_tz".to_string(), + deposits: vec![], + outcome: Some(RestOutcome { + delta: None, + maturity: None, + errors: vec![], + }), + }; + let actual_bank_tz = response.0.banks.first().unwrap().bank_tz.clone(); + assert_eq!(actual_bank_tz, expected_bank.bank_tz); + } +} diff --git a/drive-deposits-rest-types/Cargo.toml b/drive-deposits-rest-types/Cargo.toml index da9d306..dfe45ea 100644 --- a/drive-deposits-rest-types/Cargo.toml +++ b/drive-deposits-rest-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drive-deposits-rest-types" -version = "0.20.0" +version = "0.30.0" edition = "2021" [dependencies] diff --git a/justfile b/justfile index 0196df6..385139c 100644 --- a/justfile +++ b/justfile @@ -47,7 +47,7 @@ build-drive-deposits-grpc-server: build-drive-deposits-rest-gateway-server: cd drive-deposits-rest-gateway-server && \ - cargo build --package drive-deposits-rest-gateway-server + cargo build --package drive-deposits-rest-gateway-server --tests # watching builds # .cargo/cargo.toml has SEND_CAL_EVENTS = "true" so we can overrride it here as needed @@ -65,6 +65,10 @@ watch-build-drive-deposits-event-source: cd drive-deposits-event-source cargo watch -x "build --package drive-deposits-event-source" +watch-build-drive-deposits-rest-gateway-server-with-tests: + cd drive-deposits-rest-gateway-server && \ + cargo watch -x "build --package drive-deposits-rest-gateway-server --tests" + watch-build-drive-deposits-grpc-server: cd drive-deposits-grpc-server cargo watch -x "build --package drive-deposits-grpc-server" @@ -116,11 +120,21 @@ watch-run-drive-deposits-rest-gateway-server: cargo watch --why --poll -x "run --package drive-deposits-rest-gateway-server --bin drive-deposits-rest-gateway-server" # test +# cargo help test +# cargo test -- --help +# With --nocapture, since tests run in parallel by default, the output from different tests would indeed be mixed together. This can lead to interleaved output in the console test: - cargo test --workspace + cargo test --workspace --tests -- --show-output + +test-intg: + cargo test --workspace --test '*' -- --show-output + +test-rest-gateway-server: + cargo test --package drive-deposits-rest-gateway-server --tests -- --show-output -test-localtsack-aws-deploy:localstack-deploy-drive-deposits-event-rules - cargo test -p drive-deposits-check-cmd --test test_delta_calculator_cli --features localstack_aws_deploy +# localstart should be started before running this; use just localstack-start +test-e2e:localstack-deploy-drive-deposits-event-rules + cargo test -p drive-deposits-check-cmd --test test_delta_calculator_cli --features localstack_aws_deploy -- --show-output # watch test watch-test: