Add giveaway* tests

This commit is contained in:
Andrey Brusnik 2024-07-17 19:15:49 +04:00
parent fda1890425
commit dc3660b308
No known key found for this signature in database
GPG key ID: D33232F28CFF442C
5 changed files with 416 additions and 0 deletions

View file

@ -43,3 +43,28 @@ pub struct Giveaway {
/// giveaway will be active for /// giveaway will be active for
pub premium_subscription_month_count: Option<u8>, pub premium_subscription_month_count: Option<u8>,
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize() {
let data = r#"
{
"chats": [
{
"id": -1002236736395,
"title": "Test",
"type": "channel"
}
],
"winners_selection_date": 1721162701,
"winner_count": 1,
"has_public_winners": true,
"premium_subscription_month_count": 6
}
"#;
serde_json::from_str::<Giveaway>(data).unwrap();
}
}

View file

@ -16,3 +16,46 @@ pub struct GiveawayCompleted {
/// Message with the giveaway that was completed, if it wasn't deleted /// Message with the giveaway that was completed, if it wasn't deleted
pub giveaway_message: Option<Box<Message>>, pub giveaway_message: Option<Box<Message>>,
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize() {
let data = r#"
{
"winner_count": 0,
"unclaimed_prize_count": 1,
"giveaway_message": {
"message_id": 24,
"sender_chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"date": 1721161230,
"giveaway": {
"chats": [
{
"id": -1002236736395,
"title": "Test",
"type": "channel"
}
],
"winners_selection_date": 1721162701,
"winner_count": 1,
"has_public_winners": true,
"premium_subscription_month_count": 6
}
}
}
"#;
serde_json::from_str::<GiveawayCompleted>(data).unwrap();
}
}

View file

@ -5,3 +5,14 @@ use serde::{Deserialize, Serialize};
#[serde_with::skip_serializing_none] #[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct GiveawayCreated {} pub struct GiveawayCreated {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize() {
let data = r#"{}"#;
serde_json::from_str::<GiveawayCreated>(data).unwrap();
}
}

View file

@ -53,3 +53,34 @@ pub struct GiveawayWinners {
/// Description of additional giveaway prize /// Description of additional giveaway prize
pub prize_description: Option<String>, pub prize_description: Option<String>,
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize() {
let data = r#"
{
"chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"giveaway_message_id": 27,
"winners_selection_date": 1721162701,
"premium_subscription_month_count": 6,
"winner_count": 1,
"winners": [
{
"id": 1459074222,
"is_bot": false,
"first_name": "shadowchain",
"username": "shdwchn10"
}
]
}
"#;
serde_json::from_str::<GiveawayWinners>(data).unwrap();
}
}

View file

