mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
test: add databaase event test (#2728)
* test: add tests and fix modify primary field bug * test: add more test * fix: tauri buiuld * chore: disable share link button
This commit is contained in:
@ -155,6 +155,8 @@ pub struct CreateFieldPayloadPB {
|
||||
#[pb(index = 2)]
|
||||
pub field_type: FieldType,
|
||||
|
||||
/// If the type_option_data is not empty, it will be used to create the field.
|
||||
/// Otherwise, the default value will be used.
|
||||
#[pb(index = 3, one_of)]
|
||||
pub type_option_data: Option<Vec<u8>>,
|
||||
}
|
||||
@ -163,6 +165,8 @@ pub struct CreateFieldPayloadPB {
|
||||
pub struct CreateFieldParams {
|
||||
pub view_id: String,
|
||||
pub field_type: FieldType,
|
||||
/// If the type_option_data is not empty, it will be used to create the field.
|
||||
/// Otherwise, the default value will be used.
|
||||
pub type_option_data: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
@ -189,9 +193,6 @@ pub struct UpdateFieldTypePayloadPB {
|
||||
|
||||
#[pb(index = 3)]
|
||||
pub field_type: FieldType,
|
||||
|
||||
#[pb(index = 4)]
|
||||
pub create_if_not_exist: bool,
|
||||
}
|
||||
|
||||
pub struct EditFieldParams {
|
||||
@ -401,18 +402,13 @@ pub struct FieldChangesetPB {
|
||||
pub desc: Option<String>,
|
||||
|
||||
#[pb(index = 5, one_of)]
|
||||
pub field_type: Option<FieldType>,
|
||||
|
||||
#[pb(index = 6, one_of)]
|
||||
pub frozen: Option<bool>,
|
||||
|
||||
#[pb(index = 7, one_of)]
|
||||
#[pb(index = 6, one_of)]
|
||||
pub visibility: Option<bool>,
|
||||
|
||||
#[pb(index = 8, one_of)]
|
||||
#[pb(index = 7, one_of)]
|
||||
pub width: Option<i32>,
|
||||
// #[pb(index = 9, one_of)]
|
||||
// pub type_option_data: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl TryInto<FieldChangesetParams> for FieldChangesetPB {
|
||||
@ -421,7 +417,6 @@ impl TryInto<FieldChangesetParams> for FieldChangesetPB {
|
||||
fn try_into(self) -> Result<FieldChangesetParams, Self::Error> {
|
||||
let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::DatabaseIdIsEmpty)?;
|
||||
let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
|
||||
let field_type = self.field_type.map(FieldType::from);
|
||||
// if let Some(type_option_data) = self.type_option_data.as_ref() {
|
||||
// if type_option_data.is_empty() {
|
||||
// return Err(ErrorCode::TypeOptionDataIsEmpty);
|
||||
@ -433,7 +428,6 @@ impl TryInto<FieldChangesetParams> for FieldChangesetPB {
|
||||
view_id: view_id.0,
|
||||
name: self.name,
|
||||
desc: self.desc,
|
||||
field_type,
|
||||
frozen: self.frozen,
|
||||
visibility: self.visibility,
|
||||
width: self.width,
|
||||
@ -452,8 +446,6 @@ pub struct FieldChangesetParams {
|
||||
|
||||
pub desc: Option<String>,
|
||||
|
||||
pub field_type: Option<FieldType>,
|
||||
|
||||
pub frozen: Option<bool>,
|
||||
|
||||
pub visibility: Option<bool>,
|
||||
|
@ -249,7 +249,7 @@ pub(crate) async fn get_field_type_option_data_handler(
|
||||
}
|
||||
}
|
||||
|
||||
/// Create FieldMeta and save it. Return the FieldTypeOptionData.
|
||||
/// Create TypeOptionPB and save it. Return the FieldTypeOptionData.
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn create_field_type_option_data_handler(
|
||||
data: AFPluginData<CreateFieldPayloadPB>,
|
||||
|
@ -10,7 +10,7 @@ use collab_database::views::{DatabaseLayout, DatabaseView, LayoutSetting};
|
||||
use parking_lot::Mutex;
|
||||
use tokio::sync::{broadcast, RwLock};
|
||||
|
||||
use flowy_error::{internal_error, FlowyError, FlowyResult};
|
||||
use flowy_error::{internal_error, ErrorCode, FlowyError, FlowyResult};
|
||||
use flowy_task::TaskDispatcher;
|
||||
use lib_infra::future::{to_fut, Fut};
|
||||
|
||||
@ -199,32 +199,32 @@ impl DatabaseEditor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a list of fields of the view.
|
||||
/// If `field_ids` is not provided, all the fields will be returned in the order of the field that
|
||||
/// defined in the view. Otherwise, the fields will be returned in the order of the `field_ids`.
|
||||
pub fn get_fields(&self, view_id: &str, field_ids: Option<Vec<String>>) -> Vec<Field> {
|
||||
self.database.lock().get_fields(view_id, field_ids)
|
||||
let database = self.database.lock();
|
||||
let field_ids = field_ids.unwrap_or_else(|| {
|
||||
database
|
||||
.fields
|
||||
.get_all_field_orders()
|
||||
.into_iter()
|
||||
.map(|field| field.id)
|
||||
.collect()
|
||||
});
|
||||
database.get_fields(view_id, Some(field_ids))
|
||||
}
|
||||
|
||||
pub async fn update_field(&self, params: FieldChangesetParams) -> FlowyResult<()> {
|
||||
let is_primary = self
|
||||
.database
|
||||
.lock()
|
||||
.fields
|
||||
.get_field(¶ms.field_id)
|
||||
.map(|field| field.is_primary)
|
||||
.unwrap_or(false);
|
||||
self
|
||||
.database
|
||||
.lock()
|
||||
.fields
|
||||
.update_field(¶ms.field_id, |mut update| {
|
||||
update = update
|
||||
.update_field(¶ms.field_id, |update| {
|
||||
update
|
||||
.set_name_if_not_none(params.name)
|
||||
.set_width_at_if_not_none(params.width.map(|value| value as i64))
|
||||
.set_visibility_if_not_none(params.visibility);
|
||||
if is_primary {
|
||||
tracing::warn!("Cannot update primary field type");
|
||||
} else {
|
||||
update.set_field_type_if_not_none(params.field_type.map(|field_type| field_type.into()));
|
||||
}
|
||||
});
|
||||
self
|
||||
.notify_did_update_database_field(¶ms.field_id)
|
||||
@ -233,6 +233,21 @@ impl DatabaseEditor {
|
||||
}
|
||||
|
||||
pub async fn delete_field(&self, field_id: &str) -> FlowyResult<()> {
|
||||
let is_primary = self
|
||||
.database
|
||||
.lock()
|
||||
.fields
|
||||
.get_field(field_id)
|
||||
.map(|field| field.is_primary)
|
||||
.unwrap_or(false);
|
||||
|
||||
if is_primary {
|
||||
return Err(FlowyError::new(
|
||||
ErrorCode::Internal,
|
||||
"Can not delete primary field",
|
||||
));
|
||||
}
|
||||
|
||||
let database_id = {
|
||||
let database = self.database.lock();
|
||||
database.delete_field(field_id);
|
||||
@ -283,6 +298,13 @@ impl DatabaseEditor {
|
||||
match field {
|
||||
None => {},
|
||||
Some(field) => {
|
||||
if field.is_primary {
|
||||
return Err(FlowyError::new(
|
||||
ErrorCode::Internal,
|
||||
"Can not update primary field's field type",
|
||||
));
|
||||
}
|
||||
|
||||
let old_field_type = FieldType::from(field.field_type);
|
||||
let old_type_option = field.get_any_type_option(old_field_type.clone());
|
||||
let new_type_option = field
|
||||
@ -312,6 +334,21 @@ impl DatabaseEditor {
|
||||
}
|
||||
|
||||
pub async fn duplicate_field(&self, view_id: &str, field_id: &str) -> FlowyResult<()> {
|
||||
let is_primary = self
|
||||
.database
|
||||
.lock()
|
||||
.fields
|
||||
.get_field(field_id)
|
||||
.map(|field| field.is_primary)
|
||||
.unwrap_or(false);
|
||||
|
||||
if is_primary {
|
||||
return Err(FlowyError::new(
|
||||
ErrorCode::Internal,
|
||||
"Can not duplicate primary field",
|
||||
));
|
||||
}
|
||||
|
||||
let value = self
|
||||
.database
|
||||
.lock()
|
||||
|
@ -90,7 +90,9 @@ impl CellDataDecoder for CheckboxTypeOption {
|
||||
return Ok(Default::default());
|
||||
}
|
||||
|
||||
self.parse_cell(cell)
|
||||
let cell = self.parse_cell(cell);
|
||||
println!("cell: {:?}", cell);
|
||||
return cell;
|
||||
}
|
||||
|
||||
fn stringify_cell_data(&self, cell_data: <Self as TypeOption>::CellData) -> String {
|
||||
|
@ -233,30 +233,6 @@ async fn grid_switch_from_checkbox_to_text_test() {
|
||||
test.run_scripts(scripts).await;
|
||||
}
|
||||
|
||||
// Test when switching the current field from Checkbox to Text test
|
||||
// input:
|
||||
// "Yes" -> check
|
||||
// "" -> unchecked
|
||||
#[tokio::test]
|
||||
async fn grid_switch_from_text_to_checkbox_test() {
|
||||
let mut test = DatabaseFieldTest::new().await;
|
||||
let field = test.get_first_field(FieldType::RichText).clone();
|
||||
|
||||
let scripts = vec![
|
||||
SwitchToField {
|
||||
field_id: field.id.clone(),
|
||||
new_field_type: FieldType::Checkbox,
|
||||
},
|
||||
AssertCellContent {
|
||||
field_id: field.id.clone(),
|
||||
row_index: 0,
|
||||
from_field_type: FieldType::RichText,
|
||||
expected_content: "".to_string(),
|
||||
},
|
||||
];
|
||||
test.run_scripts(scripts).await;
|
||||
}
|
||||
|
||||
// Test when switching the current field from Date to Text test
|
||||
// input:
|
||||
// 1647251762 -> Mar 14,2022 (This string will be different base on current data setting)
|
||||
|
Reference in New Issue
Block a user