Skip to content

Commit 594f919

Browse files
committed
tests: Improve tests and comments to address review
1 parent dc6f228 commit 594f919

File tree

8 files changed

+44
-11
lines changed

8 files changed

+44
-11
lines changed

graph/src/components/store/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ pub struct EntityKey {
129129
/// ID of the individual entity.
130130
pub entity_id: Word,
131131

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

graph/src/data_source/causality_region.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ impl CausalityRegion {
5151
}
5252

5353
pub fn from_entity(entity: &Entity) -> Self {
54-
CausalityRegion(
55-
entity
56-
.get("causality_region")
57-
.and_then(|v| v.as_int())
58-
.unwrap_or(0),
59-
)
54+
entity
55+
.get("causality_region")
56+
.and_then(|v| v.as_int())
57+
.map(CausalityRegion)
58+
.unwrap_or(CausalityRegion::ONCHAIN)
6059
}
6160
}
6261

graph/src/data_source/tests.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use cid::Cid;
22

3-
use crate::{components::subgraph::Entity, ipfs_client::CidFile, prelude::Link};
3+
use crate::{
4+
blockchain::mock::{MockBlockchain, MockDataSource},
5+
components::subgraph::Entity,
6+
ipfs_client::CidFile,
7+
prelude::Link,
8+
};
49

510
use super::{
611
offchain::{Mapping, Source},
@@ -45,6 +50,21 @@ fn offchain_mark_processed_error() {
4550
x.mark_processed_at(-1)
4651
}
4752

53+
#[test]
54+
fn data_source_helpers() {
55+
let offchain = new_datasource();
56+
let offchain_ds = DataSource::<MockBlockchain>::Offchain(offchain.clone());
57+
assert!(offchain_ds.causality_region() == offchain.causality_region);
58+
assert!(offchain_ds
59+
.as_offchain()
60+
.unwrap()
61+
.is_duplicate_of(&offchain));
62+
63+
let onchain = DataSource::<MockBlockchain>::Onchain(MockDataSource);
64+
assert!(onchain.causality_region() == CausalityRegion::ONCHAIN);
65+
assert!(onchain.as_offchain().is_none());
66+
}
67+
4868
fn new_datasource() -> offchain::DataSource {
4969
offchain::DataSource::new(
5070
"theKind".into(),

store/postgres/src/deployment_store.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,8 @@ impl DeploymentStore {
10531053
layout.find(&conn, &key, block)
10541054
}
10551055

1056-
/// Retrieve all the entities matching `ids_for_type` from the
1057-
/// deployment `site`. Only consider entities as of the given `block`
1056+
/// Retrieve all the entities matching `ids_for_type`, both the type and causality region, from
1057+
/// the deployment `site`. Only consider entities as of the given `block`
10581058
pub(crate) fn get_many(
10591059
&self,
10601060
site: Arc<Site>,

store/postgres/src/relational.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ impl Layout {
502502
.transpose()
503503
}
504504

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

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

store/postgres/tests/relational_bytes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ fn find() {
253253
.expect("Failed to read Thing[deadbeef]")
254254
.unwrap();
255255
assert_entity_eq!(scrub(&*BEEF_ENTITY), entity);
256+
assert!(CausalityRegion::from_entity(&entity) == CausalityRegion::ONCHAIN);
256257

257258
// Find non-existing entity
258259
let entity = layout

tests/integration-tests/file-data-sources/src/mapping.ts

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export function handleBlock(block: ethereum.Block): void {
1717
}
1818

1919
if (block.number == BigInt.fromI32(1)) {
20+
let entity = IpfsFile.load("onchain")!
21+
assert(entity.content == "onchain")
22+
2023
// The test assumes file data sources are processed in the block in which they are created.
2124
// So the ds created at block 0 will have been processed.
2225
//
@@ -71,6 +74,10 @@ export function handleFile(data: Bytes): void {
7174
let entity = new IpfsFile(dataSource.stringParam())
7275
entity.content = data.toString()
7376
entity.save()
77+
78+
// Test that an offchain data source can load its own entities
79+
let loaded_entity = IpfsFile.load(dataSource.stringParam())!
80+
assert(loaded_entity.content == entity.content)
7481
}
7582

7683
export function handleFile1(data: Bytes): void {

tests/tests/runner.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ async fn file_data_sources() {
213213
let stop_block = test_ptr(5);
214214
let err = ctx.start_and_sync_to_error(stop_block.clone()).await;
215215
let message = "entity type `IpfsFile1` is not on the 'entities' list for data source `File2`. \
216-
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();
216+
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();
217217
let expected_err = SubgraphError {
218218
subgraph_id: ctx.deployment.hash.clone(),
219219
message,

0 commit comments

Comments
 (0)