Skip to content

Commit

Permalink
tests: Improve tests and comments to address review
Browse files Browse the repository at this point in the history
  • Loading branch information
leoyvens committed Jan 4, 2023
1 parent dc6f228 commit 594f919
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 11 deletions.
5 changes: 5 additions & 0 deletions graph/src/components/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ pub struct EntityKey {
/// ID of the individual entity.
pub entity_id: Word,

/// This is the causality region of the data source that created the entity.
///
/// In the case of an entity lookup, this is the causality region of the data source that is
/// doing the lookup. So if the entity exists but was created on a different causality region,
/// the lookup will return empty.
pub causality_region: CausalityRegion,
}

Expand Down
11 changes: 5 additions & 6 deletions graph/src/data_source/causality_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ impl CausalityRegion {
}

pub fn from_entity(entity: &Entity) -> Self {
CausalityRegion(
entity
.get("causality_region")
.and_then(|v| v.as_int())
.unwrap_or(0),
)
entity
.get("causality_region")
.and_then(|v| v.as_int())
.map(CausalityRegion)
.unwrap_or(CausalityRegion::ONCHAIN)
}
}

Expand Down
22 changes: 21 additions & 1 deletion graph/src/data_source/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use cid::Cid;

use crate::{components::subgraph::Entity, ipfs_client::CidFile, prelude::Link};
use crate::{
blockchain::mock::{MockBlockchain, MockDataSource},
components::subgraph::Entity,
ipfs_client::CidFile,
prelude::Link,
};

use super::{
offchain::{Mapping, Source},
Expand Down Expand Up @@ -45,6 +50,21 @@ fn offchain_mark_processed_error() {
x.mark_processed_at(-1)
}

#[test]
fn data_source_helpers() {
let offchain = new_datasource();
let offchain_ds = DataSource::<MockBlockchain>::Offchain(offchain.clone());
assert!(offchain_ds.causality_region() == offchain.causality_region);
assert!(offchain_ds
.as_offchain()
.unwrap()
.is_duplicate_of(&offchain));

let onchain = DataSource::<MockBlockchain>::Onchain(MockDataSource);
assert!(onchain.causality_region() == CausalityRegion::ONCHAIN);
assert!(onchain.as_offchain().is_none());
}

fn new_datasource() -> offchain::DataSource {
offchain::DataSource::new(
"theKind".into(),
Expand Down
4 changes: 2 additions & 2 deletions store/postgres/src/deployment_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,8 +1053,8 @@ impl DeploymentStore {
layout.find(&conn, &key, block)
}

/// Retrieve all the entities matching `ids_for_type` from the
/// deployment `site`. Only consider entities as of the given `block`
/// Retrieve all the entities matching `ids_for_type`, both the type and causality region, from
/// the deployment `site`. Only consider entities as of the given `block`
pub(crate) fn get_many(
&self,
site: Arc<Site>,
Expand Down
3 changes: 2 additions & 1 deletion store/postgres/src/relational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ impl Layout {
.transpose()
}

// An optimization when looking up multiple entities, it will generate a single sql query using `UNION ALL`.
pub fn find_many(
&self,
conn: &PgConnection,
Expand Down Expand Up @@ -1229,7 +1230,7 @@ pub struct Table {
pub(crate) immutable: bool,

/// Whether this table has an explicit `causality_region` column. If `false`, then the column is
/// not present and the causality region for all rows is implicitly `0`.
/// not present and the causality region for all rows is implicitly `0` (equivalent to CasualityRegion::ONCHAIN).
pub(crate) has_causality_region: bool,
}

Expand Down
1 change: 1 addition & 0 deletions store/postgres/tests/relational_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ fn find() {
.expect("Failed to read Thing[deadbeef]")
.unwrap();
assert_entity_eq!(scrub(&*BEEF_ENTITY), entity);
assert!(CausalityRegion::from_entity(&entity) == CausalityRegion::ONCHAIN);

// Find non-existing entity
let entity = layout
Expand Down
7 changes: 7 additions & 0 deletions tests/integration-tests/file-data-sources/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export function handleBlock(block: ethereum.Block): void {
}

if (block.number == BigInt.fromI32(1)) {
let entity = IpfsFile.load("onchain")!
assert(entity.content == "onchain")

// The test assumes file data sources are processed in the block in which they are created.
// So the ds created at block 0 will have been processed.
//
Expand Down Expand Up @@ -71,6 +74,10 @@ export function handleFile(data: Bytes): void {
let entity = new IpfsFile(dataSource.stringParam())
entity.content = data.toString()
entity.save()

// Test that an offchain data source can load its own entities
let loaded_entity = IpfsFile.load(dataSource.stringParam())!
assert(loaded_entity.content == entity.content)
}

export function handleFile1(data: Bytes): void {
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ async fn file_data_sources() {
let stop_block = test_ptr(5);
let err = ctx.start_and_sync_to_error(stop_block.clone()).await;
let message = "entity type `IpfsFile1` is not on the 'entities' list for data source `File2`. \
Hint: Add `IpfsFile1` to the 'entities' list, which currently is: `IpfsFile`.\twasm backtrace:\t 0: 0x35a8 - <unknown>!src/mapping/handleFile1\t in handler `handleFile1` at block #5 ()".to_string();
Hint: Add `IpfsFile1` to the 'entities' list, which currently is: `IpfsFile`.\twasm backtrace:\t 0: 0x365d - <unknown>!src/mapping/handleFile1\t in handler `handleFile1` at block #5 ()".to_string();
let expected_err = SubgraphError {
subgraph_id: ctx.deployment.hash.clone(),
message,
Expand Down

0 comments on commit 594f919

Please sign in to comment.