Skip to content

Commit c5e00ae

Browse files
committed
test(client-lib): simplify cardano stake distribution tests
- unify error handlings tests by testing the inner function that handle them directly instead of copying those tests for each - use dummy directly instead of modifying them since it does not bring much value here
1 parent dee0d33 commit c5e00ae

File tree

1 file changed

+44
-121
lines changed

1 file changed

+44
-121
lines changed

mithril-client/src/cardano_stake_distribution_client.rs

Lines changed: 44 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ mod tests {
145145
use mockall::predicate::eq;
146146

147147
use crate::aggregator_client::MockAggregatorClient;
148-
use crate::common::StakeDistribution;
148+
use crate::common::test::Dummy;
149149

150150
use super::*;
151151

@@ -171,24 +171,7 @@ mod tests {
171171
}
172172

173173
#[tokio::test]
174-
async fn list_cardano_stake_distributions_returns_messages() {
175-
let message = fake_messages();
176-
let mut http_client = MockAggregatorClient::new();
177-
http_client
178-
.expect_get_content()
179-
.with(eq(AggregatorRequest::ListCardanoStakeDistributions))
180-
.return_once(move |_| Ok(serde_json::to_string(&message).unwrap()));
181-
let client = CardanoStakeDistributionClient::new(Arc::new(http_client));
182-
183-
let messages = client.list().await.unwrap();
184-
185-
assert_eq!(2, messages.len());
186-
assert_eq!("hash-123".to_string(), messages[0].hash);
187-
assert_eq!("hash-456".to_string(), messages[1].hash);
188-
}
189-
190-
#[tokio::test]
191-
async fn list_cardano_stake_distributions_returns_error_when_invalid_json_structure_in_response()
174+
async fn fetching_cardano_stake_distribution_from_aggregator_client_returns_error_when_invalid_json_structure_in_response()
192175
{
193176
let mut http_client = MockAggregatorClient::new();
194177
http_client
@@ -197,61 +180,13 @@ mod tests {
197180
let client = CardanoStakeDistributionClient::new(Arc::new(http_client));
198181

199182
client
200-
.list()
201-
.await
202-
.expect_err("List Cardano stake distributions should return an error");
203-
}
204-
205-
#[tokio::test]
206-
async fn get_cardano_stake_distribution_returns_message() {
207-
let expected_stake_distribution = StakeDistribution::from([("pool123".to_string(), 123)]);
208-
let message = CardanoStakeDistribution {
209-
epoch: Epoch(3),
210-
hash: "hash-123".to_string(),
211-
certificate_hash: "certificate-hash-123".to_string(),
212-
stake_distribution: expected_stake_distribution.clone(),
213-
created_at: DateTime::<Utc>::default(),
214-
};
215-
let mut http_client = MockAggregatorClient::new();
216-
http_client
217-
.expect_get_content()
218-
.with(eq(AggregatorRequest::GetCardanoStakeDistribution {
219-
hash: "hash-123".to_string(),
220-
}))
221-
.return_once(move |_| Ok(serde_json::to_string(&message).unwrap()));
222-
let client = CardanoStakeDistributionClient::new(Arc::new(http_client));
223-
224-
let cardano_stake_distribution = client
225-
.get("hash-123")
226-
.await
227-
.unwrap()
228-
.expect("This test returns a Cardano stake distribution");
229-
230-
assert_eq!("hash-123".to_string(), cardano_stake_distribution.hash);
231-
assert_eq!(Epoch(3), cardano_stake_distribution.epoch);
232-
assert_eq!(
233-
expected_stake_distribution,
234-
cardano_stake_distribution.stake_distribution
235-
);
236-
}
237-
238-
#[tokio::test]
239-
async fn get_cardano_stake_distribution_returns_error_when_invalid_json_structure_in_response()
240-
{
241-
let mut http_client = MockAggregatorClient::new();
242-
http_client
243-
.expect_get_content()
244-
.return_once(move |_| Ok("invalid json structure".to_string()));
245-
let client = CardanoStakeDistributionClient::new(Arc::new(http_client));
246-
247-
client
248-
.get("hash-123")
183+
.fetch_with_aggregator_request(AggregatorRequest::ListCardanoStakeDistributions)
249184
.await
250185
.expect_err("Get Cardano stake distribution should return an error");
251186
}
252187

253188
#[tokio::test]
254-
async fn get_cardano_stake_distribution_returns_none_when_not_found_or_remote_server_logical_error()
189+
async fn fetching_cardano_stake_distribution_from_aggregator_client_returns_none_when_not_found_or_remote_server_logical_error()
255190
{
256191
let mut http_client = MockAggregatorClient::new();
257192
http_client.expect_get_content().return_once(move |_| {
@@ -261,100 +196,88 @@ mod tests {
261196
});
262197
let client = CardanoStakeDistributionClient::new(Arc::new(http_client));
263198

264-
let result = client.get("hash-123").await.unwrap();
199+
let result = client
200+
.fetch_with_aggregator_request(AggregatorRequest::ListCardanoStakeDistributions)
201+
.await
202+
.unwrap();
265203

266204
assert!(result.is_none());
267205
}
268206

269207
#[tokio::test]
270-
async fn get_cardano_stake_distribution_returns_error() {
208+
async fn fetching_cardano_stake_distribution_from_aggregator_client_returns_error() {
271209
let mut http_client = MockAggregatorClient::new();
272210
http_client
273211
.expect_get_content()
274212
.return_once(move |_| Err(AggregatorClientError::SubsystemError(anyhow!("error"))));
275213
let client = CardanoStakeDistributionClient::new(Arc::new(http_client));
276214

277215
client
278-
.get("hash-123")
216+
.fetch_with_aggregator_request(AggregatorRequest::ListCardanoStakeDistributions)
279217
.await
280218
.expect_err("Get Cardano stake distribution should return an error");
281219
}
282220

283221
#[tokio::test]
284-
async fn get_cardano_stake_distribution_by_epoch_returns_message() {
285-
let expected_stake_distribution = StakeDistribution::from([("pool123".to_string(), 123)]);
286-
let message = CardanoStakeDistribution {
287-
epoch: Epoch(3),
288-
hash: "hash-123".to_string(),
289-
certificate_hash: "certificate-hash-123".to_string(),
290-
stake_distribution: expected_stake_distribution.clone(),
291-
created_at: DateTime::<Utc>::default(),
292-
};
222+
async fn list_cardano_stake_distributions_returns_messages() {
223+
let message = fake_messages();
293224
let mut http_client = MockAggregatorClient::new();
294225
http_client
295226
.expect_get_content()
296-
.with(eq(AggregatorRequest::GetCardanoStakeDistributionByEpoch {
297-
epoch: Epoch(3),
298-
}))
227+
.with(eq(AggregatorRequest::ListCardanoStakeDistributions))
299228
.return_once(move |_| Ok(serde_json::to_string(&message).unwrap()));
300229
let client = CardanoStakeDistributionClient::new(Arc::new(http_client));
301230

302-
let cardano_stake_distribution = client
303-
.get_by_epoch(Epoch(3))
304-
.await
305-
.unwrap()
306-
.expect("This test returns a Cardano stake distribution");
231+
let messages = client.list().await.unwrap();
307232

308-
assert_eq!("hash-123".to_string(), cardano_stake_distribution.hash);
309-
assert_eq!(Epoch(3), cardano_stake_distribution.epoch);
310-
assert_eq!(
311-
expected_stake_distribution,
312-
cardano_stake_distribution.stake_distribution
313-
);
233+
assert_eq!(2, messages.len());
234+
assert_eq!("hash-123".to_string(), messages[0].hash);
235+
assert_eq!("hash-456".to_string(), messages[1].hash);
314236
}
315237

316238
#[tokio::test]
317-
async fn get_cardano_stake_distribution_by_epoch_returns_error_when_invalid_json_structure_in_response()
318-
{
239+
async fn get_cardano_stake_distribution_returns_message() {
240+
let expected_message = CardanoStakeDistribution::dummy();
319241
let mut http_client = MockAggregatorClient::new();
320242
http_client
321243
.expect_get_content()
322-
.return_once(move |_| Ok("invalid json structure".to_string()));
244+
.with(eq(AggregatorRequest::GetCardanoStakeDistribution {
245+
hash: expected_message.hash.clone(),
246+
}))
247+
.return_once(move |_| {
248+
Ok(serde_json::to_string(&CardanoStakeDistribution::dummy()).unwrap())
249+
});
323250
let client = CardanoStakeDistributionClient::new(Arc::new(http_client));
324251

325-
client
326-
.get_by_epoch(Epoch(3))
252+
let cardano_stake_distribution = client
253+
.get(&expected_message.hash)
327254
.await
328-
.expect_err("Get Cardano stake distribution by epoch should return an error");
329-
}
330-
331-
#[tokio::test]
332-
async fn get_cardano_stake_distribution_by_epoch_returns_none_when_not_found_or_remote_server_logical_error()
333-
{
334-
let mut http_client = MockAggregatorClient::new();
335-
http_client.expect_get_content().return_once(move |_| {
336-
Err(AggregatorClientError::RemoteServerLogical(anyhow!(
337-
"not found"
338-
)))
339-
});
340-
let client = CardanoStakeDistributionClient::new(Arc::new(http_client));
341-
342-
let result = client.get_by_epoch(Epoch(3)).await.unwrap();
255+
.unwrap()
256+
.expect("This test returns a Cardano stake distribution");
343257

344-
assert!(result.is_none());
258+
assert_eq!(expected_message, cardano_stake_distribution);
345259
}
346260

347261
#[tokio::test]
348-
async fn get_cardano_stake_distribution_by_epoch_returns_error() {
262+
async fn get_cardano_stake_distribution_by_epoch_returns_message() {
263+
let expected_message = CardanoStakeDistribution::dummy();
349264
let mut http_client = MockAggregatorClient::new();
350265
http_client
351266
.expect_get_content()
352-
.return_once(move |_| Err(AggregatorClientError::SubsystemError(anyhow!("error"))));
267+
.with(eq(AggregatorRequest::GetCardanoStakeDistributionByEpoch {
268+
epoch: expected_message.epoch,
269+
}))
270+
.return_once(move |_| {
271+
Ok(serde_json::to_string(&CardanoStakeDistribution::dummy()).unwrap())
272+
});
353273
let client = CardanoStakeDistributionClient::new(Arc::new(http_client));
354274

355-
client
356-
.get_by_epoch(Epoch(3))
275+
let cardano_stake_distribution = client
276+
.get_by_epoch(expected_message.epoch)
357277
.await
358-
.expect_err("Get Cardano stake distribution by epoch should return an error");
278+
.unwrap()
279+
.expect("This test returns a Cardano stake distribution");
280+
281+
assert_eq!(expected_message, cardano_stake_distribution);
359282
}
360283
}

0 commit comments

Comments
 (0)