@ -1758,6 +1758,7 @@ impl Message {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use chrono::DateTime;
use cool_asserts::assert_matches; use cool_asserts::assert_matches;
use serde_json::from_str; use serde_json::from_str;
@ -2320,4 +2321,309 @@ mod tests {
let _: Message = serde_json::from_str(json).unwrap(); let _: Message = serde_json::from_str(json).unwrap();
} }
#[test]
fn giveaway() {
let json = r#"{
"message_id": 27,
"sender_chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"date": 1721162577,
"giveaway": {
"chats": [
{
"id": -1002236736395,
"title": "Test",
"type": "channel"
}
],
"winners_selection_date": 1721162701,
"winner_count": 1,
"has_public_winners": true,
"premium_subscription_month_count": 6
}
}"#;
let message: Message = from_str(json).unwrap();
assert_eq!(
message.giveaway().unwrap(),
&Giveaway {
chats: vec![Chat {
id: ChatId(-1002236736395),
kind: ChatKind::Public(ChatPublic {
title: Some("Test".to_owned()),
kind: PublicChatKind::Channel(PublicChatChannel {
username: None,
linked_chat_id: None
}),
description: None,
invite_link: None,
has_protected_content: None
}),
photo: None,
pinned_message: None,
message_auto_delete_time: None,
has_hidden_members: false,
has_aggressive_anti_spam_enabled: false,
chat_full_info: ChatFullInfo { emoji_status_expiration_date: None }
}],
winners_selection_date: DateTime::from_timestamp(1721162701, 0).unwrap(),
winner_count: 1,
only_new_members: false,
has_public_winners: true,
prize_description: None,
country_codes: None,
premium_subscription_month_count: Some(6)
}
)
}
#[test]
fn giveaway_created() {
let json = r#"{
"message_id": 27,
"sender_chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"date": 1721162577,
"giveaway_created": {}
}"#;
let message: Message = from_str(json).unwrap();
assert_eq!(message.giveaway_created().unwrap(), &GiveawayCreated {})
}
#[test]
fn giveaway_completed() {
let json = r#"{
"message_id": 27,
"sender_chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"date": 1721162577,
"giveaway_completed": {
"winner_count": 0,
"unclaimed_prize_count": 1,
"giveaway_message": {
"message_id": 24,
"sender_chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"date": 1721161230,
"giveaway": {
"chats": [
{
"id": -1002236736395,
"title": "Test",
"type": "channel"
}
],
"winners_selection_date": 1721162701,
"winner_count": 1,
"has_public_winners": true,
"premium_subscription_month_count": 6
}
}
}
}"#;
let message: Message = from_str(json).unwrap();
assert_eq!(
message.giveaway_completed().unwrap(),
&GiveawayCompleted {
winner_count: 0,
unclaimed_prize_count: Some(1),
giveaway_message: Some(Box::new(Message {
id: MessageId(24),
thread_id: None,
date: DateTime::from_timestamp(1721161230, 0).unwrap(),
chat: Chat {
id: ChatId(-1002236736395),
kind: ChatKind::Public(ChatPublic {
title: Some("Test".to_owned()),
kind: PublicChatKind::Channel(PublicChatChannel {
username: None,
linked_chat_id: None
}),
description: None,
invite_link: None,
has_protected_content: None
}),
photo: None,
pinned_message: None,
message_auto_delete_time: None,
has_hidden_members: false,
has_aggressive_anti_spam_enabled: false,
chat_full_info: ChatFullInfo { emoji_status_expiration_date: None }
},
via_bot: None,
kind: MessageKind::Giveaway(MessageGiveaway {
giveaway: Giveaway {
chats: vec![Chat {
id: ChatId(-1002236736395),
kind: ChatKind::Public(ChatPublic {
title: Some("Test".to_owned()),
kind: PublicChatKind::Channel(PublicChatChannel {
username: None,
linked_chat_id: None
}),
description: None,
invite_link: None,
has_protected_content: None
}),
photo: None,
pinned_message: None,
message_auto_delete_time: None,
has_hidden_members: false,
has_aggressive_anti_spam_enabled: false,
chat_full_info: ChatFullInfo { emoji_status_expiration_date: None }
}],
winners_selection_date: DateTime::from_timestamp(1721162701, 0)
.unwrap(),
winner_count: 1,
only_new_members: false,
has_public_winners: true,
prize_description: None,
country_codes: None,
premium_subscription_month_count: Some(6)
}
})
}))
}
)
}
#[test]
fn giveaway_winners() {
let json = r#"{
"message_id": 28,
"sender_chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"date": 1721162702,
"reply_to_message": {
"message_id": 27,
"sender_chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"date": 1721162577,
"giveaway": {
"chats": [
{
"id": -1002236736395,
"title": "Test",
"type": "channel"
}
],
"winners_selection_date": 1721162701,
"winner_count": 1,
"has_public_winners": true,
"premium_subscription_month_count": 6
}
},
"giveaway_winners": {
"chat": {
"id": -1002236736395,
"title": "Test",
"type": "channel"
},
"giveaway_message_id": 27,
"winners_selection_date": 1721162701,
"premium_subscription_month_count": 6,
"winner_count": 1,
"winners": [
{
"id": 1459074222,
"is_bot": false,
"first_name": "shadowchain",
"username": "shdwchn10"
}
]
}
}"#;
let message: Message = from_str(json).unwrap();
assert_eq!(
message.giveaway_winners().expect("Failed to get GiveawayWinners from Message!"),
&GiveawayWinners {
chat: Chat {
id: ChatId(-1002236736395),
kind: ChatKind::Public(ChatPublic {
title: Some("Test".to_owned()),
kind: PublicChatKind::Channel(PublicChatChannel {
username: None,
linked_chat_id: None
}),
description: None,
invite_link: None,
has_protected_content: None
}),
photo: None,
pinned_message: None,
message_auto_delete_time: None,
has_hidden_members: false,
has_aggressive_anti_spam_enabled: false,
chat_full_info: ChatFullInfo { emoji_status_expiration_date: None }
},
giveaway_message_id: MessageId(27),
winners_selection_date: DateTime::from_timestamp(1721162701, 0).unwrap(),
winner_count: 1,
winners: vec![User {
id: UserId(1459074222),
is_bot: false,
first_name: "shadowchain".to_owned(),
last_name: None,
username: Some("shdwchn10".to_owned()),
language_code: None,
is_premium: false,
added_to_attachment_menu: false
}],
additional_chat_count: None,
premium_subscription_month_count: Some(6),
unclaimed_prize_count: None,
only_new_members: false,
was_refunded: false,
prize_description: None
}
)
}
} }