From ba8b4795aee29718586b8f153673a63c4888197a Mon Sep 17 00:00:00 2001 From: incrypto32 Date: Fri, 9 Jun 2023 18:37:19 +0900 Subject: [PATCH] store/test-store : add tests for subgraph features table functions --- graph/src/blockchain/mock.rs | 9 ++-- graph/src/data_source/tests.rs | 5 ++- runtime/wasm/src/module/mod.rs | 9 ++++ store/postgres/src/primary.rs | 24 +++++++++- store/postgres/src/subgraph_store.rs | 2 +- store/test-store/src/store.rs | 49 ++++++++++++++++++++- store/test-store/tests/postgres/subgraph.rs | 38 +++++++++++++++- 7 files changed, 127 insertions(+), 9 deletions(-) diff --git a/graph/src/blockchain/mock.rs b/graph/src/blockchain/mock.rs index b823a6baa31..8b0061eb20c 100644 --- a/graph/src/blockchain/mock.rs +++ b/graph/src/blockchain/mock.rs @@ -43,7 +43,10 @@ impl Block for MockBlock { } #[derive(Clone)] -pub struct MockDataSource; +pub struct MockDataSource { + pub api_version: semver::Version, + pub kind: String, +} impl TryFrom> for MockDataSource { type Error = Error; @@ -71,7 +74,7 @@ impl DataSource for MockDataSource { } fn kind(&self) -> &str { - todo!() + self.kind.as_str() } fn network(&self) -> Option<&str> { @@ -87,7 +90,7 @@ impl DataSource for MockDataSource { } fn api_version(&self) -> semver::Version { - todo!() + self.api_version.clone() } fn runtime(&self) -> Option>> { diff --git a/graph/src/data_source/tests.rs b/graph/src/data_source/tests.rs index b3205fbab79..dc1d2a86732 100644 --- a/graph/src/data_source/tests.rs +++ b/graph/src/data_source/tests.rs @@ -59,7 +59,10 @@ fn data_source_helpers() { .unwrap() .is_duplicate_of(&offchain)); - let onchain = DataSource::::Onchain(MockDataSource); + let onchain = DataSource::::Onchain(MockDataSource { + api_version: Version::new(1, 0, 0), + kind: "mock/kind".into(), + }); assert!(onchain.causality_region() == CausalityRegion::ONCHAIN); assert!(onchain.as_offchain().is_none()); } diff --git a/runtime/wasm/src/module/mod.rs b/runtime/wasm/src/module/mod.rs index fe05cf923be..3672432f526 100644 --- a/runtime/wasm/src/module/mod.rs +++ b/runtime/wasm/src/module/mod.rs @@ -1170,6 +1170,15 @@ impl WasmInstanceContext { let entity_type: String = asc_get(self, entity_type_ptr, gas)?; let id: String = asc_get(self, id_ptr, gas)?; let field: String = asc_get(self, field_ptr, gas)?; + + error!( + &self.ctx.logger, + "store_load_related"; + "type" => &entity_type, + "id" => &id, + "field" => &field, + ); + let entities = self.ctx.host_exports.store_load_related( &mut self.ctx.state, entity_type.clone(), diff --git a/store/postgres/src/primary.rs b/store/postgres/src/primary.rs index ea174b02347..d3abc38948f 100644 --- a/store/postgres/src/primary.rs +++ b/store/postgres/src/primary.rs @@ -1108,7 +1108,29 @@ impl<'a> Connection<'a> { } } - pub fn create_deployment_features( + pub fn get_subgraph_features( + &self, + id: String, + ) -> Result, Vec, Vec)>, StoreError> + { + use subgraph_features as f; + + let conn = self.conn.as_ref(); + let features = f::table + .filter(f::id.eq(id)) + .select(( + f::id, + f::spec_version, + f::api_version, + f::features, + f::data_sources, + )) + .first::<(String, String, Option, Vec, Vec)>(conn) + .optional()?; + Ok(features) + } + + pub fn create_subgraph_features( &self, id: String, spec_version: String, diff --git a/store/postgres/src/subgraph_store.rs b/store/postgres/src/subgraph_store.rs index 74b4688c68f..0803dcdee83 100644 --- a/store/postgres/src/subgraph_store.rs +++ b/store/postgres/src/subgraph_store.rs @@ -578,7 +578,7 @@ impl SubgraphStoreInner { let changes = pconn.create_subgraph_version(name, &site, node_id, mode, exists_and_synced)?; - pconn.create_deployment_features( + pconn.create_subgraph_features( features.id.to_string(), features.spec_version, features.features, diff --git a/store/test-store/src/store.rs b/store/test-store/src/store.rs index 1376be2c3c7..bea78ab7bd2 100644 --- a/store/test-store/src/store.rs +++ b/store/test-store/src/store.rs @@ -1,9 +1,12 @@ use diesel::{self, PgConnection}; +use graph::blockchain::mock::MockDataSource; use graph::data::graphql::effort::LoadManager; use graph::data::query::QueryResults; use graph::data::query::QueryTarget; use graph::data::subgraph::schema::{DeploymentCreate, SubgraphError}; +use graph::data::subgraph::SubgraphFeature; use graph::data_source::CausalityRegion; +use graph::data_source::DataSource; use graph::log; use graph::prelude::{QueryStoreManager as _, SubgraphStore as _, *}; use graph::schema::InputSchema; @@ -153,7 +156,6 @@ pub async fn create_subgraph( base: Option<(DeploymentHash, BlockPtr)>, ) -> Result { let schema = InputSchema::parse(schema, subgraph_id.clone()).unwrap(); - let manifest = SubgraphManifest:: { id: subgraph_id.clone(), spec_version: Version::new(1, 0, 0), @@ -167,6 +169,15 @@ pub async fn create_subgraph( chain: PhantomData, }; + create_subgraph_with_manifest(subgraph_id, schema, manifest, base).await +} + +pub async fn create_subgraph_with_manifest( + subgraph_id: &DeploymentHash, + schema: InputSchema, + manifest: SubgraphManifest, + base: Option<(DeploymentHash, BlockPtr)>, +) -> Result { let mut yaml = serde_yaml::Mapping::new(); yaml.insert("dataSources".into(), Vec::::new().into()); let yaml = serde_yaml::to_string(&yaml).unwrap(); @@ -190,11 +201,45 @@ pub async fn create_subgraph( .await?; Ok(deployment) } - pub async fn create_test_subgraph(subgraph_id: &DeploymentHash, schema: &str) -> DeploymentLocator { create_subgraph(subgraph_id, schema, None).await.unwrap() } +pub async fn create_test_subgraph_with_features( + subgraph_id: &DeploymentHash, + schema: &str, +) -> DeploymentLocator { + let schema = InputSchema::parse(schema, subgraph_id.clone()).unwrap(); + + let features = [ + SubgraphFeature::FullTextSearch, + SubgraphFeature::NonFatalErrors, + ] + .iter() + .cloned() + .collect::>(); + + let manifest = SubgraphManifest:: { + id: subgraph_id.clone(), + spec_version: Version::new(1, 0, 0), + features: features, + description: Some(format!("manifest for {}", subgraph_id)), + repository: Some(format!("repo for {}", subgraph_id)), + schema: schema.clone(), + data_sources: vec![DataSource::Onchain(MockDataSource { + kind: "mock/kind".into(), + api_version: Version::new(1, 0, 0), + })], + graft: None, + templates: vec![], + chain: PhantomData, + }; + + create_subgraph_with_manifest(subgraph_id, schema, manifest, None) + .await + .unwrap() +} + pub fn remove_subgraph(id: &DeploymentHash) { let name = SubgraphName::new_unchecked(id.to_string()); SUBGRAPH_STORE.remove_subgraph(name).unwrap(); diff --git a/store/test-store/tests/postgres/subgraph.rs b/store/test-store/tests/postgres/subgraph.rs index 3d3b5f477c2..0c0224e89b7 100644 --- a/store/test-store/tests/postgres/subgraph.rs +++ b/store/test-store/tests/postgres/subgraph.rs @@ -4,8 +4,8 @@ use graph::{ store::{DeploymentId, DeploymentLocator, StatusStore}, }, data::query::QueryTarget, - data::subgraph::schema::SubgraphHealth, data::subgraph::schema::{DeploymentCreate, SubgraphError}, + data::subgraph::{schema::SubgraphHealth, SubgraphFeature}, prelude::BlockPtr, prelude::EntityChange, prelude::EntityChangeOperation, @@ -52,6 +52,13 @@ fn get_version_info(store: &Store, subgraph_name: &str) -> VersionInfo { store.version_info(¤t).unwrap() } +fn get_subgraph_features( + id: String, +) -> Option<(String, String, Option, Vec, Vec)> { + let primary = primary_connection(); + primary.get_subgraph_features(id).unwrap() +} + async fn latest_block(store: &Store, deployment_id: DeploymentId) -> BlockPtr { store .subgraph_store() @@ -485,6 +492,35 @@ fn version_info() { }) } +#[test] +fn subgraph_features() { + run_test_sequentially(|_store| async move { + const NAME: &str = "subgraph_features"; + let id = DeploymentHash::new(NAME).unwrap(); + + remove_subgraphs(); + block_store::set_chain(vec![], NETWORK_NAME); + create_test_subgraph_with_features(&id, SUBGRAPH_GQL).await; + + let (subgraph_id, spec_version, api_version, features, data_sources) = + get_subgraph_features(id.to_string()).unwrap(); + + assert_eq!(NAME, subgraph_id.as_str()); + assert_eq!("1.0.0", spec_version); + assert_eq!("1.0.0", api_version.unwrap()); + assert_eq!( + vec![ + SubgraphFeature::NonFatalErrors.to_string(), + SubgraphFeature::FullTextSearch.to_string() + ], + features + ); + assert_eq!(1, data_sources.len()); + + test_store::remove_subgraph(&id) + }) +} + #[test] fn subgraph_error() { test_store::run_test_sequentially(|store| async move {