teloxide_tests 0.4.0

Test suite for teloxide bots
Documentation
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! A set of mocked structs for testing purposes. Read more in teloxide_tests crate.
use std::sync::atomic::{AtomicI32, Ordering};

use mime::Mime;
use proc_macros::Changeable;
use teloxide::types::{
    ChatPhoto, FileId, FileMeta, FileUniqueId, LinkPreviewOptions, LivePeriod, Location, Me,
    PhotoSize, Seconds, Update, UpdateId, User, UserId, Video,
};
pub mod chat;
pub mod chat_full_info;

pub mod message;
pub mod message_common;
pub mod queries;
pub mod update;
pub use chat::*;
pub use chat_full_info::*;
pub use message::*;
pub use message_common::*;
pub use queries::*;
use teloxide_tests_macros as proc_macros;
pub use update::*;
#[cfg(test)]
mod tests;

pub trait IntoUpdate {
    /// Converts the mocked struct into an update vector, incrementing the id by 1
    fn into_update(self, id: &AtomicI32) -> Vec<Update>;
}

impl<T> IntoUpdate for Vec<T>
where
    T: IntoUpdate,
{
    fn into_update(self, id: &AtomicI32) -> Vec<Update> {
        self.into_iter()
            .map(|u| {
                id.fetch_add(1, Ordering::Relaxed);
                u.into_update(id)
            })
            .flatten()
            .collect()
    }
}

// Just to be able to use raw updates anywhere
impl IntoUpdate for Update {
    fn into_update(mut self, id: &AtomicI32) -> Vec<Update> {
        self.id = UpdateId(id.fetch_add(1, Ordering::Relaxed) as u32);
        vec![self]
    }
}

//
//  Structs below are just misc mocked structs
//

#[derive(Changeable, Clone)]
pub struct MockUser {
    pub id: UserId,
    pub is_bot: bool,
    pub first_name: String,
    pub last_name: Option<String>,
    pub username: Option<String>,
    pub language_code: Option<String>,
    pub added_to_attachment_menu: bool,
    pub is_premium: bool,
}

impl MockUser {
    pub const ID: u64 = 12345678;
    pub const IS_BOT: bool = false;
    pub const FIRST_NAME: &'static str = "First";
    pub const ADDED_TO_ATTACHMENT_MENU: bool = false;
    pub const IS_PREMIUM: bool = false;

    /// Creates a new easily changable user builder
    ///
    /// # Examples
    /// ```
    /// let user = teloxide_tests::MockUser::new()
    ///     .id(12345678)
    ///     .build();
    /// assert_eq!(user.id.0, 12345678);
    /// ```
    ///
    pub fn new() -> Self {
        Self {
            id: UserId(Self::ID),
            is_bot: Self::IS_BOT,
            first_name: Self::FIRST_NAME.to_string(),
            last_name: None,
            username: None,
            language_code: None,
            added_to_attachment_menu: Self::ADDED_TO_ATTACHMENT_MENU,
            is_premium: Self::IS_PREMIUM,
        }
    }

    /// Builds the user
    ///
    /// # Examples
    /// ```
    /// let mock_user = teloxide_tests::MockUser::new();
    /// let user = mock_user.build();
    /// assert_eq!(user.id.0 as u64, teloxide_tests::MockUser::ID);  // ID is a default value
    /// ```
    ///
    pub fn build(self) -> User {
        User {
            id: self.id,
            is_bot: self.is_bot,
            first_name: self.first_name,
            last_name: self.last_name,
            username: self.username,
            language_code: self.language_code,
            added_to_attachment_menu: self.added_to_attachment_menu,
            is_premium: self.is_premium,
        }
    }
}

#[derive(Changeable, Clone)]
pub struct MockMe {
    pub id: UserId,
    pub is_bot: bool,
    pub first_name: String,
    pub last_name: Option<String>,
    pub username: Option<String>,
    pub language_code: Option<String>,
    pub can_join_groups: bool,
    pub can_read_all_group_messages: bool,
    pub supports_inline_queries: bool,
    pub can_connect_to_business: bool,
    pub has_main_web_app: bool,
}

