1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::sync::atomic::{AtomicI32, Ordering};
use teloxide::types::*;
use super::{MockMessageText, MockUser};
use crate::proc_macros::Changeable;
#[derive(Changeable, Clone)]
pub struct MockCallbackQuery {
pub id: CallbackQueryId,
pub from: User,
pub message: Option<Message>,
pub inline_message_id: Option<String>,
pub chat_instance: String,
pub data: Option<String>,
pub game_short_name: Option<String>,
make_message_inaccessible: bool,
}
impl MockCallbackQuery {
pub const ID: &'static str = "id";
pub const CHAT_INSTANCE: &'static str = "chat_instance";
/// Creates a new easily changable callback query builder
///
/// # Examples
/// ```
/// use teloxide::types::CallbackQueryId;
/// let callback_query = teremock::MockCallbackQuery::new()
/// .id(CallbackQueryId::from("id"))
/// .build();
/// assert_eq!(callback_query.id, CallbackQueryId::from("id"));
/// ```
///
pub fn new() -> Self {
Self {
id: Self::ID.into(),
from: MockUser::new().build(),
message: Some(
MockMessageText::new()
.text("This is the callback message")
.build(),
),
inline_message_id: None,
chat_instance: Self::CHAT_INSTANCE.to_string(),
data: None,
game_short_name: None,
make_message_inaccessible: false,
}
}
/// Converts the message from MaybeInaccessibleMessage::Regular to MaybeInaccessibleMessage::Inaccessible in the final build
///
/// # Example
/// ```rust
/// use teremock::{MockCallbackQuery, MockMessageText};
///
/// let message_in_query = MockMessageText::new().build();
/// let callback_query = MockCallbackQuery::new()
/// .message(message_in_query.clone())
/// .make_message_inaccessible()
/// .build();
///
/// match callback_query.message.unwrap() {
/// teloxide::types::MaybeInaccessibleMessage::Inaccessible(msg) => assert_eq!(msg.message_id, message_in_query.id),
/// teloxide::types::MaybeInaccessibleMessage::Regular(msg) => panic!("Message should be inaccessible"),
/// }
/// ```
pub fn make_message_inaccessible(mut self) -> Self {
self.make_message_inaccessible = true;
self
}
/// Builds the callback query
///
/// # Example
/// ```
/// let mock_callback_query = teremock::MockCallbackQuery::new();
/// let callback_query = mock_callback_query.build();
/// assert_eq!(
/// callback_query.id,
/// teremock::MockCallbackQuery::ID.into()
/// ); // ID is a default value
/// ```
///
pub fn build(self) -> CallbackQuery {
CallbackQuery {
id: self.id,
from: self.from,
message: self.message.map(|message| {
if !self.make_message_inaccessible {
MaybeInaccessibleMessage::Regular(Box::new(message))
} else {
MaybeInaccessibleMessage::Inaccessible(InaccessibleMessage {
chat: message.chat,
message_id: message.id,
})
}
}),
inline_message_id: self.inline_message_id,
chat_instance: self.chat_instance,
data: self.data,
game_short_name: self.game_short_name,
}
}
}
impl crate::dataset::IntoUpdate for MockCallbackQuery {
/// Converts the MockCallbackQuery into an updates vector
///
/// # Example
/// ```
/// use teremock::IntoUpdate;
/// use teloxide::types::{UpdateId, UpdateKind::CallbackQuery};
/// use std::sync::atomic::AtomicI32;
///
/// let mock_callback_query = teremock::MockCallbackQuery::new();
/// let update = mock_callback_query.clone().into_update(&AtomicI32::new(42))[0].clone();
///
/// assert_eq!(update.id, UpdateId(42));
/// assert_eq!(update.kind, CallbackQuery(mock_callback_query.build()));
/// ```
///
fn into_update(self, id: &AtomicI32) -> Vec<Update> {
vec![Update {
id: UpdateId(id.fetch_add(1, Ordering::Relaxed) as u32),
kind: UpdateKind::CallbackQuery(self.build()),
}]
}
}
// From implementation for ergonomic API - allows passing mock builders directly without .build()
impl From<MockCallbackQuery> for CallbackQuery {
fn from(mock: MockCallbackQuery) -> Self {
mock.build()
}
}
// Add more queries here like ShippingQuery, PreCheckoutQuery etc.