Add more tests for diffing algo

This commit is contained in:
SkyfallWasTaken 2024-07-01 18:24:52 +01:00
parent 01ea8485e8
commit 88107872e9
2 changed files with 116 additions and 1 deletions

View file

@ -72,4 +72,119 @@ mod test {
)
);
}
#[test]
fn description_diff() {
let old = ShopItem {
full_name: "Test".into(),
description: Some("Lorem ipsum".into()),
..Default::default()
};
let new = ShopItem {
full_name: "Test".into(),
description: Some("Dolor sit amet".into()),
..Default::default()
};
assert_eq!(
diff_items(old, new),
Some(
indoc! {"
*Item updated*
*Name:* Test
*Description:* Lorem ipsum Dolor sit amet"}
.into()
)
);
}
#[test]
fn stock_diff_limited_update() {
let old = ShopItem {
full_name: "Test".into(),
stock: Some(10),
..Default::default()
};
let new = ShopItem {
full_name: "Test".into(),
stock: Some(9),
..Default::default()
};
assert_eq!(
diff_items(old, new),
Some(
indoc! {"
*Item updated*
*Name:* Test
*Stock:* 10 9"}
.into()
)
);
}
#[test]
fn stock_diff_limited_to_unlimited() {
let old = ShopItem {
full_name: "Test".into(),
stock: Some(10),
..Default::default()
};
let new = ShopItem {
full_name: "Test".into(),
stock: None,
..Default::default()
};
assert_eq!(
diff_items(old, new),
Some(
indoc! {"
*Item updated*
*Name:* Test
*Stock:* 10 Unlimited"}
.into()
)
);
}
#[test]
fn stock_diff_unlimited_to_limited() {
let old = ShopItem {
full_name: "Test".into(),
stock: None,
..Default::default()
};
let new = ShopItem {
full_name: "Test".into(),
stock: Some(10),
..Default::default()
};
assert_eq!(
diff_items(old, new),
Some(
indoc! {"
*Item updated*
*Name:* Test
*Stock:* Unlimited 10"}
.into()
)
);
}
#[test]
fn equal_items_no_diff() {
let item = ShopItem {
full_name: "Test".into(),
price: 1,
..Default::default()
};
assert_eq!(diff_items(item.clone(), item), None);
}
}

View file

@ -2,7 +2,7 @@ use scraper::{Html, Selector};
use serde::{Deserialize, Serialize};
use worker::*;
#[derive(Serialize, Deserialize, PartialEq, Eq, Default)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Default, Clone)]
pub struct ShopItem {
#[serde(rename = "Full Name")]
pub full_name: String,