impl MockMe {
    pub const ID: u64 = 123456;
    pub const IS_BOT: bool = true;
    pub const FIRST_NAME: &'static str = "Test";
    pub const LAST_NAME: &'static str = "Bot";
    pub const USERNAME: &'static str = "test_bot";
    pub const LANGUAGE_CODE: &'static str = "en";
    pub const CAN_JOIN_GROUPS: bool = false;
    pub const CAN_READ_ALL_GROUP_MESSAGES: bool = false;
    pub const SUPPORTS_INLINE_QUERIES: bool = false;
    pub const CAN_CONNECT_TO_BUSINESS: bool = false;
    pub const HAS_MAIN_WEB_APP: bool = false;

    /// Creates a new easily changable me builder
    ///
    /// # Examples
    /// ```
    /// let me = teloxide_tests::MockMe::new()
    ///     .first_name("Test")
    ///     .build();
    /// assert_eq!(me.first_name, "Test");
    /// ```
    ///
    pub fn new() -> Self {
        Self {
            id: UserId(Self::ID),
            is_bot: Self::IS_BOT,
            first_name: Self::FIRST_NAME.to_string(),
            last_name: Some(Self::LAST_NAME.to_string()),
            username: Some(Self::USERNAME.to_string()),
            language_code: Some(Self::LANGUAGE_CODE.to_string()),
            can_join_groups: Self::CAN_JOIN_GROUPS,
            can_read_all_group_messages: Self::CAN_READ_ALL_GROUP_MESSAGES,
            supports_inline_queries: Self::SUPPORTS_INLINE_QUERIES,
            can_connect_to_business: Self::CAN_CONNECT_TO_BUSINESS,
            has_main_web_app: Self::HAS_MAIN_WEB_APP,
        }
    }

    /// Builds the me
    ///
    /// # Examples
    /// ```
    /// let mock_me = teloxide_tests::MockMe::new();
    /// let me = mock_me.build();
    /// assert_eq!(me.id.0 as u64, teloxide_tests::MockMe::ID);  // ID is a default value
    /// ```
    ///
    pub fn build(self) -> Me {
        let mut user = MockUser::new();

        user.id = self.id;
        user.is_bot = self.is_bot;
        user.first_name = self.first_name;
        user.last_name = self.last_name;
        user.username = self.username;
        user.language_code = self.language_code;

        Me {
            user: user.build(),
            can_join_groups: self.can_join_groups,
            can_read_all_group_messages: self.can_read_all_group_messages,
            supports_inline_queries: self.supports_inline_queries,
            can_connect_to_business: self.can_connect_to_business,
            has_main_web_app: self.has_main_web_app,
        }
    }
}

//
//
//

#[derive(Changeable, Clone)]
pub struct MockChatPhoto {
    pub small_file_id: FileId,
    pub small_file_unique_id: FileUniqueId,
    pub big_file_id: FileId,
    pub big_file_unique_id: FileUniqueId,
}

impl MockChatPhoto {
    pub const SMALL_FILE_ID: &'static str = "small_file_id";
    pub const SMALL_FILE_UNIQUE_ID: &'static str = "small_file_unique_id";
    pub const BIG_FILE_ID: &'static str = "big_file_id";
    pub const BIG_FILE_UNIQUE_ID: &'static str = "big_file_unique_id";

    /// Creates a new easily changable chat photo builder
    ///
    /// # Examples
    /// ```
    /// let chat_photo = teloxide_tests::MockChatPhoto::new()
    ///     .small_file_id("small_file_id".into())
    ///     .build();
    /// assert_eq!(chat_photo.small_file_id, "small_file_id".into());
    /// ```
    ///
    pub fn new() -> Self {
        Self {
            small_file_id: Self::SMALL_FILE_ID.into(),
            small_file_unique_id: Self::SMALL_FILE_UNIQUE_ID.into(),
            big_file_id: Self::BIG_FILE_ID.into(),
            big_file_unique_id: Self::BIG_FILE_UNIQUE_ID.into(),
        }
    }

