Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
chore: add calculation rust test (#6547)
Nathan.fooo2378c0c441f5
diff --git a/frontend/rust-lib/flowy-database2/tests/database/calculations_test/calculation_test.rs b/frontend/rust-lib/flowy-database2/tests/database/calculations_test/calculation_test.rsindex 99b8d01594..cfaeb858a5 100644--- a/frontend/rust-lib/flowy-database2/tests/database/calculations_test/calculation_test.rs+++ b/frontend/rust-lib/flowy-database2/tests/database/calculations_test/calculation_test.rs@@ -3,6 +3,7 @@ use std::sync::Arc;use crate::database::calculations_test::script::DatabaseCalculationTest;use collab_database::fields::Field;use flowy_database2::entities::{CalculationType, FieldType, UpdateCalculationChangesetPB};+use lib_infra::box_any::BoxAny;#[tokio::test]async fn calculations_test() {@@ -35,7 +36,7 @@ async fn calculations_test() {}).await;- test.assert_calculation_value(expected_sum).await;+ test.assert_calculation_float_value(expected_sum).await;// Insert Min calculation and assert its valuetest@@ -47,7 +48,7 @@ async fn calculations_test() {}).await;- test.assert_calculation_value(expected_min).await;+ test.assert_calculation_float_value(expected_min).await;// Insert Average calculation and assert its valuetest@@ -59,7 +60,7 @@ async fn calculations_test() {}).await;- test.assert_calculation_value(expected_average).await;+ test.assert_calculation_float_value(expected_average).await;// Insert Max calculation and assert its valuetest@@ -71,7 +72,7 @@ async fn calculations_test() {}).await;- test.assert_calculation_value(expected_max).await;+ test.assert_calculation_float_value(expected_max).await;// Insert Median calculation and assert its valuetest@@ -83,5 +84,112 @@ async fn calculations_test() {}).await;- test.assert_calculation_value(expected_median).await;+ test.assert_calculation_float_value(expected_median).await;+}++#[tokio::test]+async fn calculations_empty_test() {+ let mut test = DatabaseCalculationTest::new().await;++ let view_id = &test.view_id();+ let text_fields = test+ .fields+ .clone()+ .into_iter()+ .filter(|field| field.field_type == FieldType::RichText as i64)+ .collect::<Vec<Arc<Field>>>();+ let field_id = &text_fields.first().unwrap().id.clone();+ let calculation_id = "calc_id".to_owned();++ test+ .insert_calculation(UpdateCalculationChangesetPB {+ view_id: view_id.clone(),+ field_id: field_id.clone(),+ calculation_id: Some(calculation_id.clone()),+ calculation_type: CalculationType::CountEmpty,+ })+ .await;+ test.assert_calculation_value("1").await;++ // Update the cell with a non-empty value+ test+ .update_cell(+ field_id,+ test.rows[1].id.clone(),+ BoxAny::new("change".to_string()),+ )+ .await+ .unwrap();++ // sleep for 3 seconds to wait for the calculation to update+ tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;+ test.assert_calculation_value("0").await;+}++#[tokio::test]+async fn calculations_non_empty_test() {+ let mut test = DatabaseCalculationTest::new().await;++ let view_id = &test.view_id();+ let text_fields = test+ .fields+ .clone()+ .into_iter()+ .filter(|field| field.field_type == FieldType::RichText as i64)+ .collect::<Vec<Arc<Field>>>();+ let field_id = &text_fields.first().unwrap().id.clone();+ let calculation_id = "calc_id".to_owned();++ test+ .insert_calculation(UpdateCalculationChangesetPB {+ view_id: view_id.clone(),+ field_id: field_id.clone(),+ calculation_id: Some(calculation_id.clone()),+ calculation_type: CalculationType::CountNonEmpty,+ })+ .await;+ test.assert_calculation_value("6").await;++ // Update the cell with a non-empty value+ test+ .update_cell(+ field_id,+ test.rows[1].id.clone(),+ BoxAny::new("change".to_string()),+ )+ .await+ .unwrap();++ // sleep for 3 seconds to wait for the calculation to update+ tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;+ test.assert_calculation_value("7").await;+}++#[tokio::test]+async fn calculations_count_test() {+ let mut test = DatabaseCalculationTest::new().await;++ let view_id = &test.view_id();+ let text_fields = test+ .fields+ .clone()+ .into_iter()+ .filter(|field| field.field_type == FieldType::RichText as i64)+ .collect::<Vec<Arc<Field>>>();+ let field_id = &text_fields.first().unwrap().id.clone();+ let calculation_id = "calc_id".to_owned();++ test+ .insert_calculation(UpdateCalculationChangesetPB {+ view_id: view_id.clone(),+ field_id: field_id.clone(),+ calculation_id: Some(calculation_id.clone()),+ calculation_type: CalculationType::Count,+ })+ .await;+ test.assert_calculation_value("7").await;+ test.duplicate_row(&test.rows[1].id).await;++ tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;+ test.assert_calculation_value("8").await;}diff --git a/frontend/rust-lib/flowy-database2/tests/database/calculations_test/script.rs b/frontend/rust-lib/flowy-database2/tests/database/calculations_test/script.rsindex a3d37d2cc5..cc15e25541 100644--- a/frontend/rust-lib/flowy-database2/tests/database/calculations_test/script.rs+++ b/frontend/rust-lib/flowy-database2/tests/database/calculations_test/script.rs@@ -1,3 +1,4 @@+use collab_database::rows::RowId;use tokio::sync::broadcast::Receiver;use flowy_database2::entities::UpdateCalculationChangesetPB;@@ -34,11 +35,25 @@ impl DatabaseCalculationTest {self.editor.update_calculation(payload).await.unwrap();}- pub async fn assert_calculation_value(&mut self, expected: f64) {+ pub async fn assert_calculation_float_value(&mut self, expected: f64) {let calculations = self.editor.get_all_calculations(&self.view_id()).await;let calculation = calculations.items.first().unwrap();assert_eq!(calculation.value, format!("{:.5}", expected));}++ pub async fn assert_calculation_value(&mut self, expected: &str) {+ let calculations = self.editor.get_all_calculations(&self.view_id()).await;+ let calculation = calculations.items.first().unwrap();+ assert_eq!(calculation.value, expected);+ }++ pub async fn duplicate_row(&self, row_id: &RowId) {+ self+ .editor+ .duplicate_row(&self.view_id, row_id)+ .await+ .unwrap();+ }}impl std::ops::Deref for DatabaseCalculationTest {diff --git a/frontend/rust-lib/flowy-database2/tests/database/database_editor.rs b/frontend/rust-lib/flowy-database2/tests/database/database_editor.rsindex 86035191f8..a194e5e8c2 100644--- a/frontend/rust-lib/flowy-database2/tests/database/database_editor.rs+++ b/frontend/rust-lib/flowy-database2/tests/database/database_editor.rs@@ -195,7 +195,7 @@ impl DatabaseEditorTest {}pub async fn update_cell(- &mut self,+ &self,field_id: &str,row_id: RowId,cell_changeset: BoxAny,