test: add database event tests (#2744)

* chore: add tests

* test: add tests

* test: add tests
This commit is contained in:
Nathan.fooo
2023-06-09 18:57:29 +08:00
committed by GitHub
parent 0f9f5251f2
commit e9be37a961
26 changed files with 834 additions and 104 deletions

View File

@ -80,17 +80,11 @@ impl std::convert::From<CalendarLayout> for CalendarLayoutPB {
pub struct CalendarEventRequestPB {
#[pb(index = 1)]
pub view_id: String,
// Currently, requesting the events within the specified month
// is not supported
#[pb(index = 2)]
pub month: String,
}
#[derive(Debug, Clone, Default)]
pub struct CalendarEventRequestParams {
pub view_id: String,
pub month: String,
}
impl TryInto<CalendarEventRequestParams> for CalendarEventRequestPB {
@ -98,10 +92,7 @@ impl TryInto<CalendarEventRequestParams> for CalendarEventRequestPB {
fn try_into(self) -> Result<CalendarEventRequestParams, Self::Error> {
let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::ViewIdIsInvalid)?;
Ok(CalendarEventRequestParams {
view_id: view_id.0,
month: self.month,
})
Ok(CalendarEventRequestParams { view_id: view_id.0 })
}
}

View File

@ -135,11 +135,13 @@ impl TryInto<MoveRowParams> for MoveRowPayloadPB {
fn try_into(self) -> Result<MoveRowParams, Self::Error> {
let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::DatabaseViewIdIsEmpty)?;
let from_row_id = NotEmptyStr::parse(self.from_row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
let to_row_id = NotEmptyStr::parse(self.to_row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
Ok(MoveRowParams {
view_id: view_id.0,
from_row_id: RowId::from(self.from_row_id),
to_row_id: RowId::from(self.to_row_id),
from_row_id: RowId::from(from_row_id.0),
to_row_id: RowId::from(to_row_id.0),
})
}
}

View File

@ -76,7 +76,7 @@ pub struct GroupPB {
pub group_id: String,
#[pb(index = 3)]
pub desc: String,
pub group_name: String,
#[pb(index = 4)]
pub rows: Vec<RowPB>,
@ -93,7 +93,7 @@ impl std::convert::From<GroupData> for GroupPB {
Self {
field_id: group_data.field_id,
group_id: group_data.id,
desc: group_data.name,
group_name: group_data.name,
rows: group_data.rows.into_iter().map(RowPB::from).collect(),
is_default: group_data.is_default,
is_visible: group_data.is_visible,
@ -108,9 +108,6 @@ pub struct GroupByFieldPayloadPB {
#[pb(index = 2)]
pub view_id: String,
#[pb(index = 3)]
pub field_type: FieldType,
}
impl TryInto<GroupByFieldParams> for GroupByFieldPayloadPB {
@ -124,18 +121,13 @@ impl TryInto<GroupByFieldParams> for GroupByFieldPayloadPB {
.map_err(|_| ErrorCode::ViewIdIsInvalid)?
.0;
Ok(GroupByFieldParams {
field_id,
view_id,
field_type: self.field_type,
})
Ok(GroupByFieldParams { field_id, view_id })
}
}
pub struct GroupByFieldParams {
pub field_id: String,
pub view_id: String,
pub field_type: FieldType,
}
pub struct DeleteGroupParams {

View File

@ -158,6 +158,7 @@ impl TryInto<RowIdParams> for RowIdPB {
fn try_into(self) -> Result<RowIdParams, Self::Error> {
let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::DatabaseIdIsEmpty)?;
let row_id = NotEmptyStr::parse(self.row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
let group_id = match self.group_id {
Some(group_id) => Some(
NotEmptyStr::parse(group_id)
@ -169,7 +170,7 @@ impl TryInto<RowIdParams> for RowIdPB {
Ok(RowIdParams {
view_id: view_id.0,
row_id: RowId::from(self.row_id),
row_id: RowId::from(row_id.0),
group_id,
})
}

View File

@ -1,11 +1,12 @@
use collab_database::rows::RowId;
use flowy_derive::ProtoBuf;
use flowy_error::{ErrorCode, FlowyError};
use crate::entities::parser::NotEmptyStr;
use crate::entities::SelectOptionPB;
use crate::services::field::checklist_type_option::ChecklistCellData;
use crate::services::field::SelectOption;
use collab_database::rows::RowId;
use flowy_derive::ProtoBuf;
use flowy_error::{ErrorCode, FlowyError};
#[derive(Debug, Clone, Default, ProtoBuf)]
pub struct ChecklistCellDataPB {
@ -16,7 +17,7 @@ pub struct ChecklistCellDataPB {
pub selected_options: Vec<SelectOptionPB>,
#[pb(index = 3)]
pub(crate) percentage: f64,
pub percentage: f64,
}
impl From<ChecklistCellData> for ChecklistCellDataPB {

View File

@ -25,7 +25,7 @@ pub struct DateCellDataPB {
#[derive(Clone, Debug, Default, ProtoBuf)]
pub struct DateChangesetPB {
#[pb(index = 1)]
pub cell_path: CellIdPB,
pub cell_id: CellIdPB,
#[pb(index = 2, one_of)]
pub date: Option<String>,

View File

@ -294,7 +294,9 @@ pub(crate) async fn get_row_handler(
) -> DataResult<OptionalRowPB, FlowyError> {
let params: RowIdParams = data.into_inner().try_into()?;
let database_editor = manager.get_database_with_view_id(&params.view_id).await?;
let row = database_editor.get_row(&params.row_id).map(RowPB::from);
let row = database_editor
.get_row(&params.view_id, &params.row_id)
.map(RowPB::from);
data_result_ok(OptionalRowPB { row })
}
@ -525,7 +527,7 @@ pub(crate) async fn update_date_cell_handler(
manager: AFPluginState<Arc<DatabaseManager2>>,
) -> Result<(), FlowyError> {
let data = data.into_inner();
let cell_id: CellIdParams = data.cell_path.try_into()?;
let cell_id: CellIdParams = data.cell_id.try_into()?;
let cell_changeset = DateCellChangeset {
date: data.date,
time: data.time,

View File

@ -496,8 +496,12 @@ impl DatabaseEditor {
Ok(view_editor.v_get_rows().await)
}
pub fn get_row(&self, row_id: &RowId) -> Option<Row> {
self.database.lock().get_row(row_id)
pub fn get_row(&self, view_id: &str, row_id: &RowId) -> Option<Row> {
if self.database.lock().views.is_row_exist(view_id, row_id) {
return None;
} else {
self.database.lock().get_row(row_id)
}
}
pub async fn delete_row(&self, row_id: &RowId) {
@ -832,6 +836,11 @@ impl DatabaseEditor {
from_group: &str,
to_group: &str,
) -> FlowyResult<()> {
// Do nothing if the group is the same
if from_group == to_group {
return Ok(());
}
let view = self.database_views.get_view_editor(view_id).await?;
view.v_move_group(from_group, to_group).await?;
Ok(())

View File

@ -375,6 +375,7 @@ impl DatabaseViewEditor {
.as_ref()?
.groups()
.into_iter()
.filter(|group| group.is_visible)
.map(|group_data| GroupPB::from(group_data.clone()))
.collect::<Vec<_>>();
tracing::trace!("Number of groups: {}", groups.len());

View File

@ -351,15 +351,21 @@ where
}
pub(crate) fn update_group(&mut self, group_changeset: GroupChangeset) -> FlowyResult<()> {
self.mut_group(&group_changeset.group_id, |group| {
let update_group = self.mut_group(&group_changeset.group_id, |group| {
if let Some(visible) = group_changeset.visible {
group.visible = visible;
}
if let Some(name) = &group_changeset.name {
group.name = name.clone();
}
})?;
if let Some(group) = update_group {
self.group_by_id.get_mut(&group.id).map(|group_data| {
group_data.name = group.name.clone();
group_data.is_visible = group.visible;
});
}
Ok(())
}
@ -370,6 +376,11 @@ where
.await
}
/// # Arguments
///
/// * `mut_configuration_fn`: mutate the [GroupSetting] and return whether the [GroupSetting] is
/// changed. If the [GroupSetting] is changed, the [GroupSetting] will be saved to the storage.
///
fn mut_configuration(
&mut self,
mut_configuration_fn: impl FnOnce(&mut GroupSetting) -> bool,
@ -392,7 +403,12 @@ where
Ok(())
}
fn mut_group(&mut self, group_id: &str, mut_groups_fn: impl Fn(&mut Group)) -> FlowyResult<()> {
fn mut_group(
&mut self,
group_id: &str,
mut_groups_fn: impl Fn(&mut Group),
) -> FlowyResult<Option<Group>> {
let mut updated_group = None;
self.mut_configuration(|configuration| {
match configuration
.groups
@ -402,10 +418,12 @@ where
None => false,
Some(group) => {
mut_groups_fn(group);
updated_group = Some(group.clone());
true
},
}
})
})?;
Ok(updated_group)
}
}

View File

@ -1,6 +1,7 @@
use collab_database::database::gen_row_id;
use collab_database::fields::Field;
use collab_database::rows::{CreateRowParams, RowId};
use flowy_database2::entities::{FieldType, GroupPB, RowPB};
use flowy_database2::services::cell::{
delete_select_option_cell, insert_select_option_cell, insert_url_cell,
@ -233,7 +234,7 @@ impl DatabaseGroupTest {
} => {
let group = self.group_at_index(group_index).await;
assert_eq!(group.group_id, group_pb.group_id);
assert_eq!(group.desc, group_pb.desc);
assert_eq!(group.group_name, group_pb.group_name);
},
GroupScript::UpdateSingleSelectSelectOption { inserted_options } => {
self

View File

@ -463,7 +463,7 @@ async fn group_insert_single_select_option_test() {
];
test.run_scripts(scripts).await;
let new_group = test.group_at_index(1).await;
assert_eq!(new_group.desc, new_option_name);
assert_eq!(new_group.group_name, new_option_name);
}
#[tokio::test]