Skip to content

Commit

Permalink
tests: Test conflict between onchain and offchain
Browse files Browse the repository at this point in the history
  • Loading branch information
leoyvens committed Jan 4, 2023
1 parent 76a2aa5 commit 2986760
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
7 changes: 6 additions & 1 deletion tests/integration-tests/file-data-sources/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethereum, dataSource, BigInt, Bytes } from '@graphprotocol/graph-ts'
import { ethereum, dataSource, BigInt, Bytes, DataSourceContext } from '@graphprotocol/graph-ts'
import { TestEvent } from '../generated/Contract/Contract'
import { IpfsFile, IpfsFile1 } from '../generated/schema'

Expand Down Expand Up @@ -49,6 +49,11 @@ export function handleTestEvent(event: TestEvent): void {
let entity = new IpfsFile(KNOWN_CID)
entity.content = "empty"
entity.save()
} else if (command == "createFile1") {
// Will fail the subgraph with a conflict between two entities created by offchain data sources.
let context = new DataSourceContext();
context.setBytes("hash", event.block.hash);
dataSource.createWithContext("File1", [KNOWN_CID], context)
} else {
assert(false, "Unknown command: " + command);
}
Expand Down
34 changes: 30 additions & 4 deletions 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: 0x3528 - <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: 0x35a8 - <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 All @@ -223,7 +223,7 @@ async fn file_data_sources() {
};
assert_eq!(err, expected_err);

// Unfail the subgraph to test a different error
// Unfail the subgraph to test a conflict between an onchain and offchain entity
{
ctx.rewind(test_ptr(4));

Expand All @@ -237,17 +237,43 @@ async fn file_data_sources() {

chain.set_block_stream(blocks);

// Errors in the store pipeline can be observed by using the runner directly.
let runner = ctx.runner(block_5_1_ptr.clone()).await;
let err = runner
.run()
.await
.err()
.unwrap_or_else(|| panic!("subgraph ran successfully but an error was expected"));

let message =
"store error: conflicting key value violates exclusion constraint \"ipfs_file_id_block_range_excl\""
.to_string();
assert_eq!(err.to_string(), message);
}

// Unfail the subgraph to test a conflict between an onchain and offchain entity
{
// Replace block number 5 with one that contains a different event
let mut blocks = blocks.clone();
blocks.pop();
let block_5_2_ptr = test_ptr_reorged(5, 2);
let mut block_5_2 = empty_block(test_ptr(4), block_5_2_ptr.clone());
push_test_log(&mut block_5_2, "createFile1");
blocks.push(block_5_2);

chain.set_block_stream(blocks);

// Errors in the store pipeline can be observed by using the runner directly.
let err = ctx
.runner(block_5_1_ptr.clone())
.runner(block_5_2_ptr.clone())
.await
.run()
.await
.err()
.unwrap_or_else(|| panic!("subgraph ran successfully but an error was expected"));

let message =
"store error: conflicting key value violates exclusion constraint \"ipfs_file_id_block_range_excl\""
"store error: conflicting key value violates exclusion constraint \"ipfs_file_1_id_block_range_excl\""
.to_string();
assert_eq!(err.to_string(), message);
}
Expand Down

0 comments on commit 2986760

Please sign in to comment.