chore: add documentation in grid uint test

This commit is contained in:
appflowy 2022-07-23 16:57:28 +08:00
parent 86a8a66e12
commit f5268178c4
6 changed files with 158 additions and 256 deletions

View File

@ -1,30 +1,44 @@
#[cfg(test)]
mod tests {
use crate::services::cell::{apply_cell_data_changeset, decode_any_cell_data};
use crate::services::field::type_options::checkbox_type_option::{NO, YES};
use crate::services::field::FieldBuilder;
use crate::entities::FieldType;
use crate::services::cell::{apply_cell_data_changeset, decode_any_cell_data, CellDataOperation};
use crate::services::field::type_options::checkbox_type_option::*;
use crate::services::field::FieldBuilder;
use flowy_grid_data_model::revision::FieldRevision;
#[test]
fn checkout_box_description_test() {
let field_rev = FieldBuilder::from_field_type(&FieldType::Checkbox).build();
let data = apply_cell_data_changeset("true", None, &field_rev).unwrap();
assert_eq!(decode_any_cell_data(data, &field_rev).to_string(), YES);
let type_option = CheckboxTypeOption::default();
let field_type = FieldType::Checkbox;
let field_rev = FieldBuilder::from_field_type(&field_type).build();
let data = apply_cell_data_changeset("1", None, &field_rev).unwrap();
assert_eq!(decode_any_cell_data(data, &field_rev,).to_string(), YES);
// the checkout value will be checked if the value is "1", "true" or "yes"
assert_checkbox(&type_option, "1", CHECK, &field_type, &field_rev);
assert_checkbox(&type_option, "true", CHECK, &field_type, &field_rev);
assert_checkbox(&type_option, "yes", CHECK, &field_type, &field_rev);
let data = apply_cell_data_changeset("yes", None, &field_rev).unwrap();
assert_eq!(decode_any_cell_data(data, &field_rev,).to_string(), YES);
// the checkout value will be uncheck if the value is "false" or "No"
assert_checkbox(&type_option, "false", UNCHECK, &field_type, &field_rev);
assert_checkbox(&type_option, "No", UNCHECK, &field_type, &field_rev);
let data = apply_cell_data_changeset("false", None, &field_rev).unwrap();
assert_eq!(decode_any_cell_data(data, &field_rev,).to_string(), NO);
// the checkout value will be empty if the value is letters or empty string
assert_checkbox(&type_option, "abc", "", &field_type, &field_rev);
assert_checkbox(&type_option, "", "", &field_type, &field_rev);
}
let data = apply_cell_data_changeset("no", None, &field_rev).unwrap();
assert_eq!(decode_any_cell_data(data, &field_rev,).to_string(), NO);
let data = apply_cell_data_changeset("12", None, &field_rev).unwrap();
assert_eq!(decode_any_cell_data(data, &field_rev,).to_string(), "");
fn assert_checkbox(
type_option: &CheckboxTypeOption,
input_str: &str,
expected_str: &str,
field_type: &FieldType,
field_rev: &FieldRevision,
) {
assert_eq!(
type_option
.decode_cell_data(input_str.to_owned().into(), field_type, field_rev)
.unwrap()
.to_string(),
expected_str.to_owned()
);
}
}

View File

@ -3,14 +3,14 @@ use bytes::Bytes;
use flowy_error::{FlowyError, FlowyResult};
use std::str::FromStr;
pub const YES: &str = "Yes";
pub const NO: &str = "No";
pub const CHECK: &str = "Yes";
pub const UNCHECK: &str = "No";
pub struct CheckboxCellData(String);
impl CheckboxCellData {
pub fn is_check(&self) -> bool {
self.0 == YES
self.0 == CHECK
}
}
@ -36,8 +36,8 @@ impl FromStr for CheckboxCellData {
};
match val {
Some(true) => Ok(Self(YES.to_string())),
Some(false) => Ok(Self(NO.to_string())),
Some(true) => Ok(Self(CHECK.to_string())),
Some(false) => Ok(Self(UNCHECK.to_string())),
None => Ok(Self("".to_string())),
}
}

View File

