Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forget)!: Make keep-* Options and add keep-none #238

Merged
merged 10 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve unit tests
  • Loading branch information
aawsome committed Aug 17, 2024
commit 928d3b86417b3327e6a433b29c111a3b985c6fdb
235 changes: 144 additions & 91 deletions crates/core/src/commands/forget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,26 +563,103 @@ mod tests {

#[fixture]
fn test_snapshots() -> Result<Vec<SnapshotFile>> {
let snaps = [
("2014-09-01 10:20:30", ""),
("2014-09-02 10:20:30", ""),
("2014-09-05 10:20:30", ""),
("2014-09-06 10:20:30", ""),
("2014-09-08 10:20:30", ""),
("2014-09-09 10:20:30", ""),
("2014-09-10 10:20:30", ""),
("2014-09-11 10:20:30", ""),
("2014-09-20 10:20:30", ""),
("2014-09-22 10:20:30", ""),
("2014-08-08 10:20:30", ""),
("2014-08-10 10:20:30", ""),
("2014-08-12 10:20:30", ""),
("2014-08-13 10:20:30", ""),
("2014-08-15 10:20:30", ""),
("2014-08-18 10:20:30", ""),
("2014-08-20 10:20:30", ""),
("2014-08-21 10:20:30", ""),
("2014-08-22 10:20:30", ""),
let by_date = [
"2014-09-01 10:20:30",
"2014-09-02 10:20:30",
"2014-09-05 10:20:30",
"2014-09-06 10:20:30",
"2014-09-08 10:20:30",
"2014-09-09 10:20:30",
"2014-09-10 10:20:30",
"2014-09-11 10:20:30",
"2014-09-20 10:20:30",
"2014-09-22 10:20:30",
"2014-08-08 10:20:30",
"2014-08-10 10:20:30",
"2014-08-12 10:20:30",
"2014-08-13 10:20:30",
"2014-08-15 10:20:30",
"2014-08-18 10:20:30",
"2014-08-20 10:20:30",
"2014-08-21 10:20:30",
"2014-08-22 10:20:30",
"2014-11-18 10:20:30",
"2014-11-20 10:20:30",
"2014-11-21 10:20:30",
"2014-11-22 10:20:30",
"2015-09-01 10:20:30",
"2015-09-02 10:20:30",
"2015-09-05 10:20:30",
"2015-09-06 10:20:30",
"2015-09-08 10:20:30",
"2015-09-09 10:20:30",
"2015-09-10 10:20:30",
"2015-09-11 10:20:30",
"2015-09-20 10:20:30",
"2015-09-22 10:20:30",
"2015-08-08 10:20:30",
"2015-08-10 10:20:30",
"2015-08-12 10:20:30",
"2015-08-13 10:20:30",
"2015-08-15 10:20:30",
"2015-08-18 10:20:30",
"2015-08-20 10:20:30",
"2015-08-21 10:20:30",
"2015-08-22 10:20:30",
"2015-10-01 10:20:30",
"2015-10-02 10:20:30",
"2015-10-05 10:20:30",
"2015-10-06 10:20:30",
"2015-10-08 10:20:30",
"2015-10-09 10:20:30",
"2015-10-10 10:20:30",
"2015-10-11 10:20:30",
"2015-10-20 10:20:30",
"2015-10-22 10:20:30",
"2015-10-22 10:20:30",
"2015-11-08 10:20:30",
"2015-11-10 10:20:30",
"2015-11-12 10:20:30",
"2015-11-13 10:20:30",
"2015-11-15 10:20:30",
"2015-11-18 10:20:30",
"2015-11-20 10:20:30",
"2015-11-21 10:20:30",
"2015-11-22 10:20:30",
"2016-01-01 01:02:03",
"2016-01-01 01:03:03",
"2016-01-01 07:08:03",
"2016-01-03 07:02:03",
"2016-01-04 10:23:03",
"2016-01-04 11:23:03",
"2016-01-04 12:24:03",
"2016-01-04 12:28:03",
"2016-01-04 12:30:03",
"2016-01-04 16:23:03",
"2016-01-07 10:02:03",
"2016-01-08 20:02:03",
"2016-01-09 21:02:03",
"2016-01-12 21:02:03",
"2016-01-12 21:08:03",
"2016-01-18 12:02:03",
];

let by_date_and_id = [
(
"2016-01-05 09:02:03",
"23ef833f60639018019262ac63be5b87601ab58d23880bf6a474adea83dbbf8b",
),
(
"2016-01-06 08:02:03",
"aca6165188e4ee770bb5c7a959a7c6612121960360a2f898203dc67dd75be8da",
),
(
"2016-01-04 12:23:03",
"23ef833d367ddd53bb95cdad23207a1323b770494eae746496094f1db2416c5c",
),
];

let by_date_and_tag = [
("2014-10-01 10:20:30", "foo"),
("2014-10-02 10:20:30", "foo"),
("2014-10-05 10:20:30", "foo"),
Expand All @@ -598,84 +675,39 @@ mod tests {
("2014-11-12 10:20:30", "foo"),
("2014-11-13 10:20:30", "foo"),
("2014-11-15 10:20:30", "foo,bar"),
("2014-11-18 10:20:30", ""),
("2014-11-20 10:20:30", ""),
("2014-11-21 10:20:30", ""),
("2014-11-22 10:20:30", ""),
("2015-09-01 10:20:30", ""),
("2015-09-02 10:20:30", ""),
("2015-09-05 10:20:30", ""),
("2015-09-06 10:20:30", ""),
("2015-09-08 10:20:30", ""),
("2015-09-09 10:20:30", ""),
("2015-09-10 10:20:30", ""),
("2015-09-11 10:20:30", ""),
("2015-09-20 10:20:30", ""),
("2015-09-22 10:20:30", ""),
("2015-08-08 10:20:30", ""),
("2015-08-10 10:20:30", ""),
("2015-08-12 10:20:30", ""),
("2015-08-13 10:20:30", ""),
("2015-08-15 10:20:30", ""),
("2015-08-18 10:20:30", ""),
("2015-08-20 10:20:30", ""),
("2015-08-21 10:20:30", ""),
("2015-08-22 10:20:30", ""),
("2015-10-01 10:20:30", ""),
("2015-10-02 10:20:30", ""),
("2015-10-05 10:20:30", ""),
("2015-10-06 10:20:30", ""),
("2015-10-08 10:20:30", ""),
("2015-10-09 10:20:30", ""),
("2015-10-10 10:20:30", ""),
("2015-10-11 10:20:30", ""),
("2015-10-20 10:20:30", ""),
("2015-10-22 10:20:30", ""),
("2015-10-22 10:20:30", ""),
("2015-10-22 10:20:30", "foo,bar"),
("2015-10-22 10:20:30", "foo,bar"),
("2015-11-08 10:20:30", ""),
("2015-11-10 10:20:30", ""),
("2015-11-12 10:20:30", ""),
("2015-11-13 10:20:30", ""),
("2015-11-15 10:20:30", ""),
("2015-11-18 10:20:30", ""),
("2015-11-20 10:20:30", ""),
("2015-11-21 10:20:30", ""),
("2015-11-22 10:20:30", ""),
("2016-01-01 01:02:03", ""),
("2016-01-01 01:03:03", ""),
("2016-01-01 07:08:03", ""),
("2016-01-03 07:02:03", ""),
("2016-01-04 10:23:03", ""),
("2016-01-04 11:23:03", ""),
("2016-01-04 12:23:03", ""),
("2016-01-04 12:24:03", ""),
("2016-01-04 12:28:03", ""),
("2016-01-04 12:30:03", ""),
("2016-01-04 16:23:03", ""),
("2016-01-05 09:02:03", ""),
("2016-01-06 08:02:03", ""),
("2016-01-07 10:02:03", ""),
("2016-01-08 20:02:03", ""),
("2016-01-09 21:02:03", ""),
("2016-01-12 21:02:03", ""),
("2016-01-12 21:08:03", ""),
("2016-01-18 12:02:03", ""),
];

let mut snaps: Vec<_> = snaps
let snaps: Vec<_> = by_date
.into_iter()
.map(|(time, tags)| -> Result<_> {
.map(|time| -> Result<_> {
let time = NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S")?;
let opts = &crate::SnapshotOptions::default()
.time(Local::from_utc_datetime(&Local, &time))
.tag(vec![StringList::from_str(tags)?]);
.time(Local::from_utc_datetime(&Local, &time));
Ok(SnapshotFile::from_options(opts)?)
})
.chain(by_date_and_id.into_iter().map(|(time, id)| -> Result<_> {
let time = NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S")?;
let opts = &crate::SnapshotOptions::default()
.time(Local::from_utc_datetime(&Local, &time));
let mut snap = SnapshotFile::from_options(opts)?;
snap.id = Id::from_hex(id)?;
Ok(snap)
}))
.chain(
by_date_and_tag
.into_iter()
.map(|(time, tags)| -> Result<_> {
let time = NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S")?;
let opts = &crate::SnapshotOptions::default()
.time(Local::from_utc_datetime(&Local, &time))
.tag(vec![StringList::from_str(tags)?]);
Ok(SnapshotFile::from_options(opts)?)
}),
)
.collect::<Result<_>>()?;

snaps.sort_unstable_by(|sn1, sn2| sn1.cmp(sn2).reverse());
Ok(snaps)
}

Expand All @@ -686,6 +718,15 @@ mod tests {
settings
}

#[test]
fn apply_empty_snapshots() -> Result<()> {
let now = Local::now();
let options = KeepOptions::default().keep_last(10);
let result = options.apply(vec![], now)?;
assert!(result.is_empty());
Ok(())
}

#[rstest]
#[case(KeepOptions::default())]
fn test_apply_fails(
Expand Down Expand Up @@ -718,10 +759,8 @@ mod tests {
#[case(KeepOptions::default().keep_quarter_yearly(10))]
#[case(KeepOptions::default().keep_half_yearly(10))]
#[case(KeepOptions::default().keep_daily(7).keep_weekly(2).keep_monthly(3).keep_yearly(10))]
/*
#[case(KeepOptions::default().keep_tags("foo"))]
#[case(KeepOptions::default().keep_tags("foo,bar"))]
*/
#[case(KeepOptions::default().keep_tags(vec![StringList::from_str("foo")?]))]
#[case(KeepOptions::default().keep_tags(vec![StringList::from_str("foo,bar")?]))]
#[case(KeepOptions::default().keep_within(Duration::from_str("1d").unwrap()))]
#[case(KeepOptions::default().keep_within(Duration::from_str("2d").unwrap()))]
#[case(KeepOptions::default().keep_within(Duration::from_str("7d").unwrap()))]
Expand All @@ -743,6 +782,7 @@ mod tests {
#[case(KeepOptions::default().keep_last(-1).keep_hourly(-1))]
#[case(KeepOptions::default().keep_hourly(-1))]
#[case(KeepOptions::default().keep_daily(3).keep_weekly(2).keep_monthly(-1).keep_yearly(-1))]
#[case(KeepOptions::default().keep_ids(vec!["23ef".to_string()]))]
#[case(KeepOptions::default().keep_none(true))]
fn test_apply(
#[case] options: KeepOptions,
Expand All @@ -751,8 +791,21 @@ mod tests {
) -> Result<()> {
let now = Local::now();
let result = options.apply(test_snapshots?, now)?;

// more readable output format
#[derive(Serialize)]
struct ForgetResult(Vec<(DateTime<Local>, bool, Vec<String>)>);
let result = ForgetResult(
result
.into_iter()
.map(|s| (s.snapshot.time, s.keep, s.reasons))
.collect(),
);

// good naming of snapshots: serialize into json and remove control chars
let mut options = serde_json::to_string(&options)?;
options.retain(|c| !"{}\":".contains(c));

insta_forget_snapshots_redaction.bind(|| {
assert_ron_snapshot!(options, result);
});
Expand Down
Loading
Loading