Skip to content

Commit

Permalink
refactor: database test (AppFlowy-IO#6544)
Browse files Browse the repository at this point in the history
* chore: remove script

* chore: remove script

* chore: refactor test

* chore: clippy

* chore: fix test

* chore: fix test

* chore: fmt
  • Loading branch information
appflowy authored Oct 15, 2024
1 parent 5d578ab commit 46a3006
Show file tree
Hide file tree
Showing 36 changed files with 3,090 additions and 4,046 deletions.
12 changes: 6 additions & 6 deletions frontend/appflowy_flutter/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1535,10 +1535,10 @@ packages:
dependency: transitive
description:
name: platform
sha256: "9b71283fc13df574056616011fb138fd3b793ea47cc509c189a6c3fa5f8a1a65"
sha256: "12220bb4b65720483f8fa9450b4332347737cf8213dd2840d8b2c823e47243ec"
url: "https://pub.dev"
source: hosted
version: "3.1.5"
version: "3.1.4"
plugin_platform_interface:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -1933,10 +1933,10 @@ packages:
dependency: transitive
description:
name: string_scanner
sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3"
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
url: "https://pub.dev"
source: hosted
version: "1.3.0"
version: "1.2.0"
string_validator:
dependency: "direct main"
description:
Expand Down Expand Up @@ -2238,10 +2238,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
url: "https://pub.dev"
source: hosted
version: "14.2.5"
version: "14.2.1"
watcher:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod calculate_test;
mod event_test;
mod group_test;
mod test;
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ use lib_infra::util::timestamp;
use std::time::Duration;

use crate::database::block_test::script::DatabaseRowTest;
use crate::database::block_test::script::RowScript::*;

// Create a new row at the end of the grid and check the create time is valid.
#[tokio::test]
async fn created_at_field_test() {
let mut test = DatabaseRowTest::new().await;

// Get initial row count
let row_count = test.rows.len();
test
.run_scripts(vec![CreateEmptyRow, AssertRowCount(row_count + 1)])
.await;

// Create a new row and assert the row count has increased by 1
test.create_empty_row().await;
test.assert_row_count(row_count + 1).await;

// Get created time of the new row.
let row = test.get_rows().await.last().cloned().unwrap();
let updated_at_field = test.get_first_field(FieldType::CreatedTime).await;
let created_at_field = test.get_first_field(FieldType::CreatedTime).await;
let cell = test
.editor
.get_cell(&updated_at_field.id, &row.id)
.get_cell(&created_at_field.id, &row.id)
.await
.unwrap();
let created_at_timestamp = DateCellData::from(&cell).timestamp.unwrap();
Expand All @@ -29,10 +30,11 @@ async fn created_at_field_test() {
assert!(created_at_timestamp <= timestamp());
}

// Update row and check the update time is valid.
#[tokio::test]
async fn update_at_field_test() {
let mut test = DatabaseRowTest::new().await;

// Get the first row and the current LastEditedTime field
let row = test.get_rows().await.remove(0);
let last_edit_field = test.get_first_field(FieldType::LastEditedTime).await;
let cell = test
Expand All @@ -42,15 +44,13 @@ async fn update_at_field_test() {
.unwrap();
let old_updated_at = DateCellData::from(&cell).timestamp.unwrap();

// Wait for 1 second before updating the row
tokio::time::sleep(Duration::from_millis(1000)).await;
test
.run_script(UpdateTextCell {
row_id: row.id.clone(),
content: "test".to_string(),
})
.await;

// Get the updated time of the row.

// Update the text cell of the first row
test.update_text_cell(row.id.clone(), "test").await;

// Get the updated time of the row
let row = test.get_rows().await.remove(0);
let last_edit_field = test.get_first_field(FieldType::LastEditedTime).await;
let cell = test
Expand All @@ -59,5 +59,6 @@ async fn update_at_field_test() {
.await
.unwrap();
let new_updated_at = DateCellData::from(&cell).timestamp.unwrap();

assert!(old_updated_at < new_updated_at);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
use crate::database::database_editor::DatabaseEditorTest;
use collab_database::rows::RowId;

use flowy_database2::entities::CreateRowPayloadPB;

use crate::database::database_editor::DatabaseEditorTest;

pub enum RowScript {
CreateEmptyRow,
UpdateTextCell { row_id: RowId, content: String },
AssertRowCount(usize),
}

pub struct DatabaseRowTest {
inner: DatabaseEditorTest,
}
Expand All @@ -20,32 +12,24 @@ impl DatabaseRowTest {
Self { inner: editor_test }
}

pub async fn run_scripts(&mut self, scripts: Vec<RowScript>) {
for script in scripts {
self.run_script(script).await;
}
pub async fn create_empty_row(&mut self) {
let params = CreateRowPayloadPB {
view_id: self.view_id.clone(),
..Default::default()
};
let row_detail = self.editor.create_row(params).await.unwrap().unwrap();
self
.row_by_row_id
.insert(row_detail.row.id.to_string(), row_detail.into());
self.rows = self.get_rows().await;
}

pub async fn update_text_cell(&mut self, row_id: RowId, content: &str) {
self.inner.update_text_cell(row_id, content).await.unwrap();
}

pub async fn run_script(&mut self, script: RowScript) {
match script {
RowScript::CreateEmptyRow => {
let params = CreateRowPayloadPB {
view_id: self.view_id.clone(),
..Default::default()
};
let row_detail = self.editor.create_row(params).await.unwrap().unwrap();
self
.row_by_row_id
.insert(row_detail.row.id.to_string(), row_detail.into());
self.rows = self.get_rows().await;
},
RowScript::UpdateTextCell { row_id, content } => {
self.update_text_cell(row_id, &content).await.unwrap();
},
RowScript::AssertRowCount(expected_row_count) => {
assert_eq!(expected_row_count, self.rows.len());
},
}
pub async fn assert_row_count(&self, expected_row_count: usize) {
assert_eq!(expected_row_count, self.rows.len());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::sync::Arc;

use crate::database::calculations_test::script::{CalculationScript::*, DatabaseCalculationTest};

use crate::database::calculations_test::script::DatabaseCalculationTest;
use collab_database::fields::Field;
use flowy_database2::entities::{CalculationType, FieldType, UpdateCalculationChangesetPB};

Expand All @@ -15,7 +14,7 @@ async fn calculations_test() {
let expected_max = 14.00000;
let expected_median = 3.00000;

let view_id = &test.view_id;
let view_id = &test.view_id();
let number_fields = test
.fields
.clone()
Expand All @@ -25,63 +24,64 @@ async fn calculations_test() {
let field_id = &number_fields.first().unwrap().id;

let calculation_id = "calc_id".to_owned();
let scripts = vec![
// Insert Sum calculation first time
InsertCalculation {
payload: UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Sum,
},
},
AssertCalculationValue {
expected: expected_sum,
},
InsertCalculation {
payload: UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Min,
},
},
AssertCalculationValue {
expected: expected_min,
},
InsertCalculation {
payload: UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Average,
},
},
AssertCalculationValue {
expected: expected_average,
},
InsertCalculation {
payload: UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Max,
},
},
AssertCalculationValue {
expected: expected_max,
},
InsertCalculation {
payload: UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id),
calculation_type: CalculationType::Median,
},
},
AssertCalculationValue {
expected: expected_median,
},
];
test.run_scripts(scripts).await;

// Insert Sum calculation and assert its value
test
.insert_calculation(UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Sum,
})
.await;

test.assert_calculation_value(expected_sum).await;

// Insert Min calculation and assert its value
test
.insert_calculation(UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Min,
})
.await;

test.assert_calculation_value(expected_min).await;

// Insert Average calculation and assert its value
test
.insert_calculation(UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Average,
})
.await;

test.assert_calculation_value(expected_average).await;

// Insert Max calculation and assert its value
test
.insert_calculation(UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Max,
})
.await;

test.assert_calculation_value(expected_max).await;

// Insert Median calculation and assert its value
test
.insert_calculation(UpdateCalculationChangesetPB {
view_id: view_id.to_owned(),
field_id: field_id.to_owned(),
calculation_id: Some(calculation_id.clone()),
calculation_type: CalculationType::Median,
})
.await;

test.assert_calculation_value(expected_median).await;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ 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>>,
Expand All @@ -32,30 +23,21 @@ impl DatabaseCalculationTest {
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 insert_calculation(&mut self, payload: UpdateCalculationChangesetPB) {
self.recv = Some(
self
.editor
.subscribe_view_changed(&self.view_id())
.await
.unwrap(),
);
self.editor.update_calculation(payload).await.unwrap();
}

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));
},
}
pub async fn assert_calculation_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));
}
}

Expand Down
Loading

0 comments on commit 46a3006

Please sign in to comment.