Skip to content

Commit b83b182

Browse files
0xkelvinappflowy
andauthored
test: grid switch from number to text test (#1615)
* test: grid switch from number to text test * chore: update test * chore: fix tests * chore: cargo fmt Co-authored-by: nathan <[email protected]>
1 parent 05f99ee commit b83b182

File tree

6 files changed

+96
-53
lines changed

6 files changed

+96
-53
lines changed

frontend/rust-lib/flowy-grid/src/services/cell/cell_operation.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,25 @@ pub fn try_decode_cell_str(
137137
/// If the cell data of the `FieldType` doesn't support displaying in String then will return an
138138
/// empty string. For example, The string of the Multi-Select cell will be a list of the option's name
139139
/// separated by a comma.
140+
///
141+
/// # Arguments
142+
///
143+
/// * `cell_str`: the opaque cell string that can be decoded by corresponding structs that implement the
144+
/// `FromCellString` trait.
145+
/// * `decoded_field_type`: the field_type of the cell_str
146+
/// * `field_type`: use this field type's `TypeOption` to stringify this cell_str
147+
/// * `field_rev`: used to get the corresponding TypeOption for the specified field type.
148+
///
149+
/// returns: String
140150
pub fn stringify_cell_data(
141151
cell_str: String,
142-
from_field_type: &FieldType,
143-
to_field_type: &FieldType,
152+
decoded_field_type: &FieldType,
153+
field_type: &FieldType,
144154
field_rev: &FieldRevision,
145155
) -> String {
146-
match TypeOptionCellExt::new_with_cell_data_cache(field_rev, None).get_type_option_cell_data_handler(to_field_type)
147-
{
156+
match TypeOptionCellExt::new_with_cell_data_cache(field_rev, None).get_type_option_cell_data_handler(field_type) {
148157
None => "".to_string(),
149-
Some(handler) => handler.stringify_cell_str(cell_str, from_field_type, field_rev),
158+
Some(handler) => handler.stringify_cell_str(cell_str, decoded_field_type, field_rev),
150159
}
151160
}
152161

frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/select_type_option.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::services::cell::{
77

88
use crate::services::field::selection_type_option::type_option_transform::SelectOptionTypeOptionTransformHelper;
99
use crate::services::field::{
10-
CheckboxCellData, ChecklistTypeOptionPB, MultiSelectTypeOptionPB, SingleSelectTypeOptionPB, TypeOption,
11-
TypeOptionCellData, TypeOptionTransform,
10+
CheckboxCellData, ChecklistTypeOptionPB, MultiSelectTypeOptionPB, SingleSelectTypeOptionPB, StrCellData,
11+
TypeOption, TypeOptionCellData, TypeOptionTransform,
1212
};
1313
use bytes::Bytes;
1414
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
@@ -152,10 +152,10 @@ where
152152
fn transform_type_option_cell_str(
153153
&self,
154154
cell_str: &str,
155-
_decoded_field_type: &FieldType,
155+
decoded_field_type: &FieldType,
156156
_field_rev: &FieldRevision,
157157
) -> Option<<Self as TypeOption>::CellData> {
158-
match _decoded_field_type {
158+
match decoded_field_type {
159159
FieldType::SingleSelect | FieldType::MultiSelect | FieldType::Checklist => None,
160160
FieldType::Checkbox => match CheckboxCellData::from_cell_str(cell_str) {
161161
Ok(checkbox_cell_data) => {
@@ -169,6 +169,7 @@ where
169169
}
170170
Err(_) => None,
171171
},
172+
FieldType::RichText => SelectOptionIds::from_cell_str(cell_str).ok(),
172173
_ => Some(SelectOptionIds::from(vec![])),
173174
}
174175
}

frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_tests.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
#[cfg(test)]
22
mod tests {
33
use crate::entities::FieldType;
4+
use crate::services::cell::stringify_cell_data;
45
use crate::services::cell::CellDataDecoder;
56
use crate::services::field::FieldBuilder;
6-
77
use crate::services::field::*;
88

99
// Test parser the cell data which field's type is FieldType::Date to cell data
1010
// which field's type is FieldType::Text
1111
#[test]
1212
fn date_type_to_text_type() {
13-
let type_option = RichTextTypeOptionPB::default();
1413
let field_type = FieldType::DateTime;
1514
let field_rev = FieldBuilder::from_field_type(&field_type).build();
1615

1716
assert_eq!(
18-
type_option
19-
.decode_cell_str(1647251762.to_string(), &field_type, &field_rev)
20-
.unwrap()
21-
.as_str(),
17+
stringify_cell_data(1647251762.to_string(), &FieldType::RichText, &field_type, &field_rev),
2218
"Mar 14,2022"
2319
);
2420
}
@@ -27,19 +23,14 @@ mod tests {
2723
// which field's type is FieldType::Text
2824
#[test]
2925
fn single_select_to_text_type() {
30-
let type_option = RichTextTypeOptionPB::default();
31-
3226
let field_type = FieldType::SingleSelect;
3327
let done_option = SelectOptionPB::new("Done");
3428
let option_id = done_option.id.clone();
3529
let single_select = SingleSelectTypeOptionBuilder::default().add_option(done_option.clone());
3630
let field_rev = FieldBuilder::new(single_select).build();
3731

3832
assert_eq!(
39-
type_option
40-
.decode_cell_str(option_id, &field_type, &field_rev)
41-
.unwrap()
42-
.to_string(),
33+
stringify_cell_data(option_id, &FieldType::RichText, &field_type, &field_rev),
4334
done_option.name,
4435
);
4536
}
@@ -49,7 +40,6 @@ mod tests {
4940
*/
5041
#[test]
5142
fn multiselect_to_text_type() {
52-
let text_type_option = RichTextTypeOptionPB::default();
5343
let field_type = FieldType::MultiSelect;
5444

5545
let france = SelectOptionPB::new("france");
@@ -65,14 +55,12 @@ mod tests {
6555
let field_rev = FieldBuilder::new(multi_select).build();
6656

6757
assert_eq!(
68-
text_type_option
69-
.decode_cell_str(
70-
format!("{},{}", france_option_id, argentina_option_id),
71-
&field_type,
72-
&field_rev
73-
)
74-
.unwrap()
75-
.to_string(),
58+
stringify_cell_data(
59+
format!("{},{}", france_option_id, argentina_option_id),
60+
&FieldType::RichText,
61+
&field_type,
62+
&field_rev
63+
),
7664
format!("{},{}", france.name, argentina.name)
7765
);
7866
}

frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_type_option.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,31 @@ impl TypeOption for RichTextTypeOptionPB {
4848
type CellFilter = TextFilterPB;
4949
}
5050

51-
impl TypeOptionTransform for RichTextTypeOptionPB {}
51+
impl TypeOptionTransform for RichTextTypeOptionPB {
52+
fn transformable(&self) -> bool {
53+
true
54+
}
55+
56+
fn transform_type_option(&mut self, _old_type_option_field_type: FieldType, _old_type_option_data: String) {}
57+
58+
fn transform_type_option_cell_str(
59+
&self,
60+
cell_str: &str,
61+
decoded_field_type: &FieldType,
62+
field_rev: &FieldRevision,
63+
) -> Option<<Self as TypeOption>::CellData> {
64+
if decoded_field_type.is_date()
65+
|| decoded_field_type.is_single_select()
66+
|| decoded_field_type.is_multi_select()
67+
|| decoded_field_type.is_number()
68+
|| decoded_field_type.is_url()
69+
{
70+
Some(stringify_cell_data(cell_str.to_owned(), decoded_field_type, decoded_field_type, field_rev).into())
71+
} else {
72+
StrCellData::from_cell_str(&cell_str).ok()
73+
}
74+
}
75+
}
5276

5377
impl TypeOptionCellData for RichTextTypeOptionPB {
5478
fn convert_to_protobuf(&self, cell_data: <Self as TypeOption>::CellData) -> <Self as TypeOption>::CellProtobufType {
@@ -64,19 +88,10 @@ impl CellDataDecoder for RichTextTypeOptionPB {
6488
fn decode_cell_str(
6589
&self,
6690
cell_str: String,
67-
decoded_field_type: &FieldType,
68-
field_rev: &FieldRevision,
91+
_decoded_field_type: &FieldType,
92+
_field_rev: &FieldRevision,
6993
) -> FlowyResult<<Self as TypeOption>::CellData> {
70-
if decoded_field_type.is_date()
71-
|| decoded_field_type.is_single_select()
72-
|| decoded_field_type.is_multi_select()
73-
|| decoded_field_type.is_number()
74-
|| decoded_field_type.is_url()
75-
{
76-
Ok(stringify_cell_data(cell_str, decoded_field_type, decoded_field_type, field_rev).into())
77-
} else {
78-
StrCellData::from_cell_str(&cell_str)
79-
}
94+
StrCellData::from_cell_str(&cell_str)
8095
}
8196

8297
fn decode_cell_data_to_str(&self, cell_data: <Self as TypeOption>::CellData) -> String {

frontend/rust-lib/flowy-grid/src/services/field/type_options/type_option_cell.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ pub trait TypeOptionCellDataHandler {
4747

4848
/// Decode the cell_str to corresponding cell data, and then return the display string of the
4949
/// cell data.
50-
fn stringify_cell_str(&self, cell_str: String, field_type: &FieldType, field_rev: &FieldRevision) -> String;
50+
fn stringify_cell_str(&self, cell_str: String, decoded_field_type: &FieldType, field_rev: &FieldRevision)
51+
-> String;
5152
}
5253

5354
struct CellDataCacheKey(u64);
@@ -222,9 +223,14 @@ where
222223
perform_filter().unwrap_or(true)
223224
}
224225

225-
fn stringify_cell_str(&self, cell_str: String, field_type: &FieldType, field_rev: &FieldRevision) -> String {
226+
fn stringify_cell_str(
227+
&self,
228+
cell_str: String,
229+
decoded_field_type: &FieldType,
230+
field_rev: &FieldRevision,
231+
) -> String {
226232
if self.transformable() {
227-
let cell_data = self.transform_type_option_cell_str(&cell_str, field_type, field_rev);
233+
let cell_data = self.transform_type_option_cell_str(&cell_str, decoded_field_type, field_rev);
228234
if let Some(cell_data) = cell_data {
229235
return self.decode_cell_data_to_str(cell_data);
230236
}

frontend/rust-lib/flowy-grid/tests/grid/field_test/test.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,25 +208,25 @@ async fn grid_switch_from_multi_select_to_text_test() {
208208

209209
let mut multi_select_type_option = test.get_multi_select_type_option(&field_rev.id);
210210

211-
let script_switchfield = vec![SwitchToField {
211+
let script_switch_field = vec![SwitchToField {
212212
field_id: field_rev.id.clone(),
213213
new_field_type: FieldType::RichText,
214214
}];
215215

216-
test.run_scripts(script_switchfield).await;
216+
test.run_scripts(script_switch_field).await;
217217

218-
let script_assertfield = vec![AssertCellContent {
218+
let script_assert_field = vec![AssertCellContent {
219219
field_id: field_rev.id.clone(),
220220
row_index: 0,
221221
from_field_type: FieldType::MultiSelect,
222222
expected_content: format!(
223223
"{},{}",
224-
multi_select_type_option.get_mut(0).unwrap().id.to_string(),
225-
multi_select_type_option.get_mut(1).unwrap().id.to_string()
224+
multi_select_type_option.get_mut(0).unwrap().name.to_string(),
225+
multi_select_type_option.get_mut(1).unwrap().name.to_string()
226226
),
227227
}];
228228

229-
test.run_scripts(script_assertfield).await;
229+
test.run_scripts(script_assert_field).await;
230230
}
231231

232232
// Test when switching the current field from Checkbox to Text test
@@ -276,4 +276,28 @@ async fn grid_switch_from_date_to_text_test() {}
276276
// input:
277277
// $1 -> "$1"(This string will be different base on current data setting)
278278
#[tokio::test]
279-
async fn grid_switch_from_number_to_text_test() {}
279+
async fn grid_switch_from_number_to_text_test() {
280+
let mut test = GridFieldTest::new().await;
281+
let field_rev = test.get_first_field_rev(FieldType::Number).clone();
282+
283+
let scripts = vec![
284+
SwitchToField {
285+
field_id: field_rev.id.clone(),
286+
new_field_type: FieldType::RichText,
287+
},
288+
AssertCellContent {
289+
field_id: field_rev.id.clone(),
290+
row_index: 0,
291+
from_field_type: FieldType::Number,
292+
expected_content: "$1".to_string(),
293+
},
294+
AssertCellContent {
295+
field_id: field_rev.id.clone(),
296+
row_index: 4,
297+
from_field_type: FieldType::Number,
298+
expected_content: "".to_string(),
299+
},
300+
];
301+
302+
test.run_scripts(scripts).await;
303+
}

0 commit comments

Comments
 (0)