tests.rs (6993B)
1 use std::clone::Clone; 2 use std::fs::File; 3 use std::io::{SeekFrom, Seek, Read}; 4 use std::str; 5 6 use mediatype::MediaTypeBuf; 7 use chrono::DateTime; 8 use tempfile::NamedTempFile; 9 use tempfile::tempdir; 10 use atom_syndication::Entry as OutEntry; 11 use atom_syndication::Feed as OutFeed; 12 use atom_syndication::Person; 13 use atom_syndication::Text; 14 use quick_xml::Reader as XMLReader; 15 use quick_xml::events::Event as XMLEvent; 16 17 use crate::Sequencer; 18 use crate::io::FeedGet; 19 use crate::meta::FeedMetadata; 20 use crate::Feed; 21 use crate::Entry; 22 use crate::io::fs::FsCache; 23 use crate::mem::MemCache; 24 25 #[cfg(feature = "fs")] 26 use crate::io::fs::FsFeed; 27 28 29 fn check_xml_title(xml: Vec<u8>, title: &str) { 30 let mut rxml = XMLReader::from_str(str::from_utf8(&xml).unwrap()); 31 let mut xmlbuf = Vec::new(); 32 let mut state = 0; 33 loop { 34 match rxml.read_event_into(&mut xmlbuf) { 35 Err(e) => panic!("cant read back xml: {:?}", e), 36 Ok(XMLEvent::Eof) => break, 37 Ok(XMLEvent::Start(v)) => { 38 match v.name().as_ref() { 39 b"title" => { 40 state = 1; 41 }, 42 _ => { 43 state = 0 44 }, 45 } 46 }, 47 Ok(XMLEvent::End(v)) => { 48 state = 0; 49 }, 50 Ok(XMLEvent::Text(v)) => { 51 if state > 0 { 52 assert_eq!(v.unescape().unwrap(), title); 53 } 54 }, 55 _ => (), 56 } 57 } 58 } 59 60 #[test] 61 fn test_entry_guard() { 62 let mut r: bool; 63 let mut seq = Sequencer::new(); 64 let mut src = Entry::default(); 65 let mut s: String; 66 67 src.id = String::from("foo"); 68 s = String::from("inky"); 69 src.title = Text::plain(s); 70 71 src.published = Some(DateTime::parse_from_rfc3339("2024-06-25T20:46:00+02:00").unwrap().into()); 72 r = seq.add(src); 73 assert!(r); 74 75 let mut src_two = Entry::default(); 76 src_two.id = String::from("foo"); 77 s = String::from("pinky"); 78 src_two.title = Text::plain(s); 79 src_two.published = Some(DateTime::parse_from_rfc3339("2024-06-25T20:46:00+02:00").unwrap().into()); 80 r = seq.add(src_two); 81 assert!(!r); 82 83 let mut src_three = Entry::default(); 84 src_three.id = String::from("foo"); 85 s = String::from("blinky"); 86 src_three.title = Text::plain(s); 87 88 src_three.published = Some(DateTime::parse_from_rfc3339("2024-06-25T20:46:00+03:00").unwrap().into()); 89 r = seq.add(src_three); 90 assert!(r); 91 } 92 93 #[test] 94 #[cfg(feature = "fs")] 95 fn test_feed_get() { 96 let r: bool; 97 let fs = FsFeed{}; 98 let feed = fs.get("testdata/test.atom.xml", None).unwrap(); 99 let mut seq = Sequencer::new(); 100 //r = seq.add(feed.entries.get(0).unwrap().clone()); 101 //assert!(r); 102 } 103 104 #[test] 105 #[cfg(feature = "fs")] 106 fn test_feed_all() { 107 let r: i64; 108 let fs = FsFeed{}; 109 let feed = fs.get("testdata/test.atom.xml", None).unwrap(); 110 let mut seq = Sequencer::new(); 111 r = seq.add_from(feed); 112 assert_eq!(r, 16); 113 } 114 115 #[test] 116 #[cfg(feature = "fs")] 117 fn test_feed_mix() { 118 let mut r: i64; 119 let fs = FsFeed{}; 120 let mut feed = fs.get("testdata/test.atom.xml", None).unwrap(); 121 let mut seq = Sequencer::new(); 122 r = seq.add_from(feed); 123 assert_eq!(r, 16); 124 feed = fs.get("testdata/test2.xml", None).unwrap(); 125 r = seq.add_from(feed); 126 assert_eq!(r, 10); 127 assert_eq!(seq.by_ref().count(), 26); 128 assert_eq!(seq.count(), 26); 129 } 130 131 #[test] 132 #[cfg(feature = "fs")] 133 fn test_feed_write() { 134 let r: usize; 135 let fs = FsFeed{}; 136 let f: NamedTempFile; 137 let mut fr: File; 138 139 let feed = fs.get("testdata/test.atom.xml", None).unwrap(); 140 let mut seq = Sequencer::new(); 141 seq.add_from(feed); 142 f = NamedTempFile::new().unwrap(); 143 fr = f.reopen().unwrap(); 144 r = seq.write_to(f).unwrap(); 145 assert_eq!(r, 16); 146 assert_eq!(fr.metadata().unwrap().len(), 520327); 147 } 148 149 #[test] 150 #[cfg(feature = "fs")] 151 fn test_feed_write_extcache() { 152 let r: usize; 153 let fs = FsFeed{}; 154 let f: NamedTempFile; 155 let fr: File; 156 let mut cache: FsCache; 157 158 let d = tempdir().unwrap(); 159 cache = FsCache::new(d.into_path()); 160 161 let feed = fs.get("testdata/test.atom.xml", None).unwrap(); 162 let mut seq = Sequencer::new(); 163 seq = seq.with_cache(&mut cache); 164 165 seq.add_from(feed); 166 f = NamedTempFile::new().unwrap(); 167 fr = f.reopen().unwrap(); 168 r = seq.write_to(f).unwrap(); 169 170 assert_eq!(r, 16); 171 assert_eq!(fr.metadata().unwrap().len(), 520327); 172 } 173 174 #[test] 175 #[cfg(feature = "fs")] 176 fn test_sequence_order() { 177 let mut seq = Sequencer::new(); 178 let mut entry: Entry; 179 let mut s: String; 180 let mut r: Vec<u8>; 181 182 entry = Entry::default(); 183 entry.id = String::from("y"); 184 s = String::from("inky"); 185 entry.title = Text::plain(s); 186 entry.published = Some(DateTime::parse_from_rfc3339("2024-06-25T20:46:00+02:00").unwrap().into()); 187 seq.add(entry); 188 189 190 entry = Entry::default(); 191 entry.id = String::from("b"); 192 s = String::from("pinky"); 193 entry.title = Text::plain(s); 194 entry.published = Some(DateTime::parse_from_rfc3339("2023-06-25T20:46:00+02:00").unwrap().into()); 195 seq.add(entry); 196 197 entry = Entry::default(); 198 entry.id = String::from("d"); 199 s = String::from("blinky"); 200 entry.title = Text::plain(s); 201 entry.published = Some(DateTime::parse_from_rfc3339("2024-06-25T20:46:00+02:00").unwrap().into()); 202 seq.add(entry); 203 204 entry = Entry::default(); 205 entry.id = String::from("a"); 206 s = String::from("clyde"); 207 entry.title = Text::plain(s); 208 entry.published = Some(DateTime::parse_from_rfc3339("2024-06-25T20:46:00+02:00").unwrap().into()); 209 seq.add(entry); 210 211 212 // TODO find value where sort digest is reverse of lexical id 213 r = seq.next().unwrap(); 214 check_xml_title(r, "pinky"); 215 r = seq.next().unwrap(); 216 check_xml_title(r, "blinky"); 217 r = seq.next().unwrap(); 218 check_xml_title(r, "inky"); 219 r = seq.next().unwrap(); 220 check_xml_title(r, "clyde"); 221 } 222 223 #[test] 224 fn test_meta() { 225 let mut o = FeedMetadata::default(); 226 let mut feed = OutFeed::default(); 227 228 match o.apply(&mut feed) { 229 Ok(r) => { 230 panic!("metadata should not be ready"); 231 }, 232 Err(e) => {}, 233 }; 234 235 o.set_title(String::from("foo")); 236 match o.apply(&mut feed) { 237 Ok(r) => { 238 panic!("metadata should not be ready"); 239 }, 240 Err(e) => {}, 241 }; 242 243 o.set_author(Person{ 244 name: String::from("Foo Bar"), 245 email: Some("foo@bar.com".to_string()), 246 uri: Some("foo.bar.com".to_string()), 247 } 248 ); 249 o.apply(&mut feed).unwrap(); 250 } 251 252 #[test] 253 fn test_rss() { 254 let fs = FsFeed{}; 255 let mut cache = MemCache::new(); 256 let fs = FsFeed{}; 257 258 let feed = fs.get("testdata/test.rss.xml", None).unwrap(); 259 let mut seq = Sequencer::new(); 260 seq = seq.with_cache(&mut cache); 261 262 seq.add_from(feed); 263 }