desmos_std/
mock.rs

1use crate::{
2    query_types::{
3        DesmosQuery, DesmosQueryWrapper, PostsResponse, ReactionsResponse, ReportsResponse,
4    },
5    types::{Poll, Post, Reaction, Report},
6};
7use cosmwasm_std::{
8    testing::{MockApi, MockQuerier, MockStorage, MOCK_CONTRACT_ADDR},
9    to_binary, Binary, Coin, ContractResult, OwnedDeps, SystemResult,
10};
11use std::marker::PhantomData;
12
13/// Replacement for cosmwasm_std::testing::mock_dependencies
14/// this use our CustomQuerier
15pub fn mock_dependencies_with_custom_querier(
16    contract_balance: &[Coin],
17) -> OwnedDeps<MockStorage, MockApi, MockQuerier<DesmosQueryWrapper>, DesmosQueryWrapper> {
18    let contract_addr = MOCK_CONTRACT_ADDR;
19    let custom_querier: MockQuerier<DesmosQueryWrapper> =
20        MockQuerier::new(&[(contract_addr, contract_balance)])
21            .with_custom_handler(|query| SystemResult::Ok(custom_query_execute(query)));
22    OwnedDeps::<_, _, _, DesmosQueryWrapper> {
23        storage: MockStorage::default(),
24        api: MockApi::default(),
25        querier: custom_querier,
26        custom_query_type: PhantomData,
27    }
28}
29
30/// custom_query_execute returns mock responses to custom queries
31pub fn custom_query_execute(query: &DesmosQueryWrapper) -> ContractResult<Binary> {
32    let response = match query.clone().query_data {
33        DesmosQuery::Posts {} => {
34            let post = Post {
35                post_id: "id123".to_string(),
36                parent_id: Some("id345".to_string()),
37                message: "message".to_string(),
38                created: "date-time".to_string(),
39                last_edited: "date-time".to_string(),
40                comments_state: "ALLOWED".to_string(),
41                subspace: "subspace".to_string(),
42                additional_attributes: Some(vec![]),
43                attachments: Some(vec![]),
44                poll: Some(Poll {
45                    question: "".to_string(),
46                    provided_answers: vec![],
47                    end_date: "".to_string(),
48                    allows_multiple_answers: false,
49                    allows_answer_edits: false,
50                }),
51                creator: "default_creator".to_string(),
52            };
53            to_binary(&PostsResponse { posts: vec![post] })
54        }
55        DesmosQuery::Reports { post_id } => {
56            let report = Report {
57                post_id,
58                kind: "test".to_string(),
59                message: "test".to_string(),
60                user: "default_creator".to_string(),
61            };
62            to_binary(&ReportsResponse {
63                reports: vec![report],
64            })
65        }
66        DesmosQuery::Reactions { post_id } => {
67            let reactions = vec![Reaction {
68                post_id,
69                short_code: ":heart:".to_string(),
70                value: "❤️".to_string(),
71                owner: "user".to_string(),
72            }];
73            to_binary(&ReactionsResponse { reactions })
74        }
75    };
76    response.into()
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82    use crate::query_types::{PostsResponse, ReportsResponse};
83    use crate::types::{DesmosRoute, Report};
84    use cosmwasm_std::{from_binary, QuerierWrapper};
85
86    #[test]
87    fn custom_query_execute_posts() {
88        let post = Post {
89            post_id: "id123".to_string(),
90            parent_id: Some("id345".to_string()),
91            message: "message".to_string(),
92            created: "date-time".to_string(),
93            last_edited: "date-time".to_string(),
94            comments_state: "ALLOWED".to_string(),
95            subspace: "subspace".to_string(),
96            additional_attributes: Some(vec![]),
97            attachments: Some(vec![]),
98            poll: Some(Poll {
99                question: "".to_string(),
100                provided_answers: vec![],
101                end_date: "".to_string(),
102                allows_multiple_answers: false,
103                allows_answer_edits: false,
104            }),
105            creator: String::from("default_creator"),
106        };
107        let expected = PostsResponse { posts: vec![post] };
108        let desmos_query_wrapper = DesmosQueryWrapper {
109            route: DesmosRoute::Posts,
110            query_data: DesmosQuery::Posts {},
111        };
112        let bz = custom_query_execute(&desmos_query_wrapper).unwrap();
113        let response: PostsResponse = from_binary(&bz).unwrap();
114        assert_eq!(response, expected)
115    }
116
117    #[test]
118    fn custom_query_execute_reports() {
119        let report = Report {
120            post_id: "id123".to_string(),
121            kind: "test".to_string(),
122            message: "test".to_string(),
123            user: "default_creator".to_string(),
124        };
125        let expected = ReportsResponse {
126            reports: vec![report],
127        };
128        let desmos_query_wrapper = DesmosQueryWrapper {
129            route: DesmosRoute::Posts,
130            query_data: DesmosQuery::Reports {
131                post_id: "id123".to_string(),
132            },
133        };
134
135        let bz = custom_query_execute(&desmos_query_wrapper).unwrap();
136        let response: ReportsResponse = from_binary(&bz).unwrap();
137        assert_eq!(response, expected)
138    }
139
140    #[test]
141    fn custom_querier() {
142        let deps = mock_dependencies_with_custom_querier(&[]);
143        let req = DesmosQueryWrapper {
144            route: DesmosRoute::Posts,
145            query_data: DesmosQuery::Reports {
146                post_id: "id123".to_string(),
147            },
148        }
149        .into();
150        let wrapper: QuerierWrapper<'_, DesmosQueryWrapper> = QuerierWrapper::new(&deps.querier);
151        let response: ReportsResponse = wrapper.query(&req).unwrap();
152        let expected = vec![Report {
153            post_id: "id123".to_string(),
154            kind: "test".to_string(),
155            message: "test".to_string(),
156            user: "default_creator".to_string(),
157        }];
158        assert_eq!(response.reports, expected);
159    }
160}