Skip to content

Commit 99b2b27

Browse files
authored
test: date group_id generation logic (#4045)
1 parent 96af012 commit 99b2b27

File tree

2 files changed

+35
-78
lines changed

2 files changed

+35
-78
lines changed

frontend/appflowy_flutter/linux/flutter/dart_ffi/binding.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <stdint.h>
44
#include <stdlib.h>
55

6-
int64_t init_sdk(char *path);
6+
int64_t init_sdk(char *data);
77

88
void async_event(int64_t port, const uint8_t *input, uintptr_t len);
99

frontend/rust-lib/flowy-database2/src/services/group/controller_impls/date_controller.rs

Lines changed: 34 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use std::format;
2-
use std::str::FromStr;
32

43
use async_trait::async_trait;
5-
use chrono::{
6-
DateTime, Datelike, Days, Duration, Local, NaiveDate, NaiveDateTime, Offset, TimeZone,
7-
};
8-
use chrono_tz::Tz;
4+
use chrono::{DateTime, Datelike, Days, Duration, Local, NaiveDate, NaiveDateTime};
95
use collab_database::database::timestamp;
106
use collab_database::fields::{Field, TypeOptionData};
117
use collab_database::rows::{new_cell_builder, Cell, Cells, Row, RowDetail};
@@ -84,12 +80,7 @@ impl GroupCustomize for DateGroupController {
8480
content: &str,
8581
cell_data: &<Self::GroupTypeOption as TypeOption>::CellData,
8682
) -> bool {
87-
content
88-
== group_id(
89-
cell_data,
90-
&self.type_option,
91-
&self.context.get_setting_content(),
92-
)
83+
content == group_id(cell_data, &self.context.get_setting_content())
9384
}
9485

9586
fn create_or_delete_group_when_cell_changed(
@@ -102,27 +93,20 @@ impl GroupCustomize for DateGroupController {
10293
let mut inserted_group = None;
10394
if self
10495
.context
105-
.get_group(&group_id(
106-
&_cell_data.into(),
107-
&self.type_option,
108-
&setting_content,
109-
))
96+
.get_group(&group_id(&_cell_data.into(), &setting_content))
11097
.is_none()
11198
{
112-
let group =
113-
make_group_from_date_cell(&_cell_data.into(), &self.type_option, &setting_content);
99+
let group = make_group_from_date_cell(&_cell_data.into(), &setting_content);
114100
let mut new_group = self.context.add_new_group(group)?;
115101
new_group.group.rows.push(RowMetaPB::from(_row_detail));
116102
inserted_group = Some(new_group);
117103
}
118104

119105
// Delete the old group if there are no rows in that group
120106
let deleted_group = match _old_cell_data.and_then(|old_cell_data| {
121-
self.context.get_group(&group_id(
122-
&old_cell_data.into(),
123-
&self.type_option,
124-
&setting_content,
125-
))
107+
self
108+
.context
109+
.get_group(&group_id(&old_cell_data.into(), &setting_content))
126110
}) {
127111
None => None,
128112
Some((_, group)) => {
@@ -154,7 +138,7 @@ impl GroupCustomize for DateGroupController {
154138
let setting_content = self.context.get_setting_content();
155139
self.context.iter_mut_status_groups(|group| {
156140
let mut changeset = GroupRowsNotificationPB::new(group.id.clone());
157-
if group.id == group_id(&cell_data.into(), &self.type_option, &setting_content) {
141+
if group.id == group_id(&cell_data.into(), &setting_content) {
158142
if !group.contains_row(&row_detail.row.id) {
159143
changeset
160144
.inserted_rows
@@ -194,14 +178,13 @@ impl GroupCustomize for DateGroupController {
194178
});
195179

196180
let setting_content = self.context.get_setting_content();
197-
let deleted_group =
198-
match self
199-
.context
200-
.get_group(&group_id(cell_data, &self.type_option, &setting_content))
201-
{
202-
Some((_, group)) if group.rows.len() == 1 => Some(group.clone()),
203-
_ => None,
204-
};
181+
let deleted_group = match self
182+
.context
183+
.get_group(&group_id(cell_data, &setting_content))
184+
{
185+
Some((_, group)) if group.rows.len() == 1 => Some(group.clone()),
186+
_ => None,
187+
};
205188

206189
let deleted_group = deleted_group.map(|group| {
207190
let _ = self.context.delete_group(&group.id);
@@ -232,11 +215,10 @@ impl GroupCustomize for DateGroupController {
232215
) -> Option<GroupPB> {
233216
let mut deleted_group = None;
234217
let setting_content = self.context.get_setting_content();
235-
if let Some((_, group)) = self.context.get_group(&group_id(
236-
&_cell_data.into(),
237-
&self.type_option,
238-
&setting_content,
239-
)) {
218+
if let Some((_, group)) = self
219+
.context
220+
.get_group(&group_id(&_cell_data.into(), &setting_content))
221+
{
240222
if group.rows.len() == 1 {
241223
deleted_group = Some(GroupPB::from(group.clone()));
242224
}
@@ -279,7 +261,7 @@ impl GroupsBuilder for DateGroupBuilder {
279261
async fn build(
280262
field: &Field,
281263
context: &Self::Context,
282-
type_option: &Self::GroupTypeOption,
264+
_type_option: &Self::GroupTypeOption,
283265
) -> GeneratedGroups {
284266
// Read all the cells for the grouping field
285267
let cells = context.get_all_cells().await;
@@ -290,7 +272,7 @@ impl GroupsBuilder for DateGroupBuilder {
290272
.flat_map(|value| value.into_date_field_cell_data())
291273
.filter(|cell| cell.timestamp.is_some())
292274
.map(|cell| {
293-
let group = make_group_from_date_cell(&cell, type_option, &context.get_setting_content());
275+
let group = make_group_from_date_cell(&cell, &context.get_setting_content());
294276
GeneratedGroupConfig {
295277
filter_content: group.id.clone(),
296278
group,
@@ -307,27 +289,19 @@ impl GroupsBuilder for DateGroupBuilder {
307289
}
308290
}
309291

310-
fn make_group_from_date_cell(
311-
cell_data: &DateCellData,
312-
type_option: &DateTypeOption,
313-
setting_content: &str,
314-
) -> Group {
315-
let group_id = group_id(cell_data, type_option, setting_content);
292+
fn make_group_from_date_cell(cell_data: &DateCellData, setting_content: &str) -> Group {
293+
let group_id = group_id(cell_data, setting_content);
316294
Group::new(
317295
group_id.clone(),
318-
group_name_from_id(&group_id, type_option, setting_content),
296+
group_name_from_id(&group_id, setting_content),
319297
)
320298
}
321299

322300
const GROUP_ID_DATE_FORMAT: &str = "%Y/%m/%d";
323301

324-
fn group_id(
325-
cell_data: &DateCellData,
326-
type_option: &DateTypeOption,
327-
setting_content: &str,
328-
) -> String {
302+
fn group_id(cell_data: &DateCellData, setting_content: &str) -> String {
329303
let config = DateGroupConfiguration::from_json(setting_content).unwrap_or_default();
330-
let date_time = date_time_from_timestamp(cell_data.timestamp, &type_option.timezone_id);
304+
let date_time = date_time_from_timestamp(cell_data.timestamp);
331305

332306
let date_format = GROUP_ID_DATE_FORMAT;
333307
let month_format = &date_format.replace("%d", "01");
@@ -342,7 +316,7 @@ fn group_id(
342316
.unwrap()
343317
.format(date_format),
344318
DateCondition::Relative => {
345-
let now = date_time_from_timestamp(Some(timestamp()), &type_option.timezone_id).date_naive();
319+
let now = date_time_from_timestamp(Some(timestamp())).date_naive();
346320
let date_time = date_time.date_naive();
347321

348322
let diff = date_time.signed_duration_since(now).num_days();
@@ -382,11 +356,7 @@ fn group_id(
382356
date.to_string()
383357
}
384358

385-
fn group_name_from_id(
386-
group_id: &str,
387-
type_option: &DateTypeOption,
388-
setting_content: &str,
389-
) -> String {
359+
fn group_name_from_id(group_id: &str, setting_content: &str) -> String {
390360
let config = DateGroupConfiguration::from_json(setting_content).unwrap_or_default();
391361
let date = NaiveDate::parse_from_str(group_id, GROUP_ID_DATE_FORMAT).unwrap();
392362

@@ -421,7 +391,7 @@ fn group_name_from_id(
421391
},
422392
DateCondition::Year => date.year().to_string(),
423393
DateCondition::Relative => {
424-
let now = date_time_from_timestamp(Some(timestamp()), &type_option.timezone_id);
394+
let now = date_time_from_timestamp(Some(timestamp()));
425395

426396
let diff = date.signed_duration_since(now.date_naive());
427397
let result = match diff.num_days() {
@@ -443,14 +413,11 @@ fn group_name_from_id(
443413
}
444414
}
445415

446-
fn date_time_from_timestamp(timestamp: Option<i64>, timezone_id: &str) -> DateTime<Local> {
416+
fn date_time_from_timestamp(timestamp: Option<i64>) -> DateTime<Local> {
447417
match timestamp {
448418
Some(timestamp) => {
449419
let naive = NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap();
450-
let offset = match Tz::from_str(timezone_id) {
451-
Ok(timezone) => timezone.offset_from_utc_datetime(&naive).fix(),
452-
Err(_) => *Local::now().offset(),
453-
};
420+
let offset = *Local::now().offset();
454421

455422
DateTime::<Local>::from_naive_utc_and_offset(naive, offset)
456423
},
@@ -480,12 +447,11 @@ mod tests {
480447

481448
#[test]
482449
fn group_id_name_test() {
483-
struct GroupIDTest<'a> {
450+
struct GroupIDTest {
484451
cell_data: DateCellData,
485452
setting_content: String,
486453
exp_group_id: String,
487454
exp_group_name: String,
488-
type_option: &'a DateTypeOption,
489455
}
490456

491457
let mar_14_2022 = NaiveDateTime::from_timestamp_opt(1647251762, 0).unwrap();
@@ -505,7 +471,6 @@ mod tests {
505471
let tests = vec![
506472
GroupIDTest {
507473
cell_data: mar_14_2022_cd.clone(),
508-
type_option: &local_date_type_option,
509474
setting_content: r#"{"condition": 0, "hide_empty": false}"#.to_string(),
510475
exp_group_id: "2022/03/01".to_string(),
511476
exp_group_name: "Mar 2022".to_string(),
@@ -516,7 +481,6 @@ mod tests {
516481
include_time: false,
517482
..Default::default()
518483
},
519-
type_option: &local_date_type_option,
520484
setting_content: r#"{"condition": 0, "hide_empty": false}"#.to_string(),
521485
exp_group_id: today.format(GROUP_ID_DATE_FORMAT).to_string(),
522486
exp_group_name: "Today".to_string(),
@@ -527,7 +491,6 @@ mod tests {
527491
include_time: false,
528492
..Default::default()
529493
},
530-
type_option: &local_date_type_option,
531494
setting_content: r#"{"condition": 0, "hide_empty": false}"#.to_string(),
532495
exp_group_id: today
533496
.checked_sub_days(Days::new(7))
@@ -538,7 +501,6 @@ mod tests {
538501
},
539502
GroupIDTest {
540503
cell_data: mar_14_2022_cd.clone(),
541-
type_option: &local_date_type_option,
542504
setting_content: r#"{"condition": 1, "hide_empty": false}"#.to_string(),
543505
exp_group_id: "2022/03/14".to_string(),
544506
exp_group_name: "Mar 14, 2022".to_string(),
@@ -554,21 +516,18 @@ mod tests {
554516
include_time: false,
555517
..Default::default()
556518
},
557-
type_option: &local_date_type_option,
558519
setting_content: r#"{"condition": 2, "hide_empty": false}"#.to_string(),
559520
exp_group_id: "2022/03/14".to_string(),
560521
exp_group_name: "Week of Mar 14-20 2022".to_string(),
561522
},
562523
GroupIDTest {
563524
cell_data: mar_14_2022_cd.clone(),
564-
type_option: &local_date_type_option,
565525
setting_content: r#"{"condition": 3, "hide_empty": false}"#.to_string(),
566526
exp_group_id: "2022/03/01".to_string(),
567527
exp_group_name: "Mar 2022".to_string(),
568528
},
569529
GroupIDTest {
570530
cell_data: mar_14_2022_cd,
571-
type_option: &local_date_type_option,
572531
setting_content: r#"{"condition": 4, "hide_empty": false}"#.to_string(),
573532
exp_group_id: "2022/01/01".to_string(),
574533
exp_group_name: "2022".to_string(),
@@ -579,7 +538,6 @@ mod tests {
579538
include_time: false,
580539
..Default::default()
581540
},
582-
type_option: &default_date_type_option,
583541
setting_content: r#"{"condition": 1, "hide_empty": false}"#.to_string(),
584542
exp_group_id: "2023/06/02".to_string(),
585543
exp_group_name: "".to_string(),
@@ -590,19 +548,18 @@ mod tests {
590548
include_time: false,
591549
..Default::default()
592550
},
593-
type_option: &default_date_type_option,
594551
setting_content: r#"{"condition": 1, "hide_empty": false}"#.to_string(),
595552
exp_group_id: "2023/06/03".to_string(),
596553
exp_group_name: "".to_string(),
597554
},
598555
];
599556

600557
for (i, test) in tests.iter().enumerate() {
601-
let group_id = group_id(&test.cell_data, test.type_option, &test.setting_content);
558+
let group_id = group_id(&test.cell_data, &test.setting_content);
602559
assert_eq!(test.exp_group_id, group_id, "test {}", i);
603560

604561
if !test.exp_group_name.is_empty() {
605-
let group_name = group_name_from_id(&group_id, test.type_option, &test.setting_content);
562+
let group_name = group_name_from_id(&group_id, &test.setting_content);
606563
assert_eq!(test.exp_group_name, group_name, "test {}", i);
607564
}
608565
}

0 commit comments

Comments
 (0)