From 56b8d4645a2a4ee309a6f1a297a44b9adcd6303b Mon Sep 17 00:00:00 2001 From: Asone Date: Fri, 25 Aug 2023 08:24:33 +0200 Subject: [PATCH] feat(template): adjust tests --- .env.test | 2 +- Cargo.lock | 25 ++++++++++- nostrss-core/Cargo.toml | 1 + nostrss-core/src/fixtures/default.template | 4 +- nostrss-core/src/template/template.rs | 48 +++++++++++++++++++--- 5 files changed, 71 insertions(+), 9 deletions(-) diff --git a/.env.test b/.env.test index 8b3d84a..c47ccd8 100644 --- a/.env.test +++ b/.env.test @@ -13,7 +13,7 @@ NOSTR_BANNER="https://example.com/banner.png" NOSTR_DESCRIPTION="Craig Wright is not satoshi" NOSTR_PICTURE="https://example.com/picture.jpg" -DEFAULT_TEMPLATE="test nostrss template\nFeed: {name}\nUrl: {url}\nTags: {tags}" +DEFAULT_TEMPLATE="test nostrss template\nFeed: {name}\nUrl: {url}\nTags: {tags}\nAuthor: {author}\nPublished: {published}" # Default pow to use for publishing notes DEFAULT_POW_LEVEL=0 diff --git a/Cargo.lock b/Cargo.lock index 4ee1a4a..20c28df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -498,7 +498,10 @@ checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" dependencies = [ "android-tzdata", "iana-time-zone", + "js-sys", "num-traits", + "time", + "wasm-bindgen", "winapi", ] @@ -1000,7 +1003,7 @@ dependencies = [ "cfg-if", "js-sys", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "wasm-bindgen", ] @@ -1405,7 +1408,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" dependencies = [ "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys", ] @@ -1555,6 +1558,7 @@ dependencies = [ "async-trait", "atom_syndication", "bytes", + "chrono", "clap", "dotenv", "env_logger", @@ -2535,6 +2539,17 @@ dependencies = [ "syn 2.0.29", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -2941,6 +2956,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/nostrss-core/Cargo.toml b/nostrss-core/Cargo.toml index c28a4b4..0d93a04 100644 --- a/nostrss-core/Cargo.toml +++ b/nostrss-core/Cargo.toml @@ -34,6 +34,7 @@ url = "2.4.0" tempfile = "3.6.0" mockall = "0.11.4" mime = "0.3.17" +chrono = "0.4.26" [dependencies.nostrss_grpc] path = "../nostrss-grpc" diff --git a/nostrss-core/src/fixtures/default.template b/nostrss-core/src/fixtures/default.template index 368c3d9..bbd5801 100644 --- a/nostrss-core/src/fixtures/default.template +++ b/nostrss-core/src/fixtures/default.template @@ -1,4 +1,6 @@ Default nostrss template file Feed: {name} Url: {url} -Tags: {tags} \ No newline at end of file +Tags: {tags} +Author: {author} +Published: {published} \ No newline at end of file diff --git a/nostrss-core/src/template/template.rs b/nostrss-core/src/template/template.rs index 5bac6c4..949d332 100644 --- a/nostrss-core/src/template/template.rs +++ b/nostrss-core/src/template/template.rs @@ -113,14 +113,23 @@ mod tests { extern crate mime; use super::*; + use chrono::{DateTime, NaiveDateTime, Utc}; use dotenv::from_filename; - use feed_rs::model::{Content, Link, Text}; + use feed_rs::model::{Content, Link, Person, Text}; #[test] fn test_default_template_fallback() { from_filename(".env.test").ok(); + let published_datetime = NaiveDateTime::new( + chrono::NaiveDate::from_ymd(2009, 1, 3), + chrono::NaiveTime::from_hms(18, 15, 05), + ); + + // Convert the NaiveDateTime to a DateTime in the UTC time zone + let utc_published: DateTime = DateTime::from_utc(published_datetime, Utc); + let entry = Entry { content: Some(Content { body: Some("Test".to_string()), @@ -135,6 +144,13 @@ mod tests { length: None, }] .to_vec(), + authors: [Person { + name: "Satoshi Nakamoto".to_string(), + uri: None, + email: None, + }] + .to_vec(), + published: Some(utc_published), ..Default::default() }; @@ -148,7 +164,7 @@ mod tests { assert_eq!(result.is_ok(), true); let expected = - "test nostrss template\nFeed: Generic feed\nUrl: https://www.nostr.info\nTags: " + "test nostrss template\nFeed: Generic feed\nUrl: https://www.nostr.info\nTags: \nAuthor: Satoshi Nakamoto\nPublished: 2009-01-03 18:15:05 UTC" .to_string(); let result = result.unwrap(); @@ -159,6 +175,14 @@ mod tests { fn test_custom_template() { from_filename(".env.test").ok(); + let published_datetime = NaiveDateTime::new( + chrono::NaiveDate::from_ymd(2009, 1, 3), + chrono::NaiveTime::from_hms(18, 15, 05), + ); + + // Convert the NaiveDateTime to a DateTime in the UTC time zone + let utc_published: DateTime = DateTime::from_utc(published_datetime, Utc); + let entry = Entry { title: Some(Text { content_type: mime::TEXT_PLAIN, @@ -169,6 +193,13 @@ mod tests { body: Some("Test body".to_string()), ..Default::default() }), + authors: [Person { + name: "Satoshi Nakamoto".to_string(), + uri: None, + email: None, + }] + .to_vec(), + published: Some(utc_published), links: [Link { href: "https://www.nostr.info".to_string(), rel: None, @@ -193,7 +224,7 @@ mod tests { assert_eq!(result.is_ok(), true); let result = result.unwrap(); - let expected = "Default nostrss template file\nFeed: Generic feed\nUrl: https://www.nostr.info\nTags: #Test #nostrss".to_string(); + let expected = "Default nostrss template file\nFeed: Generic feed\nUrl: https://www.nostr.info\nTags: #Test #nostrss\nAuthor: Satoshi Nakamoto\nPublished: 2009-01-03 18:15:05 UTC".to_string(); assert_eq!(result, expected); } @@ -211,6 +242,13 @@ mod tests { body: Some("Test body".to_string()), ..Default::default() }), + authors: [Person { + name: "Satoshi Nakamoto".to_string(), + uri: None, + email: None, + }] + .to_vec(), + published: None, links: [Link { href: "https://www.nostr.info".to_string(), rel: None, @@ -237,7 +275,7 @@ mod tests { let result = result.unwrap(); let expected = - "Default nostrss template file\nFeed: {name}\nUrl: {url}\nTags: {tags}".to_string(); + "Default nostrss template file\nFeed: {name}\nUrl: {url}\nTags: {tags}\nAuthor: {author}\nPublished: {published}".to_string(); assert_eq!(result, expected); let bad_path = "./src/fixture/nonexistant.template".to_string(); @@ -249,7 +287,7 @@ mod tests { assert_eq!(result.is_ok(), true); let result = result.unwrap(); - let expected = "test nostrss template\nFeed: {name}\nUrl: {url}\nTags: {tags}".to_string(); + let expected = "test nostrss template\nFeed: {name}\nUrl: {url}\nTags: {tags}\nAuthor: {author}\nPublished: {published}".to_string(); assert_eq!(result, expected); } }