Skip to content

Commit

Permalink
test: Add graphql queries to end-to-end tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leoyvens committed Aug 19, 2022
1 parent 9baaafb commit f85a33f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ async-stream = "0.3.3"
graph-node = { path = "../node" }
graph-core = { path = "../core" }
graph-mock = { path = "../mock" }
graph-graphql = { path = "../graphql" }
graph-store-postgres = { path = "../store/postgres" }
slog = { version = "2.7.0", features = ["release_max_level_trace", "max_level_trace"] }
graphql-parser = "0.4.0"

[dev-dependencies]
bollard = "0.10"
Expand Down
41 changes: 38 additions & 3 deletions tests/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ use graph::blockchain::{
};
use graph::cheap_clone::CheapClone;
use graph::components::store::{BlockStore, DeploymentLocator};
use graph::data::graphql::effort::LoadManager;
use graph::data::query::{Query, QueryTarget};
use graph::env::ENV_VARS;
use graph::ipfs_client::IpfsClient;
use graph::prelude::ethabi::ethereum_types::H256;
use graph::prelude::{
async_trait, BlockNumber, DeploymentHash, LoggerFactory, MetricsRegistry, NodeId,
SubgraphAssignmentProvider, SubgraphName, SubgraphRegistrar, SubgraphStore as _,
SubgraphVersionSwitchingMode,
async_trait, r, BlockNumber, DeploymentHash, GraphQlRunner as _, LoggerFactory,
MetricsRegistry, NodeId, QueryError, SubgraphAssignmentProvider, SubgraphName,
SubgraphRegistrar, SubgraphStore as _, SubgraphVersionSwitchingMode,
};
use graph_core::polling_monitor::ipfs_service::IpfsService;
use graph_core::{
LinkResolver, SubgraphAssignmentProvider as IpfsSubgraphAssignmentProvider,
SubgraphInstanceManager, SubgraphRegistrar as IpfsSubgraphRegistrar,
};
use graph_graphql::prelude::GraphQlRunner;
use graph_mock::MockMetricsRegistry;
use graph_node::manager::PanicSubscriptionManager;
use graph_node::{config::Config, store_builder::StoreBuilder};
Expand Down Expand Up @@ -104,6 +107,7 @@ pub struct TestContext {
>,
pub store: Arc<SubgraphStore>,
pub deployment: DeploymentLocator,
graphql_runner: Arc<GraphQlRunner<Store, PanicSubscriptionManager>>,
}

impl TestContext {
Expand All @@ -117,6 +121,25 @@ impl TestContext {
.await
.unwrap();
}

pub async fn query(&self, query: &str) -> Result<Option<r::Value>, Vec<QueryError>> {
let target = QueryTarget::Deployment(self.deployment.hash.clone());

self.graphql_runner
.clone()
.run_query(
Query::new(
graphql_parser::parse_query(query).unwrap().into_static(),
None,
),
target,
)
.await
.first()
.unwrap()
.duplicate()
.to_result()
}
}

pub struct Stores {
Expand Down Expand Up @@ -222,6 +245,17 @@ pub async fn setup<C: Blockchain>(
static_filters,
);

// Graphql runner
let subscription_manager = Arc::new(PanicSubscriptionManager {});
let load_manager = LoadManager::new(&logger, Vec::new(), mock_registry.clone());
let graphql_runner = Arc::new(GraphQlRunner::new(
&logger,
stores.network_store.clone(),
subscription_manager.clone(),
Arc::new(load_manager),
mock_registry.clone(),
));

// Create IPFS-based subgraph provider
let subgraph_provider = Arc::new(IpfsSubgraphAssignmentProvider::new(
&logger_factory,
Expand Down Expand Up @@ -263,6 +297,7 @@ pub async fn setup<C: Blockchain>(
provider: subgraph_provider,
store: subgraph_store,
deployment,
graphql_runner,
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/tests/runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use graph::blockchain::{Block, BlockPtr};
use graph::object;
use graph::prelude::ethabi::ethereum_types::H256;
use graph::prelude::{SubgraphAssignmentProvider, SubgraphName};
use graph_tests::fixture::ethereum::{chain, empty_block, genesis};
Expand Down Expand Up @@ -53,6 +54,19 @@ async fn data_source_revert() -> anyhow::Result<()> {
let stop_block = test_ptr(4);
ctx.start_and_sync_to(stop_block).await;

let query_res = ctx
.query(r#"{ dataSourceCount(id: "4") { id, count } }"#)
.await
.unwrap();

// TODO: The semantically correct value for `count` would be 5. But because the test fixture
// uses a `NoopTriggersAdapter` the data sources are not reprocessed in the block in which they
// are created.
assert_eq!(
query_res,
Some(object! { dataSourceCount: object!{ id: "4", count: 4 } })
);

fixture::cleanup(&ctx.store, &subgraph_name, &hash);

Ok(())
Expand Down

0 comments on commit f85a33f

Please sign in to comment.