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