@ -7,23 +7,6 @@ mod tests {
use flowy_grid_data_model::revision::FieldRevision;
use strum::IntoEnumIterator;
#[test]
fn date_type_option_invalid_input_test() {
let type_option = DateTypeOption::default();
let field_type = FieldType::DateTime;
let field_rev = FieldBuilder::from_field_type(&field_type).build();
assert_changeset_result(
&type_option,
DateCellChangesetPB {
date: Some("1e".to_string()),
time: Some("23:00".to_owned()),
},
&field_type,
&field_rev,
"",
);
}
#[test]
fn date_type_option_date_format_test() {
let mut type_option = DateTypeOption::default();
@ -32,23 +15,23 @@ mod tests {
type_option.date_format = date_format;
match date_format {
DateFormat::Friendly => {
assert_decode_timestamp(1647251762, &type_option, &field_rev, "Mar 14,2022");
assert_date(&type_option, 1647251762, None, "Mar 14,2022", &field_rev);
}
DateFormat::US => {
assert_decode_timestamp(1647251762, &type_option, &field_rev, "2022/03/14");
assert_date(&type_option, 1647251762, None, "2022/03/14", &field_rev);
}
DateFormat::ISO => {
assert_decode_timestamp(1647251762, &type_option, &field_rev, "2022-03-14");
assert_date(&type_option, 1647251762, None, "2022-03-14", &field_rev);
}
DateFormat::Local => {
assert_decode_timestamp(1647251762, &type_option, &field_rev, "2022/03/14");
assert_date(&type_option, 1647251762, None, "2022/03/14", &field_rev);
}
}
}
}
#[test]
fn date_type_option_time_format_test() {
fn date_type_option_different_time_format_test() {
let mut type_option = DateTypeOption::default();
let field_type = FieldType::DateTime;
let field_rev = FieldBuilder::from_field_type(&field_type).build();
@ -58,59 +41,23 @@ mod tests {
type_option.include_time = true;
match time_format {
TimeFormat::TwentyFourHour => {
assert_changeset_result(
assert_date(&type_option, 1653609600, None, "May 27,2022", &field_rev);
assert_date(
&type_option,
DateCellChangesetPB {
date: Some(1653609600.to_string()),
time: None,
},
&field_type,
&field_rev,
"May 27,2022",
);
assert_changeset_result(
&type_option,
DateCellChangesetPB {
date: Some(1653609600.to_string()),
time: Some("23:00".to_owned()),
},
&field_type,
&field_rev,
1653609600,
Some("23:00".to_owned()),
"May 27,2022 23:00",
&field_rev,
);
}
TimeFormat::TwelveHour => {
assert_changeset_result(
assert_date(&type_option, 1653609600, None, "May 27,2022", &field_rev);
assert_date(
&type_option,
DateCellChangesetPB {
date: Some(1653609600.to_string()),
time: None,
},
&field_type,
&field_rev,
"May 27,2022",
);
//
assert_changeset_result(
&type_option,
DateCellChangesetPB {
date: Some(1653609600.to_string()),
time: Some("".to_owned()),
},
&field_type,
&field_rev,
"May 27,2022",
);
assert_changeset_result(
&type_option,
DateCellChangesetPB {
date: Some(1653609600.to_string()),
time: Some("11:23 pm".to_owned()),
},
&field_type,
&field_rev,
1653609600,
Some("11:23 pm".to_owned()),
"May 27,2022 11:23 PM",
&field_rev,
);
}
}
@ -118,141 +65,71 @@ mod tests {
}
#[test]
fn date_type_option_apply_changeset_test() {
let mut type_option = DateTypeOption::new();
fn date_type_option_invalid_date_str_test() {
let type_option = DateTypeOption::default();
let field_type = FieldType::DateTime;
let field_rev = FieldBuilder::from_field_type(&field_type).build();
let date_timestamp = "1653609600".to_owned();
assert_changeset_result(
&type_option,
DateCellChangesetPB {
date: Some(date_timestamp.clone()),
time: None,
},
&field_type,
&field_rev,
"May 27,2022",
);
assert_date(&type_option, "abc", None, "", &field_rev);
}
#[test]
#[should_panic]
fn date_type_option_invalid_include_time_str_test() {
let mut type_option = DateTypeOption::new();
type_option.include_time = true;
assert_changeset_result(
&type_option,
DateCellChangesetPB {
date: Some(date_timestamp.clone()),
time: None,
},
&field_type,
&field_rev,
"May 27,2022",
);
let field_rev = FieldBuilder::from_field_type(&FieldType::DateTime).build();
assert_changeset_result(
assert_date(
&type_option,
DateCellChangesetPB {
date: Some(date_timestamp.clone()),
time: Some("1:00".to_owned()),
},
&field_type,
&field_rev,
1653609600,
Some("1:".to_owned()),
"May 27,2022 01:00",
);
type_option.time_format = TimeFormat::TwelveHour;
assert_changeset_result(
&type_option,
DateCellChangesetPB {
date: Some(date_timestamp),
time: Some("1:00 am".to_owned()),
},
&field_type,
&field_rev,
);
}
#[test]
fn date_type_option_empty_include_time_str_test() {
let mut type_option = DateTypeOption::new();
type_option.include_time = true;
let field_rev = FieldBuilder::from_field_type(&FieldType::DateTime).build();
assert_date(&type_option, 1653609600, Some("".to_owned()), "May 27,2022", &field_rev);
}
/// The default time format is TwentyFourHour, so the include_time_str in twelve_hours_format will cause parser error.
#[test]
#[should_panic]
fn date_type_option_twelve_hours_include_time_str_in_twenty_four_hours_format() {
let mut type_option = DateTypeOption::new();
type_option.include_time = true;
let field_rev = FieldBuilder::from_field_type(&FieldType::DateTime).build();
assert_date(
&type_option,
1653609600,
Some("1:00 am".to_owned()),
"May 27,2022 01:00 AM",
&field_rev,
);
}
#[test]
#[should_panic]
fn date_type_option_apply_changeset_error_test() {
let mut type_option = DateTypeOption::new();
type_option.include_time = true;
let field_rev = FieldBuilder::from_field_type(&FieldType::DateTime).build();
let date_timestamp = "1653609600".to_owned();
assert_changeset_result(
&type_option,
DateCellChangesetPB {
date: Some(date_timestamp.clone()),
time: Some("1:".to_owned()),
},
&FieldType::DateTime,
&field_rev,
"May 27,2022 01:00",
);
assert_changeset_result(
&type_option,
DateCellChangesetPB {
date: Some(date_timestamp),
time: Some("1:00".to_owned()),
},
&FieldType::DateTime,
&field_rev,
"May 27,2022 01:00",
);
}
#[test]
#[should_panic]
fn date_type_option_twelve_hours_to_twenty_four_hours() {
let mut type_option = DateTypeOption::new();
type_option.include_time = true;
let field_rev = FieldBuilder::from_field_type(&FieldType::DateTime).build();
let date_timestamp = "1653609600".to_owned();
assert_changeset_result(
&type_option,
DateCellChangesetPB {
date: Some(date_timestamp),
time: Some("1:00 am".to_owned()),
},
&FieldType::DateTime,
&field_rev,
"May 27,2022 01:00",
);
}
fn assert_changeset_result(
fn assert_date<T: ToString>(
type_option: &DateTypeOption,
changeset: DateCellChangesetPB,
_field_type: &FieldType,
timestamp: T,
include_time_str: Option<String>,
expected_str: &str,
field_rev: &FieldRevision,
expected: &str,
) {
let changeset = CellDataChangeset(Some(changeset));
let encoded_data = type_option.apply_changeset(changeset, None).unwrap();
assert_eq!(
expected.to_owned(),
decode_cell_data(encoded_data, type_option, field_rev)
);
}
fn assert_decode_timestamp(
timestamp: i64,
type_option: &DateTypeOption,
field_rev: &FieldRevision,
expected: &str,
) {
let s = serde_json::to_string(&DateCellChangesetPB {
date: Some(timestamp.to_string()),
time: None,
time: include_time_str,
})
.unwrap();
let encoded_data = type_option.apply_changeset(s.into(), None).unwrap();
assert_eq!(
expected.to_owned(),
decode_cell_data(encoded_data, type_option, field_rev)
decode_cell_data(encoded_data, type_option, field_rev),
expected_str.to_owned(),
);
}

View File

@ -7,25 +7,30 @@ mod tests {
use flowy_grid_data_model::revision::FieldRevision;
use strum::IntoEnumIterator;
/// Testing when the input is not a number.
#[test]
fn number_type_option_invalid_input_test() {
let type_option = NumberTypeOption::default();
let field_type = FieldType::Number;
let field_rev = FieldBuilder::from_field_type(&field_type).build();
assert_equal(&type_option, "", "", &field_type, &field_rev);
assert_equal(&type_option, "abc", "", &field_type, &field_rev);
// Input is empty String
assert_number(&type_option, "", "", &field_type, &field_rev);
// Input is letter
assert_number(&type_option, "abc", "", &field_type, &field_rev);
}
/// Testing the strip_currency_symbol function. It should return the string without the input symbol.
#[test]
fn number_type_option_strip_symbol_test() {
let mut type_option = NumberTypeOption::new();
type_option.format = NumberFormat::USD;
// Remove the $ symbol
assert_eq!(strip_currency_symbol("$18,443"), "18,443".to_owned());
type_option.format = NumberFormat::Yuan;
assert_eq!(strip_currency_symbol("$0.2"), "0.2".to_owned());
// Remove the ¥ symbol
assert_eq!(strip_currency_symbol("¥0.2"), "0.2".to_owned());
}
/// Format the input number to the corresponding format string.
#[test]
fn number_type_option_format_number_test() {
let mut type_option = NumberTypeOption::default();
@ -36,25 +41,26 @@ mod tests {
type_option.format = format;
match format {
NumberFormat::Num => {
assert_equal(&type_option, "18443", "18443", &field_type, &field_rev);
assert_number(&type_option, "18443", "18443", &field_type, &field_rev);
}
NumberFormat::USD => {
assert_equal(&type_option, "18443", "$18,443", &field_type, &field_rev);
assert_number(&type_option, "18443", "$18,443", &field_type, &field_rev);
}
NumberFormat::Yen => {
assert_equal(&type_option, "18443", "¥18,443", &field_type, &field_rev);
assert_number(&type_option, "18443", "¥18,443", &field_type, &field_rev);
}
NumberFormat::Yuan => {
assert_equal(&type_option, "18443", "CN¥18,443", &field_type, &field_rev);
assert_number(&type_option, "18443", "CN¥18,443", &field_type, &field_rev);
}
NumberFormat::EUR => {
assert_equal(&type_option, "18443", "€18.443", &field_type, &field_rev);
assert_number(&type_option, "18443", "€18.443", &field_type, &field_rev);
}
_ => {}
}
}
}
/// Format the input String to the corresponding format string.
#[test]
fn number_type_option_format_str_test() {
let mut type_option = NumberTypeOption::default();
@ -65,33 +71,34 @@ mod tests {
type_option.format = format;
match format {
NumberFormat::Num => {
assert_equal(&type_option, "18443", "18443", &field_type, &field_rev);
assert_equal(&type_option, "0.2", "0.2", &field_type, &field_rev);
assert_number(&type_option, "18443", "18443", &field_type, &field_rev);
assert_number(&type_option, "0.2", "0.2", &field_type, &field_rev);
}
NumberFormat::USD => {
assert_equal(&type_option, "$18,44", "$1,844", &field_type, &field_rev);
assert_equal(&type_option, "$0.2", "$0.2", &field_type, &field_rev);
assert_equal(&type_option, "", "", &field_type, &field_rev);
assert_equal(&type_option, "abc", "", &field_type, &field_rev);
assert_number(&type_option, "$18,44", "$1,844", &field_type, &field_rev);
assert_number(&type_option, "$0.2", "$0.2", &field_type, &field_rev);
assert_number(&type_option, "", "", &field_type, &field_rev);
assert_number(&type_option, "abc", "", &field_type, &field_rev);
}
NumberFormat::Yen => {
assert_equal(&type_option, "¥18,44", "¥1,844", &field_type, &field_rev);
assert_equal(&type_option, "¥1844", "¥1,844", &field_type, &field_rev);
assert_number(&type_option, "¥18,44", "¥1,844", &field_type, &field_rev);
assert_number(&type_option, "¥1844", "¥1,844", &field_type, &field_rev);
}
NumberFormat::Yuan => {
assert_equal(&type_option, "CN¥18,44", "CN¥1,844", &field_type, &field_rev);
assert_equal(&type_option, "CN¥1844", "CN¥1,844", &field_type, &field_rev);
assert_number(&type_option, "CN¥18,44", "CN¥1,844", &field_type, &field_rev);
assert_number(&type_option, "CN¥1844", "CN¥1,844", &field_type, &field_rev);
}
NumberFormat::EUR => {
assert_equal(&type_option, "€18.44", "€18,44", &field_type, &field_rev);
assert_equal(&type_option, "€0.5", "€0,5", &field_type, &field_rev);
assert_equal(&type_option, "€1844", "€1.844", &field_type, &field_rev);
assert_number(&type_option, "€18.44", "€18,44", &field_type, &field_rev);
assert_number(&type_option, "€0.5", "€0,5", &field_type, &field_rev);
assert_number(&type_option, "€1844", "€1.844", &field_type, &field_rev);
}
_ => {}
}
}
}
/// Carry out the sign positive to input number
#[test]
fn number_description_sign_test() {
let mut type_option = NumberTypeOption {
@ -105,32 +112,32 @@ mod tests {
type_option.format = format;
match format {
NumberFormat::Num => {
assert_equal(&type_option, "18443", "18443", &field_type, &field_rev);
assert_number(&type_option, "18443", "18443", &field_type, &field_rev);
}
NumberFormat::USD => {
assert_equal(&type_option, "18443", "-$18,443", &field_type, &field_rev);
assert_number(&type_option, "18443", "-$18,443", &field_type, &field_rev);
}
NumberFormat::Yen => {
assert_equal(&type_option, "18443", "-¥18,443", &field_type, &field_rev);
assert_number(&type_option, "18443", "-¥18,443", &field_type, &field_rev);
}
NumberFormat::EUR => {
assert_equal(&type_option, "18443", "-€18.443", &field_type, &field_rev);
assert_number(&type_option, "18443", "-€18.443", &field_type, &field_rev);
}
_ => {}
}
}
}
fn assert_equal(
fn assert_number(
type_option: &NumberTypeOption,
cell_data: &str,
input_str: &str,
expected_str: &str,
field_type: &FieldType,
field_rev: &FieldRevision,
) {
assert_eq!(
type_option
.decode_cell_data(cell_data.to_owned().into(), field_type, field_rev)
.decode_cell_data(input_str.to_owned().into(), field_type, field_rev)
.unwrap()
.to_string(),
expected_str.to_owned()

View File

@ -6,49 +6,53 @@ mod tests {
use crate::services::field::{URLCellDataPB, URLTypeOption};
use flowy_grid_data_model::revision::FieldRevision;
/// The expected_str will equal to the input string, but the expected_url will be empty if there's no
/// http url in the input string.
#[test]
fn url_type_option_test_no_url() {
fn url_type_option_does_not_contain_url_test() {
let type_option = URLTypeOption::default();
let field_type = FieldType::URL;
let field_rev = FieldBuilder::from_field_type(&field_type).build();
assert_changeset(&type_option, "123", &field_type, &field_rev, "123", "");
assert_url(&type_option, "123", "123", "", &field_type, &field_rev);
}
/// The expected_str will equal to the input string, but the expected_url will not be empty
/// if there's a http url in the input string.
#[test]
fn url_type_option_test_contains_url() {
fn url_type_option_contains_url_test() {
let type_option = URLTypeOption::default();
let field_type = FieldType::URL;
let field_rev = FieldBuilder::from_field_type(&field_type).build();
assert_changeset(
assert_url(
&type_option,
"AppFlowy website - https://www.appflowy.io",
&field_type,
&field_rev,
"AppFlowy website - https://www.appflowy.io",
"https://www.appflowy.io/",
);
assert_changeset(
&type_option,
"AppFlowy website appflowy.io",
&field_type,
&field_rev,
);
assert_url(
&type_option,
"AppFlowy website appflowy.io",
"AppFlowy website appflowy.io",
"https://appflowy.io",
&field_type,
&field_rev,
);
}
fn assert_changeset(
fn assert_url(
type_option: &URLTypeOption,
cell_data: &str,
input_str: &str,
expected_str: &str,
expected_url: &str,
field_type: &FieldType,
field_rev: &FieldRevision,
expected: &str,
expected_url: &str,
) {
let encoded_data = type_option.apply_changeset(cell_data.to_owned().into(), None).unwrap();
let encoded_data = type_option.apply_changeset(input_str.to_owned().into(), None).unwrap();
let decode_cell_data = decode_cell_data(encoded_data, type_option, field_rev, field_type);
assert_eq!(expected.to_owned(), decode_cell_data.content);
assert_eq!(expected_str.to_owned(), decode_cell_data.content);
assert_eq!(expected_url.to_owned(), decode_cell_data.url);
}

View File

@ -2,7 +2,7 @@ use crate::grid::block_test::script::RowScript::*;
use crate::grid::block_test::script::{CreateRowScriptBuilder, GridRowTest};
use crate::grid::grid_editor::{COMPLETED, FACEBOOK, GOOGLE, PAUSED, TWITTER};
use flowy_grid::entities::FieldType;
use flowy_grid::services::field::{NO, SELECTION_IDS_SEPARATOR};
use flowy_grid::services::field::{SELECTION_IDS_SEPARATOR, UNCHECK};
use flowy_grid_data_model::revision::RowMetaChangeset;
#[tokio::test]
@ -72,7 +72,7 @@ async fn grid_row_add_cells_test() {
builder.insert(FieldType::RichText, "hello world", "hello world");
builder.insert(FieldType::DateTime, "1647251762", "2022/03/14");
builder.insert(FieldType::Number, "18,443", "$18,443.00");
builder.insert(FieldType::Checkbox, "false", NO);
builder.insert(FieldType::Checkbox, "false", UNCHECK);
builder.insert(FieldType::URL, "https://appflowy.io", "https://appflowy.io");
builder.insert_single_select_cell(|mut options| options.remove(0), COMPLETED);
builder.insert_multi_select_cell(