    /// Builds the chat photo
    ///
    /// # Examples
    /// ```
    /// let mock_chat_photo = teloxide_tests::MockChatPhoto::new();
    /// let chat_photo = mock_chat_photo.build();
    /// assert_eq!(
    ///     chat_photo.small_file_id,
    ///     teloxide_tests::MockChatPhoto::SMALL_FILE_ID.into()
    /// ); // SMALL_FILE_ID is a default value
    /// ```
    ///
    pub fn build(self) -> ChatPhoto {
        ChatPhoto {
            small_file_id: self.small_file_id,
            small_file_unique_id: self.small_file_unique_id,
            big_file_id: self.big_file_id,
            big_file_unique_id: self.big_file_unique_id,
        }
    }
}

#[derive(Changeable, Clone)]
pub struct MockLocation {
    pub latitude: f64,
    pub longitude: f64,
    pub horizontal_accuracy: Option<f64>,
    pub live_period: Option<LivePeriod>,
    pub heading: Option<u16>,
    pub proximity_alert_radius: Option<u32>,
}

impl MockLocation {
    pub const LATITUDE: f64 = 50.693416;
    pub const LONGITUDE: f64 = 30.624605;

    /// Creates a new easily changable location builder
    ///
    /// # Examples
    /// ```
    /// let location = teloxide_tests::MockLocation::new()
    ///     .latitude(50.693416)
    ///     .build();
    /// assert_eq!(location.latitude, 50.693416);
    /// ```
    ///
    pub fn new() -> Self {
        Self {
            latitude: Self::LATITUDE,
            longitude: Self::LONGITUDE,
            horizontal_accuracy: None,
            live_period: None,
            heading: None,
            proximity_alert_radius: None,
        }
    }

    /// Builds the location
    ///
    /// # Examples
    /// ```
    /// let mock_location = teloxide_tests::MockLocation::new();
    /// let location = mock_location.build();
    /// assert_eq!(location.latitude, teloxide_tests::MockLocation::LATITUDE); // LATITUDE is a default value
    /// ```
    ///
    pub fn build(self) -> Location {
        Location {
            longitude: self.longitude,
            latitude: self.latitude,
            horizontal_accuracy: self.horizontal_accuracy,
            live_period: self.live_period,
            heading: self.heading,
            proximity_alert_radius: self.proximity_alert_radius,
        }
    }
}

#[derive(Changeable, Clone)]
pub struct MockPhotoSize {
    pub width: u32,
    pub height: u32,
    // FileMeta
    pub file_id: FileId,
    pub file_unique_id: FileUniqueId,
    pub file_size: u32,
}

impl MockPhotoSize {
    pub const WIDTH: u32 = 90;
    pub const HEIGHT: u32 = 51;
    pub const FILE_ID: &'static str = "AgADBAADFak0G88YZAf8OAug7bHyS9x2ZxkABHVfpJywcloRAAGAAQABAg";
    pub const UNIQUE_FILE_ID: &'static str = "file_unique_id";
    pub const FILE_SIZE: u32 = 1101;

    /// Creates a new easily changable photo size builder
    ///
    /// # Examples
    /// ```
    /// let photo_size = teloxide_tests::MockPhotoSize::new()
    ///     .width(90)
    ///     .build();
    /// assert_eq!(photo_size.width, 90);
    /// ```
    ///
    pub fn new() -> Self {
        Self {
            width: Self::WIDTH,
            height: Self::HEIGHT,
            file_id: Self::FILE_ID.into(),
            file_unique_id: Self::UNIQUE_FILE_ID.into(),
            file_size: Self::FILE_SIZE,
        }
    }

    /// Builds the photo size
    ///
    /// # Examples
    /// ```
    /// let mock_photo_size = teloxide_tests::MockPhotoSize::new();
    /// let photo_size = mock_photo_size.build();
    /// assert_eq!(photo_size.width, teloxide_tests::MockPhotoSize::WIDTH); // WIDTH is a default value
    /// ```
    ///
    pub fn build(self) -> PhotoSize {
        PhotoSize {
            file: FileMeta {
                id: self.file_id,
                unique_id: self.file_unique_id,
                size: self.file_size,
            },
            width: self.width,
            height: self.height,
        }
    }
}

