mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
test: date group_id generation logic (#4045)
This commit is contained in:
@ -3,7 +3,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int64_t init_sdk(char *path);
|
int64_t init_sdk(char *data);
|
||||||
|
|
||||||
void async_event(int64_t port, const uint8_t *input, uintptr_t len);
|
void async_event(int64_t port, const uint8_t *input, uintptr_t len);
|
||||||
|
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
use std::format;
|
use std::format;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use chrono::{
|
use chrono::{DateTime, Datelike, Days, Duration, Local, NaiveDate, NaiveDateTime};
|
||||||
DateTime, Datelike, Days, Duration, Local, NaiveDate, NaiveDateTime, Offset, TimeZone,
|
|
||||||
};
|
|
||||||
use chrono_tz::Tz;
|
|
||||||
use collab_database::database::timestamp;
|
use collab_database::database::timestamp;
|
||||||
use collab_database::fields::{Field, TypeOptionData};
|
use collab_database::fields::{Field, TypeOptionData};
|
||||||
use collab_database::rows::{new_cell_builder, Cell, Cells, Row, RowDetail};
|
use collab_database::rows::{new_cell_builder, Cell, Cells, Row, RowDetail};
|
||||||
@ -84,12 +80,7 @@ impl GroupCustomize for DateGroupController {
|
|||||||
content: &str,
|
content: &str,
|
||||||
cell_data: &<Self::GroupTypeOption as TypeOption>::CellData,
|
cell_data: &<Self::GroupTypeOption as TypeOption>::CellData,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
content
|
content == group_id(cell_data, &self.context.get_setting_content())
|
||||||
== group_id(
|
|
||||||
cell_data,
|
|
||||||
&self.type_option,
|
|
||||||
&self.context.get_setting_content(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_or_delete_group_when_cell_changed(
|
fn create_or_delete_group_when_cell_changed(
|
||||||
@ -102,15 +93,10 @@ impl GroupCustomize for DateGroupController {
|
|||||||
let mut inserted_group = None;
|
let mut inserted_group = None;
|
||||||
if self
|
if self
|
||||||
.context
|
.context
|
||||||
.get_group(&group_id(
|
.get_group(&group_id(&_cell_data.into(), &setting_content))
|
||||||
&_cell_data.into(),
|
|
||||||
&self.type_option,
|
|
||||||
&setting_content,
|
|
||||||
))
|
|
||||||
.is_none()
|
.is_none()
|
||||||
{
|
{
|
||||||
let group =
|
let group = make_group_from_date_cell(&_cell_data.into(), &setting_content);
|
||||||
make_group_from_date_cell(&_cell_data.into(), &self.type_option, &setting_content);
|
|
||||||
let mut new_group = self.context.add_new_group(group)?;
|
let mut new_group = self.context.add_new_group(group)?;
|
||||||
new_group.group.rows.push(RowMetaPB::from(_row_detail));
|
new_group.group.rows.push(RowMetaPB::from(_row_detail));
|
||||||
inserted_group = Some(new_group);
|
inserted_group = Some(new_group);
|
||||||
@ -118,11 +104,9 @@ impl GroupCustomize for DateGroupController {
|
|||||||
|
|
||||||
// Delete the old group if there are no rows in that group
|
// Delete the old group if there are no rows in that group
|
||||||
let deleted_group = match _old_cell_data.and_then(|old_cell_data| {
|
let deleted_group = match _old_cell_data.and_then(|old_cell_data| {
|
||||||
self.context.get_group(&group_id(
|
self
|
||||||
&old_cell_data.into(),
|
.context
|
||||||
&self.type_option,
|
.get_group(&group_id(&old_cell_data.into(), &setting_content))
|
||||||
&setting_content,
|
|
||||||
))
|
|
||||||
}) {
|
}) {
|
||||||
None => None,
|
None => None,
|
||||||
Some((_, group)) => {
|
Some((_, group)) => {
|
||||||
@ -154,7 +138,7 @@ impl GroupCustomize for DateGroupController {
|
|||||||
let setting_content = self.context.get_setting_content();
|
let setting_content = self.context.get_setting_content();
|
||||||
self.context.iter_mut_status_groups(|group| {
|
self.context.iter_mut_status_groups(|group| {
|
||||||
let mut changeset = GroupRowsNotificationPB::new(group.id.clone());
|
let mut changeset = GroupRowsNotificationPB::new(group.id.clone());
|
||||||
if group.id == group_id(&cell_data.into(), &self.type_option, &setting_content) {
|
if group.id == group_id(&cell_data.into(), &setting_content) {
|
||||||
if !group.contains_row(&row_detail.row.id) {
|
if !group.contains_row(&row_detail.row.id) {
|
||||||
changeset
|
changeset
|
||||||
.inserted_rows
|
.inserted_rows
|
||||||
@ -194,14 +178,13 @@ impl GroupCustomize for DateGroupController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let setting_content = self.context.get_setting_content();
|
let setting_content = self.context.get_setting_content();
|
||||||
let deleted_group =
|
let deleted_group = match self
|
||||||
match self
|
.context
|
||||||
.context
|
.get_group(&group_id(cell_data, &setting_content))
|
||||||
.get_group(&group_id(cell_data, &self.type_option, &setting_content))
|
{
|
||||||
{
|
Some((_, group)) if group.rows.len() == 1 => Some(group.clone()),
|
||||||
Some((_, group)) if group.rows.len() == 1 => Some(group.clone()),
|
_ => None,
|
||||||
_ => None,
|
};
|
||||||
};
|
|
||||||
|
|
||||||
let deleted_group = deleted_group.map(|group| {
|
let deleted_group = deleted_group.map(|group| {
|
||||||
let _ = self.context.delete_group(&group.id);
|
let _ = self.context.delete_group(&group.id);
|
||||||
@ -232,11 +215,10 @@ impl GroupCustomize for DateGroupController {
|
|||||||
) -> Option<GroupPB> {
|
) -> Option<GroupPB> {
|
||||||
let mut deleted_group = None;
|
let mut deleted_group = None;
|
||||||
let setting_content = self.context.get_setting_content();
|
let setting_content = self.context.get_setting_content();
|
||||||
if let Some((_, group)) = self.context.get_group(&group_id(
|
if let Some((_, group)) = self
|
||||||
&_cell_data.into(),
|
.context
|
||||||
&self.type_option,
|
.get_group(&group_id(&_cell_data.into(), &setting_content))
|
||||||
&setting_content,
|
{
|
||||||
)) {
|
|
||||||
if group.rows.len() == 1 {
|
if group.rows.len() == 1 {
|
||||||
deleted_group = Some(GroupPB::from(group.clone()));
|
deleted_group = Some(GroupPB::from(group.clone()));
|
||||||
}
|
}
|
||||||
@ -279,7 +261,7 @@ impl GroupsBuilder for DateGroupBuilder {
|
|||||||
async fn build(
|
async fn build(
|
||||||
field: &Field,
|
field: &Field,
|
||||||
context: &Self::Context,
|
context: &Self::Context,
|
||||||
type_option: &Self::GroupTypeOption,
|
_type_option: &Self::GroupTypeOption,
|
||||||
) -> GeneratedGroups {
|
) -> GeneratedGroups {
|
||||||
// Read all the cells for the grouping field
|
// Read all the cells for the grouping field
|
||||||
let cells = context.get_all_cells().await;
|
let cells = context.get_all_cells().await;
|
||||||
@ -290,7 +272,7 @@ impl GroupsBuilder for DateGroupBuilder {
|
|||||||
.flat_map(|value| value.into_date_field_cell_data())
|
.flat_map(|value| value.into_date_field_cell_data())
|
||||||
.filter(|cell| cell.timestamp.is_some())
|
.filter(|cell| cell.timestamp.is_some())
|
||||||
.map(|cell| {
|
.map(|cell| {
|
||||||
let group = make_group_from_date_cell(&cell, type_option, &context.get_setting_content());
|
let group = make_group_from_date_cell(&cell, &context.get_setting_content());
|
||||||
GeneratedGroupConfig {
|
GeneratedGroupConfig {
|
||||||
filter_content: group.id.clone(),
|
filter_content: group.id.clone(),
|
||||||
group,
|
group,
|
||||||
@ -307,27 +289,19 @@ impl GroupsBuilder for DateGroupBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_group_from_date_cell(
|
fn make_group_from_date_cell(cell_data: &DateCellData, setting_content: &str) -> Group {
|
||||||
cell_data: &DateCellData,
|
let group_id = group_id(cell_data, setting_content);
|
||||||
type_option: &DateTypeOption,
|
|
||||||
setting_content: &str,
|
|
||||||
) -> Group {
|
|
||||||
let group_id = group_id(cell_data, type_option, setting_content);
|
|
||||||
Group::new(
|
Group::new(
|
||||||
group_id.clone(),
|
group_id.clone(),
|
||||||
group_name_from_id(&group_id, type_option, setting_content),
|
group_name_from_id(&group_id, setting_content),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const GROUP_ID_DATE_FORMAT: &str = "%Y/%m/%d";
|
const GROUP_ID_DATE_FORMAT: &str = "%Y/%m/%d";
|
||||||
|
|
||||||
fn group_id(
|
fn group_id(cell_data: &DateCellData, setting_content: &str) -> String {
|
||||||
cell_data: &DateCellData,
|
|
||||||
type_option: &DateTypeOption,
|
|
||||||
setting_content: &str,
|
|
||||||
) -> String {
|
|
||||||
let config = DateGroupConfiguration::from_json(setting_content).unwrap_or_default();
|
let config = DateGroupConfiguration::from_json(setting_content).unwrap_or_default();
|
||||||
let date_time = date_time_from_timestamp(cell_data.timestamp, &type_option.timezone_id);
|
let date_time = date_time_from_timestamp(cell_data.timestamp);
|
||||||
|
|
||||||
let date_format = GROUP_ID_DATE_FORMAT;
|
let date_format = GROUP_ID_DATE_FORMAT;
|
||||||
let month_format = &date_format.replace("%d", "01");
|
let month_format = &date_format.replace("%d", "01");
|
||||||
@ -342,7 +316,7 @@ fn group_id(
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.format(date_format),
|
.format(date_format),
|
||||||
DateCondition::Relative => {
|
DateCondition::Relative => {
|
||||||
let now = date_time_from_timestamp(Some(timestamp()), &type_option.timezone_id).date_naive();
|
let now = date_time_from_timestamp(Some(timestamp())).date_naive();
|
||||||
let date_time = date_time.date_naive();
|
let date_time = date_time.date_naive();
|
||||||
|
|
||||||
let diff = date_time.signed_duration_since(now).num_days();
|
let diff = date_time.signed_duration_since(now).num_days();
|
||||||
@ -382,11 +356,7 @@ fn group_id(
|
|||||||
date.to_string()
|
date.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn group_name_from_id(
|
fn group_name_from_id(group_id: &str, setting_content: &str) -> String {
|
||||||
group_id: &str,
|
|
||||||
type_option: &DateTypeOption,
|
|
||||||
setting_content: &str,
|
|
||||||
) -> String {
|
|
||||||
let config = DateGroupConfiguration::from_json(setting_content).unwrap_or_default();
|
let config = DateGroupConfiguration::from_json(setting_content).unwrap_or_default();
|
||||||
let date = NaiveDate::parse_from_str(group_id, GROUP_ID_DATE_FORMAT).unwrap();
|
let date = NaiveDate::parse_from_str(group_id, GROUP_ID_DATE_FORMAT).unwrap();
|
||||||
|
|
||||||
@ -421,7 +391,7 @@ fn group_name_from_id(
|
|||||||
},
|
},
|
||||||
DateCondition::Year => date.year().to_string(),
|
DateCondition::Year => date.year().to_string(),
|
||||||
DateCondition::Relative => {
|
DateCondition::Relative => {
|
||||||
let now = date_time_from_timestamp(Some(timestamp()), &type_option.timezone_id);
|
let now = date_time_from_timestamp(Some(timestamp()));
|
||||||
|
|
||||||
let diff = date.signed_duration_since(now.date_naive());
|
let diff = date.signed_duration_since(now.date_naive());
|
||||||
let result = match diff.num_days() {
|
let result = match diff.num_days() {
|
||||||
@ -443,14 +413,11 @@ fn group_name_from_id(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn date_time_from_timestamp(timestamp: Option<i64>, timezone_id: &str) -> DateTime<Local> {
|
fn date_time_from_timestamp(timestamp: Option<i64>) -> DateTime<Local> {
|
||||||
match timestamp {
|
match timestamp {
|
||||||
Some(timestamp) => {
|
Some(timestamp) => {
|
||||||
let naive = NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap();
|
let naive = NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap();
|
||||||
let offset = match Tz::from_str(timezone_id) {
|
let offset = *Local::now().offset();
|
||||||
Ok(timezone) => timezone.offset_from_utc_datetime(&naive).fix(),
|
|
||||||
Err(_) => *Local::now().offset(),
|
|
||||||
};
|
|
||||||
|
|
||||||
DateTime::<Local>::from_naive_utc_and_offset(naive, offset)
|
DateTime::<Local>::from_naive_utc_and_offset(naive, offset)
|
||||||
},
|
},
|
||||||
@ -480,12 +447,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn group_id_name_test() {
|
fn group_id_name_test() {
|
||||||
struct GroupIDTest<'a> {
|
struct GroupIDTest {
|
||||||
cell_data: DateCellData,
|
cell_data: DateCellData,
|
||||||
setting_content: String,
|
setting_content: String,
|
||||||
exp_group_id: String,
|
exp_group_id: String,
|
||||||
exp_group_name: String,
|
exp_group_name: String,
|
||||||
type_option: &'a DateTypeOption,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mar_14_2022 = NaiveDateTime::from_timestamp_opt(1647251762, 0).unwrap();
|
let mar_14_2022 = NaiveDateTime::from_timestamp_opt(1647251762, 0).unwrap();
|
||||||
@ -505,7 +471,6 @@ mod tests {
|
|||||||
let tests = vec![
|
let tests = vec![
|
||||||
GroupIDTest {
|
GroupIDTest {
|
||||||
cell_data: mar_14_2022_cd.clone(),
|
cell_data: mar_14_2022_cd.clone(),
|
||||||
type_option: &local_date_type_option,
|
|
||||||
setting_content: r#"{"condition": 0, "hide_empty": false}"#.to_string(),
|
setting_content: r#"{"condition": 0, "hide_empty": false}"#.to_string(),
|
||||||
exp_group_id: "2022/03/01".to_string(),
|
exp_group_id: "2022/03/01".to_string(),
|
||||||
exp_group_name: "Mar 2022".to_string(),
|
exp_group_name: "Mar 2022".to_string(),
|
||||||
@ -516,7 +481,6 @@ mod tests {
|
|||||||
include_time: false,
|
include_time: false,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
type_option: &local_date_type_option,
|
|
||||||
setting_content: r#"{"condition": 0, "hide_empty": false}"#.to_string(),
|
setting_content: r#"{"condition": 0, "hide_empty": false}"#.to_string(),
|
||||||
exp_group_id: today.format(GROUP_ID_DATE_FORMAT).to_string(),
|
exp_group_id: today.format(GROUP_ID_DATE_FORMAT).to_string(),
|
||||||
exp_group_name: "Today".to_string(),
|
exp_group_name: "Today".to_string(),
|
||||||
@ -527,7 +491,6 @@ mod tests {
|
|||||||
include_time: false,
|
include_time: false,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
type_option: &local_date_type_option,
|
|
||||||
setting_content: r#"{"condition": 0, "hide_empty": false}"#.to_string(),
|
setting_content: r#"{"condition": 0, "hide_empty": false}"#.to_string(),
|
||||||
exp_group_id: today
|
exp_group_id: today
|
||||||
.checked_sub_days(Days::new(7))
|
.checked_sub_days(Days::new(7))
|
||||||
@ -538,7 +501,6 @@ mod tests {
|
|||||||
},
|
},
|
||||||
GroupIDTest {
|
GroupIDTest {
|
||||||
cell_data: mar_14_2022_cd.clone(),
|
cell_data: mar_14_2022_cd.clone(),
|
||||||
type_option: &local_date_type_option,
|
|
||||||
setting_content: r#"{"condition": 1, "hide_empty": false}"#.to_string(),
|
setting_content: r#"{"condition": 1, "hide_empty": false}"#.to_string(),
|
||||||
exp_group_id: "2022/03/14".to_string(),
|
exp_group_id: "2022/03/14".to_string(),
|
||||||
exp_group_name: "Mar 14, 2022".to_string(),
|
exp_group_name: "Mar 14, 2022".to_string(),
|
||||||
@ -554,21 +516,18 @@ mod tests {
|
|||||||
include_time: false,
|
include_time: false,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
type_option: &local_date_type_option,
|
|
||||||
setting_content: r#"{"condition": 2, "hide_empty": false}"#.to_string(),
|
setting_content: r#"{"condition": 2, "hide_empty": false}"#.to_string(),
|
||||||
exp_group_id: "2022/03/14".to_string(),
|
exp_group_id: "2022/03/14".to_string(),
|
||||||
exp_group_name: "Week of Mar 14-20 2022".to_string(),
|
exp_group_name: "Week of Mar 14-20 2022".to_string(),
|
||||||
},
|
},
|
||||||
GroupIDTest {
|
GroupIDTest {
|
||||||
cell_data: mar_14_2022_cd.clone(),
|
cell_data: mar_14_2022_cd.clone(),
|
||||||
type_option: &local_date_type_option,
|
|
||||||
setting_content: r#"{"condition": 3, "hide_empty": false}"#.to_string(),
|
setting_content: r#"{"condition": 3, "hide_empty": false}"#.to_string(),
|
||||||
exp_group_id: "2022/03/01".to_string(),
|
exp_group_id: "2022/03/01".to_string(),
|
||||||
exp_group_name: "Mar 2022".to_string(),
|
exp_group_name: "Mar 2022".to_string(),
|
||||||
},
|
},
|
||||||
GroupIDTest {
|
GroupIDTest {
|
||||||
cell_data: mar_14_2022_cd,
|
cell_data: mar_14_2022_cd,
|
||||||
type_option: &local_date_type_option,
|
|
||||||
setting_content: r#"{"condition": 4, "hide_empty": false}"#.to_string(),
|
setting_content: r#"{"condition": 4, "hide_empty": false}"#.to_string(),
|
||||||
exp_group_id: "2022/01/01".to_string(),
|
exp_group_id: "2022/01/01".to_string(),
|
||||||
exp_group_name: "2022".to_string(),
|
exp_group_name: "2022".to_string(),
|
||||||
@ -579,7 +538,6 @@ mod tests {
|
|||||||
include_time: false,
|
include_time: false,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
type_option: &default_date_type_option,
|
|
||||||
setting_content: r#"{"condition": 1, "hide_empty": false}"#.to_string(),
|
setting_content: r#"{"condition": 1, "hide_empty": false}"#.to_string(),
|
||||||
exp_group_id: "2023/06/02".to_string(),
|
exp_group_id: "2023/06/02".to_string(),
|
||||||
exp_group_name: "".to_string(),
|
exp_group_name: "".to_string(),
|
||||||
@ -590,7 +548,6 @@ mod tests {
|
|||||||
include_time: false,
|
include_time: false,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
type_option: &default_date_type_option,
|
|
||||||
setting_content: r#"{"condition": 1, "hide_empty": false}"#.to_string(),
|
setting_content: r#"{"condition": 1, "hide_empty": false}"#.to_string(),
|
||||||
exp_group_id: "2023/06/03".to_string(),
|
exp_group_id: "2023/06/03".to_string(),
|
||||||
exp_group_name: "".to_string(),
|
exp_group_name: "".to_string(),
|
||||||
@ -598,11 +555,11 @@ mod tests {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (i, test) in tests.iter().enumerate() {
|
for (i, test) in tests.iter().enumerate() {
|
||||||
let group_id = group_id(&test.cell_data, test.type_option, &test.setting_content);
|
let group_id = group_id(&test.cell_data, &test.setting_content);
|
||||||
assert_eq!(test.exp_group_id, group_id, "test {}", i);
|
assert_eq!(test.exp_group_id, group_id, "test {}", i);
|
||||||
|
|
||||||
if !test.exp_group_name.is_empty() {
|
if !test.exp_group_name.is_empty() {
|
||||||
let group_name = group_name_from_id(&group_id, test.type_option, &test.setting_content);
|
let group_name = group_name_from_id(&group_id, &test.setting_content);
|
||||||
assert_eq!(test.exp_group_name, group_name, "test {}", i);
|
assert_eq!(test.exp_group_name, group_name, "test {}", i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user