twilight_model/poll/
results.rs

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
use super::answer_count::AnswerCount;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
/// This contains the number of votes for each answer.
pub struct PollResults {
    /// The counts for each answer.
    pub answer_counts: Vec<AnswerCount>,
    /// Whether the votes have been precisely counted.
    pub is_finalized: bool,
}

#[cfg(test)]
mod tests {
    use super::{AnswerCount, PollResults};
    use serde_test::Token;

    #[test]
    fn poll_results() {
        let value = PollResults {
            answer_counts: vec![
                AnswerCount {
                    id: 1,
                    count: 2,
                    me_voted: true,
                },
                AnswerCount {
                    id: 3,
                    count: 4,
                    me_voted: false,
                },
            ],
            is_finalized: true,
        };

        serde_test::assert_tokens(
            &value,
            &[
                Token::Struct {
                    name: "PollResults",
                    len: 2,
                },
                Token::Str("answer_counts"),
                Token::Seq { len: Some(2) },
                Token::Struct {
                    name: "AnswerCount",
                    len: 3,
                },
                Token::Str("id"),
                Token::U8(1),
                Token::Str("count"),
                Token::U64(2),
                Token::Str("me_voted"),
                Token::Bool(true),
                Token::StructEnd,
                Token::Struct {
                    name: "AnswerCount",
                    len: 3,
                },
                Token::Str("id"),
                Token::U8(3),
                Token::Str("count"),
                Token::U64(4),
                Token::Str("me_voted"),
                Token::Bool(false),
                Token::StructEnd,
                Token::SeqEnd,
                Token::Str("is_finalized"),
                Token::Bool(true),
                Token::StructEnd,
            ],
        );
    }
}