chore: add calculation rust test (#6547)

* chore: add test

* chore: clippy
This commit is contained in:
Nathan.fooo 2024-10-15 17:20:53 +08:00 committed by GitHub
parent 7bc53d7bc6
commit 2378c0c441
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 130 additions and 7 deletions

View File

@ -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 value
test
@ -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 value
test
@ -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 value
test
@ -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 value
test
@ -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;
}

View File

@ -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 {

View File

@ -195,7 +195,7 @@ impl DatabaseEditorTest {
}
pub async fn update_cell(
&mut self,
&self,
field_id: &str,
row_id: RowId,
cell_changeset: BoxAny,