Skip to content

Commit 0bbc8d6

Browse files
stefan-mystenDaughterOfMars
authored andcommitted
graphql: add a few tests (#8)
1 parent d1f9eb8 commit 0bbc8d6

File tree

1 file changed

+177
-3
lines changed
  • crates/sui-graphql-client/src

1 file changed

+177
-3
lines changed

crates/sui-graphql-client/src/lib.rs

Lines changed: 177 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl Client {
221221
seq_num: Option<u64>,
222222
) -> Result<Option<CheckpointSummary>, Error> {
223223
ensure!(
224-
digest.is_some() != seq_num.is_some(),
224+
!(digest.is_some() && seq_num.is_some()),
225225
"Either digest or seq_num must be provided"
226226
);
227227

@@ -496,7 +496,11 @@ impl Client {
496496
.data
497497
.and_then(|tbq| tbq.transaction_block)
498498
.and_then(|tb| tb.bcs)
499-
.map(|bcs| bcs::from_bytes::<SignedTransaction>(bcs.0.as_bytes()).unwrap());
499+
.map(|bcs| base64ct::Base64::decode_vec(bcs.0.as_str()))
500+
.transpose()
501+
.map_err(|e| Error::msg(format!("Cannot decode Base64 transaction bcs bytes: {e}")))?
502+
.map(|bcs| bcs::from_bytes::<SignedTransaction>(&bcs))
503+
.transpose()?;
500504
Ok(signed_tx)
501505
}
502506

@@ -539,7 +543,11 @@ impl Client {
539543
.iter()
540544
.map(|tx| bcs::from_bytes::<SignedTransaction>(tx))
541545
.collect::<Result<Vec<_>, bcs::Error>>()
542-
.map_err(|e| Error::msg(format!("Cannot decode bcs bytes into Object: {e}")))?;
546+
.map_err(|e| {
547+
Error::msg(format!(
548+
"Cannot decode bcs bytes into SignedTransaction: {e}"
549+
))
550+
})?;
543551
let page = Page::new(page_info, transactions);
544552
Ok(Some(page))
545553
} else {
@@ -551,6 +559,7 @@ impl Client {
551559
#[cfg(test)]
552560
mod tests {
553561
use crate::{Client, DEFAULT_LOCAL_HOST, DEVNET_HOST, MAINNET_HOST, TESTNET_HOST};
562+
const NETWORKS: [(&str, &str); 2] = [(MAINNET_HOST, "35834a8a"), (TESTNET_HOST, "4c78adac")];
554563

555564
#[test]
556565
fn test_rpc_server() {
@@ -566,4 +575,169 @@ mod tests {
566575
assert!(client.set_rpc_server("localhost:9125/graphql").is_ok());
567576
assert!(client.set_rpc_server("9125/graphql").is_err());
568577
}
578+
579+
#[tokio::test]
580+
async fn test_chain_id() {
581+
for (n, id) in NETWORKS.iter() {
582+
let client = Client::new(n).unwrap();
583+
let chain_id = client.chain_id().await;
584+
assert!(chain_id.is_ok());
585+
assert_eq!(&chain_id.unwrap(), id);
586+
}
587+
}
588+
589+
#[tokio::test]
590+
async fn test_reference_gas_price_query() {
591+
for (n, _) in NETWORKS.iter() {
592+
let client = Client::new(n).unwrap();
593+
let rgp = client.reference_gas_price(None).await;
594+
assert!(
595+
rgp.is_ok(),
596+
"Reference gas price query failed for network: {n}"
597+
);
598+
}
599+
}
600+
601+
#[tokio::test]
602+
async fn test_protocol_config_query() {
603+
for (n, _) in NETWORKS {
604+
let client = Client::new(n).unwrap();
605+
let pc = client.protocol_config(None).await;
606+
assert!(pc.is_ok());
607+
608+
// test specific version
609+
let pc = client.protocol_config(Some(50)).await;
610+
assert!(pc.is_ok());
611+
let pc = pc.unwrap();
612+
if let Some(pc) = pc {
613+
assert_eq!(
614+
pc.protocol_version.0, 50,
615+
"Protocol version query mismatch for network: {n}. Expected: 50, received: {}",
616+
pc.protocol_version.0
617+
);
618+
}
619+
}
620+
}
621+
622+
#[tokio::test]
623+
async fn test_service_config_query() {
624+
for (n, _) in NETWORKS {
625+
let client = Client::new(n).unwrap();
626+
let sc = client.service_config().await;
627+
assert!(sc.is_ok(), "Service config query failed for network: {n}");
628+
}
629+
}
630+
631+
#[tokio::test]
632+
async fn test_coin_metadata_query() {
633+
for (n, _) in NETWORKS {
634+
let client = Client::new(n).unwrap();
635+
let cm = client.coin_metadata("0x2::sui::SUI").await;
636+
assert!(cm.is_ok(), "Coin metadata query failed for network: {n}");
637+
}
638+
}
639+
640+
#[tokio::test]
641+
async fn test_checkpoint_query() {
642+
for (n, _) in NETWORKS {
643+
let client = Client::new(n).unwrap();
644+
let c = client.checkpoint(None, None).await;
645+
assert!(
646+
c.is_ok(),
647+
"Checkpoint query failed for network: {n}. Error: {}",
648+
c.unwrap_err()
649+
);
650+
}
651+
}
652+
653+
#[tokio::test]
654+
async fn test_latest_checkpoint_sequence_number_query() {
655+
for (n, _) in NETWORKS {
656+
let client = Client::new(n).unwrap();
657+
let last_checkpoint = client.latest_checkpoint_sequence_number().await;
658+
assert!(
659+
last_checkpoint.is_ok(),
660+
"Latest checkpoint sequence number query failed for network: {n}. Error: {}",
661+
last_checkpoint.unwrap_err()
662+
);
663+
}
664+
}
665+
666+
#[tokio::test]
667+
async fn test_epoch_total_checkpoints_query() {
668+
for (n, _) in NETWORKS {
669+
let client = Client::new(n).unwrap();
670+
let e = client.epoch_total_checkpoints(None).await;
671+
assert!(
672+
e.is_ok(),
673+
"Epoch total checkpoints query failed for network: {n}. Error: {}",
674+
e.unwrap_err()
675+
);
676+
}
677+
}
678+
679+
#[tokio::test]
680+
async fn test_epoch_total_transaction_blocks_query() {
681+
for (n, _) in NETWORKS {
682+
let client = Client::new(n).unwrap();
683+
let e = client.epoch_total_transaction_blocks(None).await;
684+
assert!(
685+
e.is_ok(),
686+
"Epoch total transaction blocks query failed for network: {n}. Error: {}",
687+
e.unwrap_err()
688+
);
689+
}
690+
}
691+
692+
#[tokio::test]
693+
async fn test_epoch_summary_query() {
694+
for (n, _) in NETWORKS {
695+
let client = Client::new(n).unwrap();
696+
let e = client.epoch_summary(None).await;
697+
assert!(
698+
e.is_ok(),
699+
"Epoch summary query failed for network: {n}. Error: {}",
700+
e.unwrap_err()
701+
);
702+
}
703+
}
704+
705+
#[tokio::test]
706+
async fn test_objects_query() {
707+
for (n, _) in NETWORKS {
708+
let client = Client::new(n).unwrap();
709+
let objects = client.objects(None, None, None, None, None).await;
710+
assert!(
711+
objects.is_ok(),
712+
"Objects query failed for network: {n}. Error: {}",
713+
objects.unwrap_err()
714+
);
715+
}
716+
}
717+
718+
#[tokio::test]
719+
async fn test_object_query() {
720+
for (n, _) in NETWORKS {
721+
let client = Client::new(n).unwrap();
722+
let object = client.object("0x5".parse().unwrap(), None).await;
723+
assert!(
724+
object.is_ok(),
725+
"Object query failed for network: {n}. Error: {}",
726+
object.unwrap_err()
727+
);
728+
}
729+
}
730+
731+
#[tokio::test]
732+
async fn test_object_bcs_query() {
733+
for (n, _) in NETWORKS {
734+
let client = Client::new(n).unwrap();
735+
let object_bcs = client.object_bcs("0x5".parse().unwrap()).await;
736+
assert!(
737+
object_bcs.is_ok(),
738+
"Object bcs query failed for network: {n}. Error: {}",
739+
object_bcs.unwrap_err()
740+
);
741+
}
742+
}
569743
}

0 commit comments

Comments
 (0)