Mathias Mogensen 5cbc8b1e18
feat: calculations (#4473)
* feat: initial calculation controller

* fix: entities

* feat: calculations

* fix: review comments and support floats

* fix: abstract business logic into calculations service

* fix: clean calculation entities after merge

* feat: react to changes to row/cell/field_type

* chore: changes after merging main

* feat: handle delete field

* test: add grid calculations tests

* fix: add validation + format numbers

* refactor: get cell number

* chore: bump collab

* chore: fix clippy

* chore: update docs

* chore: update docs

* chore: fmt

* chore: fix flutter

* chore: collab rev

* fix: cleanup and hover to show

* fix: localization

* test: add basic rust test

* fix: clippy

* fix: support updating calculation on duplicate row

---------

Co-authored-by: nathan <nathan@appflowy.io>
2024-02-04 00:52:38 +08:00

75 lines
1.8 KiB
Rust

use tokio::sync::broadcast::Receiver;
use flowy_database2::entities::UpdateCalculationChangesetPB;
use flowy_database2::services::database_view::DatabaseViewChanged;
use crate::database::database_editor::DatabaseEditorTest;
pub enum CalculationScript {
InsertCalculation {
payload: UpdateCalculationChangesetPB,
},
AssertCalculationValue {
expected: f64,
},
}
pub struct DatabaseCalculationTest {
inner: DatabaseEditorTest,
recv: Option<Receiver<DatabaseViewChanged>>,
}
impl DatabaseCalculationTest {
pub async fn new() -> Self {
let editor_test = DatabaseEditorTest::new_grid().await;
Self {
inner: editor_test,
recv: None,
}
}
pub fn view_id(&self) -> String {
self.view_id.clone()
}
pub async fn run_scripts(&mut self, scripts: Vec<CalculationScript>) {
for script in scripts {
self.run_script(script).await;
}
}
pub async fn run_script(&mut self, script: CalculationScript) {
match script {
CalculationScript::InsertCalculation { payload } => {
self.recv = Some(
self
.editor
.subscribe_view_changed(&self.view_id())
.await
.unwrap(),
);
self.editor.update_calculation(payload).await.unwrap();
},
CalculationScript::AssertCalculationValue { expected } => {
let calculations = self.editor.get_all_calculations(&self.view_id()).await;
let calculation = calculations.items.first().unwrap();
assert_eq!(calculation.value, format!("{:.5}", expected));
},
}
}
}
impl std::ops::Deref for DatabaseCalculationTest {
type Target = DatabaseEditorTest;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl std::ops::DerefMut for DatabaseCalculationTest {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}