Testing for Event parsing

This commit is contained in:
Daniel Sockwell 2020-04-29 16:05:57 -04:00
parent d0f9d80674
commit 925fc40435
24 changed files with 420 additions and 160 deletions

View File

@ -35,3 +35,6 @@ pub enum CheckedEvent {
AnnouncementDelete { payload: String },
Conversation { payload: Conversation, queued_at: Option<i64> },
}
#[cfg(test)]
mod test;

View File

@ -6,45 +6,45 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub(super) struct Account {
pub id: Id,
username: String,
pub(super) username: String,
pub acct: String,
url: String,
display_name: String,
note: String,
avatar: String,
avatar_static: String,
header: String,
header_static: String,
locked: bool,
emojis: Vec<Emoji>,
discoverable: Option<bool>, // Shouldn't be option?
created_at: String,
statuses_count: i64,
followers_count: i64,
following_count: i64,
moved: Option<String>,
fields: Option<Vec<Field>>,
bot: Option<bool>,
source: Option<Source>,
group: Option<bool>, // undocumented
last_status_at: Option<String>, // undocumented
pub(super) url: String,
pub(super) display_name: String,
pub(super) note: String,
pub(super) avatar: String,
pub(super) avatar_static: String,
pub(super) header: String,
pub(super) header_static: String,
pub(super) locked: bool,
pub(super) emojis: Vec<Emoji>,
pub(super) discoverable: Option<bool>, // Shouldn't be option?
pub(super) created_at: String,
pub(super) statuses_count: i64,
pub(super) followers_count: i64,
pub(super) following_count: i64,
pub(super) moved: Option<String>,
pub(super) fields: Option<Vec<Field>>,
pub(super) bot: Option<bool>,
pub(super) source: Option<Source>,
pub(super) group: Option<bool>, // undocumented
pub(super) last_status_at: Option<String>, // undocumented
}
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
struct Field {
name: String,
value: String,
verified_at: Option<String>,
pub(super) struct Field {
pub(super) name: String,
pub(super) value: String,
pub(super) verified_at: Option<String>,
}
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
struct Source {
note: String,
fields: Vec<Field>,
privacy: Option<Visibility>,
sensitive: bool,
language: String,
follow_requests_count: i64,
pub(super) struct Source {
pub(super) note: String,
pub(super) fields: Vec<Field>,
pub(super) privacy: Option<Visibility>,
pub(super) sensitive: bool,
pub(super) language: String,
pub(super) follow_requests_count: i64,
}

View File

@ -22,37 +22,37 @@ use std::string::String;
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Status {
id: Id,
uri: String,
created_at: String,
account: Account,
content: String,
visibility: Visibility,
sensitive: bool,
spoiler_text: String,
media_attachments: Vec<Attachment>,
application: Option<Application>, // Should be non-optional?
mentions: Vec<Mention>,
tags: Vec<Tag>,
emojis: Vec<Emoji>,
reblogs_count: i64,
favourites_count: i64,
replies_count: i64,
url: Option<String>,
in_reply_to_id: Option<Id>,
in_reply_to_account_id: Option<Id>,
reblog: Option<Box<Status>>,
poll: Option<Poll>,
card: Option<Card>,
pub(super) id: Id,
pub(super) uri: String,
pub(super) created_at: String,
pub(super) account: Account,
pub(super) content: String,
pub(super) visibility: Visibility,
pub(super) sensitive: bool,
pub(super) spoiler_text: String,
pub(super) media_attachments: Vec<Attachment>,
pub(super) application: Option<Application>, // Should be non-optional?
pub(super) mentions: Vec<Mention>,
pub(super) tags: Vec<Tag>,
pub(super) emojis: Vec<Emoji>,
pub(super) reblogs_count: i64,
pub(super) favourites_count: i64,
pub(super) replies_count: i64,
pub(super) url: Option<String>,
pub(super) in_reply_to_id: Option<Id>,
pub(super) in_reply_to_account_id: Option<Id>,
pub(super) reblog: Option<Box<Status>>,
pub(super) poll: Option<Poll>,
pub(super) card: Option<Card>,
pub(crate) language: Option<String>,
text: Option<String>,
pub(super) text: Option<String>,
// ↓↓↓ Only for authorized users
favourited: Option<bool>,
reblogged: Option<bool>,
muted: Option<bool>,
bookmarked: Option<bool>,
pinned: Option<bool>,
pub(super) favourited: Option<bool>,
pub(super) reblogged: Option<bool>,
pub(super) muted: Option<bool>,
pub(super) bookmarked: Option<bool>,
pub(super) pinned: Option<bool>,
}
impl Payload for Status {

View File

@ -2,10 +2,10 @@ use serde::{Deserialize, Serialize};
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub(super) struct Application {
name: String,
website: Option<String>,
vapid_key: Option<String>,
client_id: Option<String>,
client_secret: Option<String>,
pub(in super::super) struct Application {
pub(super) name: String,
pub(super) website: Option<String>,
pub(super) vapid_key: Option<String>,
pub(super) client_id: Option<String>,
pub(super) client_secret: Option<String>,
}

View File

@ -2,21 +2,21 @@ use serde::{Deserialize, Serialize};
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub(super) struct Attachment {
id: String,
r#type: AttachmentType,
url: String,
preview_url: String,
remote_url: Option<String>,
text_url: Option<String>,
meta: Option<serde_json::Value>,
description: Option<String>,
blurhash: Option<String>,
pub(in super::super) struct Attachment {
pub(super) id: String,
pub(super) r#type: AttachmentType,
pub(super) url: String,
pub(super) preview_url: String,
pub(super) remote_url: Option<String>,
pub(super) text_url: Option<String>,
pub(super) meta: Option<serde_json::Value>,
pub(super) description: Option<String>,
pub(super) blurhash: Option<String>,
}
#[serde(rename_all = "lowercase", deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
enum AttachmentType {
pub(super) enum AttachmentType {
Unknown,
Image,
Gifv,

View File

@ -2,25 +2,25 @@ use serde::{Deserialize, Serialize};
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub(super) struct Card {
url: String,
title: String,
description: String,
r#type: CardType,
author_name: Option<String>,
author_url: Option<String>,
provider_name: Option<String>,
provider_url: Option<String>,
html: Option<String>,
width: Option<i64>,
height: Option<i64>,
image: Option<String>,
embed_url: Option<String>,
pub(in super::super) struct Card {
pub(super) url: String,
pub(super) title: String,
pub(super) description: String,
pub(super) r#type: CardType,
pub(super) author_name: Option<String>,
pub(super) author_url: Option<String>,
pub(super) provider_name: Option<String>,
pub(super) provider_url: Option<String>,
pub(super) html: Option<String>,
pub(super) width: Option<i64>,
pub(super) height: Option<i64>,
pub(super) image: Option<String>,
pub(super) embed_url: Option<String>,
}
#[serde(rename_all = "lowercase", deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
enum CardType {
pub(super) enum CardType {
Link,
Photo,
Video,

View File

@ -3,22 +3,22 @@ use serde::{Deserialize, Serialize};
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub(super) struct Poll {
id: String,
expires_at: String,
expired: bool,
multiple: bool,
votes_count: i64,
voters_count: Option<i64>,
voted: Option<bool>,
own_votes: Option<Vec<i64>>,
options: Vec<PollOptions>,
emojis: Vec<Emoji>,
pub(in super::super) struct Poll {
pub(super) id: String,
pub(super) expires_at: String,
pub(super) expired: bool,
pub(super) multiple: bool,
pub(super) votes_count: i64,
pub(super) voters_count: Option<i64>,
pub(super) voted: Option<bool>,
pub(super) own_votes: Option<Vec<i64>>,
pub(super) options: Vec<PollOptions>,
pub(super) emojis: Vec<Emoji>,
}
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
struct PollOptions {
title: String,
votes_count: Option<i32>,
pub(super) struct PollOptions {
pub(super) title: String,
pub(super) votes_count: Option<i32>,
}

View File

@ -3,15 +3,15 @@ use serde::{Deserialize, Serialize};
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub(super) struct Tag {
name: String,
url: String,
history: Option<Vec<History>>,
pub(super) name: String,
pub(super) url: String,
pub(super) history: Option<Vec<History>>,
}
#[serde(deny_unknown_fields)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
struct History {
day: String,
uses: String,
accounts: String,
pub(super) struct History {
pub(super) day: String,
pub(super) uses: String,
pub(super) accounts: String,
}

View File

@ -0,0 +1,41 @@
use super::{super::*, *};
use checked_event::{
account::{Account, Field},
tag::Tag,
visibility::Visibility::*,
CheckedEvent::*,
*,
};
use std::fs;
#[test]
fn parse_redis_msg_to_event() -> Result<(), Box<dyn std::error::Error>> {
let mut test_num = 1;
let output = vec![
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test_data/event_001.rs"
)),
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test_data/event_002.rs"
)),
];
while let (Ok(input), Some(output)) = (
fs::read_to_string(format!("test_data/msg.event_txt_{:03}.txt", test_num)),
output.get(test_num - 1),
) {
println!("parsing `{:03}.resp`", test_num);
test_num += 1;
let event = Event::try_from(input)?;
println!("{:#?}", event);
assert_eq!(&event, output);
}
assert!(test_num > 1);
Ok(())
}

View File

@ -58,13 +58,10 @@ fn parse_redis_msg() -> Result<(), RedisParseErr> {
#[test]
fn parse_long_redis_msg() -> Result<(), Box<dyn std::error::Error>> {
let pwd = path::Path::new(file!()).parent().expect("TEST");
let mut test_num = 1;
while let (Ok(input), Ok(output)) = (
fs::read_to_string(format!("{}/test_input/{:03}.resp", pwd.display(), test_num)),
fs::read_to_string(format!("{}/test_output/{:03}.txt", pwd.display(), test_num)),
fs::read_to_string(format!("test_data/redis_input_{:03}.resp", test_num)),
fs::read_to_string(format!("test_data/msg.event_txt_{:03}.txt", test_num)),
) {
println!("parsing `{:03}.resp`", test_num);
test_num += 1;
@ -82,6 +79,7 @@ fn parse_long_redis_msg() -> Result<(), Box<dyn std::error::Error>> {
assert_eq!(r_msg.timeline_txt, "timeline:public");
}
assert!(test_num > 1);
Ok(())
}

118
test_data/event_001.rs Normal file
View File

@ -0,0 +1,118 @@
Event::TypeSafe(
Update {
payload: Status {
id: Id(102775370117886890),
uri: "https://mastodon.host/users/federationbot/statuses/102775346916917099".to_string(),
created_at: "2019-09-11T18:42:19.000Z".to_string(),
account: Account {
id: Id(78),
username: "federationbot".to_string(),
acct: "federationbot@mastodon.host".to_string(),
url: "https://mastodon.host/@federationbot".to_string(),
display_name: "Federation Bot".to_string(),
note: "<p>Hello, I am mastodon.host official semi bot.</p><p>Follow me if you want to have some updates on the view \
of the fediverse from here ( I only post unlisted ). </p><p>I also randomly boost one of my followers toot \
every hour !</p><p>If you don\'t feel confortable with me following you, tell me: unfollow and I\'ll do it \
:)</p><p>If you want me to follow you, just tell me follow ! </p><p>If you want automatic follow for new \
users on your instance and you are an instance admin, contact me !</p>\
<p>Other commands are private :)</p>".to_string(),
avatar: "https://instance.codesections.com/system/accounts/avatars/000/000/078/original/d9e2be5398629cf8.jpeg\
?1568127863".to_string(),
avatar_static: "https://instance.codesections.com/system/accounts/avatars/000/000/078/original/d9e2be5398629cf8\
.jpeg?1568127863".to_string(),
header: "https://instance.codesections.com/headers/original/missing.png".to_string(),
header_static: "https://instance.codesections.com/headers/original/missing.png".to_string(),
locked:
false,
emojis: [].to_vec(),
discoverable: None,
created_at: "2019-09-10T15:04:25.559Z".to_string(),
statuses_count: 50554,
followers_count: 16636,
following_count: 179532,
moved: None,
fields: Some(
[ Field {
name: "More stats".to_string(),
value: "<a href=\"https://mastodon.host/stats.html\" rel=\"nofollow noopener\" target=\"_blank\">\
<span class=\"invisible\">https://</span><span class=\"\">mastodon.host/stats.html</span>\
<span class=\"invisible\"></span></a>".to_string(),
verified_at: None
},
Field {
name: "More infos".to_string(),
value: "<a href=\"https://mastodon.host/about/more\" rel=\"nofollow noopener\" target=\"_blank\">\
<span class=\"invisible\">https://</span><span class=\"\">mastodon.host/about/more</span>\
<span class=\"invisible\"></span></a>".to_string(),
verified_at: None
},
Field {
name: "Owner/Friend".to_string(),
value: "<span class=\"h-card\"><a href=\"https://mastodon.host/@gled\" class=\"u-url mention\" \
rel=\"nofollow noopener\" target=\"_blank\">@<span>gled</span></a></span>".to_string(),
verified_at: None
}
].to_vec()
),
bot: Some(false),
source: None,
group: None,
last_status_at: None
},
content: "<p>Trending tags:<br><a href=\"https://mastodon.host/tags/neverforget\" class=\"mention hashtag\" \
rel=\"nofollow noopener\" target=\"_blank\">#<span>neverforget</span></a><br>\
<a href=\"https://mastodon.host/tags/4styles\" class=\"mention hashtag\" rel=\"nofollow noopener\" \
target=\"_blank\">#<span>4styles</span></a><br><a href=\"https://mastodon.host/tags/newpipe\" \
class=\"mention hashtag\" rel=\"nofollow noopener\" target=\"_blank\">#<span>newpipe</span></a>\
<br><a href=\"https://mastodon.host/tags/uber\" class=\"mention hashtag\" rel=\"nofollow noopener\" \
target=\"_blank\">#<span>uber</span></a><br><a href=\"https://mastodon.host/tags/mercredifiction\" \
class=\"mention hashtag\" rel=\"nofollow noopener\" target=\"_blank\">#<span>mercredifiction</span>\
</a></p>".to_string(),
visibility: Unlisted,
sensitive: false,
spoiler_text: "".to_string(),
media_attachments: [].to_vec(),
application: None,
mentions: [].to_vec(),
tags: [
Tag {
name: "4styles".to_string(),
url: "https://instance.codesections.com/tags/4styles".to_string(),
history: None
},
Tag { name: "neverforget".to_string(),
url: "https://instance.codesections.com/tags/neverforget".to_string(),
history: None
},
Tag { name: "mercredifiction".to_string(),
url: "https://instance.codesections.com/tags/mercredifiction".to_string(),
history: None
},
Tag { name: "uber".to_string(),
url: "https://instance.codesections.com/tags/uber".to_string(),
history: None
},
Tag { name: "newpipe".to_string(),
url: "https://instance.codesections.com/tags/newpipe".to_string(),
history: None
}
].to_vec(),
emojis: [].to_vec(),
reblogs_count: 0,
favourites_count: 0,
replies_count: 0,
url: Some("https://mastodon.host/@federationbot/102775346916917099".to_string()),
in_reply_to_id: None,
in_reply_to_account_id: None,
reblog: None,
poll: None,
card: None,
language: Some("en".to_string()),
text: None,
favourited: Some(false),
reblogged: Some(false),
muted: Some(false),
bookmarked: None,
pinned: None
},
queued_at: Some(1568227693541) })

100
test_data/event_002.rs Normal file
View File

@ -0,0 +1,100 @@
Event::TypeSafe(
Update {
payload: Status {
id: Id(
104072549781970698,
),
uri: "https://newsbots.eu/users/aljazeera_english/statuses/104072549673971025".to_string(),
created_at: "2020-04-27T20:58:02.000Z".to_string(),
account: Account {
id: Id(
1852,
),
username: "aljazeera_english".to_string(),
acct: "aljazeera_english@newsbots.eu".to_string(),
url: "https://newsbots.eu/@aljazeera_english".to_string(),
display_name: "Al Jazeera English".to_string(),
note: "<p>Breaking news and ongoing coverage from around the world.</p><p>• unofficial •</p>".to_string(),
avatar: "https://instance.codesections.com/system/accounts/avatars/000/001/852/original/6377f39416193690.jpeg?1584742113".to_string(),
avatar_static: "https://instance.codesections.com/system/accounts/avatars/000/001/852/original/6377f39416193690.jpeg?1584742113".to_string(),
header: "https://instance.codesections.com/system/accounts/headers/000/001/852/original/f582c7deb0ec14ac.jpeg?1584742114".to_string(),
header_static: "https://instance.codesections.com/system/accounts/headers/000/001/852/original/f582c7deb0ec14ac.jpeg?1584742114".to_string(),
locked: false,
emojis: [].to_vec(),
discoverable: Some(
true,
),
created_at: "2020-03-20T22:08:35.417Z".to_string(),
statuses_count: 26018,
followers_count: 915,
following_count: 1,
moved: None,
fields: Some(
[
Field {
name: "📍".to_string(),
value: "Doha, Qatar".to_string(),
verified_at: None,
},
Field {
name: "🔗".to_string(),
value: "<a href=\"https://www.aljazeera.com\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://www.</span><span class=\"\">aljazeera.com</span><span class=\"invisible\"></span></a>".to_string(),
verified_at: None,
},
].to_vec(),
),
bot: Some(
true,
),
source: None,
group: Some(
false,
),
last_status_at: Some(
"2020-04-27".to_string(),
),
},
content: "<p>**Far-right governor defies Rome, lifts Venice lockdown early**</p><p>\"Veneto\'s governor, Luca Zaia, of the far-rght League party, says keeping restrictions in place risks \'social conflict\'.\"</p><p><a href=\"https://www.aljazeera.com/news/2020/04/governor-defies-rome-lifts-venice-lockdown-early-200427171844336.html\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://www.</span><span class=\"ellipsis\">aljazeera.com/news/2020/04/gov</span><span class=\"invisible\">ernor-defies-rome-lifts-venice-lockdown-early-200427171844336.html</span></a></p><p><a href=\"https://newsbots.eu/tags/news\" class=\"mention hashtag\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">#<span>news</span></a> <a href=\"https://newsbots.eu/tags/bot\" class=\"mention hashtag\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">#<span>bot</span></a></p>".to_string(),
visibility: Public,
sensitive: false,
spoiler_text: "".to_string(),
media_attachments: [].to_vec(),
application: None,
mentions: [].to_vec(),
tags: [
Tag {
name: "news".to_string(),
url: "https://instance.codesections.com/tags/news".to_string(),
history: None,
},
Tag {
name: "bot".to_string(),
url: "https://instance.codesections.com/tags/bot".to_string(),
history: None,
},
].to_vec(),
emojis: [].to_vec(),
reblogs_count: 0,
favourites_count: 0,
replies_count: 0,
url: Some(
"https://newsbots.eu/@aljazeera_english/104072549673971025".to_string(),
),
in_reply_to_id: None,
in_reply_to_account_id: None,
reblog: None,
poll: None,
card: None,
language: Some(
"en".to_string(),
),
text: None,
favourited: None,
reblogged: None,
muted: None,
bookmarked: None,
pinned: None,
},
queued_at: None,
},
)