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 8aac39d
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 13 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
4 changes: 2 additions & 2 deletions graph/src/data/subgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl Graft {

#[derive(Clone, Debug)]
pub struct DeploymentFeatures {
pub id: DeploymentHash,
pub id: String,
pub spec_version: String,
pub api_version: Option<String>,
pub features: Vec<String>,
Expand Down Expand Up @@ -702,7 +702,7 @@ impl<C: Blockchain> SubgraphManifest<C> {
data_source_kinds.extend(data_source_template_kinds);

DeploymentFeatures {
id: self.id.clone(),
id: self.id.to_string(),
api_version,
features,
spec_version,
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
36 changes: 34 additions & 2 deletions store/postgres/src/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use diesel::{
use graph::{
components::store::DeploymentLocator,
constraint_violation,
data::subgraph::status,
data::subgraph::{status, DeploymentFeatures},
prelude::{
anyhow, bigdecimal::ToPrimitive, serde_json, DeploymentHash, EntityChange,
EntityChangeOperation, NodeId, StoreError, SubgraphName, SubgraphVersionSwitchingMode,
Expand Down Expand Up @@ -1108,7 +1108,39 @@ impl<'a> Connection<'a> {
}
}

pub fn create_deployment_features(
pub fn get_subgraph_features(
&self,
id: String,
) -> Result<Option<DeploymentFeatures>, 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()?;

let features = features.map(|(id, spec_version, api_version, features, data_sources)| {
DeploymentFeatures {
id,
spec_version,
api_version,
features,
data_source_kinds: data_sources,
}
});

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
46 changes: 44 additions & 2 deletions store/test-store/tests/postgres/subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use graph::{
store::{DeploymentId, DeploymentLocator, StatusStore},
},
data::query::QueryTarget,
data::subgraph::schema::SubgraphHealth,
data::subgraph::schema::{DeploymentCreate, SubgraphError},
data::subgraph::{schema::SubgraphHealth, SubgraphFeature},
data::subgraph::{
schema::{DeploymentCreate, SubgraphError},
DeploymentFeatures,
},
prelude::BlockPtr,
prelude::EntityChange,
prelude::EntityChangeOperation,
Expand Down Expand Up @@ -52,6 +55,11 @@ fn get_version_info(store: &Store, subgraph_name: &str) -> VersionInfo {
store.version_info(&current).unwrap()
}

fn get_subgraph_features(id: String) -> Option<DeploymentFeatures> {
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 +493,40 @@ 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 DeploymentFeatures {
id: subgraph_id,
spec_version,
api_version,
features,
data_source_kinds
} = 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_source_kinds.len());

test_store::remove_subgraph(&id)
})
}

#[test]
fn subgraph_error() {
test_store::run_test_sequentially(|store| async move {
Expand Down

0 comments on commit 8aac39d

Please sign in to comment.