From 7a22fdff64ae40c7aa93d3e527b9436be9d2b586 Mon Sep 17 00:00:00 2001 From: max funk Date: Sat, 14 Oct 2023 10:59:36 -0700 Subject: [PATCH 1/6] bump rust rule deps --- services/rule/Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/rule/Cargo.toml b/services/rule/Cargo.toml index 4a2a8b1f..bcaff14e 100644 --- a/services/rule/Cargo.toml +++ b/services/rule/Cargo.toml @@ -5,11 +5,11 @@ edition = "2021" publish = false [dependencies] -axum = "0.6.6" -serde = {version = "1.0.152", features = ["derive"]} -tokio = {version = "1.25.0", features = [ "full" ]} +axum = "0.6.2" +serde = {version = "1.0.189", features = ["derive"]} +tokio = {version = "1.33.0", features = [ "full" ]} tower = "0.4.13" -tower-http = "0.4.0" +tower-http = "0.4.4" tracing = "0.1.37" tracing-subscriber = {version = "0.3.16", features = ["env-filter"]} types = { path = "../../crates/types" } From 05a2642503f6ab2dd83cda7d289a438efa81f3a0 Mon Sep 17 00:00:00 2001 From: max funk Date: Sat, 14 Oct 2023 10:59:48 -0700 Subject: [PATCH 2/6] cargo lock --- Cargo.lock | 215 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 178 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e771b3cf..c3f1148e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "android_system_properties" version = "0.1.5" @@ -42,7 +57,7 @@ checksum = "6137c6234afb339e75e764c866e3594900f0211e1315d33779f269bbe2ec6967" dependencies = [ "async-trait", "axum-core", - "bitflags", + "bitflags 1.3.2", "bytes", "futures-util", "http", @@ -84,6 +99,21 @@ dependencies = [ "tower-service", ] +[[package]] +name = "backtrace" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "base64" version = "0.13.1" @@ -121,6 +151,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" + [[package]] name = "block-buffer" version = "0.10.4" @@ -398,6 +434,12 @@ dependencies = [ "wasi 0.11.0+wasi-snapshot-preview1", ] +[[package]] +name = "gimli" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" + [[package]] name = "hashbrown" version = "0.12.3" @@ -490,7 +532,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2", + "socket2 0.4.7", "tokio", "tower-service", "tracing", @@ -561,9 +603,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.139" +version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" [[package]] name = "link-cplusplus" @@ -629,6 +671,15 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "miniz_oxide" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +dependencies = [ + "adler", +] + [[package]] name = "mio" version = "0.8.6" @@ -638,7 +689,7 @@ dependencies = [ "libc", "log", "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -689,6 +740,15 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.32.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.17.1" @@ -721,7 +781,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-sys", + "windows-sys 0.45.0", ] [[package]] @@ -783,9 +843,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.9" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] name = "pin-utils" @@ -896,7 +956,7 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" dependencies = [ - "bitflags", + "bitflags 1.3.2", ] [[package]] @@ -941,6 +1001,12 @@ dependencies = [ "types", ] +[[package]] +name = "rustc-demangle" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" + [[package]] name = "rustversion" version = "1.0.11" @@ -967,29 +1033,29 @@ checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" [[package]] name = "serde" -version = "1.0.152" +version = "1.0.189" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.152" +version = "1.0.189" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" +checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.36", ] [[package]] name = "serde_json" -version = "1.0.94" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" dependencies = [ "itoa", "ryu", @@ -1105,6 +1171,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "socket2" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e" +dependencies = [ + "libc", + "windows-sys 0.48.0", +] + [[package]] name = "sqls" version = "0.1.0" @@ -1255,33 +1331,32 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.26.0" +version = "1.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03201d01c3c27a29c8a5cee5b55a93ddae1ccf6f08f65365c2c918f8c1b76f64" +checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653" dependencies = [ - "autocfg", + "backtrace", "bytes", "libc", - "memchr", "mio", "num_cpus", "parking_lot", "pin-project-lite", "signal-hook-registry", - "socket2", + "socket2 0.5.4", "tokio-macros", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] name = "tokio-macros" -version = "1.8.2" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.36", ] [[package]] @@ -1303,7 +1378,7 @@ dependencies = [ "pin-project-lite", "postgres-protocol", "postgres-types", - "socket2", + "socket2 0.4.7", "tokio", "tokio-util", ] @@ -1340,11 +1415,11 @@ dependencies = [ [[package]] name = "tower-http" -version = "0.4.0" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d1d42a9b3f3ec46ba828e8d376aec14592ea199f70a06a548587ecd1c4ab658" +checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" dependencies = [ - "bitflags", + "bitflags 2.4.0", "bytes", "futures-core", "futures-util", @@ -1612,7 +1687,16 @@ version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" dependencies = [ - "windows-targets", + "windows-targets 0.42.1", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", ] [[package]] @@ -1621,13 +1705,28 @@ version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.42.1", + "windows_aarch64_msvc 0.42.1", + "windows_i686_gnu 0.42.1", + "windows_i686_msvc 0.42.1", + "windows_x86_64_gnu 0.42.1", + "windows_x86_64_gnullvm 0.42.1", + "windows_x86_64_msvc 0.42.1", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -1636,38 +1735,80 @@ version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_msvc" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_i686_gnu" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_msvc" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_x86_64_gnu" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_msvc" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" From 7b462c50a66aff3fd31aafc004e3272e1a6e6ed7 Mon Sep 17 00:00:00 2001 From: max funk Date: Sat, 14 Oct 2023 11:00:41 -0700 Subject: [PATCH 3/6] move faq sentence --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3f17c2a6..66c94046 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,8 @@ systemaccounting optimizes the flow of capital by expediting the discovery of ec **a.** no, and please use the word *replication* **q.** i dont find any words in here used by the media. what is this? -**a.** encryption solves access risk. replication solves single point of failure and inconsistency risk. neither of these solutions are relevant to modeling currency as an electric current. this payment application solves contemporary economic issues by replacing "monetary" policy with a natural physical law. first, currency is modeled as a lightweight, dual positive-negative structured time-series between creditors and debitors respectively. encryption and replication are secondary: +**a.** encryption solves access risk. replication solves single point of failure and inconsistency risk. neither of these solutions are relevant to modeling currency as an electric current. this payment application solves contemporary economic issues by replacing "monetary" policy with a natural physical law. +first, currency is modeled as a lightweight, dual positive-negative structured time-series between creditors and debitors respectively: ```json5 { "item": "bottled water", @@ -29,6 +30,7 @@ systemaccounting optimizes the flow of capital by expediting the discovery of ec "debitor_approval_time": "2023-03-20T05:24:13.465Z" } ``` +encryption and replication are secondary **q.** where will i bank? **a.** you dont need a bank. you need accounting. if you still wish to lend your money after receiving the service of accounting, please judge the risk of the loan you intend to offer the recipient by first exploiting your access to their accounting, then assume no one except you will own that risk after you consume it From e9f4444681dd81594927a57b61fba32c48418728 Mon Sep 17 00:00:00 2001 From: max funk Date: Sat, 14 Oct 2023 11:06:19 -0700 Subject: [PATCH 4/6] rust db connection trait with blanket impl --- crates/pg/src/lib.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/crates/pg/src/lib.rs b/crates/pg/src/lib.rs index a6a48d91..bceb03e9 100644 --- a/crates/pg/src/lib.rs +++ b/crates/pg/src/lib.rs @@ -65,29 +65,35 @@ mod tests { } } -pub type DynConnPool = Arc; +pub type DynConnPool = Arc; + +pub type DynDBConn = Arc; #[async_trait] -pub trait ConnPoolTrait { - async fn get_conn(&self) -> DatabaseConnection; +pub trait DBConnPoolTrait { + async fn get_conn(&self) -> DynDBConn; } +#[async_trait] +pub trait DBConnTrait: AccountStore + RuleInstanceStore {} +impl DBConnTrait for T {} + // https://github.com/tokio-rs/axum/blob/5793e75aacfeae16f02fea144ecc2ee7dcb12f55/examples/tokio-postgres/src/main.rs #[derive(Clone)] pub struct ConnectionPool(Pool>); #[async_trait] -impl ConnPoolTrait for ConnectionPool { - async fn get_conn(&self) -> DatabaseConnection { +impl DBConnPoolTrait for ConnectionPool { + async fn get_conn(&self) -> DynDBConn { let conn = self.0.get_owned().await.unwrap(); // todo: handle error - DatabaseConnection(conn) + Arc::new(DatabaseConnection(conn)) } } pub struct DatabaseConnection(PooledConnection<'static, PostgresConnectionManager>); #[async_trait] -impl AccountStore for &DatabaseConnection { +impl AccountStore for DatabaseConnection { async fn get_account_profiles( &self, accounts: Vec, @@ -106,6 +112,7 @@ impl AccountStore for &DatabaseConnection { ¶ms[..], ) .await; + match rows { Err(rows) => Err(Box::new(rows)), Ok(rows) => { @@ -127,7 +134,7 @@ impl AccountStore for &DatabaseConnection { } #[async_trait] -impl RuleInstanceStore for &DatabaseConnection { +impl RuleInstanceStore for DatabaseConnection { async fn get_profile_state_rule_instances( &self, account_role: AccountRole, From 3b78579adbf3ebcd87dc55eeb0ac6868fcd19945 Mon Sep 17 00:00:00 2001 From: max funk Date: Sat, 14 Oct 2023 11:08:01 -0700 Subject: [PATCH 5/6] rust appy_rules unit test coverage --- crates/types/src/transaction.rs | 2 +- services/rule/src/main.rs | 155 ++++++++++++++++++++++++++++---- 2 files changed, 139 insertions(+), 18 deletions(-) diff --git a/crates/types/src/transaction.rs b/crates/types/src/transaction.rs index ddb61cd4..c6d1bdad 100644 --- a/crates/types/src/transaction.rs +++ b/crates/types/src/transaction.rs @@ -1,7 +1,7 @@ use crate::{account_role::AccountRole, time::TZTime, transaction_item::TransactionItems}; use serde::{Deserialize, Serialize}; -#[derive(Eq, PartialEq, Debug, Deserialize, Serialize)] +#[derive(Eq, PartialEq, Debug, Deserialize, Serialize, Clone)] pub struct Transaction { pub id: Option, pub rule_instance_id: Option, diff --git a/services/rule/src/main.rs b/services/rule/src/main.rs index aeb2964d..0d972098 100644 --- a/services/rule/src/main.rs +++ b/services/rule/src/main.rs @@ -4,16 +4,14 @@ use axum::{ routing::{get, post}, Router, }; -use pg::{DynConnPool, DB}; +use pg::{DynConnPool, DynDBConn, DB}; use rule::{create_response, expected_values, label_approved_transaction_items}; use std::{env, net::ToSocketAddrs, sync::Arc}; use tokio::signal; use types::approval::{Approval, Approvals}; use types::{ - account::AccountStore, account_role::{RoleSequence, CREDITOR_FIRST, DEBITOR_FIRST}, request_response::IntraTransaction, - rule::RuleInstanceStore, time::TZTime, transaction_item::TransactionItems, }; @@ -21,8 +19,8 @@ mod rules; const READINESS_CHECK_PATH: &str = "READINESS_CHECK_PATH"; -async fn apply_transaction_item_rules( - conn: C, +async fn apply_transaction_item_rules( + conn: &DynDBConn, role_sequence: RoleSequence, transaction_items: &TransactionItems, ) -> TransactionItems { @@ -107,8 +105,8 @@ async fn apply_transaction_item_rules( response } -async fn apply_approval_rules( - conn: C, +async fn apply_approval_rules( + conn: &DynDBConn, role_sequence: RoleSequence, transaction_items: &mut TransactionItems, approval_time: &TZTime, @@ -296,16 +294,16 @@ mod tests { use chrono::{DateTime, Utc}; use std::error::Error; use types::{ - account::{AccountProfile, AccountProfiles}, + account::{AccountProfile, AccountProfiles, AccountStore}, account_role::{AccountRole, DEBITOR_FIRST}, - rule::{RuleInstance, RuleInstances}, + rule::{RuleInstance, RuleInstanceStore, RuleInstances}, transaction_item::TransactionItem, }; - struct Stub(); + struct DBConnStub(); const TEST_TAX_APPROVERS: &[&str] = &["BenRoss", "DanLee", "MiriamLevy"]; #[async_trait] - impl AccountStore for &Stub { + impl AccountStore for DBConnStub { async fn get_account_profiles( &self, _accounts: Vec, @@ -403,7 +401,7 @@ mod tests { } #[async_trait] - impl RuleInstanceStore for &Stub { + impl RuleInstanceStore for DBConnStub { async fn get_profile_state_rule_instances( &self, account_role: AccountRole, @@ -525,7 +523,7 @@ mod tests { #[tokio::test] async fn it_applies_transaction_item_rules() { - let stub = Stub(); + let db_conn_stub = Arc::new(DBConnStub()) as DynDBConn; let tr_items = TransactionItems(vec![ TransactionItem { id: None, @@ -576,7 +574,7 @@ mod tests { ]); // test function - let got = apply_transaction_item_rules(&stub, DEBITOR_FIRST, &tr_items).await; + let got = apply_transaction_item_rules(&db_conn_stub, DEBITOR_FIRST, &tr_items).await; // assert #1: // save length of transaction items vec @@ -585,7 +583,7 @@ mod tests { let want_length = 4; assert_eq!( got_length, want_length, - "want {}, got {}", + "got {}, want {}", got_length, want_length ); @@ -613,7 +611,7 @@ mod tests { #[tokio::test] async fn it_applies_approval_rules() { let test_approval_time = TZTime::now(); - let stub = Stub(); + let db_conn_stub = Arc::new(DBConnStub()) as DynDBConn; let mut got_tr_items = TransactionItems(vec![ TransactionItem { id: None, @@ -710,7 +708,13 @@ mod tests { ]); // test function - apply_approval_rules(&stub, DEBITOR_FIRST, &mut got_tr_items, &test_approval_time).await; + apply_approval_rules( + &db_conn_stub, + DEBITOR_FIRST, + &mut got_tr_items, + &test_approval_time, + ) + .await; // assert #1 // save length of tax item approvals @@ -755,4 +759,121 @@ mod tests { got_approval_time, want_approval_time ); } + + #[tokio::test] + async fn it_applies_rules() { + use axum::extract::{Json, State}; + use pg::DBConnPoolTrait; + let test_tr_items = TransactionItems(vec![ + TransactionItem { + id: None, + transaction_id: None, + item_id: String::from("bread"), + price: String::from("3.000"), + quantity: String::from("2"), + debitor_first: Some(false), + rule_instance_id: None, + rule_exec_ids: Some(vec![]), + unit_of_measurement: None, + units_measured: None, + debitor: String::from("JacobWebb"), + creditor: String::from("GroceryStore"), + debitor_profile_id: None, + creditor_profile_id: None, + debitor_approval_time: None, + creditor_approval_time: None, + debitor_rejection_time: None, + creditor_rejection_time: None, + debitor_expiration_time: None, + creditor_expiration_time: None, + approvals: None, + }, + TransactionItem { + id: None, + transaction_id: None, + item_id: String::from("milk"), + price: String::from("4.000"), + quantity: String::from("3"), + debitor_first: Some(false), + rule_instance_id: None, + rule_exec_ids: Some(vec![]), + unit_of_measurement: None, + units_measured: None, + debitor: String::from("JacobWebb"), + creditor: String::from("GroceryStore"), + debitor_profile_id: None, + creditor_profile_id: None, + debitor_approval_time: None, + creditor_approval_time: None, + debitor_rejection_time: None, + creditor_rejection_time: None, + debitor_expiration_time: None, + creditor_expiration_time: None, + approvals: None, + }, + ]); + + struct DBPoolStub(); + + #[async_trait] + impl DBConnPoolTrait for DBPoolStub { + async fn get_conn(&self) -> DynDBConn { + return Arc::new(DBConnStub()); + } + } + + let db_pool_stub = Arc::new(DBPoolStub()); + + // test function + let got_response = apply_rules(State(db_pool_stub), Json(test_tr_items)) + .await + .unwrap(); + let got_transaction = got_response.0.transaction.clone(); + + // assert #1: + // save length of transaction items vec + let got_length = got_transaction.clone().transaction_items.0.len(); + let want_length = 4; + + // want length of transaction items vec to be 4 (started with 2) + assert_eq!( + got_length, want_length, + "got {}, want {}", + got_length, want_length + ); + + // assert #2 + let mut got_tax_tr_item_count = 0; + for tr_item in got_transaction.clone().transaction_items.0.iter() { + if tr_item.item_id == "9% state sales tax".to_string() { + got_tax_tr_item_count = got_tax_tr_item_count + 1 + } + } + let want_tax_tr_item_count = 2; + // want 2 rule added tax transaction items + assert_eq!( + got_tax_tr_item_count, want_tax_tr_item_count, + "got {}, want {}", + got_tax_tr_item_count, want_tax_tr_item_count + ); + + // assert #3 + let mut got_creditor_approval_count = 0; + for tr_item in got_transaction.clone().transaction_items.0.iter() { + if tr_item.creditor_approval_time.is_some() + && tr_item.item_id == "9% state sales tax".to_string() + { + got_creditor_approval_count = got_creditor_approval_count + 1 + } + } + let want_creditor_approval_count = 2; + // want 2 rule added creditor approvals + assert_eq!( + got_creditor_approval_count, want_creditor_approval_count, + "got {}, want {}", + got_creditor_approval_count, want_creditor_approval_count + ); + + // todo: more tests + } } From cef35b98e8acf733ebc9316394a86f6e841305e8 Mon Sep 17 00:00:00 2001 From: max funk Date: Sat, 14 Oct 2023 11:10:53 -0700 Subject: [PATCH 6/6] cargo lint --- crates/pg/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/pg/src/lib.rs b/crates/pg/src/lib.rs index bceb03e9..5dd404f5 100644 --- a/crates/pg/src/lib.rs +++ b/crates/pg/src/lib.rs @@ -65,9 +65,9 @@ mod tests { } } -pub type DynConnPool = Arc; +pub type DynConnPool = Arc; -pub type DynDBConn = Arc; +pub type DynDBConn = Arc; #[async_trait] pub trait DBConnPoolTrait { @@ -112,7 +112,7 @@ impl AccountStore for DatabaseConnection { ¶ms[..], ) .await; - + match rows { Err(rows) => Err(Box::new(rows)), Ok(rows) => {