summaryrefslogtreecommitdiff
path: root/schist_core
diff options
context:
space:
mode:
Diffstat (limited to 'schist_core')
-rw-r--r--schist_core/schist_fakes/src/drip.rs8
-rw-r--r--schist_core/schist_fakes/src/pipe.rs3
-rw-r--r--schist_core/schist_models/migrations/2024-08-31-084439_initial_setup/up.sql3
-rw-r--r--schist_core/schist_models/migrations/2025-08-15-090859_dummy_data/down.sql39
-rw-r--r--schist_core/schist_models/migrations/2025-08-15-090859_dummy_data/up.sql14
-rw-r--r--schist_core/schist_models/src/bucket.rs13
-rw-r--r--schist_core/schist_models/src/pipe.rs3
-rw-r--r--schist_core/schist_models/src/schema.rs3
-rw-r--r--schist_core/schist_queries/src/clear.rs13
-rw-r--r--schist_core/schist_queries/src/utils/mod.rs4
-rw-r--r--schist_core/schist_queries/tests/category_transfers.rs5
11 files changed, 36 insertions, 72 deletions
diff --git a/schist_core/schist_fakes/src/drip.rs b/schist_core/schist_fakes/src/drip.rs
index c2aa7a0..f7d0222 100644
--- a/schist_core/schist_fakes/src/drip.rs
+++ b/schist_core/schist_fakes/src/drip.rs
@@ -7,6 +7,12 @@ pub fn make_fake_drip(id: i32) -> Drip {
pub fn make_fake_drip_builder(id: i32) -> DripBuilder {
let mut builder = DripBuilder::default();
- builder.id(id).amount(0).bucket_id(0).date(DateUtc::now());
+ builder
+ .id(id)
+ .amount(0)
+ .bucket_id(0)
+ .date(DateUtc::now())
+ .inserted_at_unix_seconds(0)
+ .pipe_id(0);
builder
}
diff --git a/schist_core/schist_fakes/src/pipe.rs b/schist_core/schist_fakes/src/pipe.rs
index 765ad78..df77396 100644
--- a/schist_core/schist_fakes/src/pipe.rs
+++ b/schist_core/schist_fakes/src/pipe.rs
@@ -11,7 +11,8 @@ pub fn make_fake_pipe_builder(id: i32) -> PipeBuilder {
.id(id)
.amount(0)
.bucket_id(0)
- .period(BudgetPeriodUnit::Day)
+ .period_unit(BudgetPeriodUnit::Day)
+ .period_length(1)
.start_date(DateUtc::now());
builder
}
diff --git a/schist_core/schist_models/migrations/2024-08-31-084439_initial_setup/up.sql b/schist_core/schist_models/migrations/2024-08-31-084439_initial_setup/up.sql
index 275f280..b37c626 100644
--- a/schist_core/schist_models/migrations/2024-08-31-084439_initial_setup/up.sql
+++ b/schist_core/schist_models/migrations/2024-08-31-084439_initial_setup/up.sql
@@ -66,7 +66,8 @@ CREATE TABLE pipes(
id INTEGER NOT NULL PRIMARY KEY,
amount INTEGER NOT NULL,
bucket_id INTEGER NOT NULL,
- period TEXT NOT NULL,
+ period_length INTEGER NOT NULL,
+ period_unit TEXT NOT NULL,
start_date TEXT NOT NULL,
FOREIGN KEY (bucket_id)
REFERENCES buckets (id)
diff --git a/schist_core/schist_models/migrations/2025-08-15-090859_dummy_data/down.sql b/schist_core/schist_models/migrations/2025-08-15-090859_dummy_data/down.sql
deleted file mode 100644
index 8050572..0000000
--- a/schist_core/schist_models/migrations/2025-08-15-090859_dummy_data/down.sql
+++ /dev/null
@@ -1,39 +0,0 @@
-DELETE FROM accounts
- WHERE id=0
- AND name="Nationwide bank account"
- AND opening_balance=0
- AND opening_date="2024-01-01";
-
-DELETE FROM accounts
- WHERE id=1
- AND name="cash account"
- AND opening_balance=0
- AND opening_date="2024-01-01";
-
-DELETE FROM accounts
- WHERE id=2
- AND name="YBS savings account"
- AND opening_balance=0
- AND opening_date="2024-01-01";
-
-DELETE FROM buckets
- WHERE id=0
- AND balance IS NULL
- AND balance_cache_key IS NULL
- AND bucket_group="normal expenses"
- AND name="groceries";
-
-DELETE FROM buckets
- WHERE id=1
- AND balance IS NULL
- AND balance_cache_key IS NULL
- AND bucket_group="luxuries"
- AND name="bevvy";
-
-DELETE FROM buckets
- WHERE id=2
- AND balance IS NULL
- AND balance_cache_key IS NULL
- AND bucket_group="income"
- AND name="salary";
-
diff --git a/schist_core/schist_models/migrations/2025-08-15-090859_dummy_data/up.sql b/schist_core/schist_models/migrations/2025-08-15-090859_dummy_data/up.sql
deleted file mode 100644
index f5c97d0..0000000
--- a/schist_core/schist_models/migrations/2025-08-15-090859_dummy_data/up.sql
+++ /dev/null
@@ -1,14 +0,0 @@
-INSERT INTO accounts
- (id, name, opening_balance, opening_date)
- VALUES
- (0, "Nationwide bank account", 0, "2024-01-01"),
- (1, "cash account", 0, "2024-01-01"),
- (2, "YBS savings account", 0, "2024-01-01");
-
-INSERT INTO buckets
- (id, balance, balance_cache_key, bucket_group, name)
- VALUES
- (0, NULL, NULL, "normal expenses", "groceries"),
- (1, NULL, NULL, "luxuries", "bevvy"),
- (2, NULL, NULL, "income", "salary");
-
diff --git a/schist_core/schist_models/src/bucket.rs b/schist_core/schist_models/src/bucket.rs
index 1a78628..a28aa8f 100644
--- a/schist_core/schist_models/src/bucket.rs
+++ b/schist_core/schist_models/src/bucket.rs
@@ -2,6 +2,8 @@ use derive_builder::Builder;
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
+use crate::{Drip, Transaction};
+
#[derive(
Builder,
Clone,
@@ -19,7 +21,16 @@ pub struct Bucket {
pub id: i32,
pub balance: Option<i32>,
pub balance_cache_key: Option<String>,
- #[column_name = "bucket_group"]
+ #[diesel(column_name = "bucket_group")]
pub group: String,
pub name: String,
}
+
+impl Bucket {
+ pub fn is_empty(&self, drips: &[Drip], transactions: &[Transaction]) -> bool {
+ drips.iter().all(|drip| drip.bucket_id != self.id)
+ && transactions
+ .iter()
+ .all(|t| t.bucket_id.is_none_or(|id| id != self.id))
+ }
+}
diff --git a/schist_core/schist_models/src/pipe.rs b/schist_core/schist_models/src/pipe.rs
index fb66a20..9354c20 100644
--- a/schist_core/schist_models/src/pipe.rs
+++ b/schist_core/schist_models/src/pipe.rs
@@ -22,6 +22,7 @@ pub struct Pipe {
pub id: i32,
pub amount: i32,
pub bucket_id: i32,
- pub period: BudgetPeriodUnit,
+ pub period_length: i32,
+ pub period_unit: BudgetPeriodUnit,
pub start_date: DateUtc,
}
diff --git a/schist_core/schist_models/src/schema.rs b/schist_core/schist_models/src/schema.rs
index 3799479..d16e636 100644
--- a/schist_core/schist_models/src/schema.rs
+++ b/schist_core/schist_models/src/schema.rs
@@ -56,7 +56,8 @@ diesel::table! {
id -> Integer,
amount -> Integer,
bucket_id -> Integer,
- period -> Text,
+ period_length -> Integer,
+ period_unit -> Text,
start_date -> Text,
}
}
diff --git a/schist_core/schist_queries/src/clear.rs b/schist_core/schist_queries/src/clear.rs
index 04d22d9..a5a82f3 100644
--- a/schist_core/schist_queries/src/clear.rs
+++ b/schist_core/schist_queries/src/clear.rs
@@ -2,17 +2,16 @@ use anyhow::Result;
use diesel::SqliteConnection;
use crate::{
- accounts::delete_all_accounts, bucket_transfers::delete_all_bucket_transfers,
- buckets::delete_all_buckets, drips::delete_all_drips, pipes::delete_all_pipes,
- transactions::delete_all_transactions,
+ account_transfers::delete_all_account_transfers, accounts::delete_all_accounts, bucket_transfers::delete_all_bucket_transfers, buckets::delete_all_buckets, drips::delete_all_drips, pipes::delete_all_pipes, transactions::delete_all_transactions
};
pub fn clear(connection: &mut SqliteConnection) -> Result<()> {
- delete_all_accounts(connection)?;
+ delete_all_account_transfers(connection)?;
delete_all_drips(connection)?;
- delete_all_buckets(connection)?;
- delete_all_bucket_transfers(connection)?;
- delete_all_transactions(connection)?;
delete_all_pipes(connection)?;
+ delete_all_transactions(connection)?;
+ delete_all_accounts(connection)?;
+ delete_all_bucket_transfers(connection)?;
+ delete_all_buckets(connection)?;
Ok(())
}
diff --git a/schist_core/schist_queries/src/utils/mod.rs b/schist_core/schist_queries/src/utils/mod.rs
index 6b47b48..3a81c8c 100644
--- a/schist_core/schist_queries/src/utils/mod.rs
+++ b/schist_core/schist_queries/src/utils/mod.rs
@@ -2,7 +2,5 @@ mod calculate_bucket_balance;
mod find_by_id_or;
mod sum_drips_for_bucket;
+use find_by_id_or::find_by_id_or;
use sum_drips_for_bucket::sum_drips_for_bucket;
-
-pub use calculate_bucket_balance::calculate_bucket_balance;
-pub use find_by_id_or::find_by_id_or;
diff --git a/schist_core/schist_queries/tests/category_transfers.rs b/schist_core/schist_queries/tests/category_transfers.rs
index 13b9866..f7c75a7 100644
--- a/schist_core/schist_queries/tests/category_transfers.rs
+++ b/schist_core/schist_queries/tests/category_transfers.rs
@@ -184,6 +184,7 @@ fn given_bucket_transfers_when_sum_quantity_per_to_bucket_id_then_returns_sum_pe
make_fake_bucket_transfer_builder(2)
.from_bucket_id(1)
.to_bucket_id(0)
+ .amount(4)
.build()
.unwrap(),
];
@@ -196,7 +197,5 @@ fn given_bucket_transfers_when_sum_quantity_per_to_bucket_id_then_returns_sum_pe
assert!(sums.is_ok());
let mut sums = sums.unwrap();
sums.sort_by_key(|sum| sum.0);
- assert_eq!(buckets.len(), sums.len());
- assert_eq!(4, sums[0].1);
- assert_eq!(3, sums[1].1);
+ assert_eq!([(0, 4), (1, 3)], sums.as_slice());
}