#[derive(Changeable, Clone)]
pub struct MockVideo {
    pub width: u32,
    pub height: u32,
    pub duration: Seconds,
    pub thumbnail: Option<PhotoSize>,
    pub cover: Option<Vec<PhotoSize>>,
    pub start_timestamp: Option<Seconds>,
    pub file_name: Option<String>,
    pub mime_type: Option<Mime>,
    // FileMeta
    pub file_id: FileId,
    pub file_unique_id: FileUniqueId,
    pub file_size: u32,
}

impl MockVideo {
    pub const WIDTH: u32 = 640;
    pub const HEIGHT: u32 = 480;
    pub const DURATION: Seconds = Seconds::from_seconds(52);
    pub const FILE_ID: &'static str = "BAADAgpAADdawy_JxS72kRvV3cortAg";
    pub const UNIQUE_FILE_ID: &'static str = "unique_file_id";
    pub const FILE_SIZE: u32 = 10099782;

    /// Creates a new easily changable video builder
    ///
    /// # Examples
    /// ```
    /// let video = teloxide_tests::MockVideo::new()
    ///     .width(640)
    ///     .build();
    /// assert_eq!(video.width, 640);
    /// ```
    ///
    pub fn new() -> Self {
        Self {
            width: Self::WIDTH,
            height: Self::HEIGHT,
            duration: Self::DURATION,
            thumbnail: None,
            cover: None,
            start_timestamp: None,
            file_name: None,
            mime_type: None,
            file_id: Self::FILE_ID.into(),
            file_unique_id: Self::UNIQUE_FILE_ID.into(),
            file_size: Self::FILE_SIZE,
        }
    }

    /// Builds the video
    ///
    /// # Examples
    /// ```
    /// let mock_video = teloxide_tests::MockVideo::new();
    /// let video = mock_video.build();
    /// assert_eq!(video.width, teloxide_tests::MockVideo::WIDTH); // WIDTH is a default value
    /// ```
    ///
    pub fn build(self) -> Video {
        Video {
            width: self.width,
            height: self.height,
            duration: self.duration,
            thumbnail: self.thumbnail,
            cover: self.cover,
            start_timestamp: self.start_timestamp,
            file_name: self.file_name,
            mime_type: self.mime_type,
            file: FileMeta {
                id: self.file_id,
                unique_id: self.file_unique_id,
                size: self.file_size,
            },
        }
    }
}

#[derive(Changeable, Clone)]
pub struct MockLinkPreviewOptions {
    pub is_disabled: bool,
    pub url: Option<String>,
    pub prefer_small_media: bool,
    pub prefer_large_media: bool,
    pub show_above_text: bool,
}

impl MockLinkPreviewOptions {
    /// Creates a new easily changable link preview options builder
    ///
    /// # Examples
    /// ```
    /// let link_preview_options = teloxide_tests::MockLinkPreviewOptions::new()
    ///     .is_disabled(true)
    ///     .build();
    /// assert_eq!(link_preview_options.is_disabled, true);
    /// ```
    ///
    pub fn new() -> Self {
        Self {
            is_disabled: false,
            url: None,
            prefer_small_media: false,
            prefer_large_media: false,
            show_above_text: false,
        }
    }

    /// Builds the link preview options
    ///
    /// # Examples
    /// ```
    /// let mock_link_preview_options = teloxide_tests::MockLinkPreviewOptions::new();
    /// let link_preview_options = mock_link_preview_options.build();
    /// assert_eq!(link_preview_options.url, None);
    /// ```
    ///
    pub fn build(self) -> LinkPreviewOptions {
        LinkPreviewOptions {
            is_disabled: self.is_disabled,
            url: self.url,
            prefer_small_media: self.prefer_small_media,
            prefer_large_media: self.prefer_large_media,
            show_above_text: self.show_above_text,
        }
    }
}