Skip to content

Commit

Permalink
store/test-store : add tests for subgraph features table functions
Browse files Browse the repository at this point in the history
  • Loading branch information
incrypto32 committed Jun 9, 2023
1 parent 5c889d5 commit ba8b479
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 9 deletions.
9 changes: 6 additions & 3 deletions graph/src/blockchain/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C: Blockchain> TryFrom<DataSourceTemplateInfo<C>> for MockDataSource {
type Error = Error;
Expand Down Expand Up @@ -71,7 +74,7 @@ impl<C: Blockchain> DataSource<C> for MockDataSource {
}

fn kind(&self) -> &str {
todo!()
self.kind.as_str()
}

fn network(&self) -> Option<&str> {
Expand All @@ -87,7 +90,7 @@ impl<C: Blockchain> DataSource<C> for MockDataSource {
}

fn api_version(&self) -> semver::Version {
todo!()
self.api_version.clone()
}

fn runtime(&self) -> Option<Arc<Vec<u8>>> {
Expand Down
5 changes: 4 additions & 1 deletion graph/src/data_source/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ fn data_source_helpers() {
.unwrap()
.is_duplicate_of(&offchain));

let onchain = DataSource::<MockBlockchain>::Onchain(MockDataSource);
let onchain = DataSource::<MockBlockchain>::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());
}
Expand Down
9 changes: 9 additions & 0 deletions runtime/wasm/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,15 @@ impl<C: Blockchain> WasmInstanceContext<C> {
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(),
Expand Down
24 changes: 23 additions & 1 deletion store/postgres/src/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,29 @@ impl<'a> Connection<'a> {
}
}

pub fn create_deployment_features(
pub fn get_subgraph_features(
&self,
id: String,
) -> Result<Option<(String, String, Option<String>, Vec<String>, Vec<String>)>, 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<String>, Vec<String>, Vec<String>)>(conn)
.optional()?;
Ok(features)
}

pub fn create_subgraph_features(
&self,
id: String,
spec_version: String,
Expand Down
2 changes: 1 addition & 1 deletion store/postgres/src/subgraph_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 47 additions & 2 deletions store/test-store/src/store.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -153,7 +156,6 @@ pub async fn create_subgraph(
base: Option<(DeploymentHash, BlockPtr)>,
) -> Result<DeploymentLocator, StoreError> {
let schema = InputSchema::parse(schema, subgraph_id.clone()).unwrap();

let manifest = SubgraphManifest::<graph::blockchain::mock::MockBlockchain> {
id: subgraph_id.clone(),
spec_version: Version::new(1, 0, 0),
Expand All @@ -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<graph::blockchain::mock::MockBlockchain>,
base: Option<(DeploymentHash, BlockPtr)>,
) -> Result<DeploymentLocator, StoreError> {
let mut yaml = serde_yaml::Mapping::new();
yaml.insert("dataSources".into(), Vec::<serde_yaml::Value>::new().into());
let yaml = serde_yaml::to_string(&yaml).unwrap();
Expand All @@ -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::<BTreeSet<_>>();

let manifest = SubgraphManifest::<graph::blockchain::mock::MockBlockchain> {
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();
Expand Down
38 changes: 37 additions & 1 deletion store/test-store/tests/postgres/subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -52,6 +52,13 @@ fn get_version_info(store: &Store, subgraph_name: &str) -> VersionInfo {
store.version_info(&current).unwrap()
}

fn get_subgraph_features(
id: String,
) -> Option<(String, String, Option<String>, Vec<String>, Vec<String>)> {
let primary = primary_connection();
primary.get_subgraph_features(id).unwrap()
}

async fn latest_block(store: &Store, deployment_id: DeploymentId) -> BlockPtr {
store
.subgraph_store()
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ba8b479

Please sign